Skip to content

fix54092: return replacement ranges for completions on unclosed strings #57839

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

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2322,8 +2322,13 @@ export function createTextSpanFromNode(node: Node, sourceFile?: SourceFile, endN

/** @internal */
export function createTextSpanFromStringLiteralLikeContent(node: StringLiteralLike) {
if (node.isUnterminated) return undefined;
return createTextSpanFromBounds(node.getStart() + 1, node.getEnd() - 1);
let replacementEnd = node.getEnd() - 1;
if (node.isUnterminated) {
// we return no replacement range only if unterminated string is empty
if (node.getStart() === replacementEnd) return undefined;
replacementEnd = node.getEnd();
}
return createTextSpanFromBounds(node.getStart() + 1, replacementEnd);
}

/** @internal */
Expand Down
73 changes: 73 additions & 0 deletions tests/cases/fourslash/stringCompletionsUnterminated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
///<reference path="fourslash.ts" />

// @Filename: file0.ts
//// const a = { "foo.bar": 1 }
//// a["[|foo.b/*0*/|]

// @Filename: file1.ts
//// const a = { "foo.bar": 1 }
//// a["[|foo.b /*1*/|]

// @Filename: file2.ts
//// const a = { "foo.bar": 1 }
//// a[ "[|foo.b/*2*/|]

// @Filename: file3.ts
//// const a = { "foo.bar": 1 }
//// a["[|foo.b/*3*/|]"

// @Filename: file4.ts
//// const a = { "foo.bar": 1 }
//// a["[|foo./*4*/b|]

// @Filename: file5.ts
//// const a = { "foo.bar": 1 }
//// a['[|foo./*5*/b|]

// @Filename: file6.ts
//// const a = { "foo.bar": 1 }
//// a[`[|foo./*6*/b|]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add test cases for when you have foo. (no contents following the dot) and fo (no dot or following contents)?

Copy link
Member Author

@iisaduan iisaduan Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added! I also tested manually in vscode to see that the behavior didn't change for the previously working cases, even though it does return a replacementSpan now.


// @Filename: file7.ts
//// const a = { "foo.bar": 1 }
//// a["[|foo./*7*/|]

// @Filename: file8.ts
//// const a = { "foo.bar": 1 }
//// a["[|foo/*8*/|]

// @Filename: file9.ts
//// const a = { "foo.bar": 1 }
//// a["[|foo./*9*/|]"

// @Filename: file10.ts
//// const a = { "foo.bar": 1 }
//// a["[|foo/*10*/|]"

// @Filename: empty1.ts
//// const a = { "foo.bar": 1 }
//// a[`/*11*/

// @Filename: empty2.ts
//// const a = { "foo.bar": 1 }
//// a["/*12*/

// @Filename: empty3.ts
//// const a = { "foo.bar": 1 }
//// a['/*13*/

// tests 11-13 should return no replacementSpan
const markers = test.markers();
const ranges = test.ranges();

for (let i = 0; i < markers.length; i++) {
verify.completions({
marker: markers[i],
includes: [{
"name": "foo.bar",
"kind": "property",
"kindModifiers": "",
"replacementSpan": ranges[i],
}],
});
}