Skip to content

Commit 4ea3bb6

Browse files
author
Arthur Ozga
committed
Make 'predicate' optional and DRY
1 parent f3e03db commit 4ea3bb6

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

src/services/services.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6826,6 +6826,10 @@ namespace ts {
68266826
return undefined;
68276827
}
68286828

6829+
// TODO: add support for:
6830+
// * methods
6831+
// * constructors
6832+
// * class decls
68296833
let containingFunction = <FunctionDeclaration>getAncestor(tokenAtPos, SyntaxKind.FunctionDeclaration);
68306834

68316835
if (!containingFunction || containingFunction.getStart() < position) {
@@ -6836,12 +6840,13 @@ namespace ts {
68366840
let posLineAndChar = sourceFile.getLineAndCharacterOfPosition(position);
68376841
let lineStart = sourceFile.getLineStarts()[posLineAndChar.line];
68386842

6839-
let indentationStr = sourceFile.text.substr(lineStart, posLineAndChar.character).match(/\s*/).toString();
6843+
let indentationStr = sourceFile.text.substr(lineStart, posLineAndChar.character);
68406844

6845+
// TODO: call a helper method instead once PR #4133 gets merged in.
68416846
const newLine = host.getNewLine ? host.getNewLine() : "\r\n";
68426847

68436848
let docParams = parameters.map((p, index) =>
6844-
indentationStr + " * @param " + (p.name.kind === SyntaxKind.Identifier ? (<Identifier>p.name).text : "param" + index.toString()) + newLine);
6849+
indentationStr + " * @param " + (p.name.kind === SyntaxKind.Identifier ? (<Identifier>p.name).text : "param" + index) + newLine);
68456850

68466851

68476852
// A doc comment consists of the following
@@ -6851,16 +6856,15 @@ namespace ts {
68516856
// * TODO: other tags.
68526857
// * the closing comment line
68536858
// * if the caret was directly in front of the object, then we add an extra line and indentation.
6859+
const preamble = "/**" + newLine +
6860+
indentationStr + " * ";
68546861
let result =
6855-
"/**" + newLine +
6856-
indentationStr + " * " + newLine +
6857-
docParams.reduce((prev, cur) => prev + cur, "") +
6862+
preamble + newLine +
6863+
docParams.join("") +
68586864
indentationStr + " */" +
68596865
(tokenStart === position ? newLine + indentationStr : "");
68606866

6861-
let cursorOffset = "/**".length + newLine.length + indentationStr.length + " * ".length;
6862-
6863-
return { newText: result, caretOffset: cursorOffset };
6867+
return { newText: result, caretOffset: preamble.length };
68646868
}
68656869

68666870
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {

src/services/utilities.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,14 @@ namespace ts {
421421
}
422422

423423
export function isInComment(sourceFile: SourceFile, position: number) {
424-
return isInCommentHelper(sourceFile, position, /*predicate*/ c => true);
424+
return isInCommentHelper(sourceFile, position, /*predicate*/ undefined);
425425
}
426426

427427
/**
428428
* Returns true if the cursor at position in sourceFile is within a comment that additionally
429429
* satisfies predicate, and false otherwise.
430430
*/
431-
export function isInCommentHelper(sourceFile: SourceFile, position: number, predicate: (c: CommentRange) => boolean): boolean {
431+
export function isInCommentHelper(sourceFile: SourceFile, position: number, predicate?: (c: CommentRange) => boolean): boolean {
432432
let token = getTokenAtPosition(sourceFile, position);
433433

434434
if (token && position <= token.getStart()) {
@@ -444,9 +444,12 @@ namespace ts {
444444
// /* asdf */^
445445
//
446446
// Internally, we represent the end of the comment at the newline and closing '/', respectively.
447-
return forEach(commentRanges, c => c.pos < position &&
448-
(c.kind == SyntaxKind.SingleLineCommentTrivia ? position <= c.end : position < c.end) &&
449-
predicate(c));
447+
return predicate ?
448+
forEach(commentRanges, c => c.pos < position &&
449+
(c.kind == SyntaxKind.SingleLineCommentTrivia ? position <= c.end : position < c.end) &&
450+
predicate(c)) :
451+
forEach(commentRanges, c => c.pos < position &&
452+
(c.kind == SyntaxKind.SingleLineCommentTrivia ? position <= c.end : position < c.end));
450453
}
451454

452455
return false;
@@ -458,7 +461,7 @@ namespace ts {
458461
// First, we have to see if this position actually landed in a comment.
459462
let commentRanges = getLeadingCommentRanges(sourceFile.text, token.pos);
460463

461-
return forEach(commentRanges, c => jsDocPrefix);
464+
return forEach(commentRanges, jsDocPrefix);
462465

463466
function jsDocPrefix(c: CommentRange): boolean {
464467
var text = sourceFile.text;

0 commit comments

Comments
 (0)