This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/layers/+lang/go/funcs.el
Tim Heckman e84bd90262 Set lsp-prefer-flymake to :none when Go layer is using LSP
When switching the Go layer to use the LSP backend with `golangci-lint`, I
noticed that none of my linter errors were being rendered with my source code.
Digging in further, I eventually learned it was due to `lsp-prefer-flymake`
being set to `nil` and not `:none`.

This change updates the Go layer, when using LSP and golangci-lint, to set
`lsp-prefer-flymake` to `:none`. If this is not set, the golangci-lint errors
will not be reported.

This is an alternative to #12043.

Fixes #11680

Signed-off-by: Tim Heckman <t@heckman.io>
2019-04-04 12:46:49 +02:00

123 lines
4.6 KiB
EmacsLisp

;;; funcs.el --- Go Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun spacemacs//go-set-tab-width ()
"Set the tab width."
(when go-tab-width
(setq-local tab-width go-tab-width)))
(defun spacemacs//go-setup-backend ()
"Conditionally setup go backend"
(pcase go-backend
('lsp (spacemacs//go-setup-backend-lsp))))
(defun spacemacs//go-setup-company ()
"Conditionally setup go company based on backend"
(pcase go-backend
('go-mode (spacemacs//go-setup-company-go))
('lsp (spacemacs//go-setup-company-lsp))))
(defun spacemacs//go-setup-company-go ()
(spacemacs|add-company-backends
:backends company-go
:modes go-mode
:variables company-go-show-annotation t
:append-hooks nil
:call-hooks t)
(company-mode))
(defun spacemacs//go-setup-backend-lsp ()
"Setup lsp backend"
(if (configuration-layer/layer-used-p 'lsp)
(progn
;; without setting lsp-prefer-flymake to :none
;; golangci-lint errors won't be reported
(when go-use-golangci-lint
(message "[go] Setting lsp-prefer-flymake :none to enable golangci-lint support.")
(setq-local lsp-prefer-flymake :none))
(lsp))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
(defun spacemacs//go-setup-company-lsp ()
"Setup lsp auto-completion"
(if (configuration-layer/layer-used-p 'lsp)
(progn
(spacemacs|add-company-backends
:backends company-lsp
:modes go-mode
:append-hooks nil
:call-hooks t)
(company-mode))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
(defun spacemacs//go-enable-gometalinter ()
"Enable `flycheck-gometalinter' and disable overlapping `flycheck' linters."
(setq flycheck-disabled-checkers '(go-gofmt
go-golint
go-vet
go-build
go-test
go-errcheck))
(flycheck-gometalinter-setup))
(defun spacemacs//go-enable-golangci-lint ()
"Enable `flycheck-golangci-lint' and disable overlapping `flycheck' linters."
(setq flycheck-disabled-checkers '(go-gofmt
go-golint
go-vet
go-build
go-test
go-errcheck))
(flycheck-golangci-lint-setup))
(defun spacemacs/go-run-tests (args)
(interactive)
(compilation-start (concat "go test " (when go-test-verbose "-v ") args " " go-use-test-args)
nil (lambda (n) go-test-buffer-name) nil))
(defun spacemacs/go-run-package-tests ()
(interactive)
(spacemacs/go-run-tests ""))
(defun spacemacs/go-run-package-tests-nested ()
(interactive)
(spacemacs/go-run-tests "./..."))
(defun spacemacs/go-run-test-current-function ()
(interactive)
(if (string-match "_test\\.go" buffer-file-name)
(let ((test-method (if go-use-gocheck-for-testing
"-check.f"
"-run")))
(save-excursion
(re-search-backward "^func[ ]+\\(([[:alnum:]]*?[ ]?[*]?[[:alnum:]]+)[ ]+\\)?\\(Test[[:alnum:]_]+\\)(.*)")
(spacemacs/go-run-tests (concat test-method "='" (match-string-no-properties 2) "$'"))))
(message "Must be in a _test.go file to run go-run-test-current-function")))
(defun spacemacs/go-run-test-current-suite ()
(interactive)
(if (string-match "_test\.go" buffer-file-name)
(if go-use-gocheck-for-testing
(save-excursion
(re-search-backward "^func[ ]+\\(([[:alnum:]]*?[ ]?[*]?\\([[:alnum:]]+\\))[ ]+\\)?Test[[:alnum:]_]+(.*)")
(spacemacs/go-run-tests (concat "-check.f='" (match-string-no-properties 2) "'")))
(message "Gocheck is needed to test the current suite"))
(message "Must be in a _test.go file to run go-test-current-suite")))
(defun spacemacs/go-run-main ()
(interactive)
(shell-command
(format "go run %s"
(shell-quote-argument (or (file-remote-p (buffer-file-name (buffer-base-buffer)) 'localname)
(buffer-file-name (buffer-base-buffer)))))))
(defun spacemacs/go-packages-gopkgs ()
"Return a list of all Go packages, using `gopkgs'."
(sort (process-lines "gopkgs") #'string<))