/// <summary> /// Finds the definition of a symbol in the script file or any of the /// files that it references. /// </summary> /// <param name="sourceFile">The initial script file to be searched for the symbol's definition.</param> /// <param name="foundSymbol">The symbol for which a definition will be found.</param> /// <returns>The resulting GetDefinitionResult for the symbol's definition.</returns> public async Task <SymbolReference> GetDefinitionOfSymbolAsync( ScriptFile sourceFile, SymbolReference foundSymbol) { Validate.IsNotNull(nameof(sourceFile), sourceFile); Validate.IsNotNull(nameof(foundSymbol), foundSymbol); ScriptFile[] referencedFiles = _workspaceService.ExpandScriptReferences( sourceFile); var filesSearched = new HashSet <string>(StringComparer.OrdinalIgnoreCase); // look through the referenced files until definition is found // or there are no more file to look through SymbolReference foundDefinition = null; foreach (ScriptFile scriptFile in referencedFiles) { foundDefinition = AstOperations.FindDefinitionOfSymbol( scriptFile.ScriptAst, foundSymbol); filesSearched.Add(scriptFile.FilePath); if (foundDefinition != null) { foundDefinition.FilePath = scriptFile.FilePath; break; } if (foundSymbol.SymbolType == SymbolType.Function) { // Dot-sourcing is parsed as a "Function" Symbol. string dotSourcedPath = GetDotSourcedPath(foundSymbol, scriptFile); if (scriptFile.FilePath == dotSourcedPath) { foundDefinition = new SymbolReference(SymbolType.Function, foundSymbol.SymbolName, scriptFile.ScriptAst.Extent, scriptFile.FilePath); break; } } } // if the definition the not found in referenced files // look for it in all the files in the workspace if (foundDefinition == null) { // Get a list of all powershell files in the workspace path IEnumerable <string> allFiles = _workspaceService.EnumeratePSFiles(); foreach (string file in allFiles) { if (filesSearched.Contains(file)) { continue; } foundDefinition = AstOperations.FindDefinitionOfSymbol( Parser.ParseFile(file, out Token[] tokens, out ParseError[] parseErrors),
/// <summary> /// Finds the definition of a symbol in the script file or any of the /// files that it references. /// </summary> /// <param name="sourceFile">The initial script file to be searched for the symbol's definition.</param> /// <param name="foundSymbol">The symbol for which a definition will be found.</param> /// <returns>The resulting GetDefinitionResult for the symbol's definition.</returns> public async Task <SymbolReference> GetDefinitionOfSymbolAsync( ScriptFile sourceFile, SymbolReference foundSymbol) { Validate.IsNotNull(nameof(sourceFile), sourceFile); Validate.IsNotNull(nameof(foundSymbol), foundSymbol); // If symbol is an alias, resolve it. (Dictionary <string, List <string> > _, Dictionary <string, string> aliasToCmdlets) = await CommandHelpers.GetAliasesAsync(_executionService).ConfigureAwait(false); if (aliasToCmdlets.ContainsKey(foundSymbol.SymbolName)) { foundSymbol = new SymbolReference( foundSymbol.SymbolType, aliasToCmdlets[foundSymbol.SymbolName], foundSymbol.ScriptRegion, foundSymbol.FilePath, foundSymbol.SourceLine); } ScriptFile[] referencedFiles = _workspaceService.ExpandScriptReferences( sourceFile); HashSet <string> filesSearched = new(StringComparer.OrdinalIgnoreCase); // look through the referenced files until definition is found // or there are no more file to look through SymbolReference foundDefinition = null; foreach (ScriptFile scriptFile in referencedFiles) { foundDefinition = AstOperations.FindDefinitionOfSymbol( scriptFile.ScriptAst, foundSymbol); filesSearched.Add(scriptFile.FilePath); if (foundDefinition != null) { foundDefinition.FilePath = scriptFile.FilePath; break; } if (foundSymbol.SymbolType == SymbolType.Function) { // Dot-sourcing is parsed as a "Function" Symbol. string dotSourcedPath = GetDotSourcedPath(foundSymbol, scriptFile); if (scriptFile.FilePath == dotSourcedPath) { foundDefinition = new SymbolReference(SymbolType.Function, foundSymbol.SymbolName, scriptFile.ScriptAst.Extent, scriptFile.FilePath); break; } } } // if the definition the not found in referenced files // look for it in all the files in the workspace if (foundDefinition == null) { // Get a list of all powershell files in the workspace path foreach (string file in _workspaceService.EnumeratePSFiles()) { if (filesSearched.Contains(file)) { continue; } foundDefinition = AstOperations.FindDefinitionOfSymbol( Parser.ParseFile(file, out Token[] tokens, out ParseError[] parseErrors),