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: option for not automatically inserting brackets #43903

Closed
jwalton opened this issue Jan 25, 2021 · 10 comments · May be fixed by golang/tools#272
Closed

x/tools/gopls: option for not automatically inserting brackets #43903

jwalton opened this issue Jan 25, 2021 · 10 comments · May be fixed by golang/tools#272
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@jwalton
Copy link

jwalton commented Jan 25, 2021

What version of Go are you using (go version)?

$ go version
go version go1.15.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jwalton/Library/Caches/go-build"
GOENV="/Users/jwalton/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/jwalton/.go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jwalton/.go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/jwalton/solink/solink-cli/go.mod"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/42/y3h31nrx5c98b7sq_vqskskw0000gn/T/go-build712473597=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

In VSCode, if I type fmt.Prin<tab>, then VSCode replaces what I typed with fmt.Printf() and places my cursor between the brackets. There seems to be no option to disable auto-adding the brackets.

In Typescript, if I type console.lo<tab>, VSCode fills in console.log, and I have to type the "(". Since I spend 90% of my time in Typescript, I'm kinda hard wired to typing console.lo<tab>(. In Golang, because I type that extra bracket (and VSCode isn't smart enough to "overtype" the opening bracket) it adds an extra bracket, which I then have to go back and remove. In fact, in golang if I type fmt.Prin<tab>("test"), then you might expect this to "work out" because I ought to get fmt.Println(("test")), and then the code formatter would fix it (assuming I only have one variable at any rate), but if you try it you'll actually get fmt.Println(("test") because VSCode will "overtype" the closing bracket for you but not the opening bracket, so you end up with a syntax error, even though you typed something perfectly sensible.

@hyangah directed me here, and noted that in order to implement this option in VSCode, support would need to be added to gopls.

I would also reference this issue in the old vscode-go repo, where MS explained they didn't auto-add the ()s because "this is not the standard interaction model for VS Code." I understand why some people might like this behavior, but there should at least be an option to turn it off.

What did you expect to see?

There should be an option to disable auto-brackets.

fmt.Println

What did you see instead?

There is no such option. :(

fmt.Println()

@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 Jan 25, 2021
@gopherbot gopherbot added this to the Unreleased milestone Jan 25, 2021
@stamblerre stamblerre changed the title x/tools/gopls: Option for not automatically inserting brackets x/tools/gopls: option for not automatically inserting brackets Jan 25, 2021
@pjweinb
Copy link

pjweinb commented Jan 25, 2021

I'm sympathetic when the editor won't do what we want. In my case, the Typescript behavior is infuriating: i almost always want to call the function whose name is being completed. If I want just the function name a simple backspace throws the parens away. On the other hand, if there's a profusion of user settings it's hard to find the one (if it's only one) to change. It would be nice if the editor would just adapt to each user's behavior, but getting there seems very hard.

@jwalton
Copy link
Author

jwalton commented Jan 25, 2021

I'd suggest using an existing option to key off of instead of adding a new one, but unfortunately there's no "existing" option in VSCode to build off of, because VSCode doesn't anticipate this interaction model at all. The closest is "editor.autoClosingBrackets" which "controls whether the editor should automatically close brackets after the user adds an opening bracket."

And this is the problem; the whole VSCode experience is built around the idea that you're going to type that bracket yourself - indeed, the whole Visual Studio tool set. If I open up a .py file in VSCode and type pri<tab> it completes to print, and not print(). If I open up a C# project in Visual Studio (not code), and type in this.Equ<tab>, it replaces it with this.Equals, and not this.Equals(). I'm sure it's fine if you only write Golang code, but having one language behave differently than tall the others is... not fun.

While I agree - in most cases - less options is better, in this case the golang plugin is the one actively fighting against the "established norm" in VSCode. So if we want to not have an option, that's fine, but I'd argue in this case we should fix the golang plugin to start behaving like all other languages do, and just not add the ()s. If we're going to keep the auto ()s, then the golang plugin should add an option so those of us long time Visual Studio users who are set in our ways can at least make it behave sensibly (and I'd strongly argue the default for that option should be to not auto-add the ()).

@muirdm
Copy link

muirdm commented Jan 26, 2021

You can disable the parens by disabling snippets (this will disable all gopls snippets, which may or may not be what you want). I don't know in particular how to configure VSCode to disable gopls snippets.

@jwalton
Copy link
Author

jwalton commented Jan 26, 2021

@muirdom Could you please elaborate on this workaround? How would you disable snippets? I don't see any options to disable snippets in the gopls settings. Disabling snippets globally in VSCode via editor.suggest.showSnippets doesn't seem to help (but I assume this isn't what you mean anyways), nor does disabling snippets for go via:

    "[go]": {
        "editor.snippetSuggestions": "none"
    },

@muirdm
Copy link

muirdm commented Jan 26, 2021

It is a client capability. The LSP client sets the textDocument.completion.snippetSupport capability to false (see https://microsoft.github.io/language-server-protocol/specification#textDocument_completion).

@jwalton
Copy link
Author

jwalton commented Jan 27, 2021

Aha. If I comment out these lines in gopls, it does indeed fix this. That does seem like kind of a sledgehammer though.

@jwalton
Copy link
Author

jwalton commented Feb 1, 2021

@pjweinb I've just discovered that typescript has a setting that behaves (more or less) like gopls with ui.completion.usePlaceholders turned on. If you enable the setting typescript.suggest.completeFunctionCalls in vscode, then myFun<tab> will complete to myFunction(x, y, z) with "x" selected ready to type over (and you can tab to y and z). I don't think there's a setting that will make it behave exactly like golang, though.

@pjweinb
Copy link

pjweinb commented Feb 1, 2021 via email

@muirdm
Copy link

muirdm commented May 5, 2021

I think this is a dupe of #41813.

@findleyr
Copy link
Contributor

findleyr commented May 9, 2021

Thanks @muirdm, I agree. Closing this in favor of the earlier issue.

@findleyr findleyr closed this as completed May 9, 2021
@stamblerre stamblerre removed this from the gopls/unplanned milestone May 10, 2021
@golang golang locked and limited conversation to collaborators May 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest 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

Successfully merging a pull request may close this issue.

6 participants