Make 'predicate' optional and DRY

This commit is contained in:
Arthur Ozga 2015-08-05 16:22:27 -07:00
parent f3e03dbec7
commit 4ea3bb6485
2 changed files with 21 additions and 14 deletions

View File

@ -6826,6 +6826,10 @@ namespace ts {
return undefined;
}
// TODO: add support for:
// * methods
// * constructors
// * class decls
let containingFunction = <FunctionDeclaration>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 ? (<Identifier>p.name).text : "param" + index.toString()) + newLine);
indentationStr + " * @param " + (p.name.kind === SyntaxKind.Identifier ? (<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[] {

View File

@ -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;