Merge pull request #1746 from Microsoft/formattingTemplateLiterals
fixed smart indentation\formatting in template literals
This commit is contained in:
commit
7f749909da
5 changed files with 54 additions and 6 deletions
|
@ -243,8 +243,18 @@ module ts.formatting {
|
|||
}
|
||||
|
||||
var precedingToken = findPrecedingToken(originalRange.pos, sourceFile);
|
||||
// no preceding token found - start from the beginning of enclosing node
|
||||
return precedingToken ? precedingToken.end : enclosingNode.pos;
|
||||
if (!precedingToken) {
|
||||
// no preceding token found - start from the beginning of enclosing node
|
||||
return enclosingNode.pos;
|
||||
}
|
||||
|
||||
// preceding token ends after the start of original range (i.e when originaRange.pos falls in the middle of literal)
|
||||
// start from the beginning of enclosingNode to handle the entire 'originalRange'
|
||||
if (precedingToken.end >= originalRange.pos) {
|
||||
return enclosingNode.pos;
|
||||
}
|
||||
|
||||
return precedingToken.end;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -187,6 +187,9 @@ module ts.formatting {
|
|||
}
|
||||
|
||||
// consume trailing trivia
|
||||
if (trailingTrivia) {
|
||||
trailingTrivia = undefined;
|
||||
}
|
||||
while(scanner.getStartPos() < endPos) {
|
||||
currentToken = scanner.scan();
|
||||
if (!isTrivia(currentToken)) {
|
||||
|
|
|
@ -12,10 +12,15 @@ module ts.formatting {
|
|||
return 0;
|
||||
}
|
||||
|
||||
// no indentation in string \regex literals
|
||||
if ((precedingToken.kind === SyntaxKind.StringLiteral || precedingToken.kind === SyntaxKind.RegularExpressionLiteral) &&
|
||||
precedingToken.getStart(sourceFile) <= position &&
|
||||
precedingToken.end > position) {
|
||||
// no indentation in string \regex\template literals
|
||||
var precedingTokenIsLiteral =
|
||||
precedingToken.kind === SyntaxKind.StringLiteral ||
|
||||
precedingToken.kind === SyntaxKind.RegularExpressionLiteral ||
|
||||
precedingToken.kind === SyntaxKind.NoSubstitutionTemplateLiteral ||
|
||||
precedingToken.kind === SyntaxKind.TemplateHead ||
|
||||
precedingToken.kind === SyntaxKind.TemplateMiddle ||
|
||||
precedingToken.kind === SyntaxKind.TemplateTail;
|
||||
if (precedingTokenIsLiteral && precedingToken.getStart(sourceFile) <= position && precedingToken.end > position) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
13
tests/cases/fourslash/formatTemplateLiteral.ts
Normal file
13
tests/cases/fourslash/formatTemplateLiteral.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
/// <reference path="fourslash.ts"/>
|
||||
////var x = `sadasdasdasdasfegsfd
|
||||
/////*1*/rasdesgeryt35t35y35 e4 ergt er 35t 3535 `;
|
||||
////var y = `1${2}/*2*/3`;
|
||||
|
||||
|
||||
goTo.marker("1");
|
||||
edit.insert("\r\n"); // edit will trigger formatting - should succeeed
|
||||
|
||||
goTo.marker("2");
|
||||
edit.insert("\r\n");
|
||||
verify.indentationIs(0);
|
||||
verify.currentLineContentIs("3`;")
|
17
tests/cases/fourslash/smartIndentTemplateLiterals.ts
Normal file
17
tests/cases/fourslash/smartIndentTemplateLiterals.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
/// <reference path="fourslash.ts"/>
|
||||
////var x0 = `sadasdasdasdas/*1*/fegsfdrasdesgeryt35t35y35 e4 ergt er 35t 3535 `;
|
||||
////var x1 = `sadasdasdasdas/*2*/fegsfdr${0}asdesgeryt35t35y35 e4 ergt er 35t 3535 `;
|
||||
////var x2 = `sadasdasdasdasfegsfdra${0}sdesge/*3*/ryt35t35y35 e4 ergt er 35t 3535 `;
|
||||
////var x3 = `sadasdasdasdasfegsfdra${0}sdesge/*4*/ryt35${1}t35y35 e4 ergt er 35t 3535 `;
|
||||
////var x2 = `sadasdasdasdasfegsfdra${0}sdesge${1}sf/*5*/ryt35t35y35 e4 ergt er 35t 3535 `;
|
||||
|
||||
function verifyIndentation(marker: string): void {
|
||||
goTo.marker(marker);
|
||||
edit.insert("\r\n");
|
||||
verify.indentationIs(0);
|
||||
}
|
||||
verifyIndentation("1");
|
||||
verifyIndentation("2");
|
||||
verifyIndentation("3");
|
||||
verifyIndentation("4");
|
||||
verifyIndentation("5");
|
Loading…
Reference in a new issue