MCPcopy Index your code
hub / github.com/nodejs/node / createSnippetPrinter

Function createSnippetPrinter

test/fixtures/snapshot/typescript.js:132832–132887  ·  view source on GitHub ↗
(printerOptions)

Source from the content-addressed store, hash-verified

132830 }
132831 }
132832 function createSnippetPrinter(printerOptions) {
132833 var escapes;
132834 var baseWriter = ts.textChanges.createWriter(ts.getNewLineCharacter(printerOptions));
132835 var printer = ts.createPrinter(printerOptions, baseWriter);
132836 var writer = __assign(__assign({}, baseWriter), { write: function (s) { return escapingWrite(s, function () { return baseWriter.write(s); }); }, nonEscapingWrite: baseWriter.write, writeLiteral: function (s) { return escapingWrite(s, function () { return baseWriter.writeLiteral(s); }); }, writeStringLiteral: function (s) { return escapingWrite(s, function () { return baseWriter.writeStringLiteral(s); }); }, writeSymbol: function (s, symbol) { return escapingWrite(s, function () { return baseWriter.writeSymbol(s, symbol); }); }, writeParameter: function (s) { return escapingWrite(s, function () { return baseWriter.writeParameter(s); }); }, writeComment: function (s) { return escapingWrite(s, function () { return baseWriter.writeComment(s); }); }, writeProperty: function (s) { return escapingWrite(s, function () { return baseWriter.writeProperty(s); }); } });
132837 return {
132838 printSnippetList: printSnippetList,
132839 printAndFormatSnippetList: printAndFormatSnippetList,
132840 };
132841 // The formatter/scanner will have issues with snippet-escaped text,
132842 // so instead of writing the escaped text directly to the writer,
132843 // generate a set of changes that can be applied to the unescaped text
132844 // to escape it post-formatting.
132845 function escapingWrite(s, write) {
132846 var escaped = ts.escapeSnippetText(s);
132847 if (escaped !== s) {
132848 var start = baseWriter.getTextPos();
132849 write();
132850 var end = baseWriter.getTextPos();
132851 escapes = ts.append(escapes || (escapes = []), { newText: escaped, span: { start: start, length: end - start } });
132852 }
132853 else {
132854 write();
132855 }
132856 }
132857 /* Snippet-escaping version of `printer.printList`. */
132858 function printSnippetList(format, list, sourceFile) {
132859 var unescaped = printUnescapedSnippetList(format, list, sourceFile);
132860 return escapes ? ts.textChanges.applyChanges(unescaped, escapes) : unescaped;
132861 }
132862 function printUnescapedSnippetList(format, list, sourceFile) {
132863 escapes = undefined;
132864 writer.clear();
132865 printer.writeList(format, list, sourceFile, writer);
132866 return writer.getText();
132867 }
132868 function printAndFormatSnippetList(format, list, sourceFile, formatContext) {
132869 var syntheticFile = {
132870 text: printUnescapedSnippetList(format, list, sourceFile),
132871 getLineAndCharacterOfPosition: function (pos) {
132872 return ts.getLineAndCharacterOfPosition(this, pos);
132873 },
132874 };
132875 var formatOptions = ts.getFormatCodeSettingsForWriting(formatContext, sourceFile);
132876 var changes = ts.flatMap(list, function (node) {
132877 var nodeWithPos = ts.textChanges.assignPositionsToNode(node);
132878 return ts.formatting.formatNodeGivenIndentation(nodeWithPos, syntheticFile, sourceFile.languageVariant,
132879 /* indentation */ 0,
132880 /* delta */ 0, __assign(__assign({}, formatContext), { options: formatOptions }));
132881 });
132882 var allChanges = escapes
132883 ? ts.stableSort(ts.concatenate(changes, escapes), function (a, b) { return ts.compareTextSpans(a.span, b.span); })
132884 : changes;
132885 return ts.textChanges.applyChanges(syntheticFile.text, allChanges);
132886 }
132887 }
132888 function originToCompletionEntryData(origin) {
132889 var ambientModuleName = origin.fileName ? undefined : ts.stripQuotes(origin.moduleSymbol.name);

Calls 2

escapingWriteFunction · 0.85
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…