Skip to content

Commit

Permalink
feat: fix golang lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonjoe committed May 27, 2024
1 parent c1c27b0 commit df2b3fd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 102 deletions.
27 changes: 27 additions & 0 deletions lisp/init-copilot-local.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
;;; init-copilot-local --- Support copilot
;;; Commentary:
;;; Code:

(add-to-list 'load-path
(expand-file-name (concat user-emacs-directory "lisp/copilot.el")))
(require 'copilot)
;; copilot automatically provide completions
(add-hook 'prog-mode-hook 'copilot-mode)

; complete by copilot first, then auto-complete
(defun my-tab ()
(interactive)
(or (copilot-accept-completion)
(ac-expand nil)))

(with-eval-after-load 'auto-complete
; disable inline preview
(setq ac-disable-inline t)
; show menu if have only one candidate
(setq ac-candidate-menu-min 0))

(define-key copilot-completion-map (kbd "<tab>") 'copilot-accept-completion)
(define-key copilot-completion-map (kbd "TAB") 'copilot-accept-completion)

(provide 'init-copilot-local)
;;; init-copilot-local.el ends here
93 changes: 18 additions & 75 deletions lisp/init-golang-local.el
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
;;; golang config --- Support golang
;;; init-golang-local.el --- Support golang
;;; Commentary:
;;; Code:


(require-package 'go-mode)
(require-package 'go-tag)
(require-package 'go-stacktracer)
Expand All @@ -22,123 +21,67 @@
(require-package 'go-dlv)
(require-package 'gorepl-mode)


(ac-config-default)
(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize))

;; (setenv "GO111MODULE" "off")



(global-flycheck-mode)
(with-eval-after-load 'go-mode
(autoload 'flycheck-get-checker-for-buffer "golint")
(autoload 'flycheck-get-checker-for-buffer "govet")
(autoload 'flycheck-get-checker-for-buffer "flycheck"))
;; 添加新的检测器
;; (add-to-list 'flycheck-checkers 'golangci-lint)

(setq flycheck-golangci-lint-config "~/.emacs.d/conf/.golangci.yml")
;; 设置timemout
(setq flycheck-golangci-lint-deadline "10m")
(eval-after-load 'flycheck
'(add-hook '-mode-hook #'flycheck-golangci-lint-setup))
;; (eval-after-load 'flycheck
;; '(add-hook 'flycheck-mode-hook 'flycheck-golangci-lint-setup))
;; skips 'vendor' directories and sets GO15VENDOREXPERIMENT=1
'(add-hook 'go-mode-hook #'flycheck-golangci-lint-setup))

(setq flycheck-gometalinter-vendor t)
;; only show errors
;; (setq flycheck-gometalinter-errors-only t)
;; only run fast linters
;; (setq flycheck-gometalinter-fast t)
;; use in tests files
;; (setq flycheck-gometalinter-tests t)
;; disable linters
;; (setq flycheck-gometalinter-disable-linters '("gotype" "gocyclo"))
;; Only enable selected linters
;; (setq flycheck-gometalinter-disable-all t)
(setq flycheck-gometalinter-enable-linters '("golangci-lint"))
;; Set different deadline (default: 5s)
(setq flycheck-gometalinter-deadline "10s")
;; Use a gometalinter configuration file (default: nil)
(setq flycheck-gometalinter-config "~/.emacs.d/config/.gometalinter-config.json")
;; (setq flycheck-check-syntax-automatically '(mode-enabled save))
;; (setq flycheck-check-syntax-automatically '(add-hook save))


;;; C-c C-r注释未使用的引入
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-r") 'go-remove-unused-imports)))

;; ;;; 跳转引入
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-g") 'go-goto-imports)))

(local-set-key (kbd "C-c C-r") 'go-remove-unused-imports)
(local-set-key (kbd "C-c C-g") 'go-goto-imports)
(local-set-key (kbd "C-c C-f") 'gofmt)
(local-set-key (kbd "C-c C-k") 'godoc)))

;; How to set gofmt-args
;; I think gofmt-args is a list. Can you try again with (setq gofmt-args '("-local" "github.com"))?
;; https://github.com/dominikh/go-mode.el/issues/365
(setq gofmt-command "goimports")
(setq gofmt-args '("-local" "git.code.oa.com"))
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-f") 'gofmt)))

(add-hook 'before-save-hook 'gofmt-before-save)
(defun run-go-mod-tidy ()
"Run `go mod tidy` in the current project."
(when (string-equal major-mode 'go-mode)
(shell-command "go mod tidy")))

(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-k") 'godoc)))
(add-hook 'before-save-hook 'gofmt-before-save)
(add-hook 'before-save-hook 'run-go-mod-tidy)

(setq go-test-args "-gcflags=all=-l")

;; go auto comment
;; go get -u github.com/Gnouc/gocmt
(defun maple/go-auto-comment()
(interactive)
(setq gocmt (concat "gocmt -i " buffer-file-name))
(shell-command-on-region
(point-min) (point-max)
gocmt))

(shell-command-on-region (point-min) (point-max) gocmt))


;; 支持以下转换
;; snakecase:BaseDomain- >base_domain
;; camelcase:BaseDomain- >baseDomain
;; lispcase:BaseDomain- >base-domain
;; eg: (setq go-tag-args (list "-transform" "camelcase"))
(setq go-tag-args (list "-transform" "snakecase"))


(defun alison-go-tag-add (n)
"Pelase select type for TRANSFORM."
"Please select type for TRANSFORM."
(interactive "xSelect transform 1->base_domain 2->baseDomain 3->base-domain 4->BaseDomain:")
(setq selectForm (pcase n
(1 "snakecase")
(2 "camelcase")
(3 "lispcase")
(4 "keep")
))
(4 "keep")))
(setq go-tag-args (list "-transform" selectForm)))


(with-eval-after-load 'go-mode
(define-key go-mode-map (kbd "C-c t") #'go-tag-add)
(define-key go-mode-map (kbd "C-c s") #'alison-go-tag-add)
(define-key go-mode-map (kbd "C-c T") #'go-tag-remove))
;;
;; (go-guru-hl-identifier-mode)
;; (add-hook 'go-mode-hook #'go-guru-hl-identifier-mode)
;; (add-hook 'go-mode-hook 'go-eldoc-setup)
;; (set-face-attribute 'eldoc-highlight-function-argument nil
;; :underline t :foreground "green"
;; :weight 'bold)

;; (defun my-go-gen-test-setup ()
;; "My keybindings for generating go tests."
;; (interactive)
;; (local-set-key (kbd "C-c C-g") #'go-gen-test-dwim))

;; (add-hook 'go-mode-hook #'my-go-gen-test-setup)

(provide 'init-golang-local)
;;; init-golang-local.el ends here

1 change: 1 addition & 0 deletions lisp/init-local.el
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
(require 'init-git-messenger-local)
(require 'insert-translated-name)
(require 'init-leetcode-local)
(require 'init-copilot-local)
;;(require 'init-go-translate-local)

(provide 'init-local)
Expand Down
64 changes: 37 additions & 27 deletions lisp/init-lsp-mode-local.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; lsp-mode -- Support lsp-mode
;;; init-lsp-mode-local.el --- Support lsp-mode
;;; Commentary:
;;; Code:

Expand All @@ -7,37 +7,41 @@
(require-package 'lsp-treemacs)
(require-package 'dap-mode)
(require-package 'helm-lsp)
;; (require-package 'project)
;; (require-package 'eglot)
(require-package 'projectile)

(add-hook 'python-mode-hook #'lsp-deferred)
(add-hook 'c++-mode-hook #'lsp-deferred)
(add-hook 'c-mode-hook #'lsp-deferred)
(add-hook 'rust-mode-hook #'lsp-deferred)
(add-hook 'html-mode-hook #'lsp-deferred)
(add-hook 'js-mode-hook #'lsp-deferred)
(add-hook 'typescript-mode-hook #'lsp-deferred)
(add-hook 'json-mode-hook #'lsp-deferred)
(add-hook 'yaml-mode-hook #'lsp-deferred)
(add-hook 'dockerfile-mode-hook #'lsp-deferred)
(add-hook 'shell-mode-hook #'lsp-deferred)
(add-hook 'css-mode-hook #'lsp-deferred)
(add-hook 'elisp-mode-hook #'lsp-deferred)
(add-hook 'go-mode-hook #'lsp-deferred)
(add-hook 'go-mode-hook #'yas-minor-mode)
(dolist (hook '(python-mode-hook
c++-mode-hook
c-mode-hook
rust-mode-hook
html-mode-hook
js-mode-hook
typescript-mode-hook
json-mode-hook
yaml-mode-hook
dockerfile-mode-hook
shell-mode-hook
css-mode-hook
elisp-mode-hook
go-mode-hook))
(add-hook hook #'lsp-deferred)
(add-hook hook #'yas-minor-mode))

;; Company mode
(setq gc-cons-threshold (* 100 1024 1024)
read-process-output-max (* 1024 1024)
treemacs-space-between-root-nodes nil
company-idle-delay 0.0
company-minimum-prefix-length 1
lsp-idle-delay 0.1) ;; clangd is fast
lsp-idle-delay 0.1
lsp-log-io t)

(with-eval-after-load 'lsp-mode
(add-hook 'lsp-mode-hook #'lsp-enable-which-key-integration)
(require 'dap-cpptools)
(yas-global-mode))
(yas-global-mode)
(lsp-register-custom-settings
'(("gopls.completeUnimported" t t)
("gopls.staticcheck" t t)
("gopls.usePlaceholders" t t))))

(lsp-treemacs-sync-mode t)

Expand All @@ -47,20 +51,26 @@
(defun company-yasnippet/disable-after-dot (fun command &optional arg &rest _ignore)
(if (eq command 'prefix)
(let ((prefix (funcall fun 'prefix)))
(when (and prefix (not
(eq
(char-before (- (point) (length prefix)))
?.)))
(when (and prefix (not (eq (char-before (- (point) (length prefix))) ?.)))
prefix))
(funcall fun command arg)))

(advice-add #'company-yasnippet :around #'company-yasnippet/disable-after-dot)

;; Ensure projectile is used to manage project switching
(projectile-mode +1)

;; (setq lsp-clients-python-command "${HOME}/.local/bin/pyls")
;; (setq lsp-clients-rust-command "${HOME}/.cargo/bin/rls")
;; Track the previous project
(defvar my/last-project nil)

(defun my/switch-project-hook ()
"Hook to run when switching projects with projectile."
(when (and my/last-project
(not (string= my/last-project (projectile-project-root))))
(lsp-workspace-shutdown))
(setq my/last-project (projectile-project-root)))

(add-hook 'projectile-after-switch-project-hook 'my/switch-project-hook)

(provide 'init-lsp-mode-local)
;;; init-lsp-mode-local.el ends here

0 comments on commit df2b3fd

Please sign in to comment.