From a4f8b8bd84f21e4dcd17499657534c2f9d109aa6 Mon Sep 17 00:00:00 2001 From: Andy Jordan Date: Tue, 10 Jan 2023 17:33:21 -0800 Subject: [PATCH] Deduplicate `GetSymbolKind`, `GetDecoratedSymbolName`, and `GetRangeFromScriptRegion` At least I'm pretty sure these were unnecessary duplicates. --- .../Services/Symbols/SymbolType.cs | 44 ++++++++++++- .../Handlers/DefinitionHandler.cs | 19 +----- .../Handlers/DocumentSymbolHandler.cs | 59 +----------------- .../TextDocument/Handlers/HoverHandler.cs | 19 +----- .../Handlers/ReferencesHandler.cs | 19 +----- .../Services/TextDocument/ScriptRegion.cs | 17 +++++ .../Handlers/WorkspaceSymbolsHandler.cs | 62 ++----------------- 7 files changed, 70 insertions(+), 169 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs b/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs index 7f5850f45..c9a113a25 100644 --- a/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs +++ b/src/PowerShellEditorServices/Services/Symbols/SymbolType.cs @@ -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 { /// @@ -14,7 +16,7 @@ internal enum SymbolType Unknown = 0, /// - /// The symbol is a vairable + /// The symbol is a variable /// Variable, @@ -78,4 +80,44 @@ internal enum SymbolType /// 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; + } + } } diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DefinitionHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DefinitionHandler.cs index 8b9c2fc60..95dba3c5a 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DefinitionHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DefinitionHandler.cs @@ -57,29 +57,12 @@ public override async Task 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 - } - }; - } } } diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs index 04dfa7b8e..d54f7232b 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs @@ -62,13 +62,13 @@ public override Task 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() @@ -123,58 +123,5 @@ protected IEnumerable InvokeProviders( 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 - } - }; - } } } diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs index cf90c6565..02264d469 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs @@ -67,7 +67,7 @@ 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 { @@ -75,22 +75,5 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync( 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 - } - }; - } } } diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs index 882b7fe4b..73bf491bf 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs @@ -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 - } - }; - } } } diff --git a/src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs b/src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs index c4d407105..5973f59b1 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs @@ -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 diff --git a/src/PowerShellEditorServices/Services/Workspace/Handlers/WorkspaceSymbolsHandler.cs b/src/PowerShellEditorServices/Services/Workspace/Handlers/WorkspaceSymbolsHandler.cs index 3fa830f99..8ee6afbe8 100644 --- a/src/PowerShellEditorServices/Services/Workspace/Handlers/WorkspaceSymbolsHandler.cs +++ b/src/PowerShellEditorServices/Services/Workspace/Handlers/WorkspaceSymbolsHandler.cs @@ -38,9 +38,7 @@ public override async Task> Handle(WorkspaceSymbolP foreach (ScriptFile scriptFile in _workspaceService.GetOpenedFiles()) { - List foundSymbols = - _symbolsService.FindSymbolsInFile( - scriptFile); + List 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); @@ -66,15 +64,15 @@ public override async Task> 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) }); } } @@ -86,58 +84,6 @@ public override async Task> 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 } }