tools icon indicating copy to clipboard operation
tools copied to clipboard

internal/lsp/source: Add useAutoBraces option.

Open jwalton opened this issue 4 years ago • 7 comments

Fixes: https://github.com/golang/go/issues/43903

This PR adds a new option useAutoBraces to gopls which, if set false, will make it so gopls will not add a () to the end of an auto-completion for a function. The default value is true, to keep the current behavior, but actually I think you could make a pretty strong case for setting the default to false.

The problem here is that, in VSCode, when you auto-complete a function name in any programming language other than go (at least any that are maintained by Microsoft), these brackets are not inserted. The completion behavior of gopls is unique, which means if your day job is writing in some other language and you only occasionally write in go, the editor is always not behaving the way you expect it to, and you have to fight the muscle memory you've developed from other languages.

For example, let's open up VSCode and write "Hello World" in Python. You type:

pri<tab>("Hello World!")

(You can optionally not type the closing bracket if you're using the default settings, as VSCode will insert it for you when you type the open bracket. If you do type it, the close bracket will "overtype" the existing one, so it works either way.) This results in:

print("Hello World!")

It's the same in any language:

// Typescript
console.lo<tab>("Hello World!");
console.log("Hello World!");

// C# in Visual Studio (not VSCode)
System.Console.WriteL<tab>("Hello World!");
System.Console.WriteLine("Hello World!");

Etc... You'll note, aside from the fact that the function is named something different in each case, that we're typing basically exactly the same thing and getting the same result. The Visual Studio family of products have a very consistent "interaction model" here, allowing you to switch from one language to another without the behavior of Visual Studio changing.

But, in go:

fmt.Pri<tab>("Hello World!")

We get:

fmt.Println(("Hello World!"))

Note the double brackets. If we tried to pass two parameters, this would be a syntax error. It's even worse if you have the "editor.autoClosingBrackets" set to "never", then you get fmt.Println(("Hello World!") - only double brackets at the start.

gopls has this cool feature where, when it returns a suggestion for "fmt.Println", it actually returns a snippet which sends back "fmt.Println()", and places the cursor in between the brackets. This is very clever, and I'm sure lots of people like this, and if you write predominantly go all day I'm sure this works great. But as mentioned above, if you spend most of your time in some other language, and only write go occasionally, then your muscle memory is basically hard coded to type "<tab>(" every time you want to call a function, and thus in go, you find yourself constantly having to backspace away extra brackets, and slowly losing your mind. :P It's also worth noting that when this "auto bracket" feature was requested back when Microsoft was still maintaining the go plugin, they turned it down for exactly this reason.

So, this PR makes this behavior controlled by an option, so at least you can turn it off if it's driving you crazy. :)

jwalton avatar Jan 27 '21 04:01 jwalton

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

google-cla[bot] avatar Jan 27 '21 04:01 google-cla[bot]

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

google-cla[bot] avatar Jan 27 '21 04:01 google-cla[bot]

@googlebot I signed it!

jwalton avatar Jan 27 '21 04:01 jwalton

This PR (HEAD: 706fb823fdaaad56534710c8b018aa5125894321) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/tools/+/287172 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot avatar Jan 27 '21 04:01 gopherbot

Message from Go Bot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps: A maintainer will review your change and provide feedback. See https://golang.org/doc/contribute.html#review for more info and tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be surprising to people new to the project. The careful, iterative review process is our way of helping mentor contributors and ensuring that their contributions have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/287172. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Jan 27 '21 04:01 gopherbot

Message from Rebecca Stambler:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/287172. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Mar 03 '21 00:03 gopherbot

Message from Rebecca Stambler:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/287172. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Nov 08 '21 11:11 gopherbot