mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
indent block comments according to first line
This commit is contained in:
1 parent
a819e4ed17
commit
8f28a0264f
13 files changed
+81
-70
No files matched your search
@@ -1117,6 +1117,27 @@ namespace ts.formatting {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns -1 iff 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) {
|
||||
// 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 -1;
|
||||
}
|
||||
|
||||
|
||||
function getOpenTokenForList(node: Node, list: Node[]) {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.Constructor:
|
||||
|
||||
@@ -38,12 +38,17 @@ namespace ts.formatting {
|
||||
|
||||
// no indentation in string \regex\template literals
|
||||
const precedingTokenIsLiteral = isStringOrRegularExpressionOrTemplateLiteral(precedingToken.kind);
|
||||
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) {
|
||||
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && position < precedingToken.end) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line;
|
||||
|
||||
const indentationOfEnclosingMultiLineComment = getIndentationOfEnclosingMultiLineComment(sourceFile, position);
|
||||
if (indentationOfEnclosingMultiLineComment !== -1) {
|
||||
return indentationOfEnclosingMultiLineComment;
|
||||
}
|
||||
|
||||
// indentation is first non-whitespace character in a previous line
|
||||
// for block indentation, we should look for a line which contains something that's not
|
||||
// whitespace.
|
||||
|
||||
@@ -1789,21 +1789,6 @@ namespace ts {
|
||||
return JsDoc.getDocCommentTemplateAtPosition(getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position);
|
||||
}
|
||||
|
||||
function getIsInMultiLineComment(_fileName: string, position: number): boolean {
|
||||
const sourceFile = syntaxTreeCache.getCurrentSourceFile(_fileName);
|
||||
const token = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false);
|
||||
const leadingCommentRanges = getLeadingCommentRangesOfNode(token, sourceFile);
|
||||
|
||||
for (const range of leadingCommentRanges) {
|
||||
// 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())) {
|
||||
return range.kind === SyntaxKind.MultiLineCommentTrivia;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {
|
||||
// '<' is currently not supported, figuring out if we're in a Generic Type vs. a comparison is too
|
||||
// expensive to do during typing scenarios
|
||||
@@ -1833,6 +1818,11 @@ namespace ts {
|
||||
return true;
|
||||
}
|
||||
|
||||
function getisInMultiLineCommentAtPosition(fileName: string, position: number): boolean {
|
||||
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
|
||||
return ts.formatting.getIndentationOfEnclosingMultiLineComment(sourceFile, position) !== -1;
|
||||
}
|
||||
|
||||
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {
|
||||
// Note: while getting todo comments seems like a syntactic operation, we actually
|
||||
// treat it as a semantic operation here. This is because we expect our host to call
|
||||
@@ -2054,8 +2044,8 @@ namespace ts {
|
||||
getFormattingEditsForDocument,
|
||||
getFormattingEditsAfterKeystroke,
|
||||
getDocCommentTemplateAtPosition,
|
||||
getIsInMultiLineComment,
|
||||
isValidBraceCompletionAtPosition,
|
||||
getisInMultiLineCommentAtPosition,
|
||||
getCodeFixesAtPosition,
|
||||
getEmitOutput,
|
||||
getNonBoundSourceFile,
|
||||
|
||||
+13
-12
@@ -247,11 +247,6 @@ namespace ts {
|
||||
*/
|
||||
getDocCommentTemplateAtPosition(fileName: string, position: number): string;
|
||||
|
||||
/**
|
||||
* Returns JSON-encoded value of the type TextInsertion.
|
||||
*/
|
||||
getIsInMultiLineComment(fileName: string, position: number): string;
|
||||
|
||||
/**
|
||||
* Returns JSON-encoded boolean to indicate whether we should support brace location
|
||||
* at the current position.
|
||||
@@ -259,6 +254,11 @@ namespace ts {
|
||||
*/
|
||||
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): string;
|
||||
|
||||
/**
|
||||
* Returns JSON-encoded boolean to indicate whether the caret at the current position is in a multi-line comment.
|
||||
*/
|
||||
getisInMultiLineCommentAtPosition(fileName: string, position: number): string;
|
||||
|
||||
getEmitOutput(fileName: string): string;
|
||||
getEmitOutputObject(fileName: string): EmitOutput;
|
||||
}
|
||||
@@ -840,6 +840,14 @@ namespace ts {
|
||||
);
|
||||
}
|
||||
|
||||
/// GET IS IN MULTI-LINE COMMENT
|
||||
public getisInMultiLineCommentAtPosition(fileName: string, position: number): string {
|
||||
return this.forwardJSONCall(
|
||||
`getisInMultiLineCommentAtPosition('${fileName}', ${position})`,
|
||||
() => this.languageService.getisInMultiLineCommentAtPosition(fileName, position)
|
||||
);
|
||||
}
|
||||
|
||||
/// GET SMART INDENT
|
||||
public getIndentationAtPosition(fileName: string, position: number, options: string /*Services.EditorOptions*/): string {
|
||||
return this.forwardJSONCall(
|
||||
@@ -940,13 +948,6 @@ namespace ts {
|
||||
);
|
||||
}
|
||||
|
||||
public getIsInMultiLineComment(fileName: string, position: number): string {
|
||||
return this.forwardJSONCall(
|
||||
`getIsInMultiLineComment('${fileName}', ${position})`,
|
||||
() => this.languageService.getIsInMultiLineComment(fileName, position)
|
||||
);
|
||||
}
|
||||
|
||||
/// NAVIGATE TO
|
||||
|
||||
/** Return a list of symbols that are interesting to navigate to */
|
||||
|
||||
@@ -257,10 +257,11 @@ namespace ts {
|
||||
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
|
||||
|
||||
getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion;
|
||||
getIsInMultiLineComment(fileName: string, position: number): boolean;
|
||||
|
||||
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
|
||||
|
||||
getisInMultiLineCommentAtPosition(fileName: string, position: number): boolean;
|
||||
|
||||
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
|
||||
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
|
||||
getRefactorCodeActions(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string): CodeAction[] | undefined;
|
||||
|
||||
Reference in new issue
Block a user