Add support for limiting the number of NavigateTo results that you get back.

This commit is contained in:
Cyrus Najmabadi 2015-02-19 15:13:03 -08:00
parent d2c992c2de
commit 2b64e50489
10 changed files with 47 additions and 33 deletions

View file

@ -900,7 +900,7 @@ module ts {
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];
getOutliningSpans(fileName: string): OutliningSpan[]; getOutliningSpans(fileName: string): OutliningSpan[];
@ -1380,8 +1380,8 @@ module ts {
enum MatchKind { enum MatchKind {
none = 0, none = 0,
exact = 1, exact = 1,
substring = 2, prefix = 2,
prefix = 3 substring = 3,
} }
/// Language Service /// Language Service
@ -4662,7 +4662,7 @@ module ts {
} }
/// NavigateTo /// NavigateTo
function getNavigateToItems(searchValue: string): NavigateToItem[] { function getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[] {
synchronizeHostData(); synchronizeHostData();
// Split search value in terms array // Split search value in terms array
@ -4671,7 +4671,7 @@ module ts {
// default NavigateTo approach: if search term contains only lower-case chars - use case-insensitive search, otherwise switch to case-sensitive version // 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 searchTerms = map(terms, t => ({ caseSensitive: hasAnyUpperCaseCharacter(t), term: t }));
var items: NavigateToItem[] = []; var rawItems: { name: string; fileName: string; matchKind: MatchKind; declaration: Declaration }[] = [];
// Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[] // Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[]
forEach(program.getSourceFiles(), sourceFile => { forEach(program.getSourceFiles(), sourceFile => {
@ -4685,22 +4685,32 @@ module ts {
var name = (<Identifier>declaration.name).text; var name = (<Identifier>declaration.name).text;
var matchKind = getMatchKind(searchTerms, name); var matchKind = getMatchKind(searchTerms, name);
if (matchKind !== MatchKind.none) { if (matchKind !== MatchKind.none) {
var container = <Declaration>getContainerNode(declaration); rawItems.push({ name, fileName, matchKind, declaration });
items.push({
name: name,
kind: getNodeKind(declaration),
kindModifiers: getNodeModifiers(declaration),
matchKind: MatchKind[matchKind],
fileName: fileName,
textSpan: createTextSpanFromBounds(declaration.getStart(), declaration.getEnd()),
// TODO(jfreeman): What should be the containerName when the container has a computed name?
containerName: container && container.name ? (<Identifier>container.name).text : "",
containerKind: container && container.name ? getNodeKind(container) : ""
});
} }
} }
}); });
rawItems.sort((i1, i2) => i1.matchKind - i2.matchKind);
if (maxResultCount !== undefined) {
rawItems = rawItems.slice(0, maxResultCount);
}
var items = map(rawItems, i => {
var declaration = i.declaration;
var container = <Declaration>getContainerNode(declaration);
return <NavigateToItem>{
name: i.name,
kind: getNodeKind(declaration),
kindModifiers: getNodeModifiers(declaration),
matchKind: MatchKind[i.matchKind],
fileName: i.fileName,
textSpan: createTextSpanFromBounds(declaration.getStart(), declaration.getEnd()),
// TODO(jfreeman): What should be the containerName when the container has a computed name?
containerName: container && container.name ? (<Identifier>container.name).text : "",
containerKind: container && container.name ? getNodeKind(container) : ""
};
});
return items; return items;
function hasAnyUpperCaseCharacter(s: string): boolean { function hasAnyUpperCaseCharacter(s: string): boolean {

View file

@ -138,7 +138,7 @@ module ts {
* Returns a JSON-encoded value of the type: * Returns a JSON-encoded value of the type:
* { name: string; kind: string; kindModifiers: string; containerName: string; containerKind: string; matchKind: string; fileName: string; textSpan: { start: number; length: number}; } [] = []; * { name: string; kind: string; kindModifiers: string; containerName: string; containerKind: string; matchKind: string; fileName: string; textSpan: { start: number; length: number}; } [] = [];
*/ */
getNavigateToItems(searchValue: string): string; getNavigateToItems(searchValue: string, maxResultCount?: number): string;
/** /**
* Returns a JSON-encoded value of the type: * Returns a JSON-encoded value of the type:
@ -628,11 +628,11 @@ module ts {
/// NAVIGATE TO /// NAVIGATE TO
/** Return a list of symbols that are interesting to navigate to */ /** Return a list of symbols that are interesting to navigate to */
public getNavigateToItems(searchValue: string): string { public getNavigateToItems(searchValue: string, maxResultCount?: number): string {
return this.forwardJSONCall( return this.forwardJSONCall(
"getNavigateToItems('" + searchValue + "')", "getNavigateToItems('" + searchValue + "', " + maxResultCount+ ")",
() => { () => {
var items = this.languageService.getNavigateToItems(searchValue); var items = this.languageService.getNavigateToItems(searchValue, maxResultCount);
return items; return items;
}); });
} }

View file

@ -1541,7 +1541,7 @@ declare module "typescript" {
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[]; getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];
getOutliningSpans(fileName: string): OutliningSpan[]; getOutliningSpans(fileName: string): OutliningSpan[];
getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[]; getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[];

View file

@ -4986,9 +4986,10 @@ declare module "typescript" {
>position : number >position : number
>ReferenceEntry : ReferenceEntry >ReferenceEntry : ReferenceEntry
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
>getNavigateToItems : (searchValue: string) => NavigateToItem[] >getNavigateToItems : (searchValue: string, maxResultCount?: number) => NavigateToItem[]
>searchValue : string >searchValue : string
>maxResultCount : number
>NavigateToItem : NavigateToItem >NavigateToItem : NavigateToItem
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];

View file

@ -1572,7 +1572,7 @@ declare module "typescript" {
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[]; getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];
getOutliningSpans(fileName: string): OutliningSpan[]; getOutliningSpans(fileName: string): OutliningSpan[];
getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[]; getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[];

View file

@ -5130,9 +5130,10 @@ declare module "typescript" {
>position : number >position : number
>ReferenceEntry : ReferenceEntry >ReferenceEntry : ReferenceEntry
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
>getNavigateToItems : (searchValue: string) => NavigateToItem[] >getNavigateToItems : (searchValue: string, maxResultCount?: number) => NavigateToItem[]
>searchValue : string >searchValue : string
>maxResultCount : number
>NavigateToItem : NavigateToItem >NavigateToItem : NavigateToItem
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];

View file

@ -1573,7 +1573,7 @@ declare module "typescript" {
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[]; getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];
getOutliningSpans(fileName: string): OutliningSpan[]; getOutliningSpans(fileName: string): OutliningSpan[];
getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[]; getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[];

View file

@ -5082,9 +5082,10 @@ declare module "typescript" {
>position : number >position : number
>ReferenceEntry : ReferenceEntry >ReferenceEntry : ReferenceEntry
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
>getNavigateToItems : (searchValue: string) => NavigateToItem[] >getNavigateToItems : (searchValue: string, maxResultCount?: number) => NavigateToItem[]
>searchValue : string >searchValue : string
>maxResultCount : number
>NavigateToItem : NavigateToItem >NavigateToItem : NavigateToItem
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];

View file

@ -1610,7 +1610,7 @@ declare module "typescript" {
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[]; getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[]; getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];
getOutliningSpans(fileName: string): OutliningSpan[]; getOutliningSpans(fileName: string): OutliningSpan[];
getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[]; getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[];

View file

@ -5255,9 +5255,10 @@ declare module "typescript" {
>position : number >position : number
>ReferenceEntry : ReferenceEntry >ReferenceEntry : ReferenceEntry
getNavigateToItems(searchValue: string): NavigateToItem[]; getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
>getNavigateToItems : (searchValue: string) => NavigateToItem[] >getNavigateToItems : (searchValue: string, maxResultCount?: number) => NavigateToItem[]
>searchValue : string >searchValue : string
>maxResultCount : number
>NavigateToItem : NavigateToItem >NavigateToItem : NavigateToItem
getNavigationBarItems(fileName: string): NavigationBarItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[];