[tools] gopls: Improve auto-imports example for NeoVim LSP

1,718 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Aug 6, 2022, 9:10:28 AM8/6/22
to Antoine Cotten, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

View Change

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 1
    Gerrit-Owner: Antoine Cotten <he...@acotten.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Sat, 06 Aug 2022 13:10:24 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Antoine Cotten (Gerrit)

    unread,
    Aug 6, 2022, 10:25:51 AM8/6/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Antoine Cotten uploaded patch set #3 to this change.

    View Change

    gopls: Improve auto-imports example for NeoVim LSP

    Follow Lua Style Guide for function names.

    Query the client's offset encoding, with a fallback to utf-8.

    Omit handling of r.command since the source.organizeImports code action
    doesn't return any.

    Omit passing a value for wait_ms. It is optional and defaults to 1000
    (see NeoVim help for vim.lsp.buf_request_sync).

    Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    ---
    M gopls/doc/vim.md
    1 file changed, 24 insertions(+), 6 deletions(-)

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 3
    Gerrit-Owner: Antoine Cotten <he...@acotten.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-MessageType: newpatchset

    Antoine Cotten (Gerrit)

    unread,
    Aug 6, 2022, 10:25:51 AM8/6/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Antoine Cotten uploaded patch set #2 to this change.

    View Change

    gopls: Improve auto-imports example for NeoVim LSP

    Follow Lua Style Guide for function names.

    Query the client's offset encoding, with a fallback to utf-8.

    Omit handling of r.command since the source.organizeImports code action
    doesn't return any.

    Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    ---
    M gopls/doc/vim.md
    1 file changed, 20 insertions(+), 5 deletions(-)

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 2

    Antoine Cotten (Gerrit)

    unread,
    Aug 6, 2022, 10:25:51 AM8/6/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Antoine Cotten has uploaded this change for review.

    View Change

    gopls: Improve auto-imports example for NeoVim LSP

    Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    ---
    M gopls/doc/vim.md
    1 file changed, 13 insertions(+), 5 deletions(-)

    diff --git a/gopls/doc/vim.md b/gopls/doc/vim.md
    index d9b33ac..bd8944e 100644
    --- a/gopls/doc/vim.md
    +++ b/gopls/doc/vim.md
    @@ -175,16 +175,15 @@
    lua <<EOF
    -- …

    - function OrgImports(wait_ms)
    + function go_org_imports(wait_ms)
    local params = vim.lsp.util.make_range_params()
    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 cid, 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, "UTF-8")
    - else
    - vim.lsp.buf.execute_command(r.command)
    + local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-8"
    + vim.lsp.util.apply_workspace_edit(r.edit, enc)
    end
    end
    end

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 1
    Gerrit-Owner: Antoine Cotten <he...@acotten.com>
    Gerrit-MessageType: newchange

    Robert Findley (Gerrit)

    unread,
    Aug 8, 2022, 11:31:09 AM8/8/22
    to Antoine Cotten, goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

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

    View Change

    1 comment:

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 3
    Gerrit-Owner: Antoine Cotten <he...@acotten.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: Antoine Cotten <he...@acotten.com>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Comment-Date: Mon, 08 Aug 2022 15:31:05 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Antoine Cotten (Gerrit)

    unread,
    Aug 8, 2022, 1:08:38 PM8/8/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

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

    Antoine Cotten uploaded patch set #4 to this change.

    View Change

    gopls: Improve auto-imports example for NeoVim LSP

    Follow Lua Style Guide for function names.

    Query the client's offset encoding, with a fallback to utf-16.


    Omit handling of r.command since the source.organizeImports code action
    doesn't return any.

    Omit passing a value for wait_ms. It is optional and defaults to 1000
    (see NeoVim help for vim.lsp.buf_request_sync).

    Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    ---
    M gopls/doc/vim.md
    1 file changed, 24 insertions(+), 6 deletions(-)

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 4
    Gerrit-Owner: Antoine Cotten <he...@acotten.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: Antoine Cotten <he...@acotten.com>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-MessageType: newpatchset

    Antoine Cotten (Gerrit)

    unread,
    Aug 8, 2022, 1:15:13 PM8/8/22
    to goph...@pubsubhelper.golang.org, Robert Findley, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

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

    View Change

    1 comment:

    • File gopls/doc/vim.md:

      • Huh, I'm not actually sure whether this is correct. […]

        Great point, I ignored that utf-16 was the default.

        My main goal on this line was actually to guard against `get_client_by_id()` potentially returning `nil`, which would raise the following error:

          E5108: Error executing lua [...] attempt to index global 'client' (a nil value)

        I just fixed the default encoding.

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 4
    Gerrit-Owner: Antoine Cotten <he...@acotten.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: Robert Findley <rfin...@google.com>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Comment-Date: Mon, 08 Aug 2022 17:15:06 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Robert Findley <rfin...@google.com>
    Gerrit-MessageType: comment

    Robert Findley (Gerrit)

    unread,
    Aug 8, 2022, 1:27:19 PM8/8/22
    to Antoine Cotten, goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

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

    View Change

    1 comment:

    • File gopls/doc/vim.md:

      • Great point, I ignored that utf-16 was the default. […]

        Thanks!

        Since we're looking at it, do you think you could confirm that the previous setting was broken? For example, can you try organizing imports in the following file?

        ```
        package main

        import (
        "mod.test/a𐐀b"
        	"fmt"
        )
        func main() {
        fmt.Println("foo")
        }
        ```

        Does the previous setting result in corrupted state? (a𐐀b has length 6 on utf-16 and 8 on utf-8).

        That would make me feel more confident that we're doing the right thing here, since I'm not familiar with the lua client.

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 4
    Gerrit-Owner: Antoine Cotten <he...@acotten.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: Antoine Cotten <he...@acotten.com>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Comment-Date: Mon, 08 Aug 2022 17:27:14 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Robert Findley <rfin...@google.com>
    Comment-In-Reply-To: Antoine Cotten <he...@acotten.com>
    Gerrit-MessageType: comment

    Antoine Cotten (Gerrit)

    unread,
    Aug 8, 2022, 2:39:06 PM8/8/22
    to goph...@pubsubhelper.golang.org, Robert Findley, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

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

    View Change

    1 comment:

    • File gopls/doc/vim.md:

      • Thanks! […]

        This is going to be very disappointing but no, reverting the code then executing the function manually inside the buffer yields a perfectly valid file.

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 4
    Gerrit-Owner: Antoine Cotten <he...@acotten.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: Robert Findley <rfin...@google.com>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Comment-Date: Mon, 08 Aug 2022 18:39:00 +0000

    Robert Findley (Gerrit)

    unread,
    Aug 9, 2022, 11:19:25 AM8/9/22
    to Antoine Cotten, goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

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

    Patch set 4:Code-Review +2

    View Change

    1 comment:

    • File gopls/doc/vim.md:

      • This is going to be very disappointing but no, reverting the code then executing the function manual […]

        Hmm... well I suppose let's merge this, as it seems more correct.

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

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
    Gerrit-Change-Number: 421295
    Gerrit-PatchSet: 4
    Gerrit-Owner: Antoine Cotten <he...@acotten.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: Antoine Cotten <he...@acotten.com>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Comment-Date: Tue, 09 Aug 2022 15:19:19 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes

    Dylan Le (Gerrit)

    unread,
    Aug 9, 2022, 11:21:19 AM8/9/22
    to Antoine Cotten, goph...@pubsubhelper.golang.org, Robert Findley, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

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

    Patch set 4:Code-Review +1

    View Change

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

      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
      Gerrit-Change-Number: 421295
      Gerrit-PatchSet: 4
      Gerrit-Owner: Antoine Cotten <he...@acotten.com>
      Gerrit-Reviewer: Dylan Le <dungt...@google.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: Antoine Cotten <he...@acotten.com>
      Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
      Gerrit-Comment-Date: Tue, 09 Aug 2022 15:21:15 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      Gerrit-MessageType: comment

      Jamal Carvalho (Gerrit)

      unread,
      Aug 9, 2022, 11:21:37 AM8/9/22
      to Antoine Cotten, goph...@pubsubhelper.golang.org, Dylan Le, Robert Findley, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

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

      Patch set 4:Code-Review +1

      View Change

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

        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
        Gerrit-Change-Number: 421295
        Gerrit-PatchSet: 4
        Gerrit-Owner: Antoine Cotten <he...@acotten.com>
        Gerrit-Reviewer: Dylan Le <dungt...@google.com>
        Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
        Gerrit-Reviewer: Jamal Carvalho <ja...@golang.org>
        Gerrit-Reviewer: Robert Findley <rfin...@google.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-Attention: Antoine Cotten <he...@acotten.com>
        Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
        Gerrit-Comment-Date: Tue, 09 Aug 2022 15:21:31 +0000

        Robert Findley (Gerrit)

        unread,
        Aug 9, 2022, 11:22:02 AM8/9/22
        to Antoine Cotten, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Jamal Carvalho, Dylan Le, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

        Robert Findley submitted this change.

        View Change


        Approvals: Jamal Carvalho: Looks good to me, but someone else must approve Dylan Le: Looks good to me, but someone else must approve Robert Findley: Looks good to me, approved
        gopls: Improve auto-imports example for NeoVim LSP

        Follow Lua Style Guide for function names.

        Query the client's offset encoding, with a fallback to utf-16.

        Omit handling of r.command since the source.organizeImports code action
        doesn't return any.

        Omit passing a value for wait_ms. It is optional and defaults to 1000
        (see NeoVim help for vim.lsp.buf_request_sync).

        Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
        Reviewed-on: https://go-review.googlesource.com/c/tools/+/421295
        Reviewed-by: Dylan Le <dungt...@google.com>
        Reviewed-by: Robert Findley <rfin...@google.com>
        Reviewed-by: Jamal Carvalho <ja...@golang.org>
        ---
        M gopls/doc/vim.md
        1 file changed, 28 insertions(+), 6 deletions(-)

        diff --git a/gopls/doc/vim.md b/gopls/doc/vim.md
        index d9b33ac..af54a7e 100644
        --- a/gopls/doc/vim.md
        +++ b/gopls/doc/vim.md
        @@ -175,23 +175,22 @@

        lua <<EOF
        -- …

        - function OrgImports(wait_ms)
        + function go_org_imports(wait_ms)
        local params = vim.lsp.util.make_range_params()
        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 cid, 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, "UTF-8")
        - else
        - vim.lsp.buf.execute_command(r.command)
        +          local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16"

        + vim.lsp.util.apply_workspace_edit(r.edit, enc)
        end
        end
        end
           end
        EOF

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

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

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

        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: I5e5eb6cee3a46fee1edc1e6d15b40ad88498a26c
        Gerrit-Change-Number: 421295
        Gerrit-PatchSet: 5
        Gerrit-Owner: Antoine Cotten <he...@acotten.com>
        Gerrit-Reviewer: Dylan Le <dungt...@google.com>
        Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
        Gerrit-Reviewer: Jamal Carvalho <ja...@golang.org>
        Gerrit-Reviewer: Robert Findley <rfin...@google.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-MessageType: merged
        Reply all
        Reply to author
        Forward
        0 new messages