From 4ea3bb6485a14515da46053ea119ea6eb8f14ade Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Wed, 5 Aug 2015 16:22:27 -0700 Subject: [PATCH] Make 'predicate' optional and DRY --- src/services/services.ts | 20 ++++++++++++-------- src/services/utilities.ts | 15 +++++++++------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 0a4f60d916d..ad158192cf6 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -6826,6 +6826,10 @@ namespace ts { return undefined; } + // TODO: add support for: + // * methods + // * constructors + // * class decls let containingFunction = getAncestor(tokenAtPos, SyntaxKind.FunctionDeclaration); if (!containingFunction || containingFunction.getStart() < position) { @@ -6836,12 +6840,13 @@ namespace ts { let posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position); let lineStart = sourceFile.getLineStarts()[posLineAndChar.line]; - let indentationStr = sourceFile.text.substr(lineStart, posLineAndChar.character).match(/\s*/).toString(); + let indentationStr = sourceFile.text.substr(lineStart, posLineAndChar.character); + // TODO: call a helper method instead once PR #4133 gets merged in. const newLine = host.getNewLine ? host.getNewLine() : "\r\n"; let docParams = parameters.map((p, index) => - indentationStr + " * @param " + (p.name.kind === SyntaxKind.Identifier ? (p.name).text : "param" + index.toString()) + newLine); + indentationStr + " * @param " + (p.name.kind === SyntaxKind.Identifier ? (p.name).text : "param" + index) + newLine); // A doc comment consists of the following @@ -6851,16 +6856,15 @@ namespace ts { // * TODO: other tags. // * the closing comment line // * if the caret was directly in front of the object, then we add an extra line and indentation. + const preamble = "/**" + newLine + + indentationStr + " * "; let result = - "/**" + newLine + - indentationStr + " * " + newLine + - docParams.reduce((prev, cur) => prev + cur, "") + + preamble + newLine + + docParams.join("") + indentationStr + " */" + (tokenStart === position ? newLine + indentationStr : ""); - let cursorOffset = "/**".length + newLine.length + indentationStr.length + " * ".length; - - return { newText: result, caretOffset: cursorOffset }; + return { newText: result, caretOffset: preamble.length }; } function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 36ae22ca46a..a5230e7eb08 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -421,14 +421,14 @@ namespace ts { } export function isInComment(sourceFile: SourceFile, position: number) { - return isInCommentHelper(sourceFile, position, /*predicate*/ c => true); + return isInCommentHelper(sourceFile, position, /*predicate*/ undefined); } /** * Returns true if the cursor at position in sourceFile is within a comment that additionally * satisfies predicate, and false otherwise. */ - export function isInCommentHelper(sourceFile: SourceFile, position: number, predicate: (c: CommentRange) => boolean): boolean { + export function isInCommentHelper(sourceFile: SourceFile, position: number, predicate?: (c: CommentRange) => boolean): boolean { let token = getTokenAtPosition(sourceFile, position); if (token && position <= token.getStart()) { @@ -444,9 +444,12 @@ namespace ts { // /* asdf */^ // // Internally, we represent the end of the comment at the newline and closing '/', respectively. - return forEach(commentRanges, c => c.pos < position && - (c.kind == SyntaxKind.SingleLineCommentTrivia ? position <= c.end : position < c.end) && - predicate(c)); + return predicate ? + forEach(commentRanges, c => c.pos < position && + (c.kind == SyntaxKind.SingleLineCommentTrivia ? position <= c.end : position < c.end) && + predicate(c)) : + forEach(commentRanges, c => c.pos < position && + (c.kind == SyntaxKind.SingleLineCommentTrivia ? position <= c.end : position < c.end)); } return false; @@ -458,7 +461,7 @@ namespace ts { // First, we have to see if this position actually landed in a comment. let commentRanges = getLeadingCommentRanges(sourceFile.text, token.pos); - return forEach(commentRanges, c => jsDocPrefix); + return forEach(commentRanges, jsDocPrefix); function jsDocPrefix(c: CommentRange): boolean { var text = sourceFile.text;