-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_go.el
More file actions
102 lines (83 loc) · 3.76 KB
/
_go.el
File metadata and controls
102 lines (83 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
;; -*-emacs-lisp-*-
;;;
;;; Go configuration
;;;
;; (add-hook 'go-mode-hook
;; (lambda ()
;; (add-hook 'before-save-hook 'gofmt-before-save)
;; (setq indent-tabs-mode 1)))
(when (locate-library "subword")
(add-hook 'go-mode-hook
(lambda ()
;; Surprisingly, `c-basic-offset' has no effect on
;; go-mode, but `tab-width' does.
(setq tab-width 4)
(subword-mode 1))))
(add-hook 'go-mode-hook 'cinsk/go-check-utils)
(add-hook 'go-mode-hook 'lsp-deferred)
(defun cinsk/go-check-utils (&optional nowarn)
(if (executable-find "gopls")
(progn
(add-hook 'before-save-hook #'lsp-format-buffer t t)
(add-hook 'before-save-hook #'lsp-organize-imports t t))
(unless nowarn
(warn "gopls not found; See https://github.com/golang/tools/tree/master/gopls for more"))))
(when nil
(with-eval-after-load "go-mode"
(add-hook 'before-save-hook 'gofmt-before-save))
;;
;; Prefer company-mode based auto-completion to auto-complete-mode based one.
;;
(cond ((locate-library "company")
(add-hook 'go-mode-hook 'cinsk/go-init-cp-autocomplete))
((locate-library "auto-complete")
(add-hook 'go-mode-hook 'cinsk/go-init-ac-autocomplete)))
(defun cinsk/go-check-gocode (&optional nowarn)
(unless (executable-find "gocode")
(add-to-list 'exec-path (substitute-in-file-name "$GOPATH/bin"))
(unless (and (executable-find "gocode") nowarn)
(warn "gocode not found; try build it using 'go get github.com/nsf/gocode' in $GOPATH directory"))))
(defun cinsk/go-check-utils (&optional nowarn)
(if (executable-find "goimports")
(setq gofmt-command "goimports")
(add-to-list 'exec-path (substitute-in-file-name "$GOPATH/bin"))
(if (executable-find "goimports")
(setq gofmt-command "goimports")
(unless nowarn
(warn "goimports not found; try build it using 'go get golang.org/x/tools/cmd/goimports' in $GOPATH directory")))))
(defun cinsk/go-init-cp-autocomplete-deprecated ()
"Set up auto completion of Go sources using `company-mode'"
(unless (locate-library "company-go")
(let ((go-cp-dir (substitute-in-file-name
"$GOPATH/src/github.com/nsf/gocode/emacs-company")))
(when (locate-library "company-go" nil (list go-cp-dir))
(add-to-list 'load-path go-cp-dir)
(cinsk/go-check-gocode)
(unless (featurep 'company-go)
(require 'company)
(require 'company-go)
))))
(set (make-local-variable 'company-backends) '(company-go))
(company-mode))
(defun cinsk/go-init-ac-autocomplete-deprecated ()
"Set up auto completion of Go sources.
See https://github.com/auto-complete/auto-complete for more"
;; go-autocomplete.el is distributed with gocode.
;; It seems that I need to manually add gocode into the workspace.
;; The workspace location is specified in the environment, GOPATH.
(unless (locate-library "go-autocomplete")
(let ((go-ac-dir (substitute-in-file-name
"$GOPATH/src/github.com/nsf/gocode/emacs")))
(when (locate-library "go-autocomplete" nil (list go-ac-dir))
(add-to-list 'load-path go-ac-dir)
(cinsk/go-check-gocode)
(unless (featurep 'go-autocomplete)
(require 'go-autocomplete)
(require 'auto-complete-config)
(ac-config-default)
(when (boundp 'go-mode-map)
;; A key binding to force complete (show completion candidate).
;; I thought `ac-complete' would call `ac-complete-go', but
;; `ac-complete' has no effect.
(define-key go-mode-map [(control meta ?I)] 'ac-complete-go)))))))
)