tools
                                
                                
                                
                                    tools copied to clipboard
                            
                            
                            
                        internal/lsp/source: Add useAutoBraces option.
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. :)
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
- It's possible we don't have your GitHub username or you're using a different email address on your commit. Check your existing CLA data and verify that your email is set on your git commits.
 
Corporate signers
- Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to go/cla#troubleshoot (Public version).
 - The email used to register you as an authorized contributor must be the email used for the Git commit. Check your existing CLA data and verify that your email is set on your git commits.
 - The email used to register you as an authorized contributor must also be attached to your GitHub account.
 
ℹ️ Googlers: Go here for more info.
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
- It's possible we don't have your GitHub username or you're using a different email address on your commit. Check your existing CLA data and verify that your email is set on your git commits.
 
Corporate signers
- Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to go/cla#troubleshoot (Public version).
 - The email used to register you as an authorized contributor must be the email used for the Git commit. Check your existing CLA data and verify that your email is set on your git commits.
 - The email used to register you as an authorized contributor must also be attached to your GitHub account.
 
ℹ️ Googlers: Go here for more info.
@googlebot I signed it!
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
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!
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!
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!