@@ -114,14 +114,13 @@ public IEnumerable<SymbolReference> FindSymbolsInFile(ScriptFile scriptFile)
114
114
{
115
115
Validate . IsNotNull ( nameof ( scriptFile ) , scriptFile ) ;
116
116
117
- List < SymbolReference > symbols = new ( ) ;
118
117
foreach ( IDocumentSymbolProvider symbolProvider in GetDocumentSymbolProviders ( ) )
119
118
{
120
- // TODO: Each provider needs to set the source line and filepath.
121
- symbols . AddRange ( symbolProvider . ProvideDocumentSymbols ( scriptFile ) ) ;
119
+ foreach ( SymbolReference symbol in symbolProvider . ProvideDocumentSymbols ( scriptFile ) )
120
+ {
121
+ yield return symbol ;
122
+ }
122
123
}
123
-
124
- return symbols ;
125
124
}
126
125
127
126
/// <summary>
@@ -155,15 +154,15 @@ private static string[] GetIdentifiers(string symbolName, SymbolType symbolType,
155
154
156
155
/// <summary>
157
156
/// Finds all the references of a symbol in the workspace, resolving aliases.
158
- /// TODO: Make it not return a nullable .
157
+ /// TODO: One day use IAsyncEnumerable .
159
158
/// </summary>
160
- public async Task < IEnumerable < SymbolReference > ? > ScanForReferencesOfSymbolAsync (
159
+ public async Task < IEnumerable < SymbolReference > > ScanForReferencesOfSymbolAsync (
161
160
SymbolReference symbol ,
162
161
CancellationToken cancellationToken = default )
163
162
{
164
163
if ( symbol is null )
165
164
{
166
- return null ;
165
+ return Enumerable . Empty < SymbolReference > ( ) ;
167
166
}
168
167
169
168
// TODO: Should we handle aliases at a lower level?
@@ -181,7 +180,6 @@ private static string[] GetIdentifiers(string symbolName, SymbolType symbolType,
181
180
await ScanWorkspacePSFiles ( cancellationToken ) . ConfigureAwait ( false ) ;
182
181
183
182
List < SymbolReference > symbols = new ( ) ;
184
-
185
183
string [ ] allIdentifiers = GetIdentifiers ( targetName , symbol . SymbolType , aliases ) ;
186
184
187
185
foreach ( ScriptFile file in _workspaceService . GetOpenedFiles ( ) )
@@ -191,7 +189,8 @@ private static string[] GetIdentifiers(string symbolName, SymbolType symbolType,
191
189
await Task . Yield ( ) ;
192
190
cancellationToken . ThrowIfCancellationRequested ( ) ;
193
191
194
- if ( ! file . References . TryGetReferences ( targetIdentifier , out ConcurrentBag < SymbolReference > ? references ) )
192
+ _ = file . References . TryGetReferences ( targetIdentifier , out ConcurrentBag < SymbolReference > ? references ) ;
193
+ if ( references is null )
195
194
{
196
195
continue ;
197
196
}
@@ -301,33 +300,45 @@ await CommandHelpers.GetCommandInfoAsync(
301
300
302
301
/// <summary>
303
302
/// Finds the possible definitions of the symbol in the file or workspace.
303
+ /// TODO: One day use IAsyncEnumerable.
304
+ /// TODO: Fix searching for definition of built-in commands.
305
+ /// TODO: Fix "definition" of dot-source (maybe?)
304
306
/// </summary>
305
307
public async Task < IEnumerable < SymbolReference > > GetDefinitionOfSymbolAsync (
306
308
ScriptFile scriptFile ,
307
309
SymbolReference symbol ,
308
310
CancellationToken cancellationToken = default )
309
311
{
310
- if ( scriptFile . References . TryGetReferences ( symbol . SymbolName , out ConcurrentBag < SymbolReference > ? symbols ) )
312
+ List < SymbolReference > declarations = new ( ) ;
313
+ _ = scriptFile . References . TryGetReferences ( symbol . SymbolName , out ConcurrentBag < SymbolReference > ? symbols ) ;
314
+ if ( symbols is not null )
311
315
{
312
- IEnumerable < SymbolReference > possibleLocalDeclarations = symbols . Where ( ( i ) => i . IsDeclaration ) ;
313
- if ( possibleLocalDeclarations . Any ( ) )
316
+ foreach ( SymbolReference foundReference in symbols )
314
317
{
315
- _logger . LogDebug ( $ "Found possible declarations ${ possibleLocalDeclarations } ") ;
316
- return possibleLocalDeclarations ;
318
+ if ( foundReference . IsDeclaration )
319
+ {
320
+ _logger . LogDebug ( $ "Found possible declaration in same file ${ foundReference } ") ;
321
+ declarations . Add ( foundReference ) ;
322
+ }
317
323
}
318
324
}
319
325
320
- IEnumerable < SymbolReference > ? allSymbols = await ScanForReferencesOfSymbolAsync (
321
- symbol ,
322
- cancellationToken ) . ConfigureAwait ( false ) ;
323
-
324
- IEnumerable < SymbolReference > possibleDeclarations = allSymbols . Where ( ( i ) => i . IsDeclaration ) ;
325
- _logger . LogDebug ( $ "Found possible declarations ${ possibleDeclarations } ") ;
326
+ if ( declarations . Any ( ) )
327
+ {
328
+ return declarations ;
329
+ }
326
330
327
- return possibleDeclarations ;
331
+ foreach ( SymbolReference foundReference in await ScanForReferencesOfSymbolAsync (
332
+ symbol , cancellationToken ) . ConfigureAwait ( false ) )
333
+ {
334
+ if ( foundReference . IsDeclaration )
335
+ {
336
+ _logger . LogDebug ( $ "Found possible declaration in workspace ${ foundReference } ") ;
337
+ declarations . Add ( foundReference ) ;
338
+ }
339
+ }
328
340
329
- // TODO: Fix searching for definition of built-in commands.
330
- // TODO: Fix "definition" of dot-source (maybe?)
341
+ return declarations ;
331
342
}
332
343
333
344
private async Task ScanWorkspacePSFiles ( CancellationToken cancellationToken = default )
0 commit comments