diff --git a/src/PowerShellEditorServices/Services/Symbols/Vistors/FindDeclarationVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Vistors/FindDeclarationVisitor.cs index 18aafef21..71fd99905 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Vistors/FindDeclarationVisitor.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Vistors/FindDeclarationVisitor.cs @@ -3,6 +3,7 @@ using System; using System.Management.Automation.Language; +using Microsoft.PowerShell.EditorServices.Utility; namespace Microsoft.PowerShell.EditorServices.Services.Symbols { @@ -43,27 +44,15 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun return AstVisitAction.Continue; } - // Get the start column number of the function name, - // instead of the the start column of 'function' and create new extent for the functionName - int startColumnNumber = - functionDefinitionAst.Extent.Text.IndexOf( - functionDefinitionAst.Name, StringComparison.OrdinalIgnoreCase) + 1; - - IScriptExtent nameExtent = new ScriptExtent() - { - Text = functionDefinitionAst.Name, - StartLineNumber = functionDefinitionAst.Extent.StartLineNumber, - StartColumnNumber = startColumnNumber, - EndLineNumber = functionDefinitionAst.Extent.StartLineNumber, - EndColumnNumber = startColumnNumber + functionDefinitionAst.Name.Length, - File = functionDefinitionAst.Extent.File - }; - // We compare to the SymbolName instead of its text because it may have been resolved // from an alias. if (symbolRef.SymbolType.Equals(SymbolType.Function) && - nameExtent.Text.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) + functionDefinitionAst.Name.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) { + // Get the start column number of the function name, + // instead of the the start column of 'function' and create new extent for the functionName + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionDefinitionAst); + FoundDeclaration = new SymbolReference( SymbolType.Function, @@ -92,20 +81,8 @@ public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinit if ((symbolRef.SymbolType is SymbolType.Type || symbolRef.SymbolType.Equals(symbolType)) && typeDefinitionAst.Name.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) { - // Show only type name. Offset by StartColumn to include indentation etc. - int startColumnNumber = - typeDefinitionAst.Extent.StartColumnNumber + - typeDefinitionAst.Extent.Text.IndexOf(typeDefinitionAst.Name); - - IScriptExtent nameExtent = new ScriptExtent() - { - Text = typeDefinitionAst.Name, - StartLineNumber = typeDefinitionAst.Extent.StartLineNumber, - EndLineNumber = typeDefinitionAst.Extent.StartLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = startColumnNumber + typeDefinitionAst.Name.Length, - File = typeDefinitionAst.Extent.File - }; + // We only want the type name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(typeDefinitionAst); FoundDeclaration = new SymbolReference( @@ -135,20 +112,8 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem if (symbolRef.SymbolType.Equals(symbolType) && functionMemberAst.Name.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) { - // Show only method/ctor name. Offset by StartColumn to include indentation etc. - int startColumnNumber = - functionMemberAst.Extent.StartColumnNumber + - functionMemberAst.Extent.Text.IndexOf(functionMemberAst.Name); - - IScriptExtent nameExtent = new ScriptExtent() - { - Text = functionMemberAst.Name, - StartLineNumber = functionMemberAst.Extent.StartLineNumber, - EndLineNumber = functionMemberAst.Extent.StartLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = startColumnNumber + functionMemberAst.Name.Length, - File = functionMemberAst.Extent.File - }; + // We only want the method/ctor name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst); FoundDeclaration = new SymbolReference( @@ -174,10 +139,13 @@ public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMem if (symbolRef.SymbolType.Equals(SymbolType.Property) && propertyMemberAst.Name.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) { + // We only want the property name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst); + FoundDeclaration = new SymbolReference( SymbolType.Property, - propertyMemberAst.Extent); + nameExtent); return AstVisitAction.StopVisit; } diff --git a/src/PowerShellEditorServices/Services/Symbols/Vistors/FindReferencesVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Vistors/FindReferencesVisitor.cs index 858ee8479..81fca6331 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Vistors/FindReferencesVisitor.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Vistors/FindReferencesVisitor.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; @@ -113,7 +113,7 @@ public override AstVisitAction VisitCommand(CommandAst commandAst) /// Decides if the current function definition is a reference of the symbol being searched for. /// A reference of the symbol will be a of type SymbolType.Function and have the same name as the symbol /// - /// A functionDefinitionAst in the script's AST + /// A FunctionDefinitionAst in the script's AST /// A visit action that continues the search for references public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst) { @@ -124,31 +124,21 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun return AstVisitAction.Continue; } - (int startColumnNumber, int startLineNumber) = VisitorUtils.GetNameStartColumnAndLineNumbersFromAst(functionDefinitionAst); - - IScriptExtent nameExtent = new ScriptExtent() - { - Text = functionDefinitionAst.Name, - StartLineNumber = startLineNumber, - EndLineNumber = startLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = startColumnNumber + functionDefinitionAst.Name.Length, - File = functionDefinitionAst.Extent.File - }; - if (_symbolRef.SymbolType.Equals(SymbolType.Function) && - nameExtent.Text.Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) + functionDefinitionAst.Name.Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) { + // We only want the function name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionDefinitionAst); FoundReferences.Add(new SymbolReference(SymbolType.Function, nameExtent)); } return base.VisitFunctionDefinition(functionDefinitionAst); } /// - /// Decides if the current function definition is a reference of the symbol being searched for. + /// Decides if the current command parameter is a reference of the symbol being searched for. /// A reference of the symbol will be a of type SymbolType.Parameter and have the same name as the symbol /// - /// A commandParameterAst in the script's AST + /// A CommandParameterAst in the script's AST /// A visit action that continues the search for references public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst) { @@ -161,10 +151,10 @@ public override AstVisitAction VisitCommandParameter(CommandParameterAst command } /// - /// Decides if the current function definition is a reference of the symbol being searched for. + /// Decides if the current variable expression is a reference of the symbol being searched for. /// A reference of the symbol will be a of type SymbolType.Variable and have the same name as the symbol /// - /// A variableExpressionAst in the script's AST + /// A VariableExpressionAst in the script's AST /// A visit action that continues the search for references public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst) { @@ -191,21 +181,8 @@ public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinit if ((_symbolRef.SymbolType is SymbolType.Type || _symbolRef.SymbolType.Equals(symbolType)) && typeDefinitionAst.Name.Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) { - // Show only type name. Offset by StartColumn to include indentation etc. - int startColumnNumber = - typeDefinitionAst.Extent.StartColumnNumber + - typeDefinitionAst.Extent.Text.IndexOf(typeDefinitionAst.Name); - - IScriptExtent nameExtent = new ScriptExtent() - { - Text = typeDefinitionAst.Name, - StartLineNumber = typeDefinitionAst.Extent.StartLineNumber, - EndLineNumber = typeDefinitionAst.Extent.StartLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = startColumnNumber + typeDefinitionAst.Name.Length, - File = typeDefinitionAst.Extent.File - }; - + // We only want the type name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(typeDefinitionAst); FoundReferences.Add(new SymbolReference(symbolType, nameExtent)); } return AstVisitAction.Continue; @@ -260,21 +237,8 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem if (_symbolRef.SymbolType.Equals(symbolType) && functionMemberAst.Name.Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) { - // Show only method/ctor name. Offset by StartColumn to include indentation etc. - int startColumnNumber = - functionMemberAst.Extent.StartColumnNumber + - functionMemberAst.Extent.Text.IndexOf(functionMemberAst.Name); - - IScriptExtent nameExtent = new ScriptExtent() - { - Text = functionMemberAst.Name, - StartLineNumber = functionMemberAst.Extent.StartLineNumber, - EndLineNumber = functionMemberAst.Extent.StartLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = startColumnNumber + functionMemberAst.Name.Length, - File = functionMemberAst.Extent.File - }; - + // We only want the method/ctor name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst); FoundReferences.Add(new SymbolReference(symbolType, nameExtent)); } return AstVisitAction.Continue; @@ -291,7 +255,9 @@ public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMem if (_symbolRef.SymbolType.Equals(SymbolType.Property) && propertyMemberAst.Name.Equals(_symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase)) { - FoundReferences.Add(new SymbolReference(SymbolType.Property, propertyMemberAst.Extent)); + // We only want the property name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst); + FoundReferences.Add(new SymbolReference(SymbolType.Property, nameExtent)); } return AstVisitAction.Continue; } diff --git a/src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolVisitor.cs index 71a773c0e..697a92041 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolVisitor.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolVisitor.cs @@ -65,30 +65,25 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun return AstVisitAction.Continue; } - int startLineNumber = functionDefinitionAst.Extent.StartLineNumber; - int startColumnNumber = functionDefinitionAst.Extent.StartColumnNumber; - int endLineNumber = functionDefinitionAst.Extent.EndLineNumber; - int endColumnNumber = functionDefinitionAst.Extent.EndColumnNumber; + IScriptExtent nameExtent; - if (!includeDefinitions) + if (includeDefinitions) { - // We only want the function name - (int startColumn, int startLine) = VisitorUtils.GetNameStartColumnAndLineNumbersFromAst(functionDefinitionAst); - startLineNumber = startLine; - startColumnNumber = startColumn; - endLineNumber = startLine; - endColumnNumber = startColumn + functionDefinitionAst.Name.Length; + nameExtent = new ScriptExtent() + { + Text = functionDefinitionAst.Name, + StartLineNumber = functionDefinitionAst.Extent.StartLineNumber, + EndLineNumber = functionDefinitionAst.Extent.EndLineNumber, + StartColumnNumber = functionDefinitionAst.Extent.StartColumnNumber, + EndColumnNumber = functionDefinitionAst.Extent.EndColumnNumber, + File = functionDefinitionAst.Extent.File + }; } - - IScriptExtent nameExtent = new ScriptExtent() + else { - Text = functionDefinitionAst.Name, - StartLineNumber = startLineNumber, - EndLineNumber = endLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = endColumnNumber, - File = functionDefinitionAst.Extent.File - }; + // We only want the function name + nameExtent = VisitorUtils.GetNameExtent(functionDefinitionAst); + } if (IsPositionInExtent(nameExtent)) { @@ -163,20 +158,8 @@ private bool IsPositionInExtent(IScriptExtent extent) /// or a decision to continue if it wasn't found public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMemberAst) { - // Show only method/ctor name. Offset by StartColumn to include indentation etc. - int startColumnNumber = - functionMemberAst.Extent.StartColumnNumber + - functionMemberAst.Extent.Text.IndexOf(functionMemberAst.Name); - - IScriptExtent nameExtent = new ScriptExtent() - { - Text = functionMemberAst.Name, - StartLineNumber = functionMemberAst.Extent.StartLineNumber, - EndLineNumber = functionMemberAst.Extent.StartLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = startColumnNumber + functionMemberAst.Name.Length, - File = functionMemberAst.Extent.File - }; + // We only want the method/ctor name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst); if (IsPositionInExtent(nameExtent)) { @@ -203,31 +186,25 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem /// or a decision to continue if it wasn't found public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinitionAst) { - int startLineNumber = typeDefinitionAst.Extent.StartLineNumber; - int startColumnNumber = typeDefinitionAst.Extent.StartColumnNumber; - int endLineNumber = typeDefinitionAst.Extent.EndLineNumber; - int endColumnNumber = typeDefinitionAst.Extent.EndColumnNumber; + IScriptExtent nameExtent; - if (!includeDefinitions) + if (includeDefinitions) { - // We only want the function name - startColumnNumber = - typeDefinitionAst.Extent.StartColumnNumber + - typeDefinitionAst.Extent.Text.IndexOf(typeDefinitionAst.Name); - startLineNumber = typeDefinitionAst.Extent.StartLineNumber; - endColumnNumber = startColumnNumber + typeDefinitionAst.Name.Length; - endLineNumber = typeDefinitionAst.Extent.StartLineNumber; + nameExtent = new ScriptExtent() + { + Text = typeDefinitionAst.Name, + StartLineNumber = typeDefinitionAst.Extent.StartLineNumber, + EndLineNumber = typeDefinitionAst.Extent.EndLineNumber, + StartColumnNumber = typeDefinitionAst.Extent.StartColumnNumber, + EndColumnNumber = typeDefinitionAst.Extent.EndColumnNumber, + File = typeDefinitionAst.Extent.File + }; } - - IScriptExtent nameExtent = new ScriptExtent() + else { - Text = typeDefinitionAst.Name, - StartLineNumber = startLineNumber, - EndLineNumber = endLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = endColumnNumber, - File = typeDefinitionAst.Extent.File - }; + // We only want the type name + nameExtent = VisitorUtils.GetNameExtent(typeDefinitionAst); + } if (IsPositionInExtent(nameExtent)) { @@ -254,10 +231,8 @@ public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinit /// or a decision to continue if it wasn't found public override AstVisitAction VisitTypeExpression(TypeExpressionAst typeExpressionAst) { - // Show only type name. Offset by StartColumn to include indentation etc. - int startColumnNumber = - typeExpressionAst.Extent.StartColumnNumber + - typeExpressionAst.Extent.Text.IndexOf(typeExpressionAst.TypeName.Name); + // Show only type name (skip leading '['). Offset by StartColumn to include indentation etc. + int startColumnNumber = typeExpressionAst.Extent.StartColumnNumber + 1; IScriptExtent nameExtent = new ScriptExtent() { @@ -288,10 +263,8 @@ public override AstVisitAction VisitTypeExpression(TypeExpressionAst typeExpress /// or a decision to continue if it wasn't found public override AstVisitAction VisitTypeConstraint(TypeConstraintAst typeConstraintAst) { - // Show only type name. Offset by StartColumn to include indentation etc. - int startColumnNumber = - typeConstraintAst.Extent.StartColumnNumber + - typeConstraintAst.Extent.Text.IndexOf(typeConstraintAst.TypeName.Name); + // Show only type name (skip leading '['). Offset by StartColumn to include indentation etc. + int startColumnNumber = typeConstraintAst.Extent.StartColumnNumber + 1; IScriptExtent nameExtent = new ScriptExtent() { @@ -322,22 +295,8 @@ public override AstVisitAction VisitTypeConstraint(TypeConstraintAst typeConstra /// or a decision to continue if it wasn't found public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinitionAst configurationDefinitionAst) { - string configurationName = configurationDefinitionAst.InstanceName.Extent.Text; - - // Show only configuration name. Offset by StartColumn to include indentation etc. - int startColumnNumber = - configurationDefinitionAst.Extent.StartColumnNumber + - configurationDefinitionAst.Extent.Text.IndexOf(configurationName); - - IScriptExtent nameExtent = new ScriptExtent() - { - Text = configurationName, - StartLineNumber = configurationDefinitionAst.Extent.StartLineNumber, - EndLineNumber = configurationDefinitionAst.Extent.StartLineNumber, - StartColumnNumber = startColumnNumber, - EndColumnNumber = startColumnNumber + configurationName.Length, - File = configurationDefinitionAst.Extent.File - }; + // We only want the configuration name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(configurationDefinitionAst); if (IsPositionInExtent(nameExtent)) { @@ -353,19 +312,22 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit } /// - /// Checks to see if this variable expression is the symbol we are looking for. + /// Checks to see if this property member is the symbol we are looking for. /// - /// A VariableExpressionAst object in the script's AST + /// A PropertyMemberAst object in the script's AST /// A decision to stop searching if the right symbol was found, /// or a decision to continue if it wasn't found public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMemberAst) { - if (IsPositionInExtent(propertyMemberAst.Extent)) + // We only want the property name. Get start-location for name + IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst); + + if (IsPositionInExtent(nameExtent)) { FoundSymbolReference = new SymbolReference( SymbolType.Property, - propertyMemberAst.Extent); + nameExtent); return AstVisitAction.StopVisit; } diff --git a/src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolsVisitor.cs b/src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolsVisitor.cs index d7fe87db1..b66b6f265 100644 --- a/src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolsVisitor.cs +++ b/src/PowerShellEditorServices/Services/Symbols/Vistors/FindSymbolsVisitor.cs @@ -16,11 +16,10 @@ internal class FindSymbolsVisitor : AstVisitor2 public FindSymbolsVisitor() => SymbolReferences = new List(); /// - /// Adds each function definition as a + /// Adds each function definition to symbol reference list /// - /// A functionDefinitionAst object in the script's AST - /// A decision to stop searching if the right symbol was found, - /// or a decision to continue if it wasn't found + /// A FunctionDefinitionAst in the script's AST + /// A visit action that continues the search for references public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst) { // Extent for constructors and method trigger both this and VisitFunctionMember(). Covered in the latter. @@ -30,15 +29,7 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun return AstVisitAction.Continue; } - IScriptExtent nameExtent = new ScriptExtent() - { - Text = functionDefinitionAst.Name, - StartLineNumber = functionDefinitionAst.Extent.StartLineNumber, - EndLineNumber = functionDefinitionAst.Extent.EndLineNumber, - StartColumnNumber = functionDefinitionAst.Extent.StartColumnNumber, - EndColumnNumber = functionDefinitionAst.Extent.EndColumnNumber, - File = functionDefinitionAst.Extent.File - }; + IScriptExtent nameExtent = GetNewExtent(functionDefinitionAst, functionDefinitionAst.Name); SymbolType symbolType = functionDefinitionAst.IsWorkflow ? @@ -53,11 +44,10 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun } /// - /// Checks to see if this variable expression is the symbol we are looking for. + /// Adds each script scoped variable assignment to symbol reference list /// - /// A VariableExpressionAst object in the script's AST - /// A decision to stop searching if the right symbol was found, - /// or a decision to continue if it wasn't found + /// A VariableExpressionAst in the script's AST + /// A visit action that continues the search for references public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst) { if (!IsAssignedAtScriptScope(variableExpressionAst)) @@ -86,19 +76,13 @@ private static bool IsAssignedAtScriptScope(VariableExpressionAst variableExpres } /// - /// Adds class and AST to symbol reference list + /// Adds class and enum AST to symbol reference list /// + /// A TypeDefinitionAst in the script's AST + /// A visit action that continues the search for references public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinitionAst) { - IScriptExtent nameExtent = new ScriptExtent() - { - Text = typeDefinitionAst.Name, - StartLineNumber = typeDefinitionAst.Extent.StartLineNumber, - EndLineNumber = typeDefinitionAst.Extent.EndLineNumber, - StartColumnNumber = typeDefinitionAst.Extent.StartColumnNumber, - EndColumnNumber = typeDefinitionAst.Extent.EndColumnNumber, - File = typeDefinitionAst.Extent.File - }; + IScriptExtent nameExtent = GetNewExtent(typeDefinitionAst, typeDefinitionAst.Name); SymbolType symbolType = typeDefinitionAst.IsEnum ? @@ -115,17 +99,11 @@ public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinit /// /// Adds class method and constructor AST to symbol reference list /// + /// A FunctionMemberAst in the script's AST + /// A visit action that continues the search for references public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMemberAst) { - IScriptExtent nameExtent = new ScriptExtent() - { - Text = GetMethodOverloadName(functionMemberAst), - StartLineNumber = functionMemberAst.Extent.StartLineNumber, - EndLineNumber = functionMemberAst.Extent.EndLineNumber, - StartColumnNumber = functionMemberAst.Extent.StartColumnNumber, - EndColumnNumber = functionMemberAst.Extent.EndColumnNumber, - File = functionMemberAst.Extent.File - }; + IScriptExtent nameExtent = GetNewExtent(functionMemberAst, GetMethodOverloadName(functionMemberAst)); SymbolType symbolType = functionMemberAst.IsConstructor ? @@ -144,7 +122,8 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem /// /// A FunctionMemberAst object in the script's AST /// Function member name with parameter types and names - private static string GetMethodOverloadName(FunctionMemberAst functionMemberAst) { + private static string GetMethodOverloadName(FunctionMemberAst functionMemberAst) + { if (functionMemberAst.Parameters.Count > 0) { List parameters = new(functionMemberAst.Parameters.Count); @@ -165,17 +144,11 @@ private static string GetMethodOverloadName(FunctionMemberAst functionMemberAst) /// /// Adds class property AST to symbol reference list /// + /// A PropertyMemberAst in the script's AST + /// A visit action that continues the search for references public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMemberAst) { - IScriptExtent nameExtent = new ScriptExtent() - { - Text = propertyMemberAst.Name, - StartLineNumber = propertyMemberAst.Extent.StartLineNumber, - EndLineNumber = propertyMemberAst.Extent.EndLineNumber, - StartColumnNumber = propertyMemberAst.Extent.StartColumnNumber, - EndColumnNumber = propertyMemberAst.Extent.EndColumnNumber, - File = propertyMemberAst.Extent.File - }; + IScriptExtent nameExtent = GetNewExtent(propertyMemberAst, propertyMemberAst.Name); SymbolReferences.Add( new SymbolReference( @@ -188,17 +161,11 @@ public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMem /// /// Adds DSC configuration AST to symbol reference list /// + /// A ConfigurationDefinitionAst in the script's AST + /// A visit action that continues the search for references public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinitionAst configurationDefinitionAst) { - IScriptExtent nameExtent = new ScriptExtent() - { - Text = configurationDefinitionAst.InstanceName.Extent.Text, - StartLineNumber = configurationDefinitionAst.Extent.StartLineNumber, - EndLineNumber = configurationDefinitionAst.Extent.EndLineNumber, - StartColumnNumber = configurationDefinitionAst.Extent.StartColumnNumber, - EndColumnNumber = configurationDefinitionAst.Extent.EndColumnNumber, - File = configurationDefinitionAst.Extent.File - }; + IScriptExtent nameExtent = GetNewExtent(configurationDefinitionAst, configurationDefinitionAst.InstanceName.Extent.Text); SymbolReferences.Add( new SymbolReference( @@ -207,6 +174,22 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit return AstVisitAction.Continue; } + + /// + /// Gets a new ScriptExtent for a given Ast with same range but modified Text + /// + private static ScriptExtent GetNewExtent(Ast ast, string text) + { + return new ScriptExtent() + { + Text = text, + StartLineNumber = ast.Extent.StartLineNumber, + EndLineNumber = ast.Extent.EndLineNumber, + StartColumnNumber = ast.Extent.StartColumnNumber, + EndColumnNumber = ast.Extent.EndColumnNumber, + File = ast.Extent.File + }; + } } /// @@ -227,6 +210,8 @@ internal class FindHashtableSymbolsVisitor : AstVisitor /// /// Adds keys in the input hashtable to the symbol reference /// + /// A HashtableAst in the script's AST + /// A visit action that continues the search for references public override AstVisitAction VisitHashtable(HashtableAst hashtableAst) { if (hashtableAst.KeyValuePairs == null) diff --git a/src/PowerShellEditorServices/Utility/VisitorUtils.cs b/src/PowerShellEditorServices/Utility/VisitorUtils.cs index 861d04acc..3456c1920 100644 --- a/src/PowerShellEditorServices/Utility/VisitorUtils.cs +++ b/src/PowerShellEditorServices/Utility/VisitorUtils.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System.Management.Automation.Language; +using PSESSymbols = Microsoft.PowerShell.EditorServices.Services.Symbols; namespace Microsoft.PowerShell.EditorServices.Utility { @@ -11,15 +12,16 @@ namespace Microsoft.PowerShell.EditorServices.Utility internal static class VisitorUtils { /// - /// Calculates the start line and column of the actual function name in a function definition AST. + /// Calculates the start line and column of the actual symbol name in a AST. /// - /// A FunctionDefinitionAst object in the script's AST - /// A tuple with start column and line for the function name - internal static (int startColumn, int startLine) GetNameStartColumnAndLineNumbersFromAst(FunctionDefinitionAst ast) + /// An Ast object in the script's AST + /// An offset specifying where to begin searching in the first line of the AST's extent text + /// A tuple with start column and line of the symbol name + private static (int startColumn, int startLine) GetNameStartColumnAndLineNumbersFromAst(Ast ast, int firstLineColumnOffset) { int startColumnNumber = ast.Extent.StartColumnNumber; int startLineNumber = ast.Extent.StartLineNumber; - int astOffset = ast.IsFilter ? "filter".Length : ast.IsWorkflow ? "workflow".Length : "function".Length; + int astOffset = firstLineColumnOffset; string astText = ast.Extent.Text; // The line offset represents the offset on the line that we're on where as // astOffset is the offset on the entire text of the AST. @@ -47,5 +49,113 @@ internal static (int startColumn, int startLine) GetNameStartColumnAndLineNumber return (startColumnNumber + lineOffset, startLineNumber); } + + /// + /// Gets a new ScriptExtent for a given Ast for the symbol name only (variable) + /// + /// A FunctionDefinitionAst in the script's AST + /// A ScriptExtent with for the symbol name only + internal static PSESSymbols.ScriptExtent GetNameExtent(FunctionDefinitionAst functionDefinitionAst) + { + int astOffset = functionDefinitionAst.IsFilter ? "filter".Length : functionDefinitionAst.IsWorkflow ? "workflow".Length : "function".Length; + (int startColumn, int startLine) = GetNameStartColumnAndLineNumbersFromAst(functionDefinitionAst, astOffset); + + return new PSESSymbols.ScriptExtent() + { + Text = functionDefinitionAst.Name, + StartLineNumber = startLine, + EndLineNumber = startLine, + StartColumnNumber = startColumn, + EndColumnNumber = startColumn + functionDefinitionAst.Name.Length, + File = functionDefinitionAst.Extent.File + }; + } + + /// + /// Gets a new ScriptExtent for a given Ast for the symbol name only (variable) + /// + /// A TypeDefinitionAst in the script's AST + /// A ScriptExtent with for the symbol name only + internal static PSESSymbols.ScriptExtent GetNameExtent(TypeDefinitionAst typeDefinitionAst) + { + int astOffset = typeDefinitionAst.IsEnum ? "enum".Length : "class".Length; + (int startColumn, int startLine) = GetNameStartColumnAndLineNumbersFromAst(typeDefinitionAst, astOffset); + + return new PSESSymbols.ScriptExtent() + { + Text = typeDefinitionAst.Name, + StartLineNumber = startLine, + EndLineNumber = startLine, + StartColumnNumber = startColumn, + EndColumnNumber = startColumn + typeDefinitionAst.Name.Length, + File = typeDefinitionAst.Extent.File + }; + } + + /// + /// Gets a new ScriptExtent for a given Ast for the symbol name only (variable) + /// + /// A FunctionMemberAst in the script's AST + /// A ScriptExtent with for the symbol name only + internal static PSESSymbols.ScriptExtent GetNameExtent(FunctionMemberAst functionMemberAst) + { + // offset by [type] if return type is specified + int astOffset = functionMemberAst.ReturnType?.Extent.Text.Length ?? 0; + (int startColumn, int startLine) = GetNameStartColumnAndLineNumbersFromAst(functionMemberAst, astOffset); + + return new PSESSymbols.ScriptExtent() + { + Text = functionMemberAst.Name, + StartLineNumber = startLine, + EndLineNumber = startLine, + StartColumnNumber = startColumn, + EndColumnNumber = startColumn + functionMemberAst.Name.Length, + File = functionMemberAst.Extent.File + }; + } + + /// + /// Gets a new ScriptExtent for a given Ast for the property name only + /// + /// A PropertyMemberAst in the script's AST + /// A ScriptExtent with for the symbol name only + internal static PSESSymbols.ScriptExtent GetNameExtent(PropertyMemberAst propertyMemberAst) + { + // offset by [type] if type is specified + int astOffset = propertyMemberAst.PropertyType?.Extent.Text.Length ?? 0; + (int startColumn, int startLine) = GetNameStartColumnAndLineNumbersFromAst(propertyMemberAst, astOffset); + + return new PSESSymbols.ScriptExtent() + { + Text = propertyMemberAst.Name, + StartLineNumber = startLine, + EndLineNumber = startLine, + StartColumnNumber = startColumn, + EndColumnNumber = startColumn + propertyMemberAst.Name.Length + 1, + File = propertyMemberAst.Extent.File + }; + } + + /// + /// Gets a new ScriptExtent for a given Ast for the configuration instance name only + /// + /// A ConfigurationDefinitionAst in the script's AST + /// A ScriptExtent with for the symbol name only + internal static PSESSymbols.ScriptExtent GetNameExtent(ConfigurationDefinitionAst configurationDefinitionAst) + { + string configurationName = configurationDefinitionAst.InstanceName.Extent.Text; + const int astOffset = 13; // "configuration".Length + (int startColumn, int startLine) = GetNameStartColumnAndLineNumbersFromAst(configurationDefinitionAst, astOffset); + + return new PSESSymbols.ScriptExtent() + { + Text = configurationName, + StartLineNumber = startLine, + EndLineNumber = startLine, + StartColumnNumber = startColumn, + EndColumnNumber = startColumn + configurationName.Length, + File = configurationDefinitionAst.Extent.File + }; + } } }