7
7
using System ;
8
8
using System . Collections ;
9
9
using System . Collections . Generic ;
10
+ using System . Collections . Specialized ;
10
11
using System . Linq ;
11
12
using System . Management . Automation ;
12
13
using System . Management . Automation . Language ;
@@ -248,10 +249,12 @@ public FindOccurrencesResult FindSymbolsInFile(ScriptFile scriptFile)
248
249
/// </summary>
249
250
/// <param name="foundSymbol">The symbol to find all references for</param>
250
251
/// <param name="referencedFiles">An array of scriptFiles too search for references in</param>
252
+ /// <param name="workspace">The workspace that will be searched for symbols</param>
251
253
/// <returns>FindReferencesResult</returns>
252
254
public async Task < FindReferencesResult > FindReferencesOfSymbol (
253
255
SymbolReference foundSymbol ,
254
- ScriptFile [ ] referencedFiles )
256
+ ScriptFile [ ] referencedFiles ,
257
+ Workspace workspace )
255
258
{
256
259
if ( foundSymbol != null )
257
260
{
@@ -262,9 +265,26 @@ public async Task<FindReferencesResult> FindReferencesOfSymbol(
262
265
// Make sure aliases have been loaded
263
266
await GetAliases ( ) ;
264
267
268
+ // We want to look for references first in referenced files, hence we use ordered dictionary
269
+ var fileMap = new OrderedDictionary ( StringComparer . OrdinalIgnoreCase ) ;
270
+ foreach ( ScriptFile file in referencedFiles )
271
+ {
272
+ fileMap . Add ( file . FilePath , file ) ;
273
+ }
274
+
275
+ var allFiles = workspace . EnumeratePSFiles ( ) ;
276
+ foreach ( var file in allFiles )
277
+ {
278
+ if ( ! fileMap . Contains ( file ) )
279
+ {
280
+ fileMap . Add ( file , new ScriptFile ( file , null , this . powerShellContext . LocalPowerShellVersion . Version ) ) ;
281
+ }
282
+ }
283
+
265
284
List < SymbolReference > symbolReferences = new List < SymbolReference > ( ) ;
266
- foreach ( ScriptFile file in referencedFiles )
285
+ foreach ( var fileName in fileMap . Keys )
267
286
{
287
+ var file = ( ScriptFile ) fileMap [ fileName ] ;
268
288
IEnumerable < SymbolReference > symbolReferencesinFile =
269
289
AstOperations
270
290
. FindReferencesOfSymbol (
@@ -316,6 +336,8 @@ public async Task<GetDefinitionResult> GetDefinitionOfSymbol(
316
336
workspace . ExpandScriptReferences (
317
337
sourceFile ) ;
318
338
339
+ var filesSearched = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
340
+
319
341
// look through the referenced files until definition is found
320
342
// or there are no more file to look through
321
343
SymbolReference foundDefinition = null ;
@@ -326,14 +348,45 @@ public async Task<GetDefinitionResult> GetDefinitionOfSymbol(
326
348
referencedFiles [ i ] . ScriptAst ,
327
349
foundSymbol ) ;
328
350
351
+ filesSearched . Add ( referencedFiles [ i ] . FilePath ) ;
329
352
if ( foundDefinition != null )
330
353
{
331
354
foundDefinition . FilePath = referencedFiles [ i ] . FilePath ;
332
355
break ;
333
356
}
334
357
}
335
358
336
- // if definition is not found in referenced files
359
+ // if the definition the not found in referenced files
360
+ // look for it in all the files in the workspace
361
+ if ( foundDefinition == null )
362
+ {
363
+ // Get a list of all powershell files in the workspace path
364
+ var allFiles = workspace . EnumeratePSFiles ( ) ;
365
+ foreach ( var file in allFiles )
366
+ {
367
+ if ( filesSearched . Contains ( file ) )
368
+ {
369
+ continue ;
370
+ }
371
+
372
+ Token [ ] tokens = null ;
373
+ ParseError [ ] parseErrors = null ;
374
+ foundDefinition =
375
+ AstOperations . FindDefinitionOfSymbol (
376
+ Parser . ParseFile ( file , out tokens , out parseErrors ) ,
377
+ foundSymbol ) ;
378
+
379
+ filesSearched . Add ( file ) ;
380
+ if ( foundDefinition != null )
381
+ {
382
+ foundDefinition . FilePath = file ;
383
+ break ;
384
+ }
385
+
386
+ }
387
+ }
388
+
389
+ // if definition is not found in file in the workspace
337
390
// look for it in the builtin commands
338
391
if ( foundDefinition == null )
339
392
{
0 commit comments