2017-10-08 20:36:28 +02:00
|
|
|
;;; Go
|
2016-01-27 08:55:19 +01:00
|
|
|
|
2019-03-03 12:58:44 +01:00
|
|
|
;;; We should not need to use `use-local-map' here.
|
2017-10-08 20:36:28 +02:00
|
|
|
;;; Reported at https://github.com/dominikh/go-mode.el/issues/191.
|
2017-05-15 07:46:20 +02:00
|
|
|
(use-local-map go-mode-map)
|
|
|
|
|
2018-02-13 13:42:11 +01:00
|
|
|
(ambrevar/local-set-keys
|
2018-02-15 13:28:49 +01:00
|
|
|
"C-c m" 'ambrevar/go-main
|
2017-06-16 21:33:53 +02:00
|
|
|
"C-c D" 'godoc
|
|
|
|
"C-c d" 'godoc-at-point
|
|
|
|
"M-." #'godef-jump
|
2018-02-15 13:28:49 +01:00
|
|
|
"<f5>" 'ambrevar/go-metalinter
|
|
|
|
"C-<f5>" 'ambrevar/go-metalinter-command)
|
2017-05-15 07:46:20 +02:00
|
|
|
(when (require 'helm-go-package nil t)
|
|
|
|
(local-set-key (kbd "C-c D") 'helm-go-package))
|
|
|
|
|
|
|
|
(when (require 'company-go nil t)
|
2017-06-16 16:29:52 +02:00
|
|
|
(add-hook 'go-mode-hook 'company-mode)
|
2017-05-15 07:46:20 +02:00
|
|
|
(add-to-list 'company-backends 'company-go)
|
2017-10-17 00:52:50 +02:00
|
|
|
(local-set-key (kbd "M-<tab>") (if (require 'helm-company nil t) 'helm-company 'company-complete)))
|
2017-05-15 07:46:20 +02:00
|
|
|
|
2016-10-08 05:19:42 +02:00
|
|
|
(setq gofmt-command "goimports")
|
|
|
|
(setq godoc-command "godoc -ex")
|
|
|
|
(setq godoc-and-godef-command "godoc -ex")
|
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defvar-local ambrevar/gometalinter-args
|
2017-05-07 19:03:00 +02:00
|
|
|
(mapconcat
|
|
|
|
'identity
|
|
|
|
'("--cyclo-over=20 --deadline=20s"
|
|
|
|
;; Ignore some benign errors.
|
2017-08-31 20:47:49 +02:00
|
|
|
"-e 'declaration of \"?err\"? shadows'"
|
2017-05-07 19:03:00 +02:00
|
|
|
"-e 'error return value not checked \\(.*\\.Close\\(\\)'"
|
|
|
|
;; Customize linters.
|
|
|
|
"-E misspell"
|
|
|
|
"-E unparam"
|
|
|
|
"-E unused"
|
|
|
|
;; gofmt is only useful if not called on save with '-s'
|
|
|
|
;; (goimports does not do this) and for its first rule not
|
|
|
|
;; superseded by gosimple or solint:
|
|
|
|
;; https://golang.org/cmd/gofmt/#hdr-The_simplify_command
|
|
|
|
"-E gofmt")
|
|
|
|
" ") "Additional arguments to pass to gometalinter.")
|
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defun ambrevar/go-metalinter (arg)
|
2017-09-14 17:52:24 +02:00
|
|
|
"Run gometalinter.
|
|
|
|
With prefix argument, prompt for commandline."
|
2017-05-11 19:38:28 +02:00
|
|
|
(interactive "P")
|
2018-02-15 13:28:49 +01:00
|
|
|
(let ((compile-command (format "gometalinter %s" ambrevar/gometalinter-args)))
|
2017-05-11 19:38:28 +02:00
|
|
|
(if arg
|
|
|
|
(call-interactively 'compile)
|
|
|
|
(compile compile-command))))
|
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defun ambrevar/go-metalinter-command ()
|
2017-09-14 17:52:24 +02:00
|
|
|
"Prompt for gometalinter commandline and run it."
|
|
|
|
(interactive)
|
2018-02-15 13:28:49 +01:00
|
|
|
(ambrevar/go-metalinter t))
|
2017-09-14 17:52:24 +02:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defun ambrevar/go-set-compile-command ()
|
2016-10-08 05:19:42 +02:00
|
|
|
"Set `compile-command' depending on the context.
|
2016-12-11 06:15:54 +01:00
|
|
|
|
2016-10-08 05:19:42 +02:00
|
|
|
- go install: file is in GOPATH and is not a test file.
|
|
|
|
- go test: file is in GOPATH and is a test file.
|
2016-12-11 06:15:54 +01:00
|
|
|
- go run `buffer-file-name': file is not in GOPATH.
|
2016-11-23 07:50:03 +01:00
|
|
|
|
|
|
|
Note that the -cover test flag is left out since it shifts line numbers."
|
2015-12-09 01:02:23 +01:00
|
|
|
(interactive)
|
2017-05-12 08:34:17 +02:00
|
|
|
(setq compile-command
|
2018-02-15 13:28:49 +01:00
|
|
|
(if (ambrevar/go-buffer-in-gopath-p)
|
2017-06-16 16:29:52 +02:00
|
|
|
(if (string-match "_test.[gG][oO]$" buffer-file-name)
|
|
|
|
"go test -v -run ."
|
|
|
|
"go install")
|
|
|
|
(concat "go run " (shell-quote-argument buffer-file-name)))))
|
2015-12-09 01:02:23 +01:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defun ambrevar/go-buffer-in-gopath-p ()
|
2016-07-19 12:00:52 +02:00
|
|
|
(if (not buffer-file-name)
|
|
|
|
nil
|
2016-10-05 06:57:29 +02:00
|
|
|
(let ((dir (expand-file-name (file-name-directory buffer-file-name))) (looping t) (gopath (split-string (getenv "GOPATH") ":")))
|
2016-07-19 12:00:52 +02:00
|
|
|
(while (progn
|
2016-10-05 06:57:29 +02:00
|
|
|
(if (member dir gopath)
|
2016-07-19 12:00:52 +02:00
|
|
|
(setq looping nil)
|
|
|
|
(setq dir (expand-file-name ".." dir)))
|
|
|
|
(and looping (not (string= dir "/")))))
|
|
|
|
(if (string= dir "/") nil t))))
|
2016-06-02 22:28:00 +02:00
|
|
|
|
2016-10-06 14:37:53 +02:00
|
|
|
(when (require 'go-guru nil t)
|
|
|
|
(unless (executable-find "guru")
|
2017-06-16 16:29:52 +02:00
|
|
|
;; Requires `call-process-to-string' from `functions'."
|
2016-10-06 14:37:53 +02:00
|
|
|
(require 'functions)
|
|
|
|
(setq go-guru-command
|
2018-02-13 13:42:11 +01:00
|
|
|
(concat (replace-regexp-in-string "\n$" "" (ambrevar/call-process-to-string "go" "env" "GOTOOLDIR")) "/guru"))))
|
2016-10-06 14:37:53 +02:00
|
|
|
|
2018-02-13 13:42:11 +01:00
|
|
|
(defun ambrevar/go-turn-on-gofmt-before-save ()
|
2017-05-28 12:03:28 +02:00
|
|
|
(add-hook 'before-save-hook #'gofmt-before-save nil t))
|
|
|
|
|
2018-02-13 13:42:11 +01:00
|
|
|
(add-hook 'go-mode-hook 'ambrevar/go-turn-on-gofmt-before-save)
|
2017-05-28 12:03:28 +02:00
|
|
|
|
|
|
|
(when (require 'go-eldoc nil t)
|
2017-06-16 16:29:52 +02:00
|
|
|
(add-hook 'go-mode-hook 'go-eldoc-setup))
|
2017-05-28 12:03:28 +02:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(add-hook 'go-mode-hook 'ambrevar/go-set-compile-command)
|
2017-05-28 12:03:28 +02:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defun ambrevar/godoc-setup ()
|
2017-06-16 16:29:52 +02:00
|
|
|
(setq tab-width 8))
|
2017-05-28 12:03:28 +02:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(add-hook 'godoc-mode-hook 'ambrevar/godoc-setup)
|
2014-10-23 16:44:23 +02:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(define-skeleton ambrevar/go-main
|
2014-10-23 16:44:23 +02:00
|
|
|
"Insert main function with basic includes."
|
|
|
|
nil
|
2014-10-31 13:08:44 +01:00
|
|
|
> "package main" "\n" \n
|
|
|
|
"import (" \n
|
|
|
|
"\"fmt\"" \n
|
|
|
|
")" > "\n" \n
|
2014-10-31 13:20:11 +01:00
|
|
|
"func main() {" \n
|
2014-10-31 13:08:44 +01:00
|
|
|
> @ _ \n
|
|
|
|
"}" > \n)
|
2014-10-23 16:44:23 +02:00
|
|
|
|
2017-06-18 18:50:31 +02:00
|
|
|
(provide 'init-go)
|