Skip to content

Commit 02fbc07

Browse files
committed
Merge pull request #1906 from Microsoft/formattingTriggerInComment
ensure that autoformat is not triggered from inside comments
2 parents bbbec22 + c095bb3 commit 02fbc07

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/services/formatting/formatting.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ module ts.formatting {
120120

121121
function findOutermostParent(position: number, expectedTokenKind: SyntaxKind, sourceFile: SourceFile): Node {
122122
var precedingToken = findPrecedingToken(position, sourceFile);
123-
if (!precedingToken || precedingToken.kind !== expectedTokenKind) {
123+
124+
// when it is claimed that trigger character was typed at given position
125+
// we verify that there is a token with a matching kind whose end is equal to position (because the character was just typed).
126+
// If this condition is not hold - then trigger character was typed in some other context,
127+
// i.e.in comment and thus should not trigger autoformatting
128+
if (!precedingToken ||
129+
precedingToken.kind !== expectedTokenKind ||
130+
position !== precedingToken.getEnd()) {
124131
return undefined;
125132
}
126133

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts'/>
2+
////class A {
3+
////foo( ); // /*1*/
4+
////}
5+
////function foo() { var x; } // /*2*/
6+
7+
goTo.marker("1");
8+
edit.insert(";");
9+
verify.currentLineContentIs("foo( ); // ;")
10+
11+
goTo.marker("2");
12+
edit.insert("}");
13+
verify.currentLineContentIs("function foo() { var x; } // }");

0 commit comments

Comments
 (0)