[tools] gopls: update neovim documentation for imitating goimports

461 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Mar 2, 2022, 6:25:15 AM3/2/22
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

gopls: update neovim documentation for imitating goimports

The existing example is no longer recommended as it can have unintended
consequences; for example, if it is run while the cursor is in an
incomplete struct, it will fill in all struct fields.

This is described more at https://github.com/neovim/nvim-lspconfig/issues/115#issuecomment-866632451

Change-Id: I3640ddb747a69c864675c0483032037a5e777d59
GitHub-Last-Rev: 2335f017eb967785da28ddb1bc48fb820d59a07e
GitHub-Pull-Request: golang/tools#371
---
M gopls/doc/vim.md
1 file changed, 28 insertions(+), 26 deletions(-)

diff --git a/gopls/doc/vim.md b/gopls/doc/vim.md
index 7417d36..887a246 100644
--- a/gopls/doc/vim.md
+++ b/gopls/doc/vim.md
@@ -175,38 +175,23 @@
lua <<EOF
-- …

- function goimports(timeout_ms)
- local context = { only = { "source.organizeImports" } }
- vim.validate { context = { context, "t", true } }
-
+ function OrgImports(wait_ms)
local params = vim.lsp.util.make_range_params()
- params.context = context
-
- -- See the implementation of the textDocument/codeAction callback
- -- (lua/vim/lsp/handler.lua) for how to do this properly.
- local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, timeout_ms)
- if not result or next(result) == nil then return end
- local actions = result[1].result
- if not actions then return end
- local action = actions[1]
-
- -- textDocument/codeAction can return either Command[] or CodeAction[]. If it
- -- is a CodeAction, it can have either an edit, a command or both. Edits
- -- should be executed first.
- if action.edit or type(action.command) == "table" then
- if action.edit then
- vim.lsp.util.apply_workspace_edit(action.edit)
+ params.context = {only = {"source.organizeImports"}}
+ local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, wait_ms)
+ for _, res in pairs(result or {}) do
+ for _, r in pairs(res.result or {}) do
+ if r.edit then
+ vim.lsp.util.apply_workspace_edit(r.edit)
+ else
+ vim.lsp.buf.execute_command(r.command)
+ end
end
- if type(action.command) == "table" then
- vim.lsp.buf.execute_command(action.command)
- end
- else
- vim.lsp.buf.execute_command(action)
end
end
EOF

-autocmd BufWritePre *.go lua goimports(1000)
+autocmd BufWritePre *.go lua OrgImports(1000)
```

(Taken from the [discussion][nvim-lspconfig-imports] on Neovim issue tracker.)

To view, visit change 389194. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I3640ddb747a69c864675c0483032037a5e777d59
Gerrit-Change-Number: 389194
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-MessageType: newchange

Robert Findley (Gerrit)

unread,
Mar 2, 2022, 9:51:54 AM3/2/22
to Gerrit Bot, goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: Hyang-Ah Hana Kim.

Patch set 1:Run-TryBot +1Code-Review +2Trust +1

View Change

    To view, visit change 389194. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I3640ddb747a69c864675c0483032037a5e777d59
    Gerrit-Change-Number: 389194
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Reviewer: Robert Findley <rfin...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Comment-Date: Wed, 02 Mar 2022 14:51:51 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Hyang-Ah Hana Kim (Gerrit)

    unread,
    Mar 2, 2022, 9:53:16 AM3/2/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, Robert Findley, Gopher Robot, golang-co...@googlegroups.com

    Patch set 1:Trust +1

    View Change

      To view, visit change 389194. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I3640ddb747a69c864675c0483032037a5e777d59
      Gerrit-Change-Number: 389194
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
      Gerrit-Reviewer: Robert Findley <rfin...@google.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>

      kokoro (Gerrit)

      unread,
      Mar 2, 2022, 9:58:28 AM3/2/22
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, Robert Findley, Gopher Robot, golang-co...@googlegroups.com

      Kokoro presubmit build finished with status: SUCCESS
      Logs at: https://source.cloud.google.com/results/invocations/70bd2f4b-da71-485c-826f-989e8fb29d79

      Patch set 1:gopls-CI +1

      View Change

        To view, visit change 389194. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: I3640ddb747a69c864675c0483032037a5e777d59
        Gerrit-Change-Number: 389194
        Gerrit-PatchSet: 1
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
        Gerrit-Reviewer: Robert Findley <rfin...@google.com>
        Gerrit-Reviewer: kokoro <noreply...@google.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-Comment-Date: Wed, 02 Mar 2022 14:58:24 +0000

        Robert Findley (Gerrit)

        unread,
        Mar 2, 2022, 10:07:33 AM3/2/22
        to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Gopher Robot, kokoro, Hyang-Ah Hana Kim, golang-co...@googlegroups.com

        Robert Findley submitted this change.

        View Change


        Approvals: Robert Findley: Looks good to me, approved; Trusted; Run TryBots Hyang-Ah Hana Kim: Trusted Gopher Robot: TryBots succeeded kokoro: gopls CI succeeded
        gopls: update neovim documentation for imitating goimports

        The existing example is no longer recommended as it can have unintended
        consequences; for example, if it is run while the cursor is in an
        incomplete struct, it will fill in all struct fields.

        This is described more at https://github.com/neovim/nvim-lspconfig/issues/115#issuecomment-866632451

        Change-Id: I3640ddb747a69c864675c0483032037a5e777d59
        GitHub-Last-Rev: 2335f017eb967785da28ddb1bc48fb820d59a07e
        GitHub-Pull-Request: golang/tools#371
        Reviewed-on: https://go-review.googlesource.com/c/tools/+/389194
        Reviewed-by: Robert Findley <rfin...@google.com>
        Trust: Robert Findley <rfin...@google.com>
        Run-TryBot: Robert Findley <rfin...@google.com>
        Trust: Hyang-Ah Hana Kim <hya...@gmail.com>
        gopls-CI: kokoro <noreply...@google.com>
        TryBot-Result: Gopher Robot <go...@golang.org>
        ---
        M gopls/doc/vim.md
        1 file changed, 35 insertions(+), 26 deletions(-)

        To view, visit change 389194. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: I3640ddb747a69c864675c0483032037a5e777d59
        Gerrit-Change-Number: 389194
        Gerrit-PatchSet: 2
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
        Gerrit-Reviewer: Robert Findley <rfin...@google.com>
        Gerrit-Reviewer: kokoro <noreply...@google.com>
        Gerrit-MessageType: merged
        Reply all
        Reply to author
        Forward
        0 new messages