Skip to content

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

@rentziass

Description

@rentziass

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 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions