Skip to content

internal/lsp/source: Add useAutoBraces option. #272

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jwalton
Copy link

@jwalton jwalton commented Jan 27, 2021

Fixes: golang/go#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. :)

@google-cla
Copy link

google-cla bot commented Jan 27, 2021

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).

📝 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 google-cla bot added the cla: no label Jan 27, 2021
@google-cla
Copy link

google-cla bot commented Jan 27, 2021

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).

📝 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.

@jwalton
Copy link
Author

jwalton commented Jan 27, 2021

@googlebot I signed it!

@google-cla google-cla bot added cla: yes and removed cla: no labels Jan 27, 2021
@gopherbot
Copy link
Contributor

This PR (HEAD: 706fb82) 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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

x/tools/gopls: option for not automatically inserting brackets
2 participants