Skip to content

Commit e212f1e

Browse files
committed
Cleaned up version
1. Add test 2. Change protocol. navto-all only happens when filename is undefined or missing. 3. Change location to earlier code path. This change was largely type-guided and resulted in some duplicated code, but I think it's less fault-prone.
1 parent 40e80ca commit e212f1e

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

src/server/protocol.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,7 @@ namespace ts.server.protocol {
27842784
/**
27852785
* Arguments for navto request message.
27862786
*/
2787-
export interface NavtoRequestArgs extends FileRequestArgs {
2787+
export interface NavtoRequestArgs {
27882788
/**
27892789
* Search term to navigate to from current location; term can
27902790
* be '.*' or an identifier prefix.
@@ -2794,6 +2794,10 @@ namespace ts.server.protocol {
27942794
* Optional limit on the number of items to return.
27952795
*/
27962796
maxResultCount?: number;
2797+
/**
2798+
* The file for the request (absolute pathname required).
2799+
*/
2800+
file?: string;
27972801
/**
27982802
* Optional flag to indicate we want results for just the current file
27992803
* or the entire project.
@@ -2809,7 +2813,7 @@ namespace ts.server.protocol {
28092813
* match the search term given in argument 'searchTerm'. The
28102814
* context for the search is given by the named file.
28112815
*/
2812-
export interface NavtoRequest extends FileRequest {
2816+
export interface NavtoRequest extends Request {
28132817
command: CommandTypes.Navto;
28142818
arguments: NavtoRequestArgs;
28152819
}

src/server/session.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -448,16 +448,9 @@ namespace ts.server {
448448
});
449449
}
450450
}
451-
else {
452-
projectService.forEachEnabledProject(project => {
453-
if (!addToSeen(seenProjects, project)) return;
454-
projectService.loadAncestorProjectTree(seenProjects);
455-
toDo = callbackProjectAndLocation(project, undefined as TLocation, projectService, toDo, seenProjects, cb);
456-
});
457-
}
458451

459452
while (toDo && toDo.length) {
460-
const next = toDo.pop()
453+
const next = toDo.pop();
461454
Debug.assertIsDefined(next);
462455
toDo = callbackProjectAndLocation(next.project, next.location, projectService, toDo, seenProjects, cb);
463456
}
@@ -1907,14 +1900,27 @@ namespace ts.server {
19071900

19081901
private getFullNavigateToItems(args: protocol.NavtoRequestArgs): readonly NavigateToItem[] {
19091902
const { currentFileOnly, searchValue, maxResultCount } = args;
1903+
if (!args.file) {
1904+
const items: NavigateToItem[] = [];
1905+
this.projectService.forEachEnabledProject(project => {
1906+
this.projectService.loadAncestorProjectTree();
1907+
for (const item of project.getLanguageService().getNavigateToItems(searchValue, maxResultCount, /*filename*/ undefined, /*excludeDts*/ project.isNonTsProject())) {
1908+
if (!contains(items, item, navigateToItemIsEqualTo)) {
1909+
items.push(item);
1910+
}
1911+
}
1912+
});
1913+
return items;
1914+
}
1915+
const fileArgs = args as protocol.FileRequestArgs;
19101916
if (currentFileOnly) {
1911-
const { file, project } = this.getFileAndProject(args);
1917+
const { file, project } = this.getFileAndProject(fileArgs);
19121918
return project.getLanguageService().getNavigateToItems(searchValue, maxResultCount, file);
19131919
}
19141920
else {
19151921
return combineProjectOutputWhileOpeningReferencedProjects<NavigateToItem>(
1916-
this.getProjects(args),
1917-
this.getDefaultProject(args),
1922+
this.getProjects(fileArgs),
1923+
this.getDefaultProject(fileArgs),
19181924
project =>
19191925
project.getLanguageService().getNavigateToItems(searchValue, maxResultCount, /*fileName*/ undefined, /*excludeDts*/ project.isNonTsProject()),
19201926
documentSpanLocation,

src/testRunner/unittests/tsserver/declarationFileMaps.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace ts.projectSystem {
6363
};
6464
const aDts: File = {
6565
path: "/a/bin/a.d.ts",
66-
// Need to mangle the sourceMappingURL part or it breaks the build
66+
// ${""} is needed to mangle the sourceMappingURL part or it breaks the build
6767
content: `export declare function fnA(): void;\nexport interface IfaceA {\n}\nexport declare const instanceA: IfaceA;\n//# source${""}MappingURL=a.d.ts.map`,
6868
};
6969

@@ -86,7 +86,7 @@ namespace ts.projectSystem {
8686
content: JSON.stringify(bDtsMapContent),
8787
};
8888
const bDts: File = {
89-
// Need to mangle the sourceMappingURL part or it breaks the build
89+
// ${""} is need to mangle the sourceMappingURL part so it doesn't break the build
9090
path: "/b/bin/b.d.ts",
9191
content: `export declare function fnB(): void;\n//# source${""}MappingURL=b.d.ts.map`,
9292
};
@@ -114,15 +114,17 @@ namespace ts.projectSystem {
114114
})
115115
};
116116

117-
function makeSampleProjects(addUserTsConfig?: boolean) {
117+
function makeSampleProjects(addUserTsConfig?: boolean, keepAllFiles?: boolean) {
118118
const host = createServerHost([aTs, aTsconfig, aDtsMap, aDts, bTsconfig, bTs, bDtsMap, bDts, ...(addUserTsConfig ? [userTsForConfigProject, userTsconfig] : [userTs]), dummyFile]);
119119
const session = createSession(host);
120120

121121
checkDeclarationFiles(aTs, session, [aDtsMap, aDts]);
122122
checkDeclarationFiles(bTs, session, [bDtsMap, bDts]);
123123

124124
// Testing what happens if we delete the original sources.
125-
host.deleteFile(bTs.path);
125+
if (!keepAllFiles) {
126+
host.deleteFile(bTs.path);
127+
}
126128

127129
openFilesForSession([userTs], session);
128130
const service = session.getProjectService();
@@ -322,6 +324,46 @@ namespace ts.projectSystem {
322324
verifyATsConfigOriginalProject(session);
323325
});
324326

327+
it("navigateToAll", () => {
328+
const session = makeSampleProjects(/*addUserTsConfig*/ true, /*keepAllFiles*/ true);
329+
const response = executeSessionRequest<protocol.NavtoRequest, protocol.NavtoResponse>(session, CommandNames.Navto, { file: undefined, searchValue: "fn" });
330+
assert.deepEqual<readonly protocol.NavtoItem[] | undefined>(response, [
331+
{
332+
...protocolFileSpanFromSubstring({
333+
file: bTs,
334+
text: "export function fnB() {}"
335+
}),
336+
name: "fnB",
337+
matchKind: "prefix",
338+
isCaseSensitive: true,
339+
kind: ScriptElementKind.functionElement,
340+
kindModifiers: "export",
341+
},
342+
{
343+
...protocolFileSpanFromSubstring({
344+
file: aTs,
345+
text: "export function fnA() {}"
346+
}),
347+
name: "fnA",
348+
matchKind: "prefix",
349+
isCaseSensitive: true,
350+
kind: ScriptElementKind.functionElement,
351+
kindModifiers: "export",
352+
},
353+
{
354+
...protocolFileSpanFromSubstring({
355+
file: userTs,
356+
text: "export function fnUser() { a.fnA(); b.fnB(); a.instanceA; }"
357+
}),
358+
name: "fnUser",
359+
matchKind: "prefix",
360+
isCaseSensitive: true,
361+
kind: ScriptElementKind.functionElement,
362+
kindModifiers: "export",
363+
}
364+
]);
365+
});
366+
325367
const referenceATs = (aTs: File): protocol.ReferencesResponseItem => makeReferenceItem({
326368
file: aTs,
327369
isDefinition: true,

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8319,7 +8319,7 @@ declare namespace ts.server.protocol {
83198319
/**
83208320
* Arguments for navto request message.
83218321
*/
8322-
interface NavtoRequestArgs extends FileRequestArgs {
8322+
interface NavtoRequestArgs {
83238323
/**
83248324
* Search term to navigate to from current location; term can
83258325
* be '.*' or an identifier prefix.
@@ -8329,6 +8329,10 @@ declare namespace ts.server.protocol {
83298329
* Optional limit on the number of items to return.
83308330
*/
83318331
maxResultCount?: number;
8332+
/**
8333+
* The file for the request (absolute pathname required).
8334+
*/
8335+
file?: string;
83328336
/**
83338337
* Optional flag to indicate we want results for just the current file
83348338
* or the entire project.
@@ -8342,7 +8346,7 @@ declare namespace ts.server.protocol {
83428346
* match the search term given in argument 'searchTerm'. The
83438347
* context for the search is given by the named file.
83448348
*/
8345-
interface NavtoRequest extends FileRequest {
8349+
interface NavtoRequest extends Request {
83468350
command: CommandTypes.Navto;
83478351
arguments: NavtoRequestArgs;
83488352
}

0 commit comments

Comments
 (0)