- 
                Notifications
    
You must be signed in to change notification settings  - Fork 239
 
Overhaul workspace search for symbol references #1917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            andyleejordan
  merged 6 commits into
  PowerShell:main
from
SeeminglyScience:workspace-scan-overhaul
  
      
      
   
  Sep 15, 2022 
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      42f46ab
              
                Cache symbol references per file
              
              
                SeeminglyScience 76be2c6
              
                Add file watcher and setting for open docs only
              
              
                SeeminglyScience 97d1a18
              
                Update src/PowerShellEditorServices/Services/PowerShell/Utility/Comma…
              
              
                SeeminglyScience a4c4de1
              
                Update src/PowerShellEditorServices/Services/PowerShell/Utility/Comma…
              
              
                SeeminglyScience 06b9ff5
              
                Fix tests (and issues discovered by tests)
              
              
                SeeminglyScience 3ac1e7f
              
                Fix e2e test as well
              
              
                SeeminglyScience File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            100 changes: 100 additions & 0 deletions
          
          100 
        
  src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| 
     | 
||
| #nullable enable | ||
| 
     | 
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Management.Automation.Language; | ||
| using Microsoft.PowerShell.EditorServices.Services.TextDocument; | ||
| using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility; | ||
| 
     | 
||
| namespace Microsoft.PowerShell.EditorServices.Services; | ||
| 
     | 
||
| /// <summary> | ||
| /// Represents the symbols that are referenced and their locations within a single document. | ||
| /// </summary> | ||
| internal sealed class ReferenceTable | ||
| { | ||
| private readonly ScriptFile _parent; | ||
| 
     | 
||
| private readonly ConcurrentDictionary<string, ConcurrentBag<IScriptExtent>> _symbolReferences = new(StringComparer.OrdinalIgnoreCase); | ||
| 
     | 
||
| private bool _isInited; | ||
| 
     | 
||
| public ReferenceTable(ScriptFile parent) => _parent = parent; | ||
| 
     | 
||
| /// <summary> | ||
| /// Clears the reference table causing it to rescan the source AST when queried. | ||
| /// </summary> | ||
| public void TagAsChanged() | ||
| { | ||
| _symbolReferences.Clear(); | ||
| _isInited = false; | ||
| } | ||
| 
     | 
||
| // Prefer checking if the dictionary has contents to determine if initialized. The field | ||
| // `_isInited` is to guard against rescanning files with no command references, but will | ||
| // generally be less reliable of a check. | ||
| private bool IsInitialized => !_symbolReferences.IsEmpty || _isInited; | ||
| 
     | 
||
| internal bool TryGetReferences(string command, out ConcurrentBag<IScriptExtent>? references) | ||
| { | ||
| EnsureInitialized(); | ||
| return _symbolReferences.TryGetValue(command, out references); | ||
| } | ||
| 
     | 
||
| internal void EnsureInitialized() | ||
| { | ||
| if (IsInitialized) | ||
| { | ||
| return; | ||
| } | ||
| 
     | 
||
| _parent.ScriptAst.Visit(new ReferenceVisitor(this)); | ||
| } | ||
| 
     | 
||
| private void AddReference(string symbol, IScriptExtent extent) | ||
| { | ||
| _symbolReferences.AddOrUpdate( | ||
| symbol, | ||
| _ => new ConcurrentBag<IScriptExtent> { extent }, | ||
| (_, existing) => | ||
| { | ||
| existing.Add(extent); | ||
| return existing; | ||
| }); | ||
| } | ||
| 
     | 
||
| private sealed class ReferenceVisitor : AstVisitor | ||
| { | ||
| private readonly ReferenceTable _references; | ||
| 
     | 
||
| public ReferenceVisitor(ReferenceTable references) => _references = references; | ||
| 
     | 
||
| public override AstVisitAction VisitCommand(CommandAst commandAst) | ||
| { | ||
| string commandName = commandAst.GetCommandName(); | ||
| if (string.IsNullOrEmpty(commandName)) | ||
| { | ||
| return AstVisitAction.Continue; | ||
| } | ||
| 
     | 
||
| _references.AddReference( | ||
| CommandHelpers.StripModuleQualification(commandName, out _), | ||
| commandAst.CommandElements[0].Extent); | ||
| return AstVisitAction.Continue; | ||
| } | ||
| 
     | 
||
| public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst) | ||
| { | ||
| // TODO: Consider tracking unscoped variable references only when they declared within | ||
| // the same function definition. | ||
| _references.AddReference( | ||
| $"${variableExpressionAst.VariablePath.UserPath}", | ||
| variableExpressionAst.Extent); | ||
| 
     | 
||
| return AstVisitAction.Continue; | ||
| } | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.