Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
Better looking annotations when annotation alignment is turned off.
Browse files Browse the repository at this point in the history
Instead of displaying meta as-is when annotation alignment is turned off, the
backend removes first two words from meta, effectively leaving only function
signatures or types in annotation.

Also reindent everything and remove tabs.

Fixes nsf#266.
  • Loading branch information
nsf committed Jan 10, 2015
1 parent 71ddfa9 commit 3109790
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions emacs-company/company-go.el
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,32 @@ symbol is preceded by a \".\", ignoring `company-minimum-prefix-length'."
(defun company-go--invoke-autocomplete ()
(let ((temp-buffer (generate-new-buffer "*gocode*")))
(prog2
(call-process-region (point-min)
(point-max)
company-go-gocode-command
nil
temp-buffer
nil
"-f=csv"
"autocomplete"
(or (buffer-file-name) "")
(concat "c" (int-to-string (- (point) 1))))
(with-current-buffer temp-buffer (buffer-string))
(call-process-region (point-min)
(point-max)
company-go-gocode-command
nil
temp-buffer
nil
"-f=csv"
"autocomplete"
(or (buffer-file-name) "")
(concat "c" (int-to-string (- (point) 1))))
(with-current-buffer temp-buffer (buffer-string))
(kill-buffer temp-buffer))))

(defun company-go--format-meta (candidate)
(let ((class (nth 0 candidate))
(name (nth 1 candidate))
(type (nth 2 candidate)))
(name (nth 1 candidate))
(type (nth 2 candidate)))
(setq type (if (string-prefix-p "func" type)
(substring type 4 nil)
(concat " " type)))
(substring type 4 nil)
(concat " " type)))
(concat class " " name type)))

(defun company-go--get-candidates (strings)
(mapcar (lambda (str)
(let ((candidate (split-string str ",,")))
(propertize (nth 1 candidate) 'meta (company-go--format-meta candidate)))) strings))
(let ((candidate (split-string str ",,")))
(propertize (nth 1 candidate) 'meta (company-go--format-meta candidate)))) strings))

(defun company-go--candidates ()
(company-go--get-candidates (split-string (company-go--invoke-autocomplete) "\n" t)))
Expand All @@ -106,8 +106,8 @@ symbol is preceded by a \".\", ignoring `company-minimum-prefix-length'."
(goto-char point)
(company-go--godef-jump point)))
(ignore-errors
(with-current-buffer temp-buffer
(set-buffer-modified-p nil))
(with-current-buffer temp-buffer
(set-buffer-modified-p nil))
(kill-buffer temp-buffer)
(delete-file temp)))))

Expand Down Expand Up @@ -162,6 +162,18 @@ triggers a completion immediately."
(setq pos (1+ pos))))
(substring-no-properties str 0 pos)))

; Uses meta as-is if annotation alignment is enabled. Otherwise removes first
; two words from the meta, which are usually the class and the name of the
; entity, the rest is the function signature or type. That's how annotations are
; supposed to be used.
(defun company-go--extract-annotation (meta)
"Extract annotation from META."
(if company-tooltip-align-annotations
meta
(save-match-data
(and (string-match "\\w+ \\w+\\(.+\\)" meta)
(match-string 1 meta)))))

;;;###autoload
(defun company-go (command &optional arg &rest ignored)
(case command
Expand All @@ -172,7 +184,7 @@ triggers a completion immediately."
(meta (get-text-property 0 'meta arg))
(annotation
(when company-go-show-annotation
(get-text-property 0 'meta arg)))
(company-go--extract-annotation (get-text-property 0 'meta arg))))
(location (company-go--location arg))
(sorted t)
(post-completion
Expand Down

0 comments on commit 3109790

Please sign in to comment.