diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index f1caec5d564..5415158422c 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -390,7 +390,8 @@ namespace ts.formatting { sourceFile)); } - function formatSpanWorker(originalRange: TextRange, + function formatSpanWorker( + originalRange: TextRange, enclosingNode: Node, initialIndentation: number, delta: number, @@ -424,16 +425,20 @@ namespace ts.formatting { } if (!formattingScanner.isOnToken()) { + const indentation = SmartIndenter.nodeWillIndentChild(options, enclosingNode, /*child*/ undefined, sourceFile, /*indentByDefault*/ false) + ? initialIndentation + options.indentSize! + : initialIndentation; const leadingTrivia = formattingScanner.getCurrentLeadingTrivia(); if (leadingTrivia) { - indentTriviaItems(leadingTrivia, initialIndentation, /*indentNextTokenOrTrivia*/ false, + indentTriviaItems(leadingTrivia, indentation, /*indentNextTokenOrTrivia*/ false, item => processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!)); - if (options.trimTrailingWhitespace !== false) { - trimTrailingWhitespacesForRemainingRange(); - } } } + if (options.trimTrailingWhitespace !== false) { + trimTrailingWhitespacesForRemainingRange(); + } + return edits; // local functions diff --git a/tests/cases/fourslash/formatSelectionDocCommentInBlock.ts b/tests/cases/fourslash/formatSelectionDocCommentInBlock.ts new file mode 100644 index 00000000000..e1710018631 --- /dev/null +++ b/tests/cases/fourslash/formatSelectionDocCommentInBlock.ts @@ -0,0 +1,44 @@ +/// + +//// { +//// /*1*//** +//// * Some doc comment +//// *//*2*/ +//// const a = 1; +//// } +//// +//// while (true) { +//// /*3*//** +//// * Some doc comment +//// *//*4*/ +//// } + +format.selection("1", "2"); +verify.currentFileContentIs( +`{ + /** + * Some doc comment + */ + const a = 1; +} + +while (true) { +/** + * Some doc comment + */ +}`); + +format.selection("3", "4"); +verify.currentFileContentIs( +`{ + /** + * Some doc comment + */ + const a = 1; +} + +while (true) { + /** + * Some doc comment + */ +}`);