Skip to content

Commit 5035df6

Browse files
author
Andy
committed
Merge pull request microsoft#8756 from Microsoft/no_navigate_to_import
Don't include imports in navigateTo if the imported declaration is also in the items and has the same name
2 parents 2aa2738 + 5e799b8 commit 5035df6

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/services/navigateTo.ts

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

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

@@ -49,6 +49,19 @@ namespace ts.NavigateTo {
4949
}
5050
});
5151

52+
// Remove imports when the imported declaration is already in the list and has the same name.
53+
rawItems = filter(rawItems, item => {
54+
const decl = item.declaration;
55+
if (decl.kind === SyntaxKind.ImportClause || decl.kind === SyntaxKind.ImportSpecifier || decl.kind === SyntaxKind.ImportEqualsDeclaration) {
56+
const importer = checker.getSymbolAtLocation(decl.name);
57+
const imported = checker.getAliasedSymbol(importer);
58+
return importer.name !== imported.name;
59+
}
60+
else {
61+
return true;
62+
}
63+
});
64+
5265
rawItems.sort(compareNavigateToItems);
5366
if (maxResultCount !== undefined) {
5467
rawItems = rawItems.slice(0, maxResultCount);

src/services/services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6606,8 +6606,8 @@ namespace ts {
66066606
/// NavigateTo
66076607
function getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[] {
66086608
synchronizeHostData();
6609-
6610-
return ts.NavigateTo.getNavigateToItems(program, cancellationToken, searchValue, maxResultCount);
6609+
const checker = getProgram().getTypeChecker();
6610+
return ts.NavigateTo.getNavigateToItems(program, checker, cancellationToken, searchValue, maxResultCount);
66116611
}
66126612

66136613
function getEmitOutput(fileName: string): EmitOutput {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: library.ts
4+
////export function foo() {}
5+
////export function bar() {}
6+
// @Filename: user.ts
7+
////import {foo, bar as baz} from './library';
8+
9+
verify.navigationItemsListCount(1, "foo");
10+
verify.navigationItemsListContains("foo", "function", "foo", "exact");
11+
verify.navigationItemsListCount(1, "bar");
12+
verify.navigationItemsListContains("bar", "function", "bar", "exact");
13+
verify.navigationItemsListCount(1, "baz");
14+
verify.navigationItemsListContains("baz", "alias", "baz", "exact");

0 commit comments

Comments
 (0)