mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-07-12 23:03:10 -05:00
make indent work with trailing comments
This commit is contained in:
@@ -2500,13 +2500,13 @@ namespace FourSlash {
|
||||
}
|
||||
}
|
||||
|
||||
public verifyisInMultiLineCommentAtPosition(negative: boolean) {
|
||||
public verifyIsInMultiLineCommentAtPosition(negative: boolean) {
|
||||
const expected = !negative;
|
||||
const position = this.currentCaretPosition;
|
||||
const fileName = this.activeFile.fileName;
|
||||
const actual = this.languageService.getisInMultiLineCommentAtPosition(fileName, position);
|
||||
const actual = this.languageService.isInMultiLineCommentAtPosition(fileName, position);
|
||||
if (expected !== actual) {
|
||||
this.raiseError(`verifyIsInDocComment failed: at position '${position}' in '${fileName}', expected '${expected}'.`);
|
||||
this.raiseError(`verifyIsInMultiLineCommentAtPosition failed: at position '${position}' in '${fileName}', expected '${expected}'.`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3584,7 +3584,7 @@ namespace FourSlashInterface {
|
||||
}
|
||||
|
||||
public isInMultiLineCommentAtPosition() {
|
||||
this.state.verifyisInMultiLineCommentAtPosition(this.negative);
|
||||
this.state.verifyIsInMultiLineCommentAtPosition(this.negative);
|
||||
}
|
||||
|
||||
public codeFixAvailable() {
|
||||
|
||||
@@ -486,7 +486,7 @@ namespace Harness.LanguageService {
|
||||
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {
|
||||
return unwrapJSONCallResult(this.shim.isValidBraceCompletionAtPosition(fileName, position, openingBrace));
|
||||
}
|
||||
getisInMultiLineCommentAtPosition(fileName: string, position: number): boolean {
|
||||
isInMultiLineCommentAtPosition(fileName: string, position: number): boolean {
|
||||
return unwrapJSONCallResult(this.shim.getisInMultiLineCommentAtPosition(fileName, position));
|
||||
}
|
||||
getCodeFixesAtPosition(): ts.CodeAction[] {
|
||||
|
||||
@@ -676,7 +676,7 @@ namespace ts.server {
|
||||
return notImplemented();
|
||||
}
|
||||
|
||||
getisInMultiLineCommentAtPosition(_fileName: string, _position: number): boolean {
|
||||
isInMultiLineCommentAtPosition(_fileName: string, _position: number): boolean {
|
||||
return notImplemented();
|
||||
}
|
||||
|
||||
|
||||
@@ -965,7 +965,7 @@ namespace ts.server {
|
||||
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
|
||||
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
|
||||
const position = this.getPosition(args, scriptInfo);
|
||||
return project.getLanguageService(/*ensureSynchronized*/ false).getisInMultiLineCommentAtPosition(file, position);
|
||||
return project.getLanguageService(/*ensureSynchronized*/ false).isInMultiLineCommentAtPosition(file, position);
|
||||
}
|
||||
|
||||
private getIndentation(args: protocol.IndentationRequestArgs) {
|
||||
|
||||
@@ -1118,23 +1118,36 @@ namespace ts.formatting {
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns -1 iff the position is not in a multi-line comment.
|
||||
* 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.
|
||||
*/
|
||||
export function getIndentationOfEnclosingMultiLineComment(sourceFile: SourceFile, position: number): number {
|
||||
const token = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false);
|
||||
const leadingCommentRanges = getLeadingCommentRangesOfNode(token, sourceFile);
|
||||
if (leadingCommentRanges) {
|
||||
loop: for (const range of leadingCommentRanges) {
|
||||
export function getIndentationOfEnclosingMultiLineComment(sourceFile: SourceFile, position: number, options: EditorSettings): number {
|
||||
const range = getRangeOfEnclosingComment(sourceFile, position, SyntaxKind.MultiLineCommentTrivia);
|
||||
if (range) {
|
||||
const commentStart = range.pos;
|
||||
const commentLineStart = getLineStartPositionForPosition(commentStart, sourceFile);
|
||||
const { column, character } = SmartIndenter.findFirstNonWhitespaceCharacterAndColumn(commentLineStart, commentStart, sourceFile, options);
|
||||
return range.pos - character + column;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, kind: CommentKind): CommentRange | undefined {
|
||||
const precedingToken = findPrecedingToken(position, sourceFile);
|
||||
const trailingRangesOfPreviousToken = precedingToken && getTrailingCommentRanges(sourceFile.text, precedingToken.end);
|
||||
const leadingCommentRangesOfNextToken = getLeadingCommentRangesOfNode(getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false), sourceFile);
|
||||
const commentRanges = trailingRangesOfPreviousToken && leadingCommentRangesOfNextToken ?
|
||||
trailingRangesOfPreviousToken.concat(leadingCommentRangesOfNextToken) :
|
||||
trailingRangesOfPreviousToken || leadingCommentRangesOfNextToken;
|
||||
if (commentRanges) {
|
||||
for (const range of commentRanges) {
|
||||
// We need to extend the range when in an unclosed multi-line comment.
|
||||
if (range.pos < position && (position < range.end || position === range.end && position === sourceFile.getFullWidth())) {
|
||||
if (range.kind === SyntaxKind.MultiLineCommentTrivia) {
|
||||
return range.pos - getLineStartPositionForPosition(range.pos, sourceFile);
|
||||
}
|
||||
break loop;
|
||||
return range.kind === kind ? range : undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -44,8 +44,8 @@ namespace ts.formatting {
|
||||
|
||||
const lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line;
|
||||
|
||||
const indentationOfEnclosingMultiLineComment = getIndentationOfEnclosingMultiLineComment(sourceFile, position);
|
||||
if (indentationOfEnclosingMultiLineComment !== -1) {
|
||||
const indentationOfEnclosingMultiLineComment = getIndentationOfEnclosingMultiLineComment(sourceFile, position, options);
|
||||
if (indentationOfEnclosingMultiLineComment >= 0) {
|
||||
return indentationOfEnclosingMultiLineComment;
|
||||
}
|
||||
|
||||
@@ -410,13 +410,13 @@ namespace ts.formatting {
|
||||
return findFirstNonWhitespaceColumn(lineStart, lineStart + lineAndCharacter.character, sourceFile, options);
|
||||
}
|
||||
|
||||
/*
|
||||
Character is the actual index of the character since the beginning of the line.
|
||||
Column - position of the character after expanding tabs to spaces
|
||||
"0\t2$"
|
||||
value of 'character' for '$' is 3
|
||||
value of 'column' for '$' is 6 (assuming that tab size is 4)
|
||||
*/
|
||||
/**
|
||||
* Character is the actual index of the character since the beginning of the line.
|
||||
* Column - position of the character after expanding tabs to spaces.
|
||||
* "0\t2$"
|
||||
* value of 'character' for '$' is 3
|
||||
* value of 'column' for '$' is 6 (assuming that tab size is 4)
|
||||
*/
|
||||
export function findFirstNonWhitespaceCharacterAndColumn(startPos: number, endPos: number, sourceFile: SourceFileLike, options: EditorSettings) {
|
||||
let character = 0;
|
||||
let column = 0;
|
||||
|
||||
@@ -1818,9 +1818,9 @@ namespace ts {
|
||||
return true;
|
||||
}
|
||||
|
||||
function getisInMultiLineCommentAtPosition(fileName: string, position: number): boolean {
|
||||
function isInMultiLineCommentAtPosition(fileName: string, position: number): boolean {
|
||||
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
|
||||
return ts.formatting.getIndentationOfEnclosingMultiLineComment(sourceFile, position) !== -1;
|
||||
return !!ts.formatting.getRangeOfEnclosingComment(sourceFile, position, SyntaxKind.MultiLineCommentTrivia);
|
||||
}
|
||||
|
||||
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {
|
||||
@@ -2045,7 +2045,7 @@ namespace ts {
|
||||
getFormattingEditsAfterKeystroke,
|
||||
getDocCommentTemplateAtPosition,
|
||||
isValidBraceCompletionAtPosition,
|
||||
getisInMultiLineCommentAtPosition,
|
||||
isInMultiLineCommentAtPosition,
|
||||
getCodeFixesAtPosition,
|
||||
getEmitOutput,
|
||||
getNonBoundSourceFile,
|
||||
|
||||
@@ -844,7 +844,7 @@ namespace ts {
|
||||
public getisInMultiLineCommentAtPosition(fileName: string, position: number): string {
|
||||
return this.forwardJSONCall(
|
||||
`getisInMultiLineCommentAtPosition('${fileName}', ${position})`,
|
||||
() => this.languageService.getisInMultiLineCommentAtPosition(fileName, position)
|
||||
() => this.languageService.isInMultiLineCommentAtPosition(fileName, position)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -260,7 +260,7 @@ namespace ts {
|
||||
|
||||
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
|
||||
|
||||
getisInMultiLineCommentAtPosition(fileName: string, position: number): boolean;
|
||||
isInMultiLineCommentAtPosition(fileName: string, position: number): boolean;
|
||||
|
||||
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
|
||||
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
|
||||
|
||||
@@ -1,36 +1,46 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
//// /* x */
|
||||
//// /** x */
|
||||
//// /**
|
||||
//// * @param this doesn't make sense here.
|
||||
//// */
|
||||
//// // x
|
||||
//// let x = 1;
|
||||
//// let x = 1; /*
|
||||
//// *
|
||||
|
||||
const firstCommentStart = 0;
|
||||
const firstCommentEnd = 7;
|
||||
goTo.position(firstCommentStart);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
|
||||
for (let i = 1; i < 7; ++i) {
|
||||
goTo.position(i);
|
||||
verify.isInMultiLineCommentAtPosition();
|
||||
}
|
||||
goTo.position(firstCommentStart + 1);
|
||||
verify.isInMultiLineCommentAtPosition();
|
||||
goTo.position(firstCommentEnd - 1);
|
||||
verify.isInMultiLineCommentAtPosition();
|
||||
|
||||
for (let i = 0; i < 2; ++i) {
|
||||
goTo.position(i * 7);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
}
|
||||
goTo.position(firstCommentEnd);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
|
||||
const jsDocStart = 8;
|
||||
const multilineJsDocStart = firstCommentEnd + 1;
|
||||
const multilineJsDocEnd = multilineJsDocStart + 49;
|
||||
|
||||
for (let i = 1; i < 8; ++i) {
|
||||
goTo.position(jsDocStart + i);
|
||||
verify.isInMultiLineCommentAtPosition();
|
||||
}
|
||||
goTo.position(multilineJsDocStart);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
goTo.position(multilineJsDocStart + 1);
|
||||
verify.isInMultiLineCommentAtPosition();
|
||||
goTo.position(multilineJsDocEnd - 1);
|
||||
verify.isInMultiLineCommentAtPosition();
|
||||
goTo.position(multilineJsDocEnd);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
|
||||
for (let i = 0; i < 2; ++i) {
|
||||
goTo.position(jsDocStart + i * 8);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
}
|
||||
const singleLineCommentStart = multilineJsDocEnd + 1;
|
||||
|
||||
const singleLineCommentStart = 17;
|
||||
goTo.position(singleLineCommentStart + 1);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
|
||||
for (let i = 0; i < 5; ++i) {
|
||||
goTo.position(singleLineCommentStart + i);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
}
|
||||
const postNodeCommentStart = singleLineCommentStart + 16;
|
||||
|
||||
goTo.position(postNodeCommentStart);
|
||||
verify.not.isInMultiLineCommentAtPosition();
|
||||
goTo.position(postNodeCommentStart + 1);
|
||||
verify.isInMultiLineCommentAtPosition();
|
||||
|
||||
Reference in New Issue
Block a user