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: add location-aware snippets for append and other utility methods #39507

Open
A-UNDERSCORE-D opened this issue Jun 10, 2020 · 6 comments

Comments

@A-UNDERSCORE-D
Copy link

@A-UNDERSCORE-D A-UNDERSCORE-D commented Jun 10, 2020

Is your feature request related to a problem? Please describe.
After using GoLand for some time I'm missing a small feature or two. Specifically I'm missing type aware snippets for append, sprintf, and a few other utility methods (like things from slicetricks).

Describe the solution you'd like
I'd like a snippet under the name .aapend (auto-append) or similar to construct an append-assign statement for me.

Either from the start by starting with append and getting toAppend = append(toAppend, <>)

Or from the middle of a line using something like .aapend which would add the correct = append(name, <>) to the end of the line.

The sprintf and slicetricks I mentioned above would work similarly, sprintf simply going from thing.sprint to fmt.Sprintf("<>", thing), and delete applying a (preferably pointer aware delete implementation from the slicetricks wiki page

Describe alternatives you've considered
I've written my own snippets for this, but they get suggested in comments and other places where they are not useful, cluttering any autocomplete I may have had. And these rely on somewhat brittle regexps, which do not always get it right.

	"assign append": {
		"description": "appends to a slice and assigns it to itself",
		"prefix": "aappend",
		"body": "${1:toAppend} = append(${1:toappend}, $2)"
	},
	"assign append inplace": {
		"description": "appends to a slice and assigns it to itself",
		"prefix": ".aappend",
		"body": " = append(${TM_CURRENT_LINE/\\s*(\\S+)\\..*/$1/}, $0)"
	},
	"slice delete": {
		"description": "cuts a value from a slice",
		"prefix": ".delete",
		"body": " = append(${TM_CURRENT_LINE/\\s*(\\S+)\\..*/$1/}[:${1:pos}], ${TM_CURRENT_LINE/\\s*(\\S+)\\..*/$1/}[${1:pos}+1:]...)"
	}

The other option here is to type things manually, though that has the annoyance that for some things, you're going to end up checking a wiki page EVERY time (slicetricks things, delete et al.) or are just repetitive--typing thing = append(thing, newStuff) gets old. Not to mention that if its type aware it also suggest thing = append(thing, newStuff...) where appropriate.

This may be more applicable to gopls itself, rather than here, but I thought I'd ask about it here first.

@hyangah
Copy link
Contributor

@hyangah hyangah commented Jun 10, 2020

@A-UNDERSCORE-D Yeah, Goland's context-aware, custom postfix completion support is nice.

As you mentioned, we want to delegate most completion logic to the language server and this is the tracking issue #39354.

The completion snippets around append, or common standard functions, slice tricks are good completion candidates to be offered by default. It would be nice if customization and template support can be offered in the feature, too. @stamblerre, thought?

@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Jun 10, 2020

Thanks for the suggestion, @A-UNDERSCORE-D. In general, this does sound like a gopls feature request so I'll transfer it to the golang/go repo.

We actually should have some similar functionality around append already: https://go-review.googlesource.com/c/tools/+/221777. Maybe there are ways we can improve it, because it doesn't trigger when you write append, only when you are completing the slice variable. #39354 is definitely also relevant here.

@stamblerre stamblerre transferred this issue from golang/vscode-go Jun 10, 2020
@A-UNDERSCORE-D
Copy link
Author

@A-UNDERSCORE-D A-UNDERSCORE-D commented Jun 10, 2020

Thanks for the transfer! And yeah Ive noticed append popping up like that on occasion, it works quite well. I guess my real want here is a more direct way to tell gopls "please give me this specific autocompletion" as shown in #39354 already. Not sure if I should keep this open or defer to the #39354? I think mine provides a bit more abstract around expected behavior for postfix style completions.

That said #39354 may be an opening for something more abstract where there can be a predefined set of postfixes that can be expanded on via config if a user desires.

@stamblerre stamblerre changed the title Add location aware snippets for append and other utility methods x/tools/gopls: add location-aware snippets for append and other utility methods Jun 11, 2020
@gopherbot gopherbot added this to the Unreleased milestone Jun 11, 2020
@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Jun 11, 2020

Yeah, I agree this can be a more general issue. Let's start with seeing how we can improve append and then think of more cases from there.

@golang golang deleted a comment from gopherbot Jun 11, 2020
@hyangah
Copy link
Contributor

@hyangah hyangah commented Jul 1, 2020

FYI the CL (https://go-review.googlesource.com/c/tools/+/221777) that implements autocompletion for append is merged and available in gopls 0.4.2.

@A-UNDERSCORE-D when is the second command 'assign append inplace' rule useful? (We are trying to understand use cases and how to improve the autocompletion logic)

@A-UNDERSCORE-D
Copy link
Author

@A-UNDERSCORE-D A-UNDERSCORE-D commented Jul 1, 2020

@hyangah Its mostly just an ease of use thing I picked up from my time using goland. Its a part of the whole postfix completion request. For us with gopls the main thing I can think of its its an unambiguous way to ask gopls to provide the completion we want. Meaning that instead of typing the partial name of a slice and using arrow keys to grab the append (I have not tried 4.2 yet, so the arrow keys comment is purely speculative) meaning that one's right hand needs to move to the right for arrow keys, one can use the progression partial name -> .aa (suggested append at this point) to get the same result. Or that is the intent, anyway

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

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.