Skip to content

Commit 17eaba9

Browse files
committed
Add go-{freesymbols,doc,assembly,rename}
These four helper functions provide convenient ways to access gopls server functionality without the user needing to worry about how their LSP client does things. They should work for eglot and lsp-mode (though I haven't tested the latter). Updates dominikh/go-mode.el/#436
1 parent 6f4ff9e commit 17eaba9

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

go-mode.el

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,6 +3051,45 @@ This handles multi-line comments with a * prefix on each line."
30513051

30523052

30533053

3054+
;; Convenient go-* functions for gopls features available through code
3055+
;; actions, that work across LSP clients:
3056+
3057+
(defun go-mode--code-action (kind)
3058+
"Request and invoke the specified kind of code actions for the current selection."
3059+
(cond
3060+
((and (boundp 'eglot--managed-mode) eglot--managed-mode)
3061+
(let ((beg-end (eglot--code-action-bounds)))
3062+
(eglot-code-actions (car beg-end) (cadr beg-end) kind t)))
3063+
((and (boundp 'lsp-mode) lsp-mode)
3064+
(lsp-execute-code-action-by-kind kind))
3065+
(error "buffer is not managed by a known LSP client")))
3066+
3067+
(defun go-freesymbols ()
3068+
"View free symbols referenced by the current selection. Requires gopls v0.16."
3069+
(interactive)
3070+
(go-mode--code-action "source.freesymbols"))
3071+
3072+
(defun go-doc ()
3073+
"View documentation for the current Go package. Requires gopls v0.16."
3074+
(interactive)
3075+
(go-mode--code-action "source.doc"))
3076+
3077+
(defun go-assembly ()
3078+
"View assembly for the enclosing Go function. Requires gopls v0.16."
3079+
(interactive)
3080+
(go-mode--code-action "source.assembly"))
3081+
3082+
(defun go-rename ()
3083+
"Rename a Go symbol, prompting for the new name."
3084+
(interactive)
3085+
(cond
3086+
((and (boundp 'eglot--managed-mode) eglot--managed-mode)
3087+
(call-interactively #'eglot-rename))
3088+
((and (boundp 'lsp-mode) lsp-mode)
3089+
(call-interactively #'lsp-rename))
3090+
(error "buffer is not managed by a known LSP client")))
3091+
3092+
30543093
(provide 'go-mode)
30553094

30563095
;;; go-mode.el ends here

0 commit comments

Comments
 (0)