From 889264f45700ccde10f4ae816150329cef8be541 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Mon, 2 Feb 2015 22:21:41 -0800 Subject: [PATCH 1/2] ensure that autoformat is triggered by token characters --- src/services/formatting/formatting.ts | 4 +++- tests/cases/fourslash/formattingInComment.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/formattingInComment.ts diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index cab7547557c..c0188494f71 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -120,7 +120,9 @@ module ts.formatting { function findOutermostParent(position: number, expectedTokenKind: SyntaxKind, sourceFile: SourceFile): Node { var precedingToken = findPrecedingToken(position, sourceFile); - if (!precedingToken || precedingToken.kind !== expectedTokenKind) { + if (!precedingToken || + precedingToken.kind !== expectedTokenKind || + position !== precedingToken.getEnd()) { return undefined; } diff --git a/tests/cases/fourslash/formattingInComment.ts b/tests/cases/fourslash/formattingInComment.ts new file mode 100644 index 00000000000..9ce1c9203d5 --- /dev/null +++ b/tests/cases/fourslash/formattingInComment.ts @@ -0,0 +1,13 @@ +/// +////class A { +////foo( ); // /*1*/ +////} +////function foo() { var x; } // /*2*/ + +goTo.marker("1"); +edit.insert(";"); +verify.currentLineContentIs("foo( ); // ;") + +goTo.marker("2"); +edit.insert("}"); +verify.currentLineContentIs("function foo() { var x; } // }"); \ No newline at end of file From c095bb356c7c3acb8ce9661467900e46cab7bd7d Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Tue, 3 Feb 2015 10:17:11 -0800 Subject: [PATCH 2/2] [format on keystroke]added comment to the verification of preceding token --- src/services/formatting/formatting.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index c0188494f71..969f7260744 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -120,6 +120,11 @@ module ts.formatting { function findOutermostParent(position: number, expectedTokenKind: SyntaxKind, sourceFile: SourceFile): Node { var precedingToken = findPrecedingToken(position, sourceFile); + + // when it is claimed that trigger character was typed at given position + // we verify that there is a token with a matching kind whose end is equal to position (because the character was just typed). + // If this condition is not hold - then trigger character was typed in some other context, + // i.e.in comment and thus should not trigger autoformatting if (!precedingToken || precedingToken.kind !== expectedTokenKind || position !== precedingToken.getEnd()) {