Skip to content

Commit

Permalink
company: merge tabnine and lsp's results
Browse files Browse the repository at this point in the history
  • Loading branch information
JJPandari committed Jun 3, 2020
1 parent 469aa06 commit dbb677c
Show file tree
Hide file tree
Showing 24 changed files with 115 additions and 243 deletions.
3 changes: 2 additions & 1 deletion init.el
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@
(require 'init-buffer)
(require 'init-minibuffer)

(require 'init-ivy)
;; (require 'init-helm)
(require 'init-ivy)
;; (require 'init-selectrum)
(require 'init-company)
(require 'init-yas)
(require 'init-windows)
Expand Down
48 changes: 42 additions & 6 deletions modes/init-company.el
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
:after evil-multiedit
:init
(setq company-backends
'(company-capf company-files company-css
(company-dabbrev-code company-gtags company-etags company-keywords)
company-dabbrev))
'(company-lsp company-capf company-files
(company-dabbrev-code company-gtags company-etags company-keywords)
company-dabbrev))
:config
(global-company-mode 1)
(setq
Expand All @@ -34,8 +34,18 @@
"<tab>" 'expand-snippet-or-complete-selection
"C-g" 'company-abort
"<escape>" (lambda! (company-abort) (evil-normal-state)))
;; M-1 ~ M-0 to select candidate 1 ~ 10
(dotimes (i 10)
(define-key company-active-map (read-kbd-macro (format "M-%d" i)) 'company-complete-number))
;; H-1 ~ H-5 to select candidate 6 ~ 10
(cl-loop for num-key from 1 to 4
do (let ((candidate-index (+ num-key 5)))
(define-key company-active-map
(read-kbd-macro (format "H-%d" num-key))
`(lambda () (interactive) (company-complete-number ,candidate-index)))))
(define-key company-active-map
(read-kbd-macro "H-5")
(lambda () (interactive) (company-complete-number 10)))

(general-define-key
:states '(insert emacs)
Expand All @@ -46,13 +56,39 @@
(use-package company-tabnine
:demand t
:config
(setq jester-company-backends-with-tabnine (cons 'company-tabnine company-backends))
(setq jester-company-backends-with-tabnine
'((company-lsp :with company-tabnine) (company-capf :with company-tabnine)
company-files
company-tabnine
(company-dabbrev-code company-gtags company-etags company-keywords)
company-dabbrev))
(defun jester/use-tabnine-for-major-mode (major-mode)
"add tabnine to `COMPANY-BACKENDS' in `MAJOR-MODE'."
(add-hook (intern (format "%s-hook" major-mode))
(lambda () (setq-local company-backends jester-company-backends-with-tabnine))))
(lambda () (setq-local company-backends jester-company-backends-with-tabnine)) t))
(dolist (mode '(js2-mode web-mode typescript-mode css-mode less-css-mode scss-mode))
(jester/use-tabnine-for-major-mode mode)))
(jester/use-tabnine-for-major-mode mode))

(add-to-list 'company-transformers 'jester/company-merge-tabnine-with-other t)
(defun jester/company-merge-tabnine-with-other (candidates)
"Show first 5 of tabnine's candidates, followed by the other backend's candidates.
\"the other\" means company-foo when the group is (company-foo :with company-tabnine)."
(if (not lsp-mode)
candidates
(let ((dedup-table (make-hash-table :test #'equal))
candidates-other
candidates-tabnine)
(dolist (candidate candidates)
(if (eq (get-text-property 0 'company-backend candidate)
'company-tabnine)
(unless (gethash candidate dedup-table)
(push candidate candidates-tabnine))
(push candidate candidates-other)
(puthash candidate t dedup-table)))
(setq candidates-other (nreverse candidates-other))
(setq candidates-tabnine (nreverse candidates-tabnine))
(nconc (seq-take candidates-tabnine 5)
candidates-other)))))


;; (use-package company-quickhelp
Expand Down
3 changes: 2 additions & 1 deletion modes/init-editing-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

(electric-indent-mode -1) ;; this is global
(use-package aggressive-indent
:custom (aggressive-indent-region-function 'evil-indent "also convert tab/space when indent")
:custom ((aggressive-indent-region-function 'evil-indent "also convert tab/space when indent")
(aggressive-indent-sit-for-time 0.1))
:hook (after-init . aggressive-indent-global-mode))

(after-init (transient-mark-mode 1))
Expand Down
13 changes: 3 additions & 10 deletions modes/init-evil.el
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,8 @@

gitconfig-mode-map
diff-mode-map)
"<return>" 'jester/probably-switch-buffer)

(defun jester/probably-switch-buffer ()
"Probably switch buffer, but do enter in special cases:
when peeking definitions with lsp-ui."
(interactive)
(if (and (featurep 'lsp-ui) lsp-ui-peek-mode)
(call-interactively 'lsp-ui-peek--goto-xref)
(call-interactively 'ivy-switch-buffer)))
"<return>" (general-predicate-dispatch 'switch-to-buffer
(and (featurep 'lsp-ui) lsp-ui-peek-mode) 'lsp-ui-peek--goto-xref))

(general-define-key
:states '(visual)
Expand Down Expand Up @@ -536,5 +529,5 @@ when peeking definitions with lsp-ui."
;; TODO "vio" vs "yio": make visual "auto line-wise"?


;; TODO use another key for register 0
;; TODO use another key for register 0 ("(" looks good)
(provide 'init-evil)
1 change: 1 addition & 0 deletions modes/init-face.el
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"<tab>" 'jester/probably-symbol-overlay-put
"M-n" 'symbol-overlay-jump-next
"M-p" 'symbol-overlay-jump-prev)
(jester/with-leader "o p" 'jester/probably-symbol-overlay-put)
;; don't bind any key
(setq symbol-overlay-map (make-sparse-keymap))

Expand Down
5 changes: 4 additions & 1 deletion modes/init-git.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@
"g a" 'magit-dispatch-popup)
:commands (magit-status magit-diff-buffer-file magit-dispatch-popup)
:config
;; quit rev selection in a rebase or something
(jester/with-major-leader
'magit-log-mode-map
"a" 'magit-log-select-quit)
)
(general-define-key
:keymaps 'transient-base-map
"<escape>" 'transient-quit-one))

(use-package evil-magit
:hook (magit-mode . evil-magit-init))
Expand Down
5 changes: 1 addition & 4 deletions modes/init-javascript.el
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
(looking-at (rx (sequence bol
"import"
(1+ (or word "," "{" "}" whitespace))
"from 'react'"
(or "from 'react'" "from \"react\"")
(optional ";")
eol))))))
(setq magic-mode-alist
Expand Down Expand Up @@ -232,7 +232,4 @@
"t" 'jester/typescript-goto-typings-file)


;; (require 'init-ui5)


(provide 'init-javascript)
3 changes: 1 addition & 2 deletions modes/init-lsp.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
(lsp-prefer-flymake nil)
(lsp-restart 'auto-restart)
(lsp-enable-symbol-highlighting nil)
;; this prevents lsp from setting company backends from tabnine to lsp
;; (lsp-prefer-capf t)
(lsp-prefer-capf nil)
:init
:hook ((rust-mode . lsp) (typescript-mode . lsp))
:commands (lsp lsp-deferred))
Expand Down
8 changes: 6 additions & 2 deletions modes/init-paredit.el
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
awesome-pair-backward-delete awesome-pair-forward-delete
awesome-pair-equal awesome-pair-double-quote awesome-pair-space
awesome-pair-open-round awesome-pair-open-bracket awesome-pair-open-curly
awesome-pair-close-round awesome-pair-close-bracket awesome-pair-close-curly)
:bind (("M-u" . awesome-pair-wrap-round))
awesome-pair-close-round awesome-pair-close-bracket awesome-pair-close-curly
awesome-pair-wrap-round-pair)
:bind (("M-u" . (lambda () (interactive) )))
:init
(general-define-key
:states '(insert emacs)
Expand All @@ -25,6 +26,9 @@
;; "<backspace>" 'awesome-pair-backward-delete
;; "C-b" 'awesome-pair-forward-delete
)
(general-define-key
:states '(emacs insert normal visual)
"M-u" (lambda! (awesome-pair-wrap-round-pair)))

(defun jester/semantic-kill-maybe-whole-line ()
"Kill semantic unit after point, if only whitespace is left afterwards, delete this line."
Expand Down
21 changes: 21 additions & 0 deletions modes/init-selectrum.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(use-package selectrum
:demand t
:config
(selectrum-mode 1))


(use-package prescient
:custom (prescient-aggressive-file-save t)
:demand t
:after (selectrum)
:config
(prescient-persist-mode 1))

(use-package selectrum-prescient
:demand t
:after (selectrum prescient)
:config
(selectrum-prescient-mode 1))


(provide 'init-selectrum)
Loading

0 comments on commit dbb677c

Please sign in to comment.