Skip to content

Commit

Permalink
Deduplicate GetSymbolKind, GetDecoratedSymbolName, and `GetRangeF…
Browse files Browse the repository at this point in the history
…romScriptRegion`

At least I'm pretty sure these were unnecessary duplicates.
  • Loading branch information
andyleejordan committed Jan 11, 2023
1 parent 8b2d1b2 commit a4f8b8b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 169 deletions.
44 changes: 43 additions & 1 deletion src/PowerShellEditorServices/Services/Symbols/SymbolType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using OmniSharp.Extensions.LanguageServer.Protocol.Models;

namespace Microsoft.PowerShell.EditorServices.Services.Symbols
{
/// <summary>
Expand All @@ -14,7 +16,7 @@ internal enum SymbolType
Unknown = 0,

/// <summary>
/// The symbol is a vairable
/// The symbol is a variable
/// </summary>
Variable,

Expand Down Expand Up @@ -78,4 +80,44 @@ internal enum SymbolType
/// </summary>
Type,
}

internal static class SymbolTypeUtils
{
internal static SymbolKind GetSymbolKind(SymbolType symbolType)
{
return symbolType switch
{
SymbolType.Function or SymbolType.Configuration or SymbolType.Workflow => SymbolKind.Function,
SymbolType.Enum => SymbolKind.Enum,
SymbolType.Class => SymbolKind.Class,
SymbolType.Constructor => SymbolKind.Constructor,
SymbolType.Method => SymbolKind.Method,
SymbolType.Property => SymbolKind.Property,
SymbolType.EnumMember => SymbolKind.EnumMember,
// TODO: More delicately handle the other symbol types.
_ => SymbolKind.Variable,
};
}

internal static string GetDecoratedSymbolName(SymbolReference symbolReference)
{
string name = symbolReference.SymbolName;

// Append { } for symbols with scriptblock
// Constructors and Methods have overloaded names already
if (symbolReference.SymbolType is
SymbolType.Function or
SymbolType.Enum or
SymbolType.Class or
SymbolType.Constructor or
SymbolType.Method or
SymbolType.Configuration or
SymbolType.Workflow)
{
name += " { }";
}

return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,12 @@ public override async Task<LocationOrLocationLinks> Handle(DefinitionParams requ
new Location
{
Uri = DocumentUri.From(foundDefinition.FilePath),
Range = GetRangeFromScriptRegion(foundDefinition.ScriptRegion)
Range = ScriptRegion.GetRangeFromScriptRegion(foundDefinition.ScriptRegion)
}));
}
}

return new LocationOrLocationLinks(definitionLocations);
}

private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
{
return new Range
{
Start = new Position
{
Line = scriptRegion.StartLineNumber - 1,
Character = scriptRegion.StartColumnNumber - 1
},
End = new Position
{
Line = scriptRegion.EndLineNumber - 1,
Character = scriptRegion.EndColumnNumber - 1
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public override Task<SymbolInformationOrDocumentSymbolContainer> Handle(Document
return new SymbolInformationOrDocumentSymbol(new SymbolInformation
{
ContainerName = containerName,
Kind = GetSymbolKind(r.SymbolType),
Kind = SymbolTypeUtils.GetSymbolKind(r.SymbolType),
Location = new Location
{
Uri = DocumentUri.From(r.FilePath),
Range = GetRangeFromScriptRegion(r.ScriptRegion)
Range = ScriptRegion.GetRangeFromScriptRegion(r.ScriptRegion)
},
Name = GetDecoratedSymbolName(r)
Name = SymbolTypeUtils.GetDecoratedSymbolName(r)
});
})
.ToArray()
Expand Down Expand Up @@ -123,58 +123,5 @@ protected IEnumerable<TResult> InvokeProviders<TResult>(

return providerResults;
}

private static SymbolKind GetSymbolKind(SymbolType symbolType)
{
return symbolType switch
{
SymbolType.Function or SymbolType.Configuration or SymbolType.Workflow => SymbolKind.Function,
SymbolType.Enum => SymbolKind.Enum,
SymbolType.Class => SymbolKind.Class,
SymbolType.Constructor => SymbolKind.Constructor,
SymbolType.Method => SymbolKind.Method,
SymbolType.Property => SymbolKind.Property,
SymbolType.EnumMember => SymbolKind.EnumMember,
_ => SymbolKind.Variable,
};
}

private static string GetDecoratedSymbolName(SymbolReference symbolReference)
{
string name = symbolReference.SymbolName;

// Append { } for symbols with scriptblock
// Constructors and Methods have overloaded names already
if (symbolReference.SymbolType is
SymbolType.Function or
SymbolType.Enum or
SymbolType.Class or
SymbolType.Constructor or
SymbolType.Method or
SymbolType.Configuration or
SymbolType.Workflow)
{
name += " { }";
}

return name;
}

private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
{
return new Range
{
Start = new Position
{
Line = scriptRegion.StartLineNumber - 1,
Character = scriptRegion.StartColumnNumber - 1
},
End = new Position
{
Line = scriptRegion.EndLineNumber - 1,
Character = scriptRegion.EndColumnNumber - 1
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,13 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync(
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));
}

Range symbolRange = GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);
Range symbolRange = ScriptRegion.GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);

return new Hover
{
Contents = new MarkedStringsOrMarkupContent(symbolInfo),
Range = symbolRange
};
}

private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
{
return new Range
{
Start = new Position
{
Line = scriptRegion.StartLineNumber - 1,
Character = scriptRegion.StartColumnNumber - 1
},
End = new Position
{
Line = scriptRegion.EndLineNumber - 1,
Character = scriptRegion.EndColumnNumber - 1
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,12 @@ await _symbolsService.ScanForReferencesOfSymbol(
locations.Add(new Location
{
Uri = DocumentUri.From(foundReference.FilePath),
Range = GetRangeFromScriptRegion(foundReference.ScriptRegion)
Range = ScriptRegion.GetRangeFromScriptRegion(foundReference.ScriptRegion)
});
}
}

return new LocationContainer(locations);
}

private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
{
return new Range
{
Start = new Position
{
Line = scriptRegion.StartLineNumber - 1,
Character = scriptRegion.StartColumnNumber - 1
},
End = new Position
{
Line = scriptRegion.EndLineNumber - 1,
Character = scriptRegion.EndColumnNumber - 1
}
};
}
}
}
17 changes: 17 additions & 0 deletions src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ internal static TextEdit ToTextEdit(ScriptRegion scriptRegion)
};
}

internal static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
{
return new Range
{
Start = new Position
{
Line = scriptRegion.StartLineNumber - 1,
Character = scriptRegion.StartColumnNumber - 1
},
End = new Position
{
Line = scriptRegion.EndLineNumber - 1,
Character = scriptRegion.EndColumnNumber - 1
}
};
}

#endregion

#region Constructors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public override async Task<Container<SymbolInformation>> Handle(WorkspaceSymbolP

foreach (ScriptFile scriptFile in _workspaceService.GetOpenedFiles())
{
List<SymbolReference> foundSymbols =
_symbolsService.FindSymbolsInFile(
scriptFile);
List<SymbolReference> foundSymbols = _symbolsService.FindSymbolsInFile(scriptFile);

// TODO: Need to compute a relative path that is based on common path for all workspace files
string containerName = Path.GetFileNameWithoutExtension(scriptFile.FilePath);
Expand All @@ -66,15 +64,15 @@ public override async Task<Container<SymbolInformation>> Handle(WorkspaceSymbolP
Location location = new()
{
Uri = DocumentUri.From(foundOccurrence.FilePath),
Range = GetRangeFromScriptRegion(foundOccurrence.ScriptRegion)
Range = ScriptRegion.GetRangeFromScriptRegion(foundOccurrence.ScriptRegion)
};

symbols.Add(new SymbolInformation
{
ContainerName = containerName,
Kind = GetSymbolKind(foundOccurrence.SymbolType),
Kind = SymbolTypeUtils.GetSymbolKind(foundOccurrence.SymbolType),
Location = location,
Name = GetDecoratedSymbolName(foundOccurrence)
Name = SymbolTypeUtils.GetDecoratedSymbolName(foundOccurrence)
});
}
}
Expand All @@ -86,58 +84,6 @@ public override async Task<Container<SymbolInformation>> Handle(WorkspaceSymbolP

private static bool IsQueryMatch(string query, string symbolName) => symbolName.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0;

private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
{
return new Range
{
Start = new Position
{
Line = scriptRegion.StartLineNumber - 1,
Character = scriptRegion.StartColumnNumber - 1
},
End = new Position
{
Line = scriptRegion.EndLineNumber - 1,
Character = scriptRegion.EndColumnNumber - 1
}
};
}

private static string GetDecoratedSymbolName(SymbolReference symbolReference)
{
string name = symbolReference.SymbolName;

// Append { } for symbols with scriptblock
// Constructors and Methods have overloaded names already
if (symbolReference.SymbolType is
SymbolType.Function or
SymbolType.Enum or
SymbolType.Class or
SymbolType.Constructor or
SymbolType.Method or
SymbolType.Configuration or
SymbolType.Workflow)
{
name += " { }";
}

return name;
}

private static SymbolKind GetSymbolKind(SymbolType symbolType)
{
return symbolType switch
{
SymbolType.Function or SymbolType.Configuration or SymbolType.Workflow => SymbolKind.Function,
SymbolType.Enum => SymbolKind.Enum,
SymbolType.Class => SymbolKind.Class,
SymbolType.Constructor => SymbolKind.Constructor,
SymbolType.Method => SymbolKind.Method,
SymbolType.Property => SymbolKind.Property,
_ => SymbolKind.Variable,
};
}

#endregion
}
}

0 comments on commit a4f8b8b

Please sign in to comment.