golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.42k stars 17.71k forks source link

x/tools/gopls: add gopls.package_info command #51490

Open jackieli-tes opened 2 years ago

jackieli-tes commented 2 years ago

gopls version

Build info
----------
golang.org/x/tools/gopls v0.8.0
    golang.org/x/tools/gopls@v0.8.0 h1:a71KO95TfIvCCMQJrZBSQIGQ9lkc0kWL+dSlEdZd7HI=
    github.com/BurntSushi/toml@v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
    github.com/google/go-cmp@v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/mod@v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
    golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
    golang.org/x/sys@v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
    golang.org/x/text@v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
    golang.org/x/tools@v0.1.10-0.20220303153236-fa15af63a6f1 h1:UVkOvSIhR/pX6OflsXS9hsDvaUJn8SLHqTlvdfUDiNo=
    golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
    honnef.co/go/tools@v0.2.2 h1:MNh1AVMyVX23VUHE2O27jm6lNj3vjO5DexS4A1xvnzk=
    mvdan.cc/gofumpt@v0.3.0 h1:kTojdZo9AcEYbQYhGuLf/zszYthRdhDNDUi2JKTxas4=
    mvdan.cc/xurls/v2@v2.3.0 h1:59Olnbt67UKpxF1EwVBopJvkSUBmgtb468E4GVWIZ1I=
go: go1.17.5

go env

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jackieli/.cache/go-build"
GOENV="/home/jackieli/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jackieli/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jackieli/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/linuxbrew/.linuxbrew/Cellar/go/1.17.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/linuxbrew/.linuxbrew/Cellar/go/1.17.5/libexec/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1338523989=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I'm developing a vim/neovim plugin that need to retrieve the package name of the current buffer. E.g. I'd like to show the current package name for the buffer in status line or I need to use the package name to filter symbols belongs to the current package using the package name as prefix

What work around did you consider?

I'm using go list at the moment as a work around. I see vim-go does the same.

The command executed is just go list -f {{.Name}} file.go with the file.go's folder as cwd.

What solution do you want to see?

I think this information should be provided as a LSP command. I propose:

Identifier: gopls.package_info

Retrieve the package info for a given go file.

Args:

{
    // The go file URI
    "URI": string,
}

Result:

{
    // Name is the package name
    "Name": string,
    // ContainerName is the full package import name
    "ContainerName": string,
}

Editor and settings

I'm using neovim 0.6.1 with coc.nvim. Using the default instruction from gopls editor section

hyangah commented 2 years ago

Interestingly, vscode-go wants a similar feature when replacing the go-outline-based document symbol provider with gopls document symbol provider. vscode-go currently uses a (not-accurate) heuristic that parses the package statement. (cc @suzmue) For vscode-go, it would be convenient if this information was provided together with what gopls.list_imports provided.

gopherbot commented 2 years ago

Change https://go.dev/cl/403615 mentions this issue: [dev.boringcrypto] README.boringcrypto.md: add note about the demise of the branch