Skip to content

Commit

Permalink
WIP: Replace FindDeclarationVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleejordan committed Jan 20, 2023
1 parent 7991b98 commit 604d6a7
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 460 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ public ReferencesCodeLensProvider(WorkspaceService workspaceService, SymbolsServ
public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile, CancellationToken cancellationToken)
{
List<CodeLens> acc = new();
foreach (SymbolReference sym in _symbolProvider.ProvideDocumentSymbols(scriptFile))
foreach (SymbolReference symbol in _symbolProvider.ProvideDocumentSymbols(scriptFile))
{
cancellationToken.ThrowIfCancellationRequested();
if (sym.SymbolType is
// TODO: Can we support more here?
if (symbol.IsDeclaration &&
symbol.SymbolType is
SymbolType.Function or
SymbolType.Class or
SymbolType.Enum)
Expand All @@ -75,7 +77,7 @@ SymbolType.Class or
Uri = scriptFile.DocumentUri,
ProviderId = nameof(ReferencesCodeLensProvider)
}, LspSerializer.Instance.JsonSerializer),
Range = sym.ScriptRegion.ToRange(),
Range = symbol.NameRegion.ToRange(),
});
}
}
Expand Down Expand Up @@ -123,6 +125,7 @@ await _symbolsService.ScanForReferencesOfSymbol(
await Task.Yield();
cancellationToken.ThrowIfCancellationRequested();

// We only show lenses on declarations, so we exclude those from the references.
if (foundReference.IsDeclaration)
{
continue;
Expand All @@ -140,7 +143,7 @@ await _symbolsService.ScanForReferencesOfSymbol(
acc.Add(new Location
{
Uri = uri,
Range = foundReference.ScriptRegion.ToRange()
Range = foundReference.NameRegion.ToRange()
});
}
referenceLocations = acc.ToArray();
Expand Down
73 changes: 59 additions & 14 deletions src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ private void AddReference(SymbolType type, string name, IScriptExtent nameExtent
});
}

// TODO: Should we move this to AstOperations.cs? It is highly coupled to `ReferenceTable`,
// perhaps it doesn't have to be.
// TODO: Reconstruct this to take an action lambda that returns a visit action and accepts a
// symbol. Then ReferenceTable can add a reference and find symbol can just stop.
//
// TODO: Have a symbol name and a separate display name, the first minimally the text so the
// buckets work, the second usually a more complete signature for e.g. outline view.
private sealed class ReferenceVisitor : AstVisitor2
{
private readonly ReferenceTable _references;
Expand All @@ -115,8 +118,7 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
SymbolType.Function,
CommandHelpers.StripModuleQualification(commandName, out _),
commandAst.CommandElements[0].Extent,
commandAst.Extent
);
commandAst.Extent);

return AstVisitAction.Continue;
}
Expand Down Expand Up @@ -151,7 +153,8 @@ public override AstVisitAction VisitCommandParameter(CommandParameterAst command
SymbolType.Parameter,
commandParameterAst.Extent.Text,
commandParameterAst.Extent,
commandParameterAst.Extent);
commandParameterAst.Extent,
isDeclaration: true);

return AstVisitAction.Continue;
}
Expand All @@ -164,8 +167,8 @@ public override AstVisitAction VisitVariableExpression(VariableExpressionAst var
SymbolType.Variable,
$"${variableExpressionAst.VariablePath.UserPath}",
variableExpressionAst.Extent,
variableExpressionAst.Extent
);
variableExpressionAst.Extent, // TODO: Maybe parent?
isDeclaration: variableExpressionAst.Parent is AssignmentStatementAst or ParameterAst);

return AstVisitAction.Continue;
}
Expand All @@ -181,7 +184,8 @@ public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinit
symbolType,
typeDefinitionAst.Name,
nameExtent,
typeDefinitionAst.Extent);
typeDefinitionAst.Extent,
isDeclaration: true);

return AstVisitAction.Continue;
}
Expand Down Expand Up @@ -214,12 +218,17 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem
? SymbolType.Constructor
: SymbolType.Method;

IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst, true, false);
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(
functionMemberAst,
useQualifiedName: false,
includeReturnType: false);

_references.AddReference(
symbolType,
nameExtent.Text,
functionMemberAst.Name, // We bucket all the overloads.
nameExtent,
functionMemberAst.Extent);
functionMemberAst.Extent,
isDeclaration: true);

return AstVisitAction.Continue;
}
Expand All @@ -230,12 +239,47 @@ public override AstVisitAction VisitPropertyMember(PropertyMemberAst propertyMem
propertyMemberAst.Parent is TypeDefinitionAst typeAst && typeAst.IsEnum
? SymbolType.EnumMember : SymbolType.Property;

IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst, false);
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst, false, false);
_references.AddReference(
symbolType,
nameExtent.Text,
nameExtent,
propertyMemberAst.Extent);
propertyMemberAst.Extent,
isDeclaration: true);

return AstVisitAction.Continue;
}

public override AstVisitAction VisitMemberExpression(MemberExpressionAst memberExpressionAst)
{
string? memberName = memberExpressionAst.Member is StringConstantExpressionAst stringConstant ? stringConstant.Value : null;
if (string.IsNullOrEmpty(memberName))
{
return AstVisitAction.Continue;
}

_references.AddReference(
SymbolType.Property,
memberName,
memberExpressionAst.Member.Extent,
memberExpressionAst.Extent);

return AstVisitAction.Continue;
}

public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressionAst methodCallAst)
{
string? memberName = methodCallAst.Member is StringConstantExpressionAst stringConstant ? stringConstant.Value : null;
if (string.IsNullOrEmpty(memberName))
{
return AstVisitAction.Continue;
}

_references.AddReference(
SymbolType.Method,
memberName,
methodCallAst.Member.Extent,
methodCallAst.Extent);

return AstVisitAction.Continue;
}
Expand All @@ -247,7 +291,8 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit
SymbolType.Configuration,
nameExtent.Text,
nameExtent,
configurationDefinitionAst.Extent);
configurationDefinitionAst.Extent,
isDeclaration: true);

return AstVisitAction.Continue;
}
Expand Down
Loading

0 comments on commit 604d6a7

Please sign in to comment.