Skip to content

Commit

Permalink
Fix outline view (filter out EmptyScriptExtent)
Browse files Browse the repository at this point in the history
And also prepare `SymbolReference` for eventual support of `DocumentSymbol`.
  • Loading branch information
andyleejordan committed Jan 20, 2023
1 parent e7d3afe commit 82ec11f
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 103 deletions.
47 changes: 37 additions & 10 deletions src/PowerShellEditorServices/Services/Symbols/ReferenceTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,20 @@ internal void EnsureInitialized()
_parent.ScriptAst.Visit(new ReferenceVisitor(this));
}

private void AddReference(SymbolType type, string name, IScriptExtent extent, bool isDeclaration = false)
private static bool ExtentIsEmpty(IScriptExtent e) => string.IsNullOrEmpty(e.File) &&
e.StartLineNumber == 0 && e.StartColumnNumber == 0 &&
e.EndLineNumber == 0 && e.EndColumnNumber == 0 &&
string.IsNullOrEmpty(e.Text);

private void AddReference(SymbolType type, string name, IScriptExtent nameExtent, IScriptExtent extent, bool isDeclaration = false)
{
SymbolReference symbol = new(type, name, extent, _parent, isDeclaration);
// We have to exclude implicit things like `$this` that don't actually exist.
if (ExtentIsEmpty(extent))
{
return;
}

SymbolReference symbol = new(type, name, nameExtent, extent, _parent, isDeclaration);
_symbolReferences.AddOrUpdate(
name,
_ => new ConcurrentBag<SymbolReference> { symbol },
Expand Down Expand Up @@ -103,7 +114,8 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
_references.AddReference(
SymbolType.Function,
CommandHelpers.StripModuleQualification(commandName, out _),
commandAst.CommandElements[0].Extent
commandAst.CommandElements[0].Extent,
commandAst.Extent
);

return AstVisitAction.Continue;
Expand Down Expand Up @@ -131,6 +143,7 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
symbolType,
functionDefinitionAst.Name,
nameExtent,
functionDefinitionAst.Extent,
isDeclaration: true);

return AstVisitAction.Continue;
Expand All @@ -141,6 +154,7 @@ public override AstVisitAction VisitCommandParameter(CommandParameterAst command
_references.AddReference(
SymbolType.Parameter,
commandParameterAst.Extent.Text,
commandParameterAst.Extent,
commandParameterAst.Extent);

return AstVisitAction.Continue;
Expand All @@ -153,6 +167,7 @@ public override AstVisitAction VisitVariableExpression(VariableExpressionAst var
_references.AddReference(
SymbolType.Variable,
$"${variableExpressionAst.VariablePath.UserPath}",
variableExpressionAst.Extent,
variableExpressionAst.Extent
);

Expand All @@ -166,7 +181,11 @@ public override AstVisitAction VisitTypeDefinition(TypeDefinitionAst typeDefinit
: SymbolType.Class;

IScriptExtent nameExtent = VisitorUtils.GetNameExtent(typeDefinitionAst);
_references.AddReference(symbolType, typeDefinitionAst.Name, nameExtent);
_references.AddReference(
symbolType,
typeDefinitionAst.Name,
nameExtent,
typeDefinitionAst.Extent);

return AstVisitAction.Continue;
}
Expand All @@ -176,14 +195,19 @@ public override AstVisitAction VisitTypeExpression(TypeExpressionAst typeExpress
_references.AddReference(
SymbolType.Type,
typeExpressionAst.TypeName.Name,
typeExpressionAst.Extent,
typeExpressionAst.Extent);

return AstVisitAction.Continue;
}

public override AstVisitAction VisitTypeConstraint(TypeConstraintAst typeConstraintAst)
{
_references.AddReference(SymbolType.Type, typeConstraintAst.TypeName.Name, typeConstraintAst.Extent);
_references.AddReference(
SymbolType.Type,
typeConstraintAst.TypeName.Name,
typeConstraintAst.Extent,
typeConstraintAst.Extent);

return AstVisitAction.Continue;
}
Expand All @@ -197,8 +221,9 @@ public override AstVisitAction VisitFunctionMember(FunctionMemberAst functionMem
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionMemberAst, true, false);
_references.AddReference(
symbolType,
VisitorUtils.GetMemberOverloadName(functionMemberAst, true, false),
nameExtent);
nameExtent.Text,
nameExtent,
functionMemberAst.Extent);

return AstVisitAction.Continue;
}
Expand All @@ -212,8 +237,9 @@ propertyMemberAst.Parent is TypeDefinitionAst typeAst && typeAst.IsEnum
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(propertyMemberAst, false);
_references.AddReference(
symbolType,
VisitorUtils.GetMemberOverloadName(propertyMemberAst, false),
nameExtent);
nameExtent.Text,
nameExtent,
propertyMemberAst.Extent);

return AstVisitAction.Continue;
}
Expand All @@ -224,7 +250,8 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit
_references.AddReference(
SymbolType.Configuration,
nameExtent.Text,
nameExtent);
nameExtent,
configurationDefinitionAst.Extent);

return AstVisitAction.Continue;
}
Expand Down
2 changes: 2 additions & 0 deletions src/PowerShellEditorServices/Services/Symbols/ScriptExtent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public int EndOffset
set;
}

public override string ToString() => Text;

/// <summary>
/// Gets the ending script position of the extent.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal record SymbolReference

public string SymbolName { get; }

public ScriptRegion NameRegion { get; }

public ScriptRegion ScriptRegion { get; }

public string SourceLine { get; internal set; }
Expand Down Expand Up @@ -48,6 +50,7 @@ public SymbolReference(
SymbolType = symbolType;
SymbolName = symbolName;
ScriptRegion = new(scriptExtent);
NameRegion = ScriptRegion;
FilePath = filePath;
SourceLine = sourceLine;
IsDeclaration = isDeclaration;
Expand All @@ -56,12 +59,14 @@ public SymbolReference(
public SymbolReference(
SymbolType symbolType,
string symbolName,
IScriptExtent nameExtent,
IScriptExtent scriptExtent,
ScriptFile file,
bool isDeclaration)
{
SymbolType = symbolType;
SymbolName = symbolName;
NameRegion = new(nameExtent);
ScriptRegion = new(scriptExtent);
FilePath = file.FilePath;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ public async Task<SymbolReference> GetDefinitionOfSymbolAsync(
SymbolReference foundDefinition = null;
foreach (ScriptFile scriptFile in referencedFiles)
{
// TODO: This needs to just search the file's references (filtered to declarations);
foundDefinition = AstOperations.FindDefinitionOfSymbol(scriptFile.ScriptAst, foundSymbol);

filesSearched.Add(scriptFile.FilePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override Task<DocumentHighlightContainer> Handle(
highlights.Add(new DocumentHighlight
{
Kind = DocumentHighlightKind.Write, // TODO: Which symbol types are writable?
Range = occurrence.ScriptRegion.ToRange()
Range = occurrence.NameRegion.ToRange() // Just the symbol name
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public override async Task<SymbolInformationOrDocumentSymbolContainer> Handle(Do
}

string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath);

List<SymbolInformationOrDocumentSymbol> symbols = new();
foreach (SymbolReference r in foundSymbols)
{
Expand All @@ -63,15 +64,29 @@ public override async Task<SymbolInformationOrDocumentSymbolContainer> Handle(Do
await Task.Yield();
cancellationToken.ThrowIfCancellationRequested();

if (r.SymbolType is SymbolType.Type)
{
continue;
}

// TODO: This should be a DocumentSymbol now as SymbolInformation is deprecated.
// But this requires figuring out how to populate `children`.
//
// symbols.Add(new SymbolInformationOrDocumentSymbol(new DocumentSymbol
// {
// Name = SymbolTypeUtils.GetDecoratedSymbolName(r),
// Kind = SymbolTypeUtils.GetSymbolKind(r.SymbolType),
// Range = r.ScriptRegion.ToRange(),
// SelectionRange = r.NameRegion.ToRange()
// }));
symbols.Add(new SymbolInformationOrDocumentSymbol(new SymbolInformation
{
ContainerName = containerName,
Kind = SymbolTypeUtils.GetSymbolKind(r.SymbolType),
Location = new Location
{
Uri = DocumentUri.From(r.FilePath),
Range = r.ScriptRegion.ToRange()
Range = r.ScriptRegion.ToRange() // The whole thing, not just the name.
},
Name = SymbolTypeUtils.GetDecoratedSymbolName(r)
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public Range ToRange()
};
}

public override string ToString() => $"Start {StartLineNumber}:{StartColumnNumber}, End {EndLineNumber}:{EndColumnNumber}";

#region Constructors

public ScriptRegion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public override async Task<Container<SymbolInformation>> Handle(WorkspaceSymbolP
// so it's helpful to add some yields.
await Task.Yield();
cancellationToken.ThrowIfCancellationRequested();

if (foundOccurrence.SymbolType is SymbolType.Type)
{
continue;
}

if (!IsQueryMatch(request.Query, foundOccurrence.SymbolName))
{
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function My-Function ($myInput)
{
My-Function $myInput
My-Function $myInput
}

$things = 4
Expand Down
Loading

0 comments on commit 82ec11f

Please sign in to comment.