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 insert-replace support (insertReplaceEdit) #40871

Open
muirdm opened this issue Aug 18, 2020 · 14 comments
Open

x/tools/gopls: add insert-replace support (insertReplaceEdit) #40871

muirdm opened this issue Aug 18, 2020 · 14 comments
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. help wanted Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@muirdm
Copy link

muirdm commented Aug 18, 2020

Currently gopls is overwriting the surrounding suffix (in addition to prefix) when inserting candidates:

// completing to "food" currently results in "food" instead of "foodoo"
f<>oo

I think it's from https://go-review.googlesource.com/c/tools/+/208501 changing the surrounding's range from [ident.Pos(), c.pos] to [ident.Pos(), ident.End()].

/cc @heschik

@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 Aug 18, 2020
@gopherbot gopherbot added this to the Unreleased milestone Aug 18, 2020
@stamblerre stamblerre removed this from the gopls/v1.0.0 milestone Sep 9, 2020
@stamblerre stamblerre added this to the gopls/unplanned milestone Oct 21, 2020
@schellj
Copy link

schellj commented Sep 18, 2021

I'm not familiar with this codebase, but it appears that there might already be a commit relevant to a fix for this issue at https://go-review.googlesource.com/c/tools/+/196119/. There's also a related issue report that was frozen due to age at #34011.

@schellj
Copy link

schellj commented Sep 20, 2021

Also relevant to this behavior is the LSP client's insertReplaceSupport capability property (see https://microsoft.github.io/language-server-protocol/specification). If the client has that capability, the server is supposed to return two edit ranges; one for inserting the completion and one for replacing existing text with the completion.

@muirdm
Copy link
Author

muirdm commented Sep 20, 2021

This issue regressed since it was fixed in https://go-review.googlesource.com/c/tools/+/196119/ .

insertReplaceSupport

Interesting. Adding support for that could be the best way forward since preferences differ.

@zikaeroh
Copy link
Contributor

Selfishly, I still prefer the behavior I advocated for in #34011, so any way that preserves the behavior will make me happy. 🙂

@schellj
Copy link

schellj commented Sep 20, 2021

@zikaeroh I generally agree. However, it seems that according to the LSP spec, supporting insertReplaceSupport seems to be the way that the server would ideally resolve this preference between users (or even each given completion selection).

Sorry, I failed to read the second part of your comment 😊

@findleyr findleyr changed the title x/tools/gopls: completion insertion is overwriting suffix x/tools/gopls: add insert-replace support (insertReplaceEdit) Oct 4, 2021
@findleyr findleyr modified the milestones: gopls/unplanned, gopls/later Jul 20, 2022
@ackerr
Copy link

ackerr commented Mar 24, 2023

Is anyone following up

@ghost
Copy link

ghost commented Mar 27, 2023

I don't have time at the moment to implement the insertReplaceSupport capability, but I prefer not to have the suffix replaced, so I forked gopls and made this change to get my desired behavior.

I'll try to revisit at some point and implement the capability.

@liaol
Copy link

liaol commented Jul 17, 2023

still has this problem today

@olexsmir
Copy link

still has this problem and this is annoying

@elliebodi
Copy link

I can tackle this

@danslo
Copy link

danslo commented May 18, 2024

I can tackle this

It has been tackled! See:
https://go-review.googlesource.com/c/tools/+/585275
#61215 (comment)

Not yet merged though. But I can confirm that building gopls on this branch fixes the issue.

@elliebodi
Copy link

Ah cool. Thanks for the heads up.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/587135 mentions this issue: gopls/internal/protocol: customize InsertReplaceEdit JSON unmarshal

gopherbot pushed a commit to golang/tools that referenced this issue May 22, 2024
InsertReplaceEdit is used instead of TextEdit in CompletionItem
in editors that support it. These two types are alike in appearance
but can be differentiated by the presence or absence of
certain properties. UnmarshalJSON of the sum type tries to
unmarshal as TextEdit only if unmarshal as InsertReplaceEdit fails.
Due to this similarity, unmarshal with the different type never fails.

Add a custom JSON unmarshaller for InsertReplaceEdit,
so it fails when the required fields are missing. That makes
Or_CompletionItem_textEdit decode TextEdit type correctly.

For golang/go#40871
For golang/go#61215

Change-Id: I62471fa973fa376cad5eb3934522ff21c14e3647
Reviewed-on: https://go-review.googlesource.com/c/tools/+/587135
Reviewed-by: Peter Weinberger <pjw@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
@danslo
Copy link

danslo commented May 29, 2024

golang/tools@d940b33

Looks like it has been merged 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. help wanted Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests