From 5e799b87739f279a257c824d28381798192c0938 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 23 May 2016 07:01:16 -0700 Subject: [PATCH] Don't include imports in navigateTo if the imported declaration is also in the items and has the same name --- src/services/navigateTo.ts | 15 ++++++++++++++- src/services/services.ts | 4 ++-- tests/cases/fourslash/navigateToImport.ts | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/navigateToImport.ts diff --git a/src/services/navigateTo.ts b/src/services/navigateTo.ts index 8022e2fad7a..49707263c1f 100644 --- a/src/services/navigateTo.ts +++ b/src/services/navigateTo.ts @@ -2,7 +2,7 @@ namespace ts.NavigateTo { type RawNavigateToItem = { name: string; fileName: string; matchKind: PatternMatchKind; isCaseSensitive: boolean; declaration: Declaration }; - export function getNavigateToItems(program: Program, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number): NavigateToItem[] { + export function getNavigateToItems(program: Program, checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number): NavigateToItem[] { const patternMatcher = createPatternMatcher(searchValue); let rawItems: RawNavigateToItem[] = []; @@ -49,6 +49,19 @@ namespace ts.NavigateTo { } }); + // Remove imports when the imported declaration is already in the list and has the same name. + rawItems = filter(rawItems, item => { + const decl = item.declaration; + if (decl.kind === SyntaxKind.ImportClause || decl.kind === SyntaxKind.ImportSpecifier || decl.kind === SyntaxKind.ImportEqualsDeclaration) { + const importer = checker.getSymbolAtLocation(decl.name); + const imported = checker.getAliasedSymbol(importer); + return importer.name !== imported.name; + } + else { + return true; + } + }); + rawItems.sort(compareNavigateToItems); if (maxResultCount !== undefined) { rawItems = rawItems.slice(0, maxResultCount); diff --git a/src/services/services.ts b/src/services/services.ts index ef25fae7bba..4fcebaddc2c 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -6601,8 +6601,8 @@ namespace ts { /// NavigateTo function getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[] { synchronizeHostData(); - - return ts.NavigateTo.getNavigateToItems(program, cancellationToken, searchValue, maxResultCount); + const checker = getProgram().getTypeChecker(); + return ts.NavigateTo.getNavigateToItems(program, checker, cancellationToken, searchValue, maxResultCount); } function getEmitOutput(fileName: string): EmitOutput { diff --git a/tests/cases/fourslash/navigateToImport.ts b/tests/cases/fourslash/navigateToImport.ts new file mode 100644 index 00000000000..7247d443c4f --- /dev/null +++ b/tests/cases/fourslash/navigateToImport.ts @@ -0,0 +1,14 @@ +/// + +// @Filename: library.ts +////export function foo() {} +////export function bar() {} +// @Filename: user.ts +////import {foo, bar as baz} from './library'; + +verify.navigationItemsListCount(1, "foo"); +verify.navigationItemsListContains("foo", "function", "foo", "exact"); +verify.navigationItemsListCount(1, "bar"); +verify.navigationItemsListContains("bar", "function", "bar", "exact"); +verify.navigationItemsListCount(1, "baz"); +verify.navigationItemsListContains("baz", "alias", "baz", "exact");