Skip to content

Commit 261360c

Browse files
committed
More simplification and dead code elimination
1 parent 193405a commit 261360c

13 files changed

+50
-203
lines changed

src/PowerShellEditorServices/Services/CodeLens/PesterCodeLensProvider.cs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.PowerShell.EditorServices.Services;
88
using Microsoft.PowerShell.EditorServices.Services.Symbols;
99
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
10-
using Microsoft.PowerShell.EditorServices.Utility;
1110
using Newtonsoft.Json.Linq;
1211
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1312
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization;

src/PowerShellEditorServices/Services/CodeLens/ReferencesCodeLensProvider.cs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.PowerShell.EditorServices.Services;
1010
using Microsoft.PowerShell.EditorServices.Services.Symbols;
1111
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
12-
using Microsoft.PowerShell.EditorServices.Utility;
1312
using Newtonsoft.Json.Linq;
1413
using OmniSharp.Extensions.LanguageServer.Protocol;
1514
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

src/PowerShellEditorServices/Services/Symbols/PesterDocumentSymbolProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ internal enum PesterCommandType
196196
/// Provides a specialization of SymbolReference containing
197197
/// extra information about Pester test symbols.
198198
/// </summary>
199-
internal class PesterSymbolReference : SymbolReference
199+
internal record PesterSymbolReference : SymbolReference
200200
{
201201
/// <summary>
202202
/// Lookup for Pester keywords we support. Ideally we could extract these from Pester itself

src/PowerShellEditorServices/Services/Symbols/SymbolReference.cs

+5-57
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,30 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
#nullable enable
5+
46
using System.Diagnostics;
57
using System.Management.Automation.Language;
68
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
79

810
namespace Microsoft.PowerShell.EditorServices.Services.Symbols
911
{
10-
internal interface ISymbolReference
11-
{
12-
/// <summary>
13-
/// Gets the symbol's type
14-
/// </summary>
15-
SymbolType SymbolType { get; }
16-
17-
/// <summary>
18-
/// Gets the name of the symbol
19-
/// </summary>
20-
string SymbolName { get; }
21-
22-
/// <summary>
23-
/// Gets the script extent of the symbol
24-
/// </summary>
25-
ScriptRegion ScriptRegion { get; }
26-
27-
/// <summary>
28-
/// Gets the contents of the line the given symbol is on
29-
/// </summary>
30-
string SourceLine { get; }
31-
32-
/// <summary>
33-
/// Gets the path of the file in which the symbol was found.
34-
/// </summary>
35-
string FilePath { get; }
36-
}
37-
3812
/// <summary>
3913
/// A class that holds the type, name, script extent, and source line of a symbol
4014
/// </summary>
4115
[DebuggerDisplay("SymbolType = {SymbolType}, SymbolName = {SymbolName}")]
42-
internal class SymbolReference : ISymbolReference
16+
internal record SymbolReference
4317
{
44-
#region Properties
45-
46-
/// <summary>
47-
/// Gets the symbol's type
48-
/// </summary>
4918
public SymbolType SymbolType { get; }
5019

51-
/// <summary>
52-
/// Gets the name of the symbol
53-
/// </summary>
5420
public string SymbolName { get; }
5521

56-
/// <summary>
57-
/// Gets the script extent of the symbol
58-
/// </summary>
5922
public ScriptRegion ScriptRegion { get; }
6023

61-
/// <summary>
62-
/// Gets the contents of the line the given symbol is on
63-
/// </summary>
6424
public string SourceLine { get; internal set; }
6525

66-
/// <summary>
67-
/// Gets the path of the file in which the symbol was found.
68-
/// </summary>
6926
public string FilePath { get; internal set; }
7027

71-
#endregion
72-
7328
/// <summary>
7429
/// Constructs and instance of a SymbolReference
7530
/// </summary>
@@ -88,16 +43,9 @@ public SymbolReference(
8843
// TODO: Verify params
8944
SymbolType = symbolType;
9045
SymbolName = symbolName;
91-
ScriptRegion = ScriptRegion.Create(scriptExtent);
46+
ScriptRegion = new(scriptExtent);
9247
FilePath = filePath;
9348
SourceLine = sourceLine;
94-
95-
// TODO: Make sure end column number usage is correct
96-
97-
// Build the display string
98-
//this.DisplayString =
99-
// string.Format(
100-
// "{0} {1}")
10149
}
10250

10351
public SymbolReference(
@@ -108,7 +56,7 @@ public SymbolReference(
10856
{
10957
SymbolType = symbolType;
11058
SymbolName = symbolName;
111-
ScriptRegion = ScriptRegion.Create(scriptExtent);
59+
ScriptRegion = new(scriptExtent);
11260
FilePath = file.FilePath;
11361
try
11462
{

src/PowerShellEditorServices/Services/Symbols/Vistors/AstOperations.cs

-66
Original file line numberDiff line numberDiff line change
@@ -196,72 +196,6 @@ public static SymbolReference FindDefinitionOfSymbol(
196196
return declarationVisitor.FoundDeclaration;
197197
}
198198

199-
/// <summary>
200-
/// Finds all symbols in a script
201-
/// </summary>
202-
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
203-
/// <returns>A collection of SymbolReference objects</returns>
204-
public static IEnumerable<SymbolReference> FindSymbolsInDocument(Ast scriptAst)
205-
{
206-
FindSymbolsVisitor findSymbolsVisitor = new();
207-
scriptAst.Visit(findSymbolsVisitor);
208-
return findSymbolsVisitor.SymbolReferences;
209-
}
210-
211-
/// <summary>
212-
/// Checks if a given ast represents the root node of a *.psd1 file.
213-
/// </summary>
214-
/// <param name="ast">The abstract syntax tree of the given script</param>
215-
/// <returns>true if the AST represents a *.psd1 file, otherwise false</returns>
216-
public static bool IsPowerShellDataFileAst(Ast ast)
217-
{
218-
// sometimes we don't have reliable access to the filename
219-
// so we employ heuristics to check if the contents are
220-
// part of a psd1 file.
221-
return IsPowerShellDataFileAstNode(
222-
new { Item = ast, Children = new List<dynamic>() },
223-
new Type[] {
224-
typeof(ScriptBlockAst),
225-
typeof(NamedBlockAst),
226-
typeof(PipelineAst),
227-
typeof(CommandExpressionAst),
228-
typeof(HashtableAst) },
229-
0);
230-
}
231-
232-
private static bool IsPowerShellDataFileAstNode(dynamic node, Type[] levelAstMap, int level)
233-
{
234-
dynamic levelAstTypeMatch = node.Item.GetType().Equals(levelAstMap[level]);
235-
if (!levelAstTypeMatch)
236-
{
237-
return false;
238-
}
239-
240-
if (level == levelAstMap.Length - 1)
241-
{
242-
return levelAstTypeMatch;
243-
}
244-
245-
IEnumerable<Ast> astsFound = (node.Item as Ast)?.FindAll(a => a is not null, false);
246-
if (astsFound != null)
247-
{
248-
foreach (Ast astFound in astsFound)
249-
{
250-
if (!astFound.Equals(node.Item)
251-
&& node.Item.Equals(astFound.Parent)
252-
&& IsPowerShellDataFileAstNode(
253-
new { Item = astFound, Children = new List<dynamic>() },
254-
levelAstMap,
255-
level + 1))
256-
{
257-
return true;
258-
}
259-
}
260-
}
261-
262-
return false;
263-
}
264-
265199
/// <summary>
266200
/// Finds all files dot sourced in a script
267201
/// </summary>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public override async Task<CommandOrCodeActionContainer> Handle(CodeActionParams
9595
{
9696
Uri = request.TextDocument.Uri
9797
},
98-
Edits = new TextEditContainer(ScriptRegion.ToTextEdit(markerCorrection.Edit))
98+
Edits = new TextEditContainer(markerCorrection.Edit.ToTextEdit())
9999
}))
100100
}
101101
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public override async Task<LocationOrLocationLinks> Handle(DefinitionParams requ
5757
new Location
5858
{
5959
Uri = DocumentUri.From(foundDefinition.FilePath),
60-
Range = ScriptRegion.GetRangeFromScriptRegion(foundDefinition.ScriptRegion)
60+
Range = foundDefinition.ScriptRegion.ToRange()
6161
}));
6262
}
6363
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ public override Task<SymbolInformationOrDocumentSymbolContainer> Handle(Document
5959
? foundSymbols
6060
.Select(r =>
6161
{
62+
// TODO: This should be a DocumentSymbol now as SymbolInformation is deprecated.
6263
return new SymbolInformationOrDocumentSymbol(new SymbolInformation
6364
{
6465
ContainerName = containerName,
6566
Kind = SymbolTypeUtils.GetSymbolKind(r.SymbolType),
6667
Location = new Location
6768
{
6869
Uri = DocumentUri.From(r.FilePath),
69-
Range = ScriptRegion.GetRangeFromScriptRegion(r.ScriptRegion)
70+
Range = r.ScriptRegion.ToRange()
7071
},
7172
Name = SymbolTypeUtils.GetDecoratedSymbolName(r)
7273
});

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync(
6767
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));
6868
}
6969

70-
Range symbolRange = ScriptRegion.GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);
71-
7270
return new Hover
7371
{
7472
Contents = new MarkedStringsOrMarkupContent(symbolInfo),
75-
Range = symbolRange
73+
Range = symbolDetails.SymbolReference.ScriptRegion.ToRange()
7674
};
7775
}
7876
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ await _symbolsService.ScanForReferencesOfSymbol(
5656
locations.Add(new Location
5757
{
5858
Uri = DocumentUri.From(foundReference.FilePath),
59-
Range = ScriptRegion.GetRangeFromScriptRegion(foundReference.ScriptRegion)
59+
Range = foundReference.ScriptRegion.ToRange()
6060
});
6161
}
6262
}

src/PowerShellEditorServices/Services/TextDocument/ScriptFileMarker.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ public enum ScriptFileMarkerLevel
3434
/// <summary>
3535
        /// Information: This warning is trivial, but may be useful. They are recommended by PowerShell best practice.
3636
        /// </summary>
37-
        Information = 0,
37+
Information = 0,
3838
        /// <summary>
3939
        /// WARNING: This warning may cause a problem or does not follow PowerShell's recommended guidelines.
4040
        /// </summary>
41-
        Warning = 1,
41+
Warning = 1,
4242
        /// <summary>
4343
        /// ERROR: This warning is likely to cause a problem or does not follow PowerShell's required guidelines.
4444
        /// </summary>
45-
        Error = 2,
45+
Error = 2,
4646
        /// <summary>
4747
        /// ERROR: This diagnostic is caused by an actual parsing error, and is generated only by the engine.
4848
        /// </summary>
49-
        ParseError = 3
49+
ParseError = 3
5050
};
5151

5252
/// <summary>
@@ -102,7 +102,7 @@ internal static ScriptFileMarker FromParseError(
102102
{
103103
Message = parseError.Message,
104104
Level = ScriptFileMarkerLevel.Error,
105-
ScriptRegion = ScriptRegion.Create(parseError.Extent),
105+
ScriptRegion = new(parseError.Extent),
106106
Source = "PowerShell"
107107
};
108108
}
@@ -157,7 +157,7 @@ internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject)
157157
Message = diagnosticRecord.Message as string ?? string.Empty,
158158
RuleName = diagnosticRecord.RuleName as string ?? string.Empty,
159159
Level = level,
160-
ScriptRegion = ScriptRegion.Create(diagnosticRecord.Extent as IScriptExtent),
160+
ScriptRegion = new(diagnosticRecord.Extent as IScriptExtent),
161161
Corrections = markerCorrections,
162162
Source = "PSScriptAnalyzer"
163163
};

0 commit comments

Comments
 (0)