From 1f0cc51e82dbb6778ea5dbd35adcc9cc3c7bb002 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 27 Mar 2018 08:39:47 -0700 Subject: [PATCH] textChanges: Use InsertNodeOptions instead of ChangeNodeOptions where possible --- src/services/textChanges.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index 24ff0ba281285..bab144ea94097 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -359,7 +359,7 @@ namespace ts.textChanges { this.insertNodesAt(sourceFile, start, typeParameters, { prefix: "<", suffix: ">" }); } - private getOptionsForInsertNodeBefore(before: Node, doubleNewlines: boolean): ChangeNodeOptions { + private getOptionsForInsertNodeBefore(before: Node, doubleNewlines: boolean): InsertNodeOptions { if (isStatement(before) || isClassElement(before)) { return { suffix: doubleNewlines ? this.newLineCharacter + this.newLineCharacter : this.newLineCharacter }; } @@ -659,20 +659,17 @@ namespace ts.textChanges { } /** Note: this may mutate `nodeIn`. */ - function getFormattedTextOfNode(nodeIn: Node, sourceFile: SourceFile, pos: number, options: ChangeNodeOptions, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText): string { + function getFormattedTextOfNode(nodeIn: Node, sourceFile: SourceFile, pos: number, { indentation, prefix, delta }: InsertNodeOptions, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText): string { const { node, text } = getNonformattedText(nodeIn, sourceFile, newLineCharacter); if (validate) validate(node, text); const { options: formatOptions } = formatContext; const initialIndentation = - options.indentation !== undefined - ? options.indentation - : formatting.SmartIndenter.getIndentation(pos, sourceFile, formatOptions, options.prefix === newLineCharacter || getLineStartPositionForPosition(pos, sourceFile) === pos); - const delta = - options.delta !== undefined - ? options.delta - : formatting.SmartIndenter.shouldIndentChildNode(nodeIn) - ? (formatOptions.indentSize || 0) - : 0; + indentation !== undefined + ? indentation + : formatting.SmartIndenter.getIndentation(pos, sourceFile, formatOptions, prefix === newLineCharacter || getLineStartPositionForPosition(pos, sourceFile) === pos); + if (delta === undefined) { + delta = formatting.SmartIndenter.shouldIndentChildNode(nodeIn) ? (formatOptions.indentSize || 0) : 0; + } const file: SourceFileLike = { text, getLineAndCharacterOfPosition(pos) { return getLineAndCharacterOfPosition(this, pos); } }; const changes = formatting.formatNodeGivenIndentation(node, file, sourceFile.languageVariant, initialIndentation, delta, formatContext); return applyChanges(text, changes);