From a819e4ed172405bffe6fb419483bdccaeb1de16b Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Wed, 7 Jun 2017 16:43:13 -0700 Subject: [PATCH] isInMultiLineComment --- src/services/services.ts | 17 +++++---- tests/cases/fourslash/isInMultiLineComment.ts | 36 +++++++++++++++++-- .../fourslash/isInMultiLineCommentEOF.ts | 12 +++++++ 3 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 tests/cases/fourslash/isInMultiLineCommentEOF.ts diff --git a/src/services/services.ts b/src/services/services.ts index 8da654140e0..1f85ad427f5 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1791,12 +1791,17 @@ namespace ts { function getIsInMultiLineComment(_fileName: string, position: number): boolean { const sourceFile = syntaxTreeCache.getCurrentSourceFile(_fileName); - const token = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ true); - const _triviaWidth = token.getLeadingTriviaWidth(sourceFile); _triviaWidth; - const _text = token.getText(sourceFile); _text; - const _fullText = token.getFullText(sourceFile); _fullText; - // TODO: distinguish multi-line and single line comments... - return token.getFullStart() <= position && position < token.getStart(sourceFile, /*includeJsDocComment*/ false); + 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 { diff --git a/tests/cases/fourslash/isInMultiLineComment.ts b/tests/cases/fourslash/isInMultiLineComment.ts index 4d6c8d6af1a..ebcac7215f9 100644 --- a/tests/cases/fourslash/isInMultiLineComment.ts +++ b/tests/cases/fourslash/isInMultiLineComment.ts @@ -1,6 +1,36 @@ /// -//// /* blach /*m0*/ */ let x = 10; +//// /* x */ +//// /** x */ +//// // x +//// let x = 1; -goTo.marker("m0"); -verify.isInMultiLineComment(); \ No newline at end of file + +for (let i = 1; i < 7; ++i) { + goTo.position(i); + verify.isInMultiLineComment(); +} + +for (let i = 0; i < 2; ++i) { + goTo.position(i * 7); + verify.not.isInMultiLineComment(); +} + +const jsDocStart = 8; + +for (let i = 1; i < 8; ++i) { + goTo.position(jsDocStart + i); + verify.isInMultiLineComment(); +} + +for (let i = 0; i < 2; ++i) { + goTo.position(jsDocStart + i * 8); + verify.not.isInMultiLineComment(); +} + +const singleLineCommentStart = 17; + +for (let i = 0; i < 5; ++i) { + goTo.position(singleLineCommentStart + i); + verify.not.isInMultiLineComment(); +} diff --git a/tests/cases/fourslash/isInMultiLineCommentEOF.ts b/tests/cases/fourslash/isInMultiLineCommentEOF.ts new file mode 100644 index 00000000000..2e34264b1f6 --- /dev/null +++ b/tests/cases/fourslash/isInMultiLineCommentEOF.ts @@ -0,0 +1,12 @@ +/// + +// @Filename: f1.ts +//// /* /*0*/ blah /*1*/ */ + +// @Filename: f2.ts +//// /* /*2*/ blah /*3*/ + +for (let i = 0; i < 4; ++i) { + goTo.marker(`${i}`); + verify.isInMultiLineComment(); +} \ No newline at end of file