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

Commit

Permalink
emacs-company: backport upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdempsky committed Apr 27, 2018
1 parent 9be2e7f commit bf5e5d0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 19 deletions.
2 changes: 1 addition & 1 deletion emacs-company/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Company-go
Company-go is an alternative emacs plugin for autocompletion. Is uses [company-mode](http://company-mode.github.io).
Company-go is an alternative emacs plugin for autocompletion. It uses [company-mode](http://company-mode.github.io).
Completion will start automatically whenever the current symbol is preceded by a `.`, or after you type `company-minimum-prefix-length` letters.

## Setup
Expand Down
92 changes: 74 additions & 18 deletions emacs-company/company-go.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,57 @@ symbol is preceded by a \".\", ignoring `company-minimum-prefix-length'."
:group 'company-go
:type 'string)

(defcustom company-go-gocode-args nil
"Additional arguments to pass to `gocode'"
:group 'company-go
:type '(repeat string))

(defcustom company-go-godoc-command "go doc"
"The command to invoke `go doc' with."
:group 'company-go
:type 'string)

(defcustom company-go-godoc-args "-u"
"Arguments to pass to `go doc'."
:group 'company-go
:type 'string)

(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))
(kill-buffer temp-buffer))))
(let ((code-buffer (current-buffer))
(gocode-args (append company-go-gocode-args
(list "-f=csv-with-package"
"autocomplete"
(or (buffer-file-name) "")
(concat "c" (int-to-string (- (point) 1)))))))
(with-temp-buffer
(let ((temp-buffer (current-buffer)))
(with-current-buffer code-buffer
(apply #'call-process-region
(point-min)
(point-max)
company-go-gocode-command
nil
temp-buffer
nil
gocode-args))
(buffer-string)))))

(defun company-go--format-meta (candidate)
(let ((class (nth 0 candidate))
(name (nth 1 candidate))
(type (nth 2 candidate)))
(setq type (if (and (string= class "func")
(string-prefix-p "func" type))
(setq type (if (string-prefix-p "func" 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))
(propertize (nth 1 candidate)
'meta (company-go--format-meta candidate)
'package (nth 3 candidate))))
strings))

(defun company-go--candidates ()
(let ((candidates (company-go--get-candidates (split-string (company-go--invoke-autocomplete) "\n" t))))
Expand Down Expand Up @@ -185,19 +206,54 @@ triggers a completion immediately."
(when word
(string-match-p "^0x\\|^[0-9]+" word))))

(defun company-go--syntax-highlight (str)
"Apply syntax highlighting to STR."
;; If the user has disabled font-lock, respect that.
(if global-font-lock-mode
(with-temp-buffer
(insert str)
(delay-mode-hooks (go-mode))
(if (fboundp 'font-lock-ensure)
(font-lock-ensure)
(with-no-warnings
(font-lock-fontify-buffer)))
(buffer-string))
str))

(defun company-go--godoc-as-buffer (arg)
"Return Go documentation for QUERY as a buffer."
(unless (string= arg "")
(let* ((package (get-text-property 0 'package arg))
(query (if (string= package "")
arg
(format "%s.%s" package arg)))
(buf (godoc--get-buffer query))
(exit-code (call-process-shell-command
(concat company-go-godoc-command " " company-go-godoc-args " " query)
nil buf nil)))
(if (zerop exit-code)
buf
(kill-buffer buf)
nil))))

;;;###autoload
(defun company-go (command &optional arg &rest ignored)
(interactive (list 'interactive))
(case command
(interactive (company-begin-backend 'company-go))
(prefix (and (derived-mode-p 'go-mode)
(not (company-in-string-or-comment))
(not (company-go--in-num-literal-p))
(or (company-go--prefix) 'stop)))
(candidates (company-go--candidates))
(meta (get-text-property 0 'meta arg))
(meta
(company-go--syntax-highlight (get-text-property 0 'meta arg)))
(annotation
(when company-go-show-annotation
(company-go--extract-annotation (get-text-property 0 'meta arg))))
(location (company-go--location arg))
(doc-buffer
(company-go--godoc-as-buffer arg))
(sorted t)
(post-completion
(when (and company-go-insert-arguments
Expand Down

0 comments on commit bf5e5d0

Please sign in to comment.