* Eglot gopls and organize imports
@ 2023-01-11 7:32 Sami M'Barek
0 siblings, 0 replies; only message in thread
From: Sami M'Barek @ 2023-01-11 7:32 UTC (permalink / raw)
To: help-gnu-emacs
I’m trying to get the code action of organize imports to work with before-save-hook. Specifically for go-mode.
I’ve been searching around and found a couple of issues later linking to a solution similar to this:
(defun sami/eglot-organize-imports ()
(call-interactively 'eglot-code-action-organize-imports))
(defun sami/before-saving-go ()
(add-hook 'before-save-hook #'eglot-format-buffer -10 t)
(add-hook 'before-save-hook #'sami/eglot-organize-imports nil t))
(add-hook 'go-mode-hook #'sami/before-saving-go)
Problem seems to be, running the function eglot-code-actions and selecting Organize imports works fine, but running the stand-alone function of eglot-code-action-organize-imports does not yield anything.
Test case:
Open emacs with emacs -q for an empty config
(require 'go-mode)
(require 'eglot)
(add-hook 'go-mode-hook #'eglot-ensure)
In any go file, add something like fmt.Println(“hello”) should import the package fmt automatically, deleting or commenting the line does not delete the imported package fmt. eglot-code-action-organize-imports does not delete the imported package fmt. Running the function eglot-code-actions and selecting Organize imports DOES delete the package fmt from imports.
Here is the client-request and server-response.
[client-request] (id:613) Mon Dec 5 23:43:48 2022:
(:jsonrpc "2.0" :id 613 :method "textDocument/codeAction" :params
(:textDocument
(:uri "file:///Users/sami/Code/Go/src/sami/something/temp.go")
:range
(:start
(:line 5 :character 6)
:end
(:line 5 :character 10))
:context
(:diagnostics
[]
:only
["source.organizeImports"])))
[server-reply] (id:613) Mon Dec 5 23:43:48 2022:
(:jsonrpc "2.0" :result
[(:title "Organize Imports" :kind "source.organizeImports" :edit
(:documentChanges
[(:textDocument
(:version 172 :uri "file:///Users/sami/Code/Go/src/sami/something/temp.go")
:edits
[(:range
(:start
(:line 3 :character 2)
:end
(:line 3 :character 2))
:newText "bytes\"\n \"crypto/tls\"\n \"encoding/json\"\n \"")
(:range
(:start
(:line 4 :character 2)
:end
(:line 4 :character 2))
:newText "go/token\"\n \"")
(:range
(:start
(:line 5 :character 10)
:end
(:line 5 :character 10))
:newText "\"\n \"sami/something/models\"\n \"sami/something/tool")])]))]
:id 613)
Single difference between the usage of eglot-code-action-organize-imports and not is
...
:only
["source.organizeImports"])))
...
emacs-28.2 (emacs-mac, railwaycat/emacsmacport/emacs-mac)
eglot-20221020.1010 (melpa)
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-01-11 7:32 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-11 7:32 Eglot gopls and organize imports Sami M'Barek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).