Skip to content

Commit a4f8b8b

Browse files
committed
Deduplicate GetSymbolKind, GetDecoratedSymbolName, and GetRangeFromScriptRegion
At least I'm pretty sure these were unnecessary duplicates.
1 parent 8b2d1b2 commit a4f8b8b

File tree

7 files changed

+70
-169
lines changed

7 files changed

+70
-169
lines changed

src/PowerShellEditorServices/Services/Symbols/SymbolType.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
5+
46
namespace Microsoft.PowerShell.EditorServices.Services.Symbols
57
{
68
/// <summary>
@@ -14,7 +16,7 @@ internal enum SymbolType
1416
Unknown = 0,
1517

1618
/// <summary>
17-
/// The symbol is a vairable
19+
/// The symbol is a variable
1820
/// </summary>
1921
Variable,
2022

@@ -78,4 +80,44 @@ internal enum SymbolType
7880
/// </summary>
7981
Type,
8082
}
83+
84+
internal static class SymbolTypeUtils
85+
{
86+
internal static SymbolKind GetSymbolKind(SymbolType symbolType)
87+
{
88+
return symbolType switch
89+
{
90+
SymbolType.Function or SymbolType.Configuration or SymbolType.Workflow => SymbolKind.Function,
91+
SymbolType.Enum => SymbolKind.Enum,
92+
SymbolType.Class => SymbolKind.Class,
93+
SymbolType.Constructor => SymbolKind.Constructor,
94+
SymbolType.Method => SymbolKind.Method,
95+
SymbolType.Property => SymbolKind.Property,
96+
SymbolType.EnumMember => SymbolKind.EnumMember,
97+
// TODO: More delicately handle the other symbol types.
98+
_ => SymbolKind.Variable,
99+
};
100+
}
101+
102+
internal static string GetDecoratedSymbolName(SymbolReference symbolReference)
103+
{
104+
string name = symbolReference.SymbolName;
105+
106+
// Append { } for symbols with scriptblock
107+
// Constructors and Methods have overloaded names already
108+
if (symbolReference.SymbolType is
109+
SymbolType.Function or
110+
SymbolType.Enum or
111+
SymbolType.Class or
112+
SymbolType.Constructor or
113+
SymbolType.Method or
114+
SymbolType.Configuration or
115+
SymbolType.Workflow)
116+
{
117+
name += " { }";
118+
}
119+
120+
return name;
121+
}
122+
}
81123
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/DefinitionHandler.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,12 @@ public override async Task<LocationOrLocationLinks> Handle(DefinitionParams requ
5757
new Location
5858
{
5959
Uri = DocumentUri.From(foundDefinition.FilePath),
60-
Range = GetRangeFromScriptRegion(foundDefinition.ScriptRegion)
60+
Range = ScriptRegion.GetRangeFromScriptRegion(foundDefinition.ScriptRegion)
6161
}));
6262
}
6363
}
6464

6565
return new LocationOrLocationLinks(definitionLocations);
6666
}
67-
68-
private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
69-
{
70-
return new Range
71-
{
72-
Start = new Position
73-
{
74-
Line = scriptRegion.StartLineNumber - 1,
75-
Character = scriptRegion.StartColumnNumber - 1
76-
},
77-
End = new Position
78-
{
79-
Line = scriptRegion.EndLineNumber - 1,
80-
Character = scriptRegion.EndColumnNumber - 1
81-
}
82-
};
83-
}
8467
}
8568
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/DocumentSymbolHandler.cs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ public override Task<SymbolInformationOrDocumentSymbolContainer> Handle(Document
6262
return new SymbolInformationOrDocumentSymbol(new SymbolInformation
6363
{
6464
ContainerName = containerName,
65-
Kind = GetSymbolKind(r.SymbolType),
65+
Kind = SymbolTypeUtils.GetSymbolKind(r.SymbolType),
6666
Location = new Location
6767
{
6868
Uri = DocumentUri.From(r.FilePath),
69-
Range = GetRangeFromScriptRegion(r.ScriptRegion)
69+
Range = ScriptRegion.GetRangeFromScriptRegion(r.ScriptRegion)
7070
},
71-
Name = GetDecoratedSymbolName(r)
71+
Name = SymbolTypeUtils.GetDecoratedSymbolName(r)
7272
});
7373
})
7474
.ToArray()
@@ -123,58 +123,5 @@ protected IEnumerable<TResult> InvokeProviders<TResult>(
123123

124124
return providerResults;
125125
}
126-
127-
private static SymbolKind GetSymbolKind(SymbolType symbolType)
128-
{
129-
return symbolType switch
130-
{
131-
SymbolType.Function or SymbolType.Configuration or SymbolType.Workflow => SymbolKind.Function,
132-
SymbolType.Enum => SymbolKind.Enum,
133-
SymbolType.Class => SymbolKind.Class,
134-
SymbolType.Constructor => SymbolKind.Constructor,
135-
SymbolType.Method => SymbolKind.Method,
136-
SymbolType.Property => SymbolKind.Property,
137-
SymbolType.EnumMember => SymbolKind.EnumMember,
138-
_ => SymbolKind.Variable,
139-
};
140-
}
141-
142-
private static string GetDecoratedSymbolName(SymbolReference symbolReference)
143-
{
144-
string name = symbolReference.SymbolName;
145-
146-
// Append { } for symbols with scriptblock
147-
// Constructors and Methods have overloaded names already
148-
if (symbolReference.SymbolType is
149-
SymbolType.Function or
150-
SymbolType.Enum or
151-
SymbolType.Class or
152-
SymbolType.Constructor or
153-
SymbolType.Method or
154-
SymbolType.Configuration or
155-
SymbolType.Workflow)
156-
{
157-
name += " { }";
158-
}
159-
160-
return name;
161-
}
162-
163-
private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
164-
{
165-
return new Range
166-
{
167-
Start = new Position
168-
{
169-
Line = scriptRegion.StartLineNumber - 1,
170-
Character = scriptRegion.StartColumnNumber - 1
171-
},
172-
End = new Position
173-
{
174-
Line = scriptRegion.EndLineNumber - 1,
175-
Character = scriptRegion.EndColumnNumber - 1
176-
}
177-
};
178-
}
179126
}
180127
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,13 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync(
6767
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));
6868
}
6969

70-
Range symbolRange = GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);
70+
Range symbolRange = ScriptRegion.GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);
7171

7272
return new Hover
7373
{
7474
Contents = new MarkedStringsOrMarkupContent(symbolInfo),
7575
Range = symbolRange
7676
};
7777
}
78-
79-
private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
80-
{
81-
return new Range
82-
{
83-
Start = new Position
84-
{
85-
Line = scriptRegion.StartLineNumber - 1,
86-
Character = scriptRegion.StartColumnNumber - 1
87-
},
88-
End = new Position
89-
{
90-
Line = scriptRegion.EndLineNumber - 1,
91-
Character = scriptRegion.EndColumnNumber - 1
92-
}
93-
};
94-
}
9578
}
9679
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/ReferencesHandler.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,12 @@ await _symbolsService.ScanForReferencesOfSymbol(
5656
locations.Add(new Location
5757
{
5858
Uri = DocumentUri.From(foundReference.FilePath),
59-
Range = GetRangeFromScriptRegion(foundReference.ScriptRegion)
59+
Range = ScriptRegion.GetRangeFromScriptRegion(foundReference.ScriptRegion)
6060
});
6161
}
6262
}
6363

6464
return new LocationContainer(locations);
6565
}
66-
67-
private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
68-
{
69-
return new Range
70-
{
71-
Start = new Position
72-
{
73-
Line = scriptRegion.StartLineNumber - 1,
74-
Character = scriptRegion.StartColumnNumber - 1
75-
},
76-
End = new Position
77-
{
78-
Line = scriptRegion.EndLineNumber - 1,
79-
Character = scriptRegion.EndColumnNumber - 1
80-
}
81-
};
82-
}
8366
}
8467
}

src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ internal static TextEdit ToTextEdit(ScriptRegion scriptRegion)
6969
};
7070
}
7171

72+
internal static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
73+
{
74+
return new Range
75+
{
76+
Start = new Position
77+
{
78+
Line = scriptRegion.StartLineNumber - 1,
79+
Character = scriptRegion.StartColumnNumber - 1
80+
},
81+
End = new Position
82+
{
83+
Line = scriptRegion.EndLineNumber - 1,
84+
Character = scriptRegion.EndColumnNumber - 1
85+
}
86+
};
87+
}
88+
7289
#endregion
7390

7491
#region Constructors

src/PowerShellEditorServices/Services/Workspace/Handlers/WorkspaceSymbolsHandler.cs

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public override async Task<Container<SymbolInformation>> Handle(WorkspaceSymbolP
3838

3939
foreach (ScriptFile scriptFile in _workspaceService.GetOpenedFiles())
4040
{
41-
List<SymbolReference> foundSymbols =
42-
_symbolsService.FindSymbolsInFile(
43-
scriptFile);
41+
List<SymbolReference> foundSymbols = _symbolsService.FindSymbolsInFile(scriptFile);
4442

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

7270
symbols.Add(new SymbolInformation
7371
{
7472
ContainerName = containerName,
75-
Kind = GetSymbolKind(foundOccurrence.SymbolType),
73+
Kind = SymbolTypeUtils.GetSymbolKind(foundOccurrence.SymbolType),
7674
Location = location,
77-
Name = GetDecoratedSymbolName(foundOccurrence)
75+
Name = SymbolTypeUtils.GetDecoratedSymbolName(foundOccurrence)
7876
});
7977
}
8078
}
@@ -86,58 +84,6 @@ public override async Task<Container<SymbolInformation>> Handle(WorkspaceSymbolP
8684

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

89-
private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion)
90-
{
91-
return new Range
92-
{
93-
Start = new Position
94-
{
95-
Line = scriptRegion.StartLineNumber - 1,
96-
Character = scriptRegion.StartColumnNumber - 1
97-
},
98-
End = new Position
99-
{
100-
Line = scriptRegion.EndLineNumber - 1,
101-
Character = scriptRegion.EndColumnNumber - 1
102-
}
103-
};
104-
}
105-
106-
private static string GetDecoratedSymbolName(SymbolReference symbolReference)
107-
{
108-
string name = symbolReference.SymbolName;
109-
110-
// Append { } for symbols with scriptblock
111-
// Constructors and Methods have overloaded names already
112-
if (symbolReference.SymbolType is
113-
SymbolType.Function or
114-
SymbolType.Enum or
115-
SymbolType.Class or
116-
SymbolType.Constructor or
117-
SymbolType.Method or
118-
SymbolType.Configuration or
119-
SymbolType.Workflow)
120-
{
121-
name += " { }";
122-
}
123-
124-
return name;
125-
}
126-
127-
private static SymbolKind GetSymbolKind(SymbolType symbolType)
128-
{
129-
return symbolType switch
130-
{
131-
SymbolType.Function or SymbolType.Configuration or SymbolType.Workflow => SymbolKind.Function,
132-
SymbolType.Enum => SymbolKind.Enum,
133-
SymbolType.Class => SymbolKind.Class,
134-
SymbolType.Constructor => SymbolKind.Constructor,
135-
SymbolType.Method => SymbolKind.Method,
136-
SymbolType.Property => SymbolKind.Property,
137-
_ => SymbolKind.Variable,
138-
};
139-
}
140-
14187
#endregion
14288
}
14389
}

0 commit comments

Comments
 (0)