Merge pull request #677 from Microsoft/navigateTo

Add implementation for getNavigateToItems for the new compiler
This commit is contained in:
Mohamed Hegazy 2014-09-16 16:57:17 -07:00
commit 07e3c7f6e5
12 changed files with 200 additions and 35 deletions

View file

@ -5211,8 +5211,7 @@ module ts {
var otherKind = node.kind === SyntaxKind.GetAccessor ? SyntaxKind.SetAccessor : SyntaxKind.GetAccessor;
var otherAccessor = <AccessorDeclaration>getDeclarationOfKind(node.symbol, otherKind);
if (otherAccessor) {
var visibilityFlags = NodeFlags.Private | NodeFlags.Public;
if (((node.flags & visibilityFlags) !== (otherAccessor.flags & visibilityFlags))) {
if (((node.flags & NodeFlags.AccessibilityModifier) !== (otherAccessor.flags & NodeFlags.AccessibilityModifier))) {
error(node.name, Diagnostics.Getter_and_setter_accessors_do_not_agree_in_visibility);
}

View file

@ -1431,7 +1431,7 @@ module ts {
function emitParameterPropertyAssignments(node: ConstructorDeclaration) {
forEach(node.parameters, param => {
if (param.flags & (NodeFlags.Public | NodeFlags.Private)) {
if (param.flags & NodeFlags.AccessibilityModifier) {
writeLine();
emitStart(param);
emitStart(param.name);
@ -2630,7 +2630,7 @@ module ts {
function emitParameterProperties(constructorDeclaration: ConstructorDeclaration) {
if (constructorDeclaration) {
forEach(constructorDeclaration.parameters, param => {
if (param.flags & (NodeFlags.Public | NodeFlags.Private)) {
if (param.flags & NodeFlags.AccessibilityModifier) {
emitPropertyDeclaration(param);
}
});

View file

@ -3239,7 +3239,7 @@ module ts {
switch (modifierToken) {
case SyntaxKind.PublicKeyword:
if (flags & NodeFlags.Private || flags & NodeFlags.Public) {
if (flags & NodeFlags.AccessibilityModifier) {
grammarErrorAtPos(modifierStart, modifierLength, Diagnostics.Accessibility_modifier_already_seen);
}
else if (flags & NodeFlags.Static) {
@ -3252,7 +3252,7 @@ module ts {
break;
case SyntaxKind.PrivateKeyword:
if (flags & NodeFlags.Private || flags & NodeFlags.Public) {
if (flags & NodeFlags.AccessibilityModifier) {
grammarErrorAtPos(modifierStart, modifierLength, Diagnostics.Accessibility_modifier_already_seen);
}
else if (flags & NodeFlags.Static) {

View file

@ -239,7 +239,8 @@ module ts {
Synthetic = 0x00000100, // Synthetic node (for full fidelity)
DeclarationFile = 0x00000200, // Node is a .d.ts file
Modifier = Export | Ambient | Public | Private | Static
Modifier = Export | Ambient | Public | Private | Static,
AccessibilityModifier = Public | Private
}
export interface Node extends TextRange {

View file

@ -71,6 +71,7 @@ module ts {
getSourceUnit(): TypeScript.SourceUnitSyntax;
getSyntaxTree(): TypeScript.SyntaxTree;
getScriptSnapshot(): TypeScript.IScriptSnapshot;
getNamedDeclarations(): Declaration[];
update(scriptSnapshot: TypeScript.IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TypeScript.TextChangeRange): SourceFile;
}
@ -327,6 +328,7 @@ module ts {
private syntaxTree: TypeScript.SyntaxTree;
private scriptSnapshot: TypeScript.IScriptSnapshot;
private namedDeclarations: Declaration[];
public getSourceUnit(): TypeScript.SourceUnitSyntax {
// If we don't have a script, create one from our parse tree.
@ -341,6 +343,59 @@ module ts {
return this.getSyntaxTree().lineMap();
}
public getNamedDeclarations() {
if (!this.namedDeclarations) {
var sourceFile = this;
var namedDeclarations: Declaration[] = [];
var isExternalModule = ts.isExternalModule(sourceFile);
forEachChild(sourceFile, function visit(node: Node): boolean {
switch (node.kind) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.ImportDeclaration:
case SyntaxKind.Method:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.TypeLiteral:
if ((<Declaration>node).name) {
namedDeclarations.push(<Declaration>node);
}
forEachChild(node, visit);
break;
case SyntaxKind.VariableStatement:
case SyntaxKind.ModuleBlock:
case SyntaxKind.FunctionBlock:
forEachChild(node, visit);
break;
case SyntaxKind.Parameter:
if (!(node.flags & NodeFlags.AccessibilityModifier)) {
// Only consider properties defined as constructor parameters
break;
}
case SyntaxKind.VariableDeclaration:
case SyntaxKind.EnumMember:
case SyntaxKind.Property:
namedDeclarations.push(<Declaration>node);
break;
}
// do not go any deeper
return undefined;
});
this.namedDeclarations = namedDeclarations;
}
return this.namedDeclarations;
}
public getSyntaxTree(): TypeScript.SyntaxTree {
if (!this.syntaxTree) {
var start = new Date().getTime();
@ -850,11 +905,11 @@ module ts {
static staticModifier = "static";
}
export class MatchKind {
static none: string = null;
static exact = "exact";
static subString = "substring";
static prefix = "prefix";
enum MatchKind {
none = 0,
exact = 1,
substring = 2,
prefix = 3
}
interface IncrementalParse {
@ -2035,6 +2090,29 @@ module ts {
return ScriptElementKind.unknown;
}
function getNodeKind(node: Node): string {
switch (node.kind) {
case SyntaxKind.ModuleDeclaration: return ScriptElementKind.moduleElement;
case SyntaxKind.ClassDeclaration: return ScriptElementKind.classElement;
case SyntaxKind.InterfaceDeclaration: return ScriptElementKind.interfaceElement;
case SyntaxKind.EnumDeclaration: return ScriptElementKind.enumElement;
case SyntaxKind.VariableDeclaration: return ScriptElementKind.variableElement;
case SyntaxKind.FunctionDeclaration: return ScriptElementKind.functionElement;
case SyntaxKind.GetAccessor: return ScriptElementKind.memberGetAccessorElement;
case SyntaxKind.SetAccessor: return ScriptElementKind.memberSetAccessorElement;
case SyntaxKind.Method: return ScriptElementKind.memberFunctionElement;
case SyntaxKind.Property: return ScriptElementKind.memberVariableElement;
case SyntaxKind.IndexSignature: return ScriptElementKind.indexSignatureElement;
case SyntaxKind.ConstructSignature: return ScriptElementKind.constructSignatureElement;
case SyntaxKind.CallSignature: return ScriptElementKind.callSignatureElement;
case SyntaxKind.Constructor: return ScriptElementKind.constructorImplementationElement;
case SyntaxKind.TypeParameter: return ScriptElementKind.typeParameterElement;
case SyntaxKind.EnumMember: return ScriptElementKind.variableElement;
case SyntaxKind.Parameter: return (node.flags & NodeFlags.AccessibilityModifier) ? ScriptElementKind.memberVariableElement : ScriptElementKind.parameterElement;
return ScriptElementKind.unknown;
}
}
function getNodeModifiers(node: Node): string {
var flags = node.flags;
var result: string[] = [];
@ -2186,6 +2264,7 @@ module ts {
return result;
}
/// References and Occurances
function getOccurrencesAtPosition(filename: string, position: number): ReferenceEntry[] {
synchronizeHostData();
@ -2732,9 +2811,10 @@ module ts {
return false;
}
/// Search within node "container" for references for a search value, where the search value is defined as a
/// tuple of(searchSymbol, searchText, searchLocation, and searchMeaning).
/// searchLocation: a node where the search value
/** Search within node "container" for references for a search value, where the search value is defined as a
* tuple of(searchSymbol, searchText, searchLocation, and searchMeaning).
* searchLocation: a node where the search value
*/
function getReferencesInNode(container: Node, searchSymbol: Symbol, searchText: string, searchLocation: Node, searchMeaning: SearchMeaning, result: ReferenceEntry[]): void {
var sourceFile = container.getSourceFile();
@ -3100,12 +3180,13 @@ module ts {
}
}
/// Given an initial searchMeaning, extracted from a location, widen the search scope based on the declarations
/// of the corresponding symbol. e.g. if we are searching for "Foo" in value position, but "Foo" references a class
/// then we need to widen the search to include type positions as well.
/// On the contrary, if we are searching for "Bar" in type position and we trace bar to an interface, and an uninstantiated
/// module, we want to keep the search limited to only types, as the two declarations (interface and uninstantiated module)
/// do not intersect in any of the three spaces.
/** Given an initial searchMeaning, extracted from a location, widen the search scope based on the declarations
* of the corresponding symbol. e.g. if we are searching for "Foo" in value position, but "Foo" references a class
* then we need to widen the search to include type positions as well.
* On the contrary, if we are searching for "Bar" in type position and we trace bar to an interface, and an uninstantiated
* module, we want to keep the search limited to only types, as the two declarations (interface and uninstantiated module)
* do not intersect in any of the three spaces.
*/
function getIntersectingMeaningFromDeclarations(meaning: SearchMeaning, declarations: Declaration[]): SearchMeaning {
if (declarations) {
do {
@ -3142,7 +3223,7 @@ module ts {
return new ReferenceEntry(node.getSourceFile().filename, TypeScript.TextSpan.fromBounds(start, end), isWriteAccess(node));
}
/// A node is considered a writeAccess iff it is a name of a declaration or a target of an assignment
/** A node is considered a writeAccess iff it is a name of a declaration or a target of an assignment */
function isWriteAccess(node: Node): boolean {
if (node.kind === SyntaxKind.Identifier && isDeclarationOrFunctionExpressionOrCatchVariableName(node)) {
return true;
@ -3162,6 +3243,90 @@ module ts {
return false;
}
/// NavigateTo
function getNavigateToItems(searchValue: string): NavigateToItem[] {
synchronizeHostData();
// Split search value in terms array
var terms = searchValue.split(" ");
// default NavigateTo approach: if search term contains only lower-case chars - use case-insensitive search, otherwise switch to case-sensitive version
var searchTerms = map(terms, t => ({ caseSensitive: hasAnyUpperCaseCharacter(t), term: t }));
var items: NavigateToItem[] = [];
// Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[]
forEach(program.getSourceFiles(), sourceFile => {
cancellationToken.throwIfCancellationRequested();
var filename = sourceFile.filename;
var declarations = sourceFile.getNamedDeclarations();
for (var i = 0, n = declarations.length; i < n; i++) {
var declaration = declarations[i];
var name = declaration.name.text;
var matchKind = getMatchKind(searchTerms, name);
if (matchKind !== MatchKind.none) {
var container = <Declaration>getContainerNode(declaration);
items.push({
name: name,
kind: getNodeKind(declaration),
kindModifiers: getNodeModifiers(declaration),
matchKind: MatchKind[matchKind],
fileName: filename,
textSpan: TypeScript.TextSpan.fromBounds(declaration.getStart(), declaration.getEnd()),
containerName: container.name ? container.name.text : "",
containerKind: container.name ? getNodeKind(container) : ""
});
}
}
});
return items;
function hasAnyUpperCaseCharacter(s: string): boolean {
for (var i = 0, n = s.length; i < n; i++) {
var c = s.charCodeAt(i);
if ((CharacterCodes.A <= c && c <= CharacterCodes.Z) ||
(c >= CharacterCodes.maxAsciiCharacter && s.charAt(i).toLocaleLowerCase() !== s.charAt(i))) {
return true;
}
}
return false;
}
function getMatchKind(searchTerms: { caseSensitive: boolean; term: string }[], name: string): MatchKind {
var matchKind = MatchKind.none;
if (name) {
for (var j = 0, n = searchTerms.length; j < n; j++) {
var searchTerm = searchTerms[j];
var nameToSearch = searchTerm.caseSensitive ? name : name.toLocaleLowerCase();
// in case of case-insensitive search searchTerm.term will already be lower-cased
var index = nameToSearch.indexOf(searchTerm.term);
if (index < 0) {
// Didn't match.
return MatchKind.none;
}
var termKind = MatchKind.substring;
if (index === 0) {
// here we know that match occur at the beginning of the string.
// if search term and declName has the same length - we have an exact match, otherwise declName have longer length and this will be prefix match
termKind = name.length === searchTerm.term.length ? MatchKind.exact : MatchKind.prefix;
}
// Update our match kind if we don't have one, or if this match is better.
if (matchKind === MatchKind.none || termKind < matchKind) {
matchKind = termKind;
}
}
}
return matchKind;
}
}
/// Syntactic features
function getSyntaxTree(filename: string): TypeScript.SyntaxTree {
filename = TypeScript.switchToForwardSlashes(filename);
@ -3692,7 +3857,7 @@ module ts {
getImplementorsAtPosition: (filename, position) => [],
getNameOrDottedNameSpan: getNameOrDottedNameSpan,
getBreakpointStatementAtPosition: getBreakpointStatementAtPosition,
getNavigateToItems: (searchValue) => [],
getNavigateToItems: getNavigateToItems,
getRenameInfo: (fileName, position): RenameInfo => RenameInfo.CreateError(getLocaleSpecificMessage(Diagnostics.You_cannot_rename_this_element.key)),
getNavigationBarItems: getNavigationBarItems,
getOutliningSpans: getOutliningSpans,

View file

@ -6,17 +6,17 @@
//// // Class
//// {| "itemName": "Point", "kind": "class", "parentName": "Shapes" |}export class Point {
//// // Instance member
//// {| "itemName": "origin", "kind": "property", "parentName": "Shapes.Point", "matchKind": "exact"|}private origin = 0.0;
//// {| "itemName": "origin", "kind": "property", "parentName": "Point", "matchKind": "exact"|}private origin = 0.0;
////
//// {| "itemName": "distFromZero", "kind": "property", "parentName": "Shapes.Point", "matchKind": "exact"|}private distFromZero = 0.0;
//// {| "itemName": "distFromZero", "kind": "property", "parentName": "Point", "matchKind": "exact"|}private distFromZero = 0.0;
////
//// // Getter
//// {| "itemName": "distance", "kind": "getter", "parentName": "Shapes.Point", "matchKind": "exact" |}get distance(): number { return 0; }
//// {| "itemName": "distance", "kind": "getter", "parentName": "Point", "matchKind": "exact" |}get distance(): number { return 0; }
//// }
////}
////
////// Local variables
////{| "itemName": "point", "kind": "var", "parentName": "", "matchKind": "exact" |}var point = new Shapes.Point();
////{| "itemName": "point", "kind": "var", "parentName": "", "matchKind": "exact"|}var point = new Shapes.Point();
//// Testing for exact matching of navigationItems

View file

@ -20,7 +20,7 @@
goTo.marker("file1");
verify.navigationItemsListCount(2, "point", "exact");
verify.navigationItemsListCount(3, "distance", "prefix");
verify.navigationItemsListCount(5, "distance", "prefix");
verify.navigationItemsListCount(1, "origin", "substring");
verify.navigationItemsListCount(0, "square", "exact");

View file

@ -6,12 +6,12 @@
//// // Class
//// {| "itemName": "Point", "kind": "class", "parentName": "Shapes" |}export class Point {
//// // Instance member
//// {| "itemName": "originality", "kind": "property", "parentName": "Shapes.Point", "matchKind": "prefix"|}private originality = 0.0;
//// {| "itemName": "originality", "kind": "property", "parentName": "Point", "matchKind": "prefix"|}private originality = 0.0;
////
//// {| "itemName": "distanceFromOrig", "kind": "property", "parentName": "Shapes.Point", "matchKind": "prefix"|}private distanceFromOrig = 0.0;
//// {| "itemName": "distanceFromOrig", "kind": "property", "parentName": "Point", "matchKind": "prefix"|}private distanceFromOrig = 0.0;
////
//// // Getter
//// {| "itemName": "distanceFarFarAway", "kind": "getter", "parentName": "Shapes.Point", "matchKind": "prefix" |}get distanceFarFarAway(): number { return 0; }
//// {| "itemName": "distanceFarFarAway", "kind": "getter", "parentName": "Point", "matchKind": "prefix" |}get distanceFarFarAway(): number { return 0; }
//// }
////}
////

View file

@ -25,7 +25,7 @@ var notFoundSearchValue = "mPointThatIJustInitiated wrongKeyWord";
goTo.marker("file1");
verify.navigationItemsListCount(3, "origin", "prefix");
verify.navigationItemsListCount(2, "distance", "prefix");
verify.navigationItemsListCount(3, "distance", "prefix");
verify.navigationItemsListCount(0, notFoundSearchValue, "exact");
verify.navigationItemsListCount(0, notFoundSearchValue, "prefix");

View file

@ -6,10 +6,10 @@
//// // Class
//// {| "itemName": "Point", "kind": "class", "parentName": "Shapes", "matchKind": "substring" |}export class Point {
//// // Instance member
//// {| "itemName": "originPointAttheHorizon", "kind": "property", "parentName": "Shapes.Point", "matchKind": "substring"|}private originPointAttheHorizon = 0.0;
//// {| "itemName": "originPointAttheHorizon", "kind": "property", "parentName": "Point", "matchKind": "substring"|}private originPointAttheHorizon = 0.0;
////
//// // Getter
//// {| "itemName": "distanceFromOrigin", "kind": "getter", "parentName": "Shapes.Point", "matchKind": "substring" |}get distanceFromOrigin(): number { return 0; }
//// {| "itemName": "distanceFromOrigin", "kind": "getter", "parentName": "Point", "matchKind": "substring" |}get distanceFromOrigin(): number { return 0; }
////
//// }
////}

View file

@ -14,7 +14,7 @@
////
////var myPointThatIJustInitiated = new Shapes.Point();
////interface IDistance{
//// var INITIATED123;
//// INITIATED123;
//// public horizon(): void;
////}