@@ -16,6 +16,7 @@ import {
1616 ImportClause ,
1717 ImportEqualsDeclaration ,
1818 ImportSpecifier ,
19+ isInsideNodeModules ,
1920 isPropertyAccessExpression ,
2021 isPropertyNameLiteral ,
2122 NavigateToItem ,
@@ -37,11 +38,11 @@ interface RawNavigateToItem {
3738}
3839
3940/** @internal */
40- export function getNavigateToItems ( sourceFiles : readonly SourceFile [ ] , checker : TypeChecker , cancellationToken : CancellationToken , searchValue : string , maxResultCount : number | undefined , excludeDtsFiles : boolean ) : NavigateToItem [ ] {
41+ export function getNavigateToItems ( sourceFiles : readonly SourceFile [ ] , checker : TypeChecker , cancellationToken : CancellationToken , searchValue : string , maxResultCount : number | undefined , excludeDtsFiles : boolean , excludeLibFiles ?: boolean ) : NavigateToItem [ ] {
4142 const patternMatcher = createPatternMatcher ( searchValue ) ;
4243 if ( ! patternMatcher ) return emptyArray ;
4344 const rawItems : RawNavigateToItem [ ] = [ ] ;
44-
45+ const singleCurrentFile = sourceFiles . length === 1 ? sourceFiles [ 0 ] : undefined ;
4546 // Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[]
4647 for ( const sourceFile of sourceFiles ) {
4748 cancellationToken . throwIfCancellationRequested ( ) ;
@@ -50,16 +51,37 @@ export function getNavigateToItems(sourceFiles: readonly SourceFile[], checker:
5051 continue ;
5152 }
5253
54+ if ( shouldExcludeFile ( sourceFile , ! ! excludeLibFiles , singleCurrentFile ) ) {
55+ continue ;
56+ }
57+
5358 sourceFile . getNamedDeclarations ( ) . forEach ( ( declarations , name ) => {
54- getItemsFromNamedDeclaration ( patternMatcher , name , declarations , checker , sourceFile . fileName , rawItems ) ;
59+ getItemsFromNamedDeclaration ( patternMatcher , name , declarations , checker , sourceFile . fileName , ! ! excludeLibFiles , singleCurrentFile , rawItems ) ;
5560 } ) ;
5661 }
5762
5863 rawItems . sort ( compareNavigateToItems ) ;
5964 return ( maxResultCount === undefined ? rawItems : rawItems . slice ( 0 , maxResultCount ) ) . map ( createNavigateToItem ) ;
6065}
6166
62- function getItemsFromNamedDeclaration ( patternMatcher : PatternMatcher , name : string , declarations : readonly Declaration [ ] , checker : TypeChecker , fileName : string , rawItems : RawNavigateToItem [ ] ) : void {
67+ /**
68+ * Exclude 'node_modules/' files and standard library files if 'excludeLibFiles' is true.
69+ * If we're in current file only mode, we don't exclude the current file, even if it is a library file.
70+ */
71+ function shouldExcludeFile ( file : SourceFile , excludeLibFiles : boolean , singleCurrentFile : SourceFile | undefined ) : boolean {
72+ return file !== singleCurrentFile && excludeLibFiles && ( isInsideNodeModules ( file . path ) || file . hasNoDefaultLib ) ;
73+ }
74+
75+ function getItemsFromNamedDeclaration (
76+ patternMatcher : PatternMatcher ,
77+ name : string ,
78+ declarations : readonly Declaration [ ] ,
79+ checker : TypeChecker ,
80+ fileName : string ,
81+ excludeLibFiles : boolean ,
82+ singleCurrentFile : SourceFile | undefined ,
83+ rawItems : RawNavigateToItem [ ] ,
84+ ) : void {
6385 // First do a quick check to see if the name of the declaration matches the
6486 // last portion of the (possibly) dotted name they're searching for.
6587 const match = patternMatcher . getMatchForLastSegmentOfPattern ( name ) ;
@@ -68,7 +90,7 @@ function getItemsFromNamedDeclaration(patternMatcher: PatternMatcher, name: stri
6890 }
6991
7092 for ( const declaration of declarations ) {
71- if ( ! shouldKeepItem ( declaration , checker ) ) continue ;
93+ if ( ! shouldKeepItem ( declaration , checker , excludeLibFiles , singleCurrentFile ) ) continue ;
7294
7395 if ( patternMatcher . patternContainsDots ) {
7496 // If the pattern has dots in it, then also see if the declaration container matches as well.
@@ -83,14 +105,20 @@ function getItemsFromNamedDeclaration(patternMatcher: PatternMatcher, name: stri
83105 }
84106}
85107
86- function shouldKeepItem ( declaration : Declaration , checker : TypeChecker ) : boolean {
108+ function shouldKeepItem (
109+ declaration : Declaration ,
110+ checker : TypeChecker ,
111+ excludeLibFiles : boolean ,
112+ singleCurrentFile : SourceFile | undefined ,
113+ ) : boolean {
87114 switch ( declaration . kind ) {
88115 case SyntaxKind . ImportClause :
89116 case SyntaxKind . ImportSpecifier :
90117 case SyntaxKind . ImportEqualsDeclaration :
91118 const importer = checker . getSymbolAtLocation ( ( declaration as ImportClause | ImportSpecifier | ImportEqualsDeclaration ) . name ! ) ! ; // TODO: GH#18217
92119 const imported = checker . getAliasedSymbol ( importer ) ;
93- return importer . escapedName !== imported . escapedName ;
120+ return importer . escapedName !== imported . escapedName
121+ && ! imported . declarations ?. every ( d => shouldExcludeFile ( d . getSourceFile ( ) , excludeLibFiles , singleCurrentFile ) ) ;
94122 default :
95123 return true ;
96124 }
0 commit comments