init-go.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; Go
  2. ;;; REVIEW: We should not need to use `use-local-map' here.
  3. ;;; Reported at https://github.com/dominikh/go-mode.el/issues/191.
  4. (use-local-map go-mode-map)
  5. (ambrevar/local-set-keys
  6. "C-c m" 'ambrevar/go-main
  7. "C-c D" 'godoc
  8. "C-c d" 'godoc-at-point
  9. "M-." #'godef-jump
  10. "<f5>" 'ambrevar/go-metalinter
  11. "C-<f5>" 'ambrevar/go-metalinter-command)
  12. (when (require 'helm-go-package nil t)
  13. (local-set-key (kbd "C-c D") 'helm-go-package))
  14. (when (require 'company-go nil t)
  15. (add-hook 'go-mode-hook 'company-mode)
  16. (add-to-list 'company-backends 'company-go)
  17. (local-set-key (kbd "M-<tab>") (if (require 'helm-company nil t) 'helm-company 'company-complete)))
  18. (setq gofmt-command "goimports")
  19. (setq godoc-command "godoc -ex")
  20. (setq godoc-and-godef-command "godoc -ex")
  21. (defvar-local ambrevar/gometalinter-args
  22. (mapconcat
  23. 'identity
  24. '("--cyclo-over=20 --deadline=20s"
  25. ;; Ignore some benign errors.
  26. "-e 'declaration of \"?err\"? shadows'"
  27. "-e 'error return value not checked \\(.*\\.Close\\(\\)'"
  28. ;; Customize linters.
  29. "-E misspell"
  30. "-E unparam"
  31. "-E unused"
  32. ;; gofmt is only useful if not called on save with '-s'
  33. ;; (goimports does not do this) and for its first rule not
  34. ;; superseded by gosimple or solint:
  35. ;; https://golang.org/cmd/gofmt/#hdr-The_simplify_command
  36. "-E gofmt")
  37. " ") "Additional arguments to pass to gometalinter.")
  38. (defun ambrevar/go-metalinter (arg)
  39. "Run gometalinter.
  40. With prefix argument, prompt for commandline."
  41. (interactive "P")
  42. (let ((compile-command (format "gometalinter %s" ambrevar/gometalinter-args)))
  43. (if arg
  44. (call-interactively 'compile)
  45. (compile compile-command))))
  46. (defun ambrevar/go-metalinter-command ()
  47. "Prompt for gometalinter commandline and run it."
  48. (interactive)
  49. (ambrevar/go-metalinter t))
  50. (defun ambrevar/go-set-compile-command ()
  51. "Set `compile-command' depending on the context.
  52. - go install: file is in GOPATH and is not a test file.
  53. - go test: file is in GOPATH and is a test file.
  54. - go run `buffer-file-name': file is not in GOPATH.
  55. Note that the -cover test flag is left out since it shifts line numbers."
  56. (interactive)
  57. (setq compile-command
  58. (if (ambrevar/go-buffer-in-gopath-p)
  59. (if (string-match "_test.[gG][oO]$" buffer-file-name)
  60. "go test -v -run ."
  61. "go install")
  62. (concat "go run " (shell-quote-argument buffer-file-name)))))
  63. (defun ambrevar/go-buffer-in-gopath-p ()
  64. (if (not buffer-file-name)
  65. nil
  66. (let ((dir (expand-file-name (file-name-directory buffer-file-name))) (looping t) (gopath (split-string (getenv "GOPATH") ":")))
  67. (while (progn
  68. (if (member dir gopath)
  69. (setq looping nil)
  70. (setq dir (expand-file-name ".." dir)))
  71. (and looping (not (string= dir "/")))))
  72. (if (string= dir "/") nil t))))
  73. (when (require 'go-guru nil t)
  74. (unless (executable-find "guru")
  75. ;; Requires `call-process-to-string' from `functions'."
  76. (require 'functions)
  77. (setq go-guru-command
  78. (concat (replace-regexp-in-string "\n$" "" (ambrevar/call-process-to-string "go" "env" "GOTOOLDIR")) "/guru"))))
  79. (defun ambrevar/go-turn-on-gofmt-before-save ()
  80. (add-hook 'before-save-hook #'gofmt-before-save nil t))
  81. (add-hook 'go-mode-hook 'ambrevar/go-turn-on-gofmt-before-save)
  82. (when (require 'go-eldoc nil t)
  83. (add-hook 'go-mode-hook 'go-eldoc-setup))
  84. (add-hook 'go-mode-hook 'ambrevar/go-set-compile-command)
  85. (defun ambrevar/godoc-setup ()
  86. (setq tab-width 8))
  87. (add-hook 'godoc-mode-hook 'ambrevar/godoc-setup)
  88. (define-skeleton ambrevar/go-main
  89. "Insert main function with basic includes."
  90. nil
  91. > "package main" "\n" \n
  92. "import (" \n
  93. "\"fmt\"" \n
  94. ")" > "\n" \n
  95. "func main() {" \n
  96. > @ _ \n
  97. "}" > \n)
  98. (provide 'init-go)