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

Format completion snippet text before escaping #48793

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 40 additions & 9 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,30 +1199,57 @@ namespace ts.Completions {
function createSnippetPrinter(
printerOptions: PrinterOptions,
) {
let escapes: TextChange[] | undefined;
const baseWriter = textChanges.createWriter(getNewLineCharacter(printerOptions));
const printer = createPrinter(printerOptions, baseWriter);
const writer: EmitTextWriter = {
...baseWriter,
write: s => baseWriter.write(escapeSnippetText(s)),
write: s => escapingWrite(s, () => baseWriter.write(s)),
nonEscapingWrite: baseWriter.write,
writeLiteral: s => baseWriter.writeLiteral(escapeSnippetText(s)),
writeStringLiteral: s => baseWriter.writeStringLiteral(escapeSnippetText(s)),
writeSymbol: (s, symbol) => baseWriter.writeSymbol(escapeSnippetText(s), symbol),
writeParameter: s => baseWriter.writeParameter(escapeSnippetText(s)),
writeComment: s => baseWriter.writeComment(escapeSnippetText(s)),
writeProperty: s => baseWriter.writeProperty(escapeSnippetText(s)),
writeLiteral: s => escapingWrite(s, () => baseWriter.writeLiteral(s)),
writeStringLiteral: s => escapingWrite(s, () => baseWriter.writeStringLiteral(s)),
writeSymbol: (s, symbol) => escapingWrite(s, () => baseWriter.writeSymbol(s, symbol)),
writeParameter: s => escapingWrite(s, () => baseWriter.writeParameter(s)),
writeComment: s => escapingWrite(s, () => baseWriter.writeComment(s)),
writeProperty: s => escapingWrite(s, () => baseWriter.writeProperty(s)),
};

return {
printSnippetList,
printAndFormatSnippetList,
};

// The formatter/scanner will have issues with snippet-escaped text,
// so instead of writing the escaped text directly to the writer,
// generate a set of changes that can be applied to the unescaped text
// to escape it post-formatting.
function escapingWrite(s: string, write: () => void) {
const escaped = escapeSnippetText(s);
if (escaped !== s) {
const start = baseWriter.getTextPos();
write();
const end = baseWriter.getTextPos();
escapes = append(escapes ||= [], { newText: escaped, span: { start, length: end - start } });
}
else {
write();
}
}

/* Snippet-escaping version of `printer.printList`. */
function printSnippetList(
format: ListFormat,
list: NodeArray<Node>,
sourceFile: SourceFile | undefined,
): string {
const unescaped = printUnescapedSnippetList(format, list, sourceFile);
return escapes ? textChanges.applyChanges(unescaped, escapes) : unescaped;
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
}

function printUnescapedSnippetList(
format: ListFormat,
list: NodeArray<Node>,
sourceFile: SourceFile | undefined,
): string {
writer.clear();
printer.writeList(format, list, sourceFile, writer);
Expand All @@ -1236,7 +1263,7 @@ namespace ts.Completions {
formatContext: formatting.FormatContext,
): string {
const syntheticFile = {
text: printSnippetList(
text: printUnescapedSnippetList(
format,
list,
sourceFile),
Expand All @@ -1256,7 +1283,11 @@ namespace ts.Completions {
/* delta */ 0,
{ ...formatContext, options: formatOptions });
});
return textChanges.applyChanges(syntheticFile.text, changes);

const allChanges = escapes
? stableSort(concatenate(changes, escapes), (a, b) => compareTextSpans(a.span, b.span))
Copy link
Member

Choose a reason for hiding this comment

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

this might be a dumb question, but is there a chance that we have overlapping text changes, and if so, will this sorting make sure everything works correctly?

Copy link
Member Author

Choose a reason for hiding this comment

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

Overlapping edits can defeat the sorting, but since the formatter deals only in whitespace and semicolons, and the characters we need to escape are only part of tokens, I don’t think it can ever be a problem in practice.

Copy link
Member

Choose a reason for hiding this comment

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

I see, thanks.

: changes;
return textChanges.applyChanges(syntheticFile.text, allChanges);
}
}

Expand Down
23 changes: 22 additions & 1 deletion tests/cases/fourslash/completionsOverridingMethod2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// Case: Snippet text needs escaping
////interface DollarSign {
//// "$usd"(a: number): number;
//// $cad(b: number): number;
//// cla$$y(c: number): number;
//// isDollarAmountString(s: string): s is `$${number}`
////}
////class USD implements DollarSign {
//// /*a*/
Expand All @@ -25,6 +28,24 @@ verify.completions({
sortText: completion.SortText.ClassMemberSnippets,
isSnippet: true,
insertText: "\"\\$usd\"(a: number): number {\n $0\n}",
}
},
{
name: "$cad",
sortText: completion.SortText.ClassMemberSnippets,
isSnippet: true,
insertText: "\\$cad(b: number): number {\n $0\n}",
},
{
name: "cla$$y",
sortText: completion.SortText.ClassMemberSnippets,
isSnippet: true,
insertText: "cla\\$\\$y(c: number): number {\n $0\n}",
},
{
name: "isDollarAmountString",
sortText: completion.SortText.ClassMemberSnippets,
isSnippet: true,
insertText: "isDollarAmountString(s: string): s is `\\$\\${number}` {\n $0\n}"
},
],
});