diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index c34585c5bca..22602d6031c 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -1152,23 +1152,6 @@ namespace ts.formatting { } } - /** - * Gets the indentation level of the multi-line comment enclosing position, - * and a negative value if the position is not in a multi-line comment. - * - * @param precedingToken Must be the result of `findPrecedingToken(position, sourceFile)`. - */ - export function getIndentationOfEnclosingMultiLineComment(sourceFile: SourceFile, position: number, precedingToken: Node | undefined, options: EditorSettings): number { - const range = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true, precedingToken || null); // tslint:disable-line:no-null-keyword - if (range) { - const commentStart = range.pos; - const commentLineStart = getLineStartPositionForPosition(commentStart, sourceFile); - const { column, character } = SmartIndenter.findFirstNonWhitespaceCharacterAndColumn(commentLineStart, commentStart, sourceFile, options); - return column + /*length after whitespace ends*/ range.pos - (commentLineStart + character); - } - return -1; - } - /** * @param precedingToken pass `null` if preceding token was already computed and result was `undefined`. */ diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index c868ce04847..5af93bc025f 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -32,9 +32,28 @@ namespace ts.formatting { } const precedingToken = findPrecedingToken(position, sourceFile); - const indentationOfEnclosingMultiLineComment = getIndentationOfEnclosingMultiLineComment(sourceFile, position, precedingToken, options); - if (indentationOfEnclosingMultiLineComment >= 0) { - return indentationOfEnclosingMultiLineComment; + + const enclosingCommentRange = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true, precedingToken || null); // tslint:disable-line:no-null-keyword + if (enclosingCommentRange) { + const previousLine = getLineAndCharacterOfPosition(sourceFile, position).line - 1; + const commentStartLine = getLineAndCharacterOfPosition(sourceFile, enclosingCommentRange.pos).line; + + Debug.assert(commentStartLine >= 0); + + if (previousLine <= commentStartLine) { + return findFirstNonWhitespaceColumn(getStartPositionOfLine(commentStartLine, sourceFile), position, sourceFile, options); + } + + // get first character of previous line -- if it is '*', move back one more character (or stay at 0) + const startPostionOfLine = getStartPositionOfLine(previousLine, sourceFile); + const { column, character } = findFirstNonWhitespaceCharacterAndColumn(startPostionOfLine, position, sourceFile, options); + + if (column === 0) { + return column; + } + + const firstNonWhitespaceCharacterCode = sourceFile.text.charCodeAt(startPostionOfLine + character); + return firstNonWhitespaceCharacterCode === CharacterCodes.asterisk ? column - 1 : column; } if (!precedingToken) {