Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: documented imports function for Neovim LSP executes any available code action #47181

Closed
rentziass opened this issue Jul 14, 2021 · 3 comments
Labels
Documentation FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@rentziass
Copy link
Contributor

The Lua function for supporting imports with Neovim native LSP support documented here does work for imports, but also runs any other code action available in the current range.

For example, if your cursor is on an empty struct declaration like this

type A struct {
  name string
}

func example() {
  myStruct := &A{} # <- cursor on curly braces
}

and run the function with imports currently already organized, the next available code action that will be applied is filling that struct. So the result would be

type A struct {
  name string
}

func example() {
  myStruct := &A{
    name: "",
  }
}

The reason for that is the context being provided to gopls does not limit code actions of interest to just organizeImports:

local context = { source = { organizeImports = true } }

This would instead do what's intended here:

local context = { only = { "source.organizeImports" } }

Although I figured this out the hard way, this has also been discussed in the discussion cited in the docs 😄

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Jul 14, 2021
@gopherbot gopherbot added this to the Unreleased milestone Jul 14, 2021
@rentziass
Copy link
Contributor Author

Happy to submit a CL if this makes sense :)

@rentziass rentziass changed the title x/tools/gopls: documented imports function for Neovim executes any available code action x/tools/gopls: documented imports function for Neovim LSP executes any available code action Jul 14, 2021
@hyangah
Copy link
Contributor

hyangah commented Jul 14, 2021

Feel free to send a CL to fix the document.
cc @ainar-g

@hyangah hyangah modified the milestones: Unreleased, gopls/unplanned Jul 14, 2021
@gopherbot
Copy link

Change https://golang.org/cl/334749 mentions this issue: tools/gopls: fix imports function for Neovim LSP

@stamblerre stamblerre removed this from the gopls/unplanned milestone Jul 21, 2021
@stamblerre stamblerre added this to the gopls/v0.7.1 milestone Aug 4, 2021
@golang golang locked and limited conversation to collaborators Aug 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants