Skip to content

Commit 3205d2e

Browse files
authored
Set completion item's text edit if optional replacement span and insert text are present (#1579)
1 parent 1ee2ba9 commit 3205d2e

7 files changed

+71
-152
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
completionListInClosedFunction05
2+
completionsAtIncompleteObjectLiteralProperty
3+
completionsSelfDeclaring1
4+
completionsWithDeprecatedTag4

internal/fourslash/tests/completionFilterText1_test.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ class Foo8 {
149149
IsIncomplete: false,
150150
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
151151
CommitCharacters: &DefaultCommitCharacters,
152-
EditRange: &fourslash.EditRange{
153-
Insert: f.Ranges()[3],
154-
Replace: f.Ranges()[3],
155-
},
152+
EditRange: Ignored,
156153
},
157154
Items: &fourslash.CompletionsExpectedItems{
158155
Includes: []fourslash.CompletionsExpectedItem{
@@ -161,7 +158,13 @@ class Foo8 {
161158
Kind: PtrTo(lsproto.CompletionItemKindField),
162159
SortText: PtrTo(string(ls.SortTextSuggestedClassMembers)),
163160
FilterText: PtrTo("bar"),
164-
InsertText: PtrTo("this.#bar"),
161+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
162+
InsertReplaceEdit: &lsproto.InsertReplaceEdit{
163+
NewText: "this.#bar",
164+
Insert: f.Ranges()[3].LSRange,
165+
Replace: f.Ranges()[3].LSRange,
166+
},
167+
},
165168
},
166169
},
167170
},
@@ -187,18 +190,21 @@ class Foo8 {
187190
IsIncomplete: false,
188191
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
189192
CommitCharacters: &DefaultCommitCharacters,
190-
EditRange: &fourslash.EditRange{
191-
Insert: f.Ranges()[4],
192-
Replace: f.Ranges()[4],
193-
},
193+
EditRange: Ignored,
194194
},
195195
Items: &fourslash.CompletionsExpectedItems{
196196
Includes: []fourslash.CompletionsExpectedItem{
197197
&lsproto.CompletionItem{
198-
Label: "#bar",
199-
Kind: PtrTo(lsproto.CompletionItemKindField),
200-
SortText: PtrTo(string(ls.SortTextSuggestedClassMembers)),
201-
InsertText: PtrTo("this.#bar"),
198+
Label: "#bar",
199+
Kind: PtrTo(lsproto.CompletionItemKindField),
200+
SortText: PtrTo(string(ls.SortTextSuggestedClassMembers)),
201+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
202+
InsertReplaceEdit: &lsproto.InsertReplaceEdit{
203+
NewText: "this.#bar",
204+
Insert: f.Ranges()[4].LSRange,
205+
Replace: f.Ranges()[4].LSRange,
206+
},
207+
},
202208
},
203209
},
204210
},
@@ -207,18 +213,21 @@ class Foo8 {
207213
IsIncomplete: false,
208214
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
209215
CommitCharacters: &DefaultCommitCharacters,
210-
EditRange: &fourslash.EditRange{
211-
Insert: f.Ranges()[5],
212-
Replace: f.Ranges()[5],
213-
},
216+
EditRange: Ignored,
214217
},
215218
Items: &fourslash.CompletionsExpectedItems{
216219
Includes: []fourslash.CompletionsExpectedItem{
217220
&lsproto.CompletionItem{
218-
Label: "#bar",
219-
Kind: PtrTo(lsproto.CompletionItemKindField),
220-
SortText: PtrTo(string(ls.SortTextSuggestedClassMembers)),
221-
InsertText: PtrTo("this.#bar"),
221+
Label: "#bar",
222+
Kind: PtrTo(lsproto.CompletionItemKindField),
223+
SortText: PtrTo(string(ls.SortTextSuggestedClassMembers)),
224+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
225+
InsertReplaceEdit: &lsproto.InsertReplaceEdit{
226+
NewText: "this.#bar",
227+
Insert: f.Ranges()[5].LSRange,
228+
Replace: f.Ranges()[5].LSRange,
229+
},
230+
},
222231
},
223232
},
224233
},

internal/fourslash/tests/gen/completionsAtIncompleteObjectLiteralProperty_test.go renamed to internal/fourslash/tests/manual/completionsAtIncompleteObjectLiteralProperty_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestCompletionsAtIncompleteObjectLiteralProperty(t *testing.T) {
1616
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
1717
const content = `// @noLib: true
1818
f({
19-
a/**/
19+
[|a|]/**/
2020
xyz: ` + "`" + `` + "`" + `,
2121
});
2222
declare function f(options: { abc?: number, xyz?: string }): void;`
@@ -31,10 +31,16 @@ declare function f(options: { abc?: number, xyz?: string }): void;`
3131
Exact: []fourslash.CompletionsExpectedItem{
3232
&lsproto.CompletionItem{
3333
Label: "abc?",
34-
InsertText: PtrTo("abc"),
3534
FilterText: PtrTo("abc"),
3635
Kind: PtrTo(lsproto.CompletionItemKindField),
3736
SortText: PtrTo(string(ls.SortTextOptionalMember)),
37+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
38+
InsertReplaceEdit: &lsproto.InsertReplaceEdit{
39+
NewText: "abc",
40+
Insert: f.Ranges()[0].LSRange,
41+
Replace: f.Ranges()[0].LSRange,
42+
},
43+
},
3844
},
3945
},
4046
},

internal/fourslash/tests/gen/completionsSelfDeclaring1_test.go renamed to internal/fourslash/tests/manual/completionsSelfDeclaring1_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function test<T extends Record<string, Test>>(opt: T) { }
2424
test({
2525
a: {
2626
keyPath: '',
27-
a/**/
27+
[|a|]/**/
2828
}
2929
})`
3030
f := fourslash.NewFourslash(t, nil /*capabilities*/, content)
@@ -38,9 +38,15 @@ test({
3838
Exact: []fourslash.CompletionsExpectedItem{
3939
&lsproto.CompletionItem{
4040
Label: "autoIncrement?",
41-
InsertText: PtrTo("autoIncrement"),
4241
FilterText: PtrTo("autoIncrement"),
4342
SortText: PtrTo(string(ls.SortTextOptionalMember)),
43+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
44+
InsertReplaceEdit: &lsproto.InsertReplaceEdit{
45+
NewText: "autoIncrement",
46+
Insert: f.Ranges()[0].LSRange,
47+
Replace: f.Ranges()[0].LSRange,
48+
},
49+
},
4450
},
4551
},
4652
},

internal/fourslash/tests/gen/completionsWithDeprecatedTag4_test.go renamed to internal/fourslash/tests/manual/completionsWithDeprecatedTag4_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestCompletionsWithDeprecatedTag4(t *testing.T) {
1616
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
1717
const content = `// @noLib: true
1818
f({
19-
a/**/
19+
[|a|]/**/
2020
xyz: ` + "`" + `` + "`" + `,
2121
});
2222
declare function f(options: {
@@ -35,10 +35,16 @@ declare function f(options: {
3535
Exact: []fourslash.CompletionsExpectedItem{
3636
&lsproto.CompletionItem{
3737
Label: "abc?",
38-
InsertText: PtrTo("abc"),
3938
FilterText: PtrTo("abc"),
4039
Kind: PtrTo(lsproto.CompletionItemKindField),
4140
SortText: PtrTo(string(ls.DeprecateSortText(ls.SortTextOptionalMember))),
41+
TextEdit: &lsproto.TextEditOrInsertReplaceEdit{
42+
InsertReplaceEdit: &lsproto.InsertReplaceEdit{
43+
NewText: "abc",
44+
Insert: f.Ranges()[0].LSRange,
45+
Replace: f.Ranges()[0].LSRange,
46+
},
47+
},
4248
},
4349
},
4450
},

internal/ls/completions.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4005,6 +4005,20 @@ func (l *LanguageService) setItemDefaults(
40054005
Replace: *optionalReplacementSpan,
40064006
},
40074007
}
4008+
for _, item := range items {
4009+
// If `editRange` is set, `insertText` is ignored by the client, so we need to
4010+
// provide `textEdit` instead.
4011+
if item.InsertText != nil && item.TextEdit == nil {
4012+
item.TextEdit = &lsproto.TextEditOrInsertReplaceEdit{
4013+
InsertReplaceEdit: &lsproto.InsertReplaceEdit{
4014+
NewText: *item.InsertText,
4015+
Insert: insertRange,
4016+
Replace: *optionalReplacementSpan,
4017+
},
4018+
}
4019+
item.InsertText = nil
4020+
}
4021+
}
40084022
} else if clientSupportsItemInsertReplace(clientOptions) {
40094023
for _, item := range items {
40104024
if item.TextEdit == nil {

internal/ls/completions_test.go

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -646,131 +646,6 @@ class bar5{ constructor(public /*constructorParameter1*/`,
646646
},
647647
},
648648
},
649-
{
650-
name: "completionEntryForClassMembers_StaticWhenBaseTypeIsNotResolved",
651-
files: map[string]string{
652-
defaultMainFileName: `import React from 'react'
653-
class Slider extends React.Component {
654-
static defau/**/ltProps = {
655-
onMouseDown: () => { },
656-
onMouseUp: () => { },
657-
unit: 'px',
658-
}
659-
handleChange = () => 10;
660-
}`,
661-
"/node_modules/@types/react/index.d.ts": `export = React;
662-
export as namespace React;
663-
declare namespace React {
664-
function createElement(): any;
665-
interface Component<P = {}, S = {}, SS = any> { }
666-
class Component<P, S> {
667-
static contextType?: any;
668-
context: any;
669-
constructor(props: Readonly<P>);
670-
setState<K extends keyof S>(
671-
state: ((prevState: Readonly<S>, props: Readonly<P>) => (Pick<S, K> | S | null)) | (Pick<S, K> | S | null),
672-
callback?: () => void
673-
): void;
674-
}
675-
}`,
676-
},
677-
expectedResult: map[string]*testCaseResult{
678-
"": {
679-
list: &lsproto.CompletionList{
680-
IsIncomplete: false,
681-
ItemDefaults: &lsproto.CompletionItemDefaults{
682-
CommitCharacters: &[]string{},
683-
EditRange: &lsproto.RangeOrEditRangeWithInsertReplace{
684-
EditRangeWithInsertReplace: &lsproto.EditRangeWithInsertReplace{
685-
Insert: lsproto.Range{
686-
Start: lsproto.Position{Line: 2, Character: 11},
687-
End: lsproto.Position{Line: 2, Character: 16},
688-
},
689-
Replace: lsproto.Range{
690-
Start: lsproto.Position{Line: 2, Character: 11},
691-
End: lsproto.Position{Line: 2, Character: 23},
692-
},
693-
},
694-
},
695-
},
696-
Items: []*lsproto.CompletionItem{
697-
{
698-
Label: "contextType?",
699-
Kind: fieldKind,
700-
SortText: sortTextLocationPriority,
701-
FilterText: ptrTo("contextType"),
702-
InsertText: ptrTo("contextType"),
703-
},
704-
{
705-
Label: "abstract",
706-
Kind: keywordKind,
707-
SortText: sortTextGlobalsOrKeywords,
708-
},
709-
{
710-
Label: "accessor",
711-
Kind: keywordKind,
712-
SortText: sortTextGlobalsOrKeywords,
713-
},
714-
{
715-
Label: "async",
716-
Kind: keywordKind,
717-
SortText: sortTextGlobalsOrKeywords,
718-
},
719-
{
720-
Label: "constructor",
721-
Kind: keywordKind,
722-
SortText: sortTextGlobalsOrKeywords,
723-
},
724-
{
725-
Label: "declare",
726-
Kind: keywordKind,
727-
SortText: sortTextGlobalsOrKeywords,
728-
},
729-
{
730-
Label: "get",
731-
Kind: keywordKind,
732-
SortText: sortTextGlobalsOrKeywords,
733-
},
734-
{
735-
Label: "override",
736-
Kind: keywordKind,
737-
SortText: sortTextGlobalsOrKeywords,
738-
},
739-
{
740-
Label: "private",
741-
Kind: keywordKind,
742-
SortText: sortTextGlobalsOrKeywords,
743-
},
744-
{
745-
Label: "protected",
746-
Kind: keywordKind,
747-
SortText: sortTextGlobalsOrKeywords,
748-
},
749-
{
750-
Label: "public",
751-
Kind: keywordKind,
752-
SortText: sortTextGlobalsOrKeywords,
753-
},
754-
{
755-
Label: "readonly",
756-
Kind: keywordKind,
757-
SortText: sortTextGlobalsOrKeywords,
758-
},
759-
{
760-
Label: "set",
761-
Kind: keywordKind,
762-
SortText: sortTextGlobalsOrKeywords,
763-
},
764-
{
765-
Label: "static",
766-
Kind: keywordKind,
767-
SortText: sortTextGlobalsOrKeywords,
768-
},
769-
},
770-
},
771-
},
772-
},
773-
},
774649
{
775650
name: "completionsInJsxTag",
776651
mainFileName: "/index.tsx",

0 commit comments

Comments
 (0)