Produce a map of named declarations instead of a flat list.

Produce a map of named declarations instead of a flat list.
This commit is contained in:
Cyrus Najmabadi
2015-04-03 16:21:24 -07:00
parent d56f2f62a8
commit 4cdc97094f
13 changed files with 97 additions and 92 deletions
+27 -42
View File
@@ -9,11 +9,10 @@ module ts.NavigateTo {
forEach(program.getSourceFiles(), sourceFile => {
cancellationToken.throwIfCancellationRequested();
let declarations = sourceFile.getNamedDeclarations();
for (let declaration of declarations) {
var name = getDeclarationName(declaration);
if (name !== undefined) {
let nameToDeclarations = sourceFile.getNamedDeclarations();
for (let name in nameToDeclarations) {
let declarations = getProperty(nameToDeclarations, name);
if (declarations) {
// First do a quick check to see if the name of the declaration matches the
// last portion of the (possibly) dotted name they're searching for.
let matches = patternMatcher.getMatchesForLastSegmentOfPattern(name);
@@ -22,24 +21,26 @@ module ts.NavigateTo {
continue;
}
// It was a match! If the pattern has dots in it, then also see if the
// declaration container matches as well.
if (patternMatcher.patternContainsDots) {
let containers = getContainers(declaration);
if (!containers) {
return undefined;
for (let declaration of declarations) {
// It was a match! If the pattern has dots in it, then also see if the
// declaration container matches as well.
if (patternMatcher.patternContainsDots) {
let containers = getContainers(declaration);
if (!containers) {
return undefined;
}
matches = patternMatcher.getMatches(containers, name);
if (!matches) {
continue;
}
}
matches = patternMatcher.getMatches(containers, name);
if (!matches) {
continue;
}
let fileName = sourceFile.fileName;
let matchKind = bestMatchKind(matches);
rawItems.push({ name, fileName, matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration });
}
let fileName = sourceFile.fileName;
let matchKind = bestMatchKind(matches);
rawItems.push({ name, fileName, matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration });
}
}
});
@@ -66,30 +67,14 @@ module ts.NavigateTo {
return true;
}
function getDeclarationName(declaration: Declaration): string {
let result = getTextOfIdentifierOrLiteral(declaration.name);
if (result !== undefined) {
return result;
}
if (declaration.name.kind === SyntaxKind.ComputedPropertyName) {
let expr = (<ComputedPropertyName>declaration.name).expression;
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
return (<PropertyAccessExpression>expr).name.text;
}
return getTextOfIdentifierOrLiteral(expr);
}
return undefined;
}
function getTextOfIdentifierOrLiteral(node: Node) {
if (node.kind === SyntaxKind.Identifier ||
node.kind === SyntaxKind.StringLiteral ||
node.kind === SyntaxKind.NumericLiteral) {
if (node) {
if (node.kind === SyntaxKind.Identifier ||
node.kind === SyntaxKind.StringLiteral ||
node.kind === SyntaxKind.NumericLiteral) {
return (<Identifier | LiteralExpression>node).text;
return (<Identifier | LiteralExpression>node).text;
}
}
return undefined;
+64 -20
View File
@@ -63,7 +63,8 @@ module ts {
/* @internal */ scriptSnapshot: IScriptSnapshot;
/* @internal */ nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
/* @internal */ getNamedDeclarations(): Map<Declaration[]>;
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
getPositionOfLineAndCharacter(line: number, character: number): number;
@@ -749,7 +750,7 @@ module ts {
public identifiers: Map<string>;
public nameTable: Map<string>;
private namedDeclarations: Declaration[];
private namedDeclarations: Map<Declaration[]>;
public update(newText: string, textChangeRange: TextChangeRange): SourceFile {
return updateSourceFile(this, newText, textChangeRange);
@@ -767,7 +768,7 @@ module ts {
return ts.getPositionOfLineAndCharacter(this, line, character);
}
public getNamedDeclarations() {
public getNamedDeclarations(): Map<Declaration[]> {
if (!this.namedDeclarations) {
this.namedDeclarations = this.computeNamedDeclarations();
}
@@ -775,12 +776,57 @@ module ts {
return this.namedDeclarations;
}
private computeNamedDeclarations() {
let namedDeclarations: Declaration[] = [];
private computeNamedDeclarations(): Map<Declaration[]> {
let result: Map<Declaration[]> = {};
forEachChild(this, visit);
return namedDeclarations;
return result;
function addDeclaration(declaration: Declaration) {
let name = getDeclarationName(declaration);
if (name) {
let declarations = getDeclarations(name);
declarations.push(declaration);
}
}
function getDeclarations(name: string) {
return getProperty(result, name) || (result[name] = []);
}
function getDeclarationName(declaration: Declaration) {
if (declaration.name) {
let result = getTextOfIdentifierOrLiteral(declaration.name);
if (result !== undefined) {
return result;
}
if (declaration.name.kind === SyntaxKind.ComputedPropertyName) {
let expr = (<ComputedPropertyName>declaration.name).expression;
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
return (<PropertyAccessExpression>expr).name.text;
}
return getTextOfIdentifierOrLiteral(expr);
}
}
return undefined;
}
function getTextOfIdentifierOrLiteral(node: Node) {
if (node) {
if (node.kind === SyntaxKind.Identifier ||
node.kind === SyntaxKind.StringLiteral ||
node.kind === SyntaxKind.NumericLiteral) {
return (<Identifier | LiteralExpression>node).text;
}
}
return undefined;
}
function visit(node: Node): void {
switch (node.kind) {
@@ -788,22 +834,22 @@ module ts {
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
let functionDeclaration = <FunctionLikeDeclaration>node;
let declarationName = getDeclarationName(functionDeclaration);
if (functionDeclaration.name && functionDeclaration.name.getFullWidth() > 0) {
let lastDeclaration = namedDeclarations.length > 0 ?
namedDeclarations[namedDeclarations.length - 1] :
undefined;
if (declarationName) {
let declarations = getDeclarations(declarationName);
let lastDeclaration = lastOrUndefined(declarations);
// Check whether this declaration belongs to an "overload group".
if (lastDeclaration && functionDeclaration.symbol === lastDeclaration.symbol) {
if (lastDeclaration && functionDeclaration.parent === lastDeclaration.parent && functionDeclaration.symbol === lastDeclaration.symbol) {
// Overwrite the last declaration if it was an overload
// and this one is an implementation.
if (functionDeclaration.body && !(<FunctionLikeDeclaration>lastDeclaration).body) {
namedDeclarations[namedDeclarations.length - 1] = functionDeclaration;
declarations[declarations.length - 1] = functionDeclaration;
}
}
else {
namedDeclarations.push(functionDeclaration);
declarations.push(functionDeclaration);
}
forEachChild(node, visit);
@@ -824,10 +870,8 @@ module ts {
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.TypeLiteral:
if ((<Declaration>node).name) {
namedDeclarations.push(<Declaration>node);
}
// fall through
addDeclaration(<Declaration>node);
// fall through
case SyntaxKind.Constructor:
case SyntaxKind.VariableStatement:
case SyntaxKind.VariableDeclarationList:
@@ -858,7 +902,7 @@ module ts {
case SyntaxKind.EnumMember:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
namedDeclarations.push(<Declaration>node);
addDeclaration(<Declaration>node);
break;
case SyntaxKind.ExportDeclaration:
@@ -875,7 +919,7 @@ module ts {
// Handle default import case e.g.:
// import d from "mod";
if (importClause.name) {
namedDeclarations.push(importClause);
addDeclaration(importClause);
}
// Handle named bindings in imports e.g.:
@@ -883,7 +927,7 @@ module ts {
// import {a, b as B} from "mod";
if (importClause.namedBindings) {
if (importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
namedDeclarations.push(<NamespaceImport>importClause.namedBindings);
addDeclaration(<NamespaceImport>importClause.namedBindings);
}
else {
forEach((<NamedImports>importClause.namedBindings).elements, visit);
@@ -1562,7 +1562,6 @@ declare module "typescript" {
getDocumentationComment(): SymbolDisplayPart[];
}
interface SourceFile {
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
getPositionOfLineAndCharacter(line: number, character: number): number;
@@ -5018,10 +5018,6 @@ declare module "typescript" {
interface SourceFile {␍
>SourceFile : SourceFile
getNamedDeclarations(): Declaration[];␍
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;␍
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
>pos : number
@@ -1593,7 +1593,6 @@ declare module "typescript" {
getDocumentationComment(): SymbolDisplayPart[];
}
interface SourceFile {
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
getPositionOfLineAndCharacter(line: number, character: number): number;
@@ -5164,10 +5164,6 @@ declare module "typescript" {
interface SourceFile {␍
>SourceFile : SourceFile
getNamedDeclarations(): Declaration[];␍
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;␍
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
>pos : number
@@ -5164,10 +5164,6 @@ declare module "typescript" {
interface SourceFile {
>SourceFile : SourceFile
getNamedDeclarations(): Declaration[];
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
>pos : number
@@ -1594,7 +1594,6 @@ declare module "typescript" {
getDocumentationComment(): SymbolDisplayPart[];
}
interface SourceFile {
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
getPositionOfLineAndCharacter(line: number, character: number): number;
@@ -5114,10 +5114,6 @@ declare module "typescript" {
interface SourceFile {
>SourceFile : SourceFile
getNamedDeclarations(): Declaration[];
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
>pos : number
@@ -1631,7 +1631,6 @@ declare module "typescript" {
getDocumentationComment(): SymbolDisplayPart[];
}
interface SourceFile {
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
getPositionOfLineAndCharacter(line: number, character: number): number;
@@ -5287,10 +5287,6 @@ declare module "typescript" {
interface SourceFile {
>SourceFile : SourceFile
getNamedDeclarations(): Declaration[];
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
>pos : number
@@ -8,5 +8,5 @@
////interface I {
//// interfaceMethodSignature(b: boolean): boolean;
////}
debugger;
verify.navigationItemsListCount(2, "interfaceMethodSignature", "exact");
@@ -4,7 +4,7 @@
////function overload1(b: boolean): boolean;
////function overload1(b: number): boolean;
////
////var heyImNotInterruptingAnythingAmI = '?';
////var x= '?';
////
////function overload1(f: typeof overload): boolean;
////function overload1(x: any, b = (function overload() { return false })): boolean {
@@ -15,7 +15,7 @@
////function overload2(b: boolean): boolean;
////function overload2(b: number): boolean;
////
////function iJustRuinEverything(x: any, b = (function overload() { return false })): boolean {
////function y(x: any, b = (function overload() { return false })): boolean {
//// throw overload;
////}
////
@@ -24,6 +24,6 @@
//// throw overload;
////}
verify.navigationItemsListCount(2, "overload1", "exact");
verify.navigationItemsListCount(2, "overload2", "exact");
verify.navigationItemsListCount(4, "overload", "prefix");
verify.navigationItemsListCount(1, "overload1", "exact");
verify.navigationItemsListCount(1, "overload2", "exact");
verify.navigationItemsListCount(2, "overload", "prefix");