Rename Comment->CommentRange.

This commit is contained in:
Cyrus Najmabadi 2014-09-21 14:52:51 -07:00
parent 276a735347
commit e3609e20af
5 changed files with 60 additions and 33 deletions

View file

@ -182,7 +182,7 @@ module ts {
});
}
function emitComments(comments: Comment[], trailingSeparator: boolean, writer: EmitTextWriter, writeComment: (comment: Comment, writer: EmitTextWriter) => void) {
function emitComments(comments: CommentRange[], trailingSeparator: boolean, writer: EmitTextWriter, writeComment: (comment: CommentRange, writer: EmitTextWriter) => void) {
var emitLeadingSpace = !trailingSeparator;
forEach(comments, comment => {
if (emitLeadingSpace) {
@ -203,7 +203,7 @@ module ts {
});
}
function emitNewLineBeforeLeadingComments(node: TextRange, leadingComments: Comment[], writer: EmitTextWriter) {
function emitNewLineBeforeLeadingComments(node: TextRange, leadingComments: CommentRange[], writer: EmitTextWriter) {
// If the leading comments start on different line than the start of node, write new line
if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos &&
getLineOfLocalPosition(node.pos) !== getLineOfLocalPosition(leadingComments[0].pos)) {
@ -211,7 +211,7 @@ module ts {
}
}
function writeCommentRange(comment: Comment, writer: EmitTextWriter) {
function writeCommentRange(comment: CommentRange, writer: EmitTextWriter) {
if (currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk) {
var firstCommentLineAndCharacter = currentSourceFile.getLineAndCharacterFromPosition(comment.pos);
var firstCommentLineIndent: number;
@ -585,7 +585,7 @@ module ts {
sourceMapNameIndices.pop();
};
function writeCommentRangeWithMap(comment: Comment, writer: EmitTextWriter) {
function writeCommentRangeWithMap(comment: CommentRange, writer: EmitTextWriter) {
recordSourceMapSpan(comment.pos);
writeCommentRange(comment, writer);
recordSourceMapSpan(comment.end);
@ -2108,7 +2108,7 @@ module ts {
function getLeadingCommentsWithoutDetachedComments() {
// get the leading comments from detachedPos
var leadingComments = getLeadingComments(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
var leadingComments = getLeadingCommentRanges(currentSourceFile.text, detachedCommentsInfo[detachedCommentsInfo.length - 1].detachedCommentEndPos);
if (detachedCommentsInfo.length - 1) {
detachedCommentsInfo.pop();
}
@ -2122,14 +2122,14 @@ module ts {
function getLeadingCommentsToEmit(node: Node) {
// Emit the leading comments only if the parent's pos doesn't match because parent should take care of emitting these comments
if (node.parent.kind === SyntaxKind.SourceFile || node.pos !== node.parent.pos) {
var leadingComments: Comment[];
var leadingComments: CommentRange[];
if (hasDetachedComments(node.pos)) {
// get comments without detached comments
leadingComments = getLeadingCommentsWithoutDetachedComments();
}
else {
// get the leading comments from the node
leadingComments = getLeadingCommentsOfNode(node, currentSourceFile);
leadingComments = getLeadingCommentRangesOfNode(node, currentSourceFile);
}
return leadingComments;
}
@ -2145,21 +2145,21 @@ module ts {
function emitTrailingDeclarationComments(node: Node) {
// Emit the trailing comments only if the parent's end doesn't match
if (node.parent.kind === SyntaxKind.SourceFile || node.end !== node.parent.end) {
var trailingComments = getTrailingComments(currentSourceFile.text, node.end);
var trailingComments = getTrailingCommentRanges(currentSourceFile.text, node.end);
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/
emitComments(trailingComments, /*trailingSeparator*/ false, writer, writeComment);
}
}
function emitLeadingCommentsOfLocalPosition(pos: number) {
var leadingComments: Comment[];
var leadingComments: CommentRange[];
if (hasDetachedComments(pos)) {
// get comments without detached comments
leadingComments = getLeadingCommentsWithoutDetachedComments();
}
else {
// get the leading comments from the node
leadingComments = getLeadingComments(currentSourceFile.text, pos);
leadingComments = getLeadingCommentRanges(currentSourceFile.text, pos);
}
emitNewLineBeforeLeadingComments({ pos: pos, end: pos }, leadingComments, writer);
// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
@ -2167,10 +2167,10 @@ module ts {
}
function emitDetachedCommentsAtPosition(node: TextRange) {
var leadingComments = getLeadingComments(currentSourceFile.text, node.pos);
var leadingComments = getLeadingCommentRanges(currentSourceFile.text, node.pos);
if (leadingComments) {
var detachedComments: Comment[] = [];
var lastComment: Comment;
var detachedComments: CommentRange[] = [];
var lastComment: CommentRange;
forEach(leadingComments, comment => {
if (lastComment) {
@ -2214,7 +2214,7 @@ module ts {
function emitPinnedOrTripleSlashCommentsOfNode(node: Node) {
var pinnedComments = ts.filter(getLeadingCommentsToEmit(node), isPinnedOrTripleSlashComment);
function isPinnedOrTripleSlashComment(comment: Comment) {
function isPinnedOrTripleSlashComment(comment: CommentRange) {
if (currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk) {
return currentSourceFile.text.charCodeAt(comment.pos + 2) === CharacterCodes.exclamation;
}

View file

@ -138,25 +138,27 @@ module ts {
return (<Identifier>(<ExpressionStatement>node).expression).text === "use strict";
}
export function getLeadingCommentsOfNode(node: Node, sourceFileOfNode: SourceFile) {
export function getLeadingCommentRangesOfNode(node: Node, sourceFileOfNode?: SourceFile) {
sourceFileOfNode = sourceFileOfNode || getSourceFileOfNode(node);
// If parameter/type parameter, the prev token trailing comments are part of this node too
if (node.kind === SyntaxKind.Parameter || node.kind === SyntaxKind.TypeParameter) {
// e.g. (/** blah */ a, /** blah */ b);
return concatenate(getTrailingComments(sourceFileOfNode.text, node.pos),
return concatenate(getTrailingCommentRanges(sourceFileOfNode.text, node.pos),
// e.g.: (
// /** blah */ a,
// /** blah */ b);
getLeadingComments(sourceFileOfNode.text, node.pos));
getLeadingCommentRanges(sourceFileOfNode.text, node.pos));
}
else {
return getLeadingComments(sourceFileOfNode.text, node.pos);
return getLeadingCommentRanges(sourceFileOfNode.text, node.pos);
}
}
export function getJsDocComments(node: Declaration, sourceFileOfNode: SourceFile) {
return filter(getLeadingCommentsOfNode(node, sourceFileOfNode), comment => isJsDocComment(comment));
return filter(getLeadingCommentRangesOfNode(node, sourceFileOfNode), comment => isJsDocComment(comment));
function isJsDocComment(comment: Comment) {
function isJsDocComment(comment: CommentRange) {
// True if the comment starts with '/**' but not if it is '/**/'
return sourceFileOfNode.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk &&
sourceFileOfNode.text.charCodeAt(comment.pos + 2) === CharacterCodes.asterisk &&

View file

@ -371,8 +371,8 @@ module ts {
// between the given position and the next line break are returned. The return value is an array containing a TextRange for each
// comment. Single-line comment ranges include the beginning '//' characters but not the ending line break. Multi-line comment
// ranges include the beginning '/* and ending '*/' characters. The return value is undefined if no comments were found.
function getCommentRanges(text: string, pos: number, trailing: boolean): Comment[] {
var result: Comment[];
function getCommentRanges(text: string, pos: number, trailing: boolean): CommentRange[] {
var result: CommentRange[];
var collecting = trailing || pos === 0;
while (true) {
var ch = text.charCodeAt(pos);
@ -440,11 +440,11 @@ module ts {
}
}
export function getLeadingComments(text: string, pos: number): Comment[] {
export function getLeadingCommentRanges(text: string, pos: number): CommentRange[] {
return getCommentRanges(text, pos, /*trailing*/ false);
}
export function getTrailingComments(text: string, pos: number): Comment[] {
export function getTrailingCommentRanges(text: string, pos: number): CommentRange[] {
return getCommentRanges(text, pos, /*trailing*/ true);
}

View file

@ -529,7 +529,7 @@ module ts {
filename: string;
}
export interface Comment extends TextRange {
export interface CommentRange extends TextRange {
hasTrailingNewLine?: boolean;
}

View file

@ -97,9 +97,7 @@ module ts {
private _children: Node[];
public getSourceFile(): SourceFile {
var node: Node = this;
while (node.kind !== SyntaxKind.SourceFile) node = node.parent;
return <SourceFile>node;
return getSourceFileOfNode(this);
}
public getStart(sourceFile?: SourceFile): number {
@ -225,19 +223,44 @@ module ts {
flags: SymbolFlags;
name: string;
declarations: Declaration[];
documentationComment: string;
constructor(flags: SymbolFlags, name: string) {
this.flags = flags;
this.name = name;
}
getFlags(): SymbolFlags {
return this.flags;
}
getName(): string {
return this.name;
}
getDeclarations(): Declaration[] {
return this.declarations;
}
//getDocumentationComment(): string {
// if (this.documentationComment === undefined) {
// var result = "";
// var declarations = this.getDeclarations();
// if (declarations) {
// for (var i = 0, n = declarations.length; i < n; i++) {
// var declaration = declarations[0];
// var commentRanges = getLeadingCommentRangesOfNode(declaration);
// }
// }
// this.documentationComment = result;
// }
// return this.documentationComment;
//}
}
class TypeObject implements Type {
@ -657,7 +680,8 @@ module ts {
constructor(public kind: string,
public kindModifiers: string,
public textSpan: TypeScript.TextSpan,
public displayParts: SymbolDisplayPart[]) {
public displayParts: SymbolDisplayPart[],
public documentation: SymbolDisplayPart[]) {
}
public toJSON() {
@ -2344,7 +2368,8 @@ module ts {
getSymbolKind(symbol),
getSymbolModifiers(symbol),
new TypeScript.TextSpan(node.getStart(), node.getWidth()),
totalParts);
totalParts,
[]/*convertDocumentation(symbol)*/);
}
function getTypeAtPosition(fileName: string, position: number): TypeInfo {
@ -4015,8 +4040,8 @@ module ts {
}
// Looks to be within the trivia. See if we can find the comment containing it.
if (!getContainingComment(getTrailingComments(fileContents, token.getFullStart()), matchPosition) &&
!getContainingComment(getLeadingComments(fileContents, token.getFullStart()), matchPosition)) {
if (!getContainingComment(getTrailingCommentRanges(fileContents, token.getFullStart()), matchPosition) &&
!getContainingComment(getLeadingCommentRanges(fileContents, token.getFullStart()), matchPosition)) {
continue;
}
@ -4103,7 +4128,7 @@ module ts {
return new RegExp(regExpString, "gim");
}
function getContainingComment(comments: Comment[], position: number): Comment {
function getContainingComment(comments: CommentRange[], position: number): CommentRange {
if (comments) {
for (var i = 0, n = comments.length; i < n; i++) {
var comment = comments[i];