Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icon support for Elpy #94

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions company-box-icons.el
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ See `company-box-icons-images' or `company-box-icons-all-the-icons' for the ICON
[1] https://github.com/Microsoft/language-server-protocol/blob/gh-pages/\
specification.md#completion-request-leftwards_arrow_with_hook.")

(defun company-box-icons--lsp (candidate)
(defun company-box-icons--lsp (candidate annotation)
(-when-let* ((lsp-item (get-text-property 0 'lsp-completion-item candidate))
(kind-num (gethash "kind" lsp-item)))
(alist-get kind-num company-box-icons--lsp-alist)))
Expand All @@ -215,12 +215,12 @@ specification.md#completion-request-leftwards_arrow_with_hook.")
("T" . Template))
"List of icon types to use with PHP candidates.")

(defun company-box-icons--acphp (candidate)
(defun company-box-icons--acphp (candidate annotation)
(when (derived-mode-p 'php-mode)
(-> (get-text-property 0 'ac-php-tag-type candidate)
(alist-get company-box-icons--php-alist))))

(defun company-box-icons--elisp (candidate)
(defun company-box-icons--elisp (candidate annotation)
(when (derived-mode-p 'emacs-lisp-mode)
(let ((sym (intern candidate)))
;; we even can move it to (predicate . kind) alist
Expand All @@ -230,10 +230,24 @@ specification.md#completion-request-leftwards_arrow_with_hook.")
((boundp sym) 'Variable)
(t . nil)))))

(defun company-box-icons--yasnippet (candidate)
(defun company-box-icons--yasnippet (candidate annotation)
(when (get-text-property 0 'yas-annotation candidate)
'Template))

(defconst company-box-icons--elpy-alist
'(("class" . Class)
("function" . Function)
("keyword" . Keyword)
("instance" . Reference)
("module" . Module)
("statement" . Variable))
"List of icon types to use with Elpy Python candidates.")

(defun company-box-icons--elpy (candidate annotation)
(when (and (derived-mode-p 'python-mode)
(bound-and-true-p elpy-mode))
(alist-get annotation company-box-icons--elpy-alist 'Unknown nil 'string-equal)))

(defun company-box-icons-resize (size)
"Set icons size in pixels."
(interactive "nIcon size in pixels: ")
Expand Down
16 changes: 8 additions & 8 deletions company-box.el
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ To change the number of _visible_ chandidates, see `company-tooltip-limit'"
:group 'company-box)

(defcustom company-box-icons-functions
'(company-box-icons--yasnippet company-box-icons--lsp company-box-icons--elisp company-box-icons--acphp)
'(company-box-icons--yasnippet company-box-icons--lsp company-box-icons--elisp company-box-icons--acphp company-box-icons--elpy)
"Functions to call on each candidate that should return an icon.
The functions takes 1 parameter, the completion candidate.

Expand Down Expand Up @@ -404,16 +404,16 @@ It doesn't nothing if a font icon is used."
(make-frame-visible (company-box--get-frame)))
(company-box--update-scrollbar (company-box--get-frame) t))

(defun company-box--get-kind (candidate)
(defun company-box--get-kind (candidate annotation)
(let ((list company-box-icons-functions)
kind)
(while (and (null kind) list)
(setq kind (funcall (car list) candidate))
(setq kind (funcall (car list) candidate annotation))
(pop list))
(or kind 'Unknown)))

(defun company-box--get-icon (candidate)
(let ((icon (alist-get (company-box--get-kind candidate)
(defun company-box--get-icon (candidate annotation)
(let ((icon (alist-get (company-box--get-kind candidate annotation)
(symbol-value company-box-icons-alist))))
(cond ((listp icon)
(cond ((eq 'image (car icon))
Expand All @@ -426,9 +426,9 @@ It doesn't nothing if a font icon is used."
(icons-in-terminal (or icon 'fa_question_circle)))
(t icon))))

(defun company-box--add-icon (candidate)
(defun company-box--add-icon (candidate annotation)
(concat
(company-box--get-icon candidate)
(company-box--get-icon candidate annotation)
(propertize " " 'display `(space :align-to (+ left-fringe ,(if (> company-box--space 2) 3 2))))))

(defun company-box--get-color (backend)
Expand Down Expand Up @@ -459,7 +459,7 @@ It doesn't nothing if a font icon is used."
(-let* (((candidate annotation len-c len-a backend) candidate)
(color (company-box--get-color backend))
((c-color a-color i-color s-color) (company-box--resolve-colors color))
(icon-string (and company-box--with-icons-p (company-box--add-icon candidate)))
(icon-string (and company-box--with-icons-p (company-box--add-icon candidate annotation)))
(candidate-string (concat (and company-common (propertize company-common 'face 'company-tooltip-common))
(substring (propertize candidate 'face 'company-box-candidate) (length company-common) nil)))
(align-string (when annotation
Expand Down