|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#nullable enable |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Concurrent; |
| 8 | +using System.Management.Automation.Language; |
| 9 | +using Microsoft.PowerShell.EditorServices.Services.TextDocument; |
| 10 | +using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility; |
| 11 | +using Microsoft.PowerShell.EditorServices.Services.Symbols; |
| 12 | + |
| 13 | +namespace Microsoft.PowerShell.EditorServices.Services; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Represents the symbols that are referenced and their locations within a single document. |
| 17 | +/// </summary> |
| 18 | +internal sealed class ReferenceTable |
| 19 | +{ |
| 20 | + private readonly ScriptFile _parent; |
| 21 | + |
| 22 | + private readonly ConcurrentDictionary<string, ConcurrentBag<IScriptExtent>> _symbolReferences = new(StringComparer.OrdinalIgnoreCase); |
| 23 | + |
| 24 | + private bool _isInited; |
| 25 | + |
| 26 | + public ReferenceTable(ScriptFile parent) => _parent = parent; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Clears the reference table causing it to rescan the source AST when queried. |
| 30 | + /// </summary> |
| 31 | + public void TagAsChanged() |
| 32 | + { |
| 33 | + _symbolReferences.Clear(); |
| 34 | + _isInited = false; |
| 35 | + } |
| 36 | + |
| 37 | + // Prefer checking if the dictionary has contents to determine if initialized. The field |
| 38 | + // `_isInited` is to guard against rescanning files with no command references, but will |
| 39 | + // generally be less reliable of a check. |
| 40 | + private bool IsInitialized => !_symbolReferences.IsEmpty || _isInited; |
| 41 | + |
| 42 | + internal bool TryGetReferences(string command, out ConcurrentBag<IScriptExtent>? references) |
| 43 | + { |
| 44 | + EnsureInitialized(); |
| 45 | + return _symbolReferences.TryGetValue(command, out references); |
| 46 | + } |
| 47 | + |
| 48 | + internal void EnsureInitialized() |
| 49 | + { |
| 50 | + if (IsInitialized) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + _parent.ScriptAst.Visit(new ReferenceVisitor(this)); |
| 56 | + } |
| 57 | + |
| 58 | + private void AddReference(string symbol, IScriptExtent extent) |
| 59 | + { |
| 60 | + _symbolReferences.AddOrUpdate( |
| 61 | + symbol, |
| 62 | + _ => new ConcurrentBag<IScriptExtent> { extent }, |
| 63 | + (_, existing) => |
| 64 | + { |
| 65 | + existing.Add(extent); |
| 66 | + return existing; |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + private sealed class ReferenceVisitor : AstVisitor |
| 71 | + { |
| 72 | + private readonly ReferenceTable _references; |
| 73 | + |
| 74 | + public ReferenceVisitor(ReferenceTable references) => _references = references; |
| 75 | + |
| 76 | + public override AstVisitAction VisitCommand(CommandAst commandAst) |
| 77 | + { |
| 78 | + string? commandName = GetCommandName(commandAst); |
| 79 | + if (string.IsNullOrEmpty(commandName)) |
| 80 | + { |
| 81 | + return AstVisitAction.Continue; |
| 82 | + } |
| 83 | + |
| 84 | + _references.AddReference( |
| 85 | + CommandHelpers.StripModuleQualification(commandName, out _), |
| 86 | + commandAst.CommandElements[0].Extent); |
| 87 | + |
| 88 | + return AstVisitAction.Continue; |
| 89 | + |
| 90 | + static string? GetCommandName(CommandAst commandAst) |
| 91 | + { |
| 92 | + string commandName = commandAst.GetCommandName(); |
| 93 | + if (!string.IsNullOrEmpty(commandName)) |
| 94 | + { |
| 95 | + return commandName; |
| 96 | + } |
| 97 | + |
| 98 | + if (commandAst.CommandElements[0] is not ExpandableStringExpressionAst expandableStringExpressionAst) |
| 99 | + { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + return AstOperations.TryGetInferredValue(expandableStringExpressionAst, out string value) ? value : null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst) |
| 108 | + { |
| 109 | + // TODO: Consider tracking unscoped variable references only when they declared within |
| 110 | + // the same function definition. |
| 111 | + _references.AddReference( |
| 112 | + $"${variableExpressionAst.VariablePath.UserPath}", |
| 113 | + variableExpressionAst.Extent); |
| 114 | + |
| 115 | + return AstVisitAction.Continue; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments