-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Format completion snippet text before escaping #48793
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1199,31 +1199,59 @@ 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; | ||
} | ||
|
||
function printUnescapedSnippetList( | ||
format: ListFormat, | ||
list: NodeArray<Node>, | ||
sourceFile: SourceFile | undefined, | ||
): string { | ||
escapes = undefined; | ||
writer.clear(); | ||
printer.writeList(format, list, sourceFile, writer); | ||
return writer.getText(); | ||
|
@@ -1236,7 +1264,7 @@ namespace ts.Completions { | |
formatContext: formatting.FormatContext, | ||
): string { | ||
const syntheticFile = { | ||
text: printSnippetList( | ||
text: printUnescapedSnippetList( | ||
format, | ||
list, | ||
sourceFile), | ||
|
@@ -1256,7 +1284,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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, thanks. |
||
: changes; | ||
return textChanges.applyChanges(syntheticFile.text, allChanges); | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.