Skip to content

x/tools/gopls: autocompletion and highlighting for template variables, like GoLand's gotype #64385

@mortenson

Description

@mortenson

gopls version

v0.14.2

go env

$ go env
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/redacted/Library/Caches/go-build'
GOENV='/Users/redacted/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/redacted/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/redacted/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.4'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/redacted/project/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/k3/11fb815x3wv5ykpbf2mq_7600000gn/T/go-build1447506697=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Configured VSCode to support Go templates with gopls.

What did you expect to see?

A way to tell gopls the types of my template variables, and have those types reflected in highlighting and autocomplete. GoLand does this with comments (ref https://www.jetbrains.com/help/go/integration-with-go-templates.html). I think the comments are slightly ugly, so not sure how to address that. Having a way for my custom template functions to work too would be great but isn't as critical.

Besides being good for DX, I think this could catch a ton of typing errors in templates. I currently use https://github.com/jba/templatecheck for that in tests, but that won't help me when viewing/writing tempalte code.

This was referenced in this issue by @a-h : #36911, but wasn't really followed up on, which is why I'm filing this feature request.

What did you see instead?

No highlighting or autocomplete for template variable types. Hovering over template variables or functions just says field or method.

Editor and settings

VSCode with this config:

{
    "gopls": {
         "build.templateExtensions": ["gtpl"],
         "ui.semanticTokens": true
    },
    "files.associations": {
        "*.gtpl": "gotmpl"
    }
}

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.ToolsThis 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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions