Skip to content

Commit e9178a5

Browse files
authored
Merge pull request #10885 from Microsoft/navtosinglefile
Implement NavigateTo for single files, instead of the project.
2 parents 42515c7 + 84caec3 commit e9178a5

File tree

9 files changed

+45
-19
lines changed

9 files changed

+45
-19
lines changed

src/harness/fourslash.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,11 +2038,12 @@ namespace FourSlash {
20382038
}
20392039

20402040
/*
2041-
Check number of navigationItems which match both searchValue and matchKind.
2041+
Check number of navigationItems which match both searchValue and matchKind,
2042+
if a filename is passed in, limit the results to that file.
20422043
Report an error if expected value and actual value do not match.
20432044
*/
2044-
public verifyNavigationItemsCount(expected: number, searchValue: string, matchKind?: string) {
2045-
const items = this.languageService.getNavigateToItems(searchValue);
2045+
public verifyNavigationItemsCount(expected: number, searchValue: string, matchKind?: string, fileName?: string) {
2046+
const items = this.languageService.getNavigateToItems(searchValue, /*maxResultCount*/ undefined, fileName);
20462047
let actual = 0;
20472048
let item: ts.NavigateToItem;
20482049

@@ -3170,8 +3171,8 @@ namespace FourSlashInterface {
31703171
this.state.verifyNavigationBar(json);
31713172
}
31723173

3173-
public navigationItemsListCount(count: number, searchValue: string, matchKind?: string) {
3174-
this.state.verifyNavigationItemsCount(count, searchValue, matchKind);
3174+
public navigationItemsListCount(count: number, searchValue: string, matchKind?: string, fileName?: string) {
3175+
this.state.verifyNavigationItemsCount(count, searchValue, matchKind, fileName);
31753176
}
31763177

31773178
public navigationItemsListContains(

src/server/protocol.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,11 @@ declare namespace ts.server.protocol {
11411141
* Optional limit on the number of items to return.
11421142
*/
11431143
maxResultCount?: number;
1144+
/**
1145+
* Optional flag to indicate we want results for just the current file
1146+
* or the entire project.
1147+
*/
1148+
currentFileOnly?: boolean;
11441149
}
11451150

11461151
/**

src/server/session.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ namespace ts.server {
940940
return this.decorateNavigationBarItem(project, fileName, items, compilerService.host.getLineIndex(fileName));
941941
}
942942

943-
private getNavigateToItems(searchValue: string, fileName: string, maxResultCount?: number): protocol.NavtoItem[] {
943+
private getNavigateToItems(searchValue: string, fileName: string, maxResultCount?: number, currentFileOnly?: boolean): protocol.NavtoItem[] {
944944
const file = ts.normalizePath(fileName);
945945
const info = this.projectService.getScriptInfo(file);
946946
const projects = this.projectService.findReferencingProjects(info);
@@ -953,7 +953,7 @@ namespace ts.server {
953953
projectsWithLanguageServiceEnabeld,
954954
(project: Project) => {
955955
const compilerService = project.compilerService;
956-
const navItems = compilerService.languageService.getNavigateToItems(searchValue, maxResultCount);
956+
const navItems = compilerService.languageService.getNavigateToItems(searchValue, maxResultCount, currentFileOnly ? fileName : undefined);
957957
if (!navItems) {
958958
return [];
959959
}
@@ -1188,7 +1188,7 @@ namespace ts.server {
11881188
},
11891189
[CommandNames.Navto]: (request: protocol.Request) => {
11901190
const navtoArgs = <protocol.NavtoRequestArgs>request.arguments;
1191-
return { response: this.getNavigateToItems(navtoArgs.searchValue, navtoArgs.file, navtoArgs.maxResultCount), responseRequired: true };
1191+
return { response: this.getNavigateToItems(navtoArgs.searchValue, navtoArgs.file, navtoArgs.maxResultCount, navtoArgs.currentFileOnly), responseRequired: true };
11921192
},
11931193
[CommandNames.Brace]: (request: protocol.Request) => {
11941194
const braceArguments = <protocol.FileLocationRequestArgs>request.arguments;

src/services/navigateTo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
namespace ts.NavigateTo {
33
type RawNavigateToItem = { name: string; fileName: string; matchKind: PatternMatchKind; isCaseSensitive: boolean; declaration: Declaration };
44

5-
export function getNavigateToItems(program: Program, checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number): NavigateToItem[] {
5+
export function getNavigateToItems(sourceFiles: SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number): NavigateToItem[] {
66
const patternMatcher = createPatternMatcher(searchValue);
77
let rawItems: RawNavigateToItem[] = [];
88

99
// This means "compare in a case insensitive manner."
1010
const baseSensitivity: Intl.CollatorOptions = { sensitivity: "base" };
1111

1212
// Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[]
13-
forEach(program.getSourceFiles(), sourceFile => {
13+
forEach(sourceFiles, sourceFile => {
1414
cancellationToken.throwIfCancellationRequested();
1515

1616
const nameToDeclarations = sourceFile.getNamedDeclarations();

src/services/services.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,10 +1347,11 @@ namespace ts {
13471347
}
13481348

13491349
/// NavigateTo
1350-
function getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[] {
1350+
function getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): NavigateToItem[] {
13511351
synchronizeHostData();
1352-
const checker = getProgram().getTypeChecker();
1353-
return ts.NavigateTo.getNavigateToItems(program, checker, cancellationToken, searchValue, maxResultCount);
1352+
1353+
const sourceFiles = fileName ? [getValidSourceFile(fileName)] : program.getSourceFiles();
1354+
return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount);
13541355
}
13551356

13561357
function getEmitOutput(fileName: string): EmitOutput {

src/services/shims.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ namespace ts {
209209
* Returns a JSON-encoded value of the type:
210210
* { name: string; kind: string; kindModifiers: string; containerName: string; containerKind: string; matchKind: string; fileName: string; textSpan: { start: number; length: number}; } [] = [];
211211
*/
212-
getNavigateToItems(searchValue: string, maxResultCount?: number): string;
212+
getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): string;
213213

214214
/**
215215
* Returns a JSON-encoded value of the type:
@@ -930,10 +930,10 @@ namespace ts {
930930
/// NAVIGATE TO
931931

932932
/** Return a list of symbols that are interesting to navigate to */
933-
public getNavigateToItems(searchValue: string, maxResultCount?: number): string {
933+
public getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): string {
934934
return this.forwardJSONCall(
935-
`getNavigateToItems('${searchValue}', ${maxResultCount})`,
936-
() => this.languageService.getNavigateToItems(searchValue, maxResultCount)
935+
`getNavigateToItems('${searchValue}', ${maxResultCount}, ${fileName})`,
936+
() => this.languageService.getNavigateToItems(searchValue, maxResultCount, fileName)
937937
);
938938
}
939939

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ namespace ts {
217217
/** @deprecated */
218218
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
219219

220-
getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
220+
getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): NavigateToItem[];
221221
getNavigationBarItems(fileName: string): NavigationBarItem[];
222222

223223
getOutliningSpans(fileName: string): OutliningSpan[];

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ declare namespace FourSlashInterface {
208208
noDocCommentTemplate(): void;
209209

210210
navigationBar(json: any): void;
211-
navigationItemsListCount(count: number, searchValue: string, matchKind?: string): void;
211+
navigationItemsListCount(count: number, searchValue: string, matchKind?: string, fileName?: string): void;
212212
navigationItemsListContains(name: string, kind: string, searchValue: string, matchKind: string, fileName?: string, parentName?: string): void;
213213
occurrencesAtPositionContains(range: Range, isWriteAccess?: boolean): void;
214214
occurrencesAtPositionCount(expectedCount: number): void;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: file1.ts
4+
/////*1*/class Greeter {
5+
//// public hello(name: string) { }
6+
////}
7+
////var x = new Greeter();
8+
// @Filename: file2.ts
9+
/////*2*/class MyGreeter {
10+
//// public hello(name: string) { }
11+
////}
12+
////class MyOtherGreeter {
13+
//// public hello(name: string) { }
14+
////}
15+
16+
verify.navigationItemsListCount(3, "hello");
17+
verify.navigationItemsListCount(1, "hello", undefined, test.marker("1").fileName);
18+
verify.navigationItemsListContains("hello", "method", "hello", "exact", test.marker("1").fileName);
19+
verify.navigationItemsListCount(2, "hello", undefined, test.marker("2").fileName);

0 commit comments

Comments
 (0)