Skip to content

Commit

Permalink
refactor and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Aug 17, 2022
1 parent 9011949 commit 5276391
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Management.Automation.Language;
using Microsoft.PowerShell.EditorServices.Utility;

namespace Microsoft.PowerShell.EditorServices.Services.Symbols
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -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
/// </summary>
/// <param name="functionDefinitionAst">A functionDefinitionAst in the script's AST</param>
/// <param name="functionDefinitionAst">A FunctionDefinitionAst in the script's AST</param>
/// <returns>A visit action that continues the search for references</returns>
public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst)
{
Expand All @@ -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);
}

/// <summary>
/// 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
/// </summary>
/// <param name="commandParameterAst">A commandParameterAst in the script's AST</param>
/// <param name="commandParameterAst">A CommandParameterAst in the script's AST</param>
/// <returns>A visit action that continues the search for references</returns>
public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst)
{
Expand All @@ -161,10 +151,10 @@ public override AstVisitAction VisitCommandParameter(CommandParameterAst command
}

/// <summary>
/// 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
/// </summary>
/// <param name="variableExpressionAst">A variableExpressionAst in the script's AST</param>
/// <param name="variableExpressionAst">A VariableExpressionAst in the script's AST</param>
/// <returns>A visit action that continues the search for references</returns>
public override AstVisitAction VisitVariableExpression(VariableExpressionAst variableExpressionAst)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit 5276391

Please sign in to comment.