Skip to content

Commit c4ff248

Browse files
code cleanup
1 parent b8f56fa commit c4ff248

File tree

4 files changed

+34
-54
lines changed

4 files changed

+34
-54
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ protected async Task HandleCommentHelpRequestAsync(
11921192
funcText = string.Join("\n", lines);
11931193
}
11941194

1195-
ScriptFileMarker[] analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync(
1195+
List<ScriptFileMarker> analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync(
11961196
funcText,
11971197
AnalysisService.GetCommentHelpRuleSettings(
11981198
enable: true,
@@ -1743,7 +1743,7 @@ await PublishScriptDiagnosticsAsync(
17431743
// Get the requested files
17441744
foreach (ScriptFile scriptFile in filesToAnalyze)
17451745
{
1746-
ScriptFileMarker[] semanticMarkers = null;
1746+
List<ScriptFileMarker> semanticMarkers = null;
17471747
if (isScriptAnalysisEnabled && editorSession.AnalysisService != null)
17481748
{
17491749
using (Logger.LogExecutionTime($"Script analysis of {scriptFile.FilePath} completed."))
@@ -1755,13 +1755,16 @@ await PublishScriptDiagnosticsAsync(
17551755
{
17561756
// Semantic markers aren't available if the AnalysisService
17571757
// isn't available
1758-
semanticMarkers = new ScriptFileMarker[0];
1758+
semanticMarkers = new List<ScriptFileMarker>();
17591759
}
17601760

1761+
// First we remove any
1762+
scriptFile.SyntaxMarkers = semanticMarkers;
1763+
17611764
await PublishScriptDiagnosticsAsync(
17621765
scriptFile,
17631766
// Concat script analysis errors to any existing parse errors
1764-
scriptFile.SyntaxMarkers.Concat(semanticMarkers).ToArray(),
1767+
scriptFile.SyntaxMarkers,
17651768
correctionIndex,
17661769
eventSender);
17671770
}
@@ -1772,14 +1775,14 @@ private async Task ClearMarkersAsync(ScriptFile scriptFile, EventContext eventCo
17721775
// send empty diagnostic markers to clear any markers associated with the given file
17731776
await PublishScriptDiagnosticsAsync(
17741777
scriptFile,
1775-
new ScriptFileMarker[0],
1778+
new List<ScriptFileMarker>(),
17761779
this.codeActionsPerFile,
17771780
eventContext);
17781781
}
17791782

17801783
private static async Task PublishScriptDiagnosticsAsync(
17811784
ScriptFile scriptFile,
1782-
ScriptFileMarker[] markers,
1785+
List<ScriptFileMarker> markers,
17831786
Dictionary<string, Dictionary<string, MarkerCorrection>> correctionIndex,
17841787
EventContext eventContext)
17851788
{
@@ -1792,7 +1795,7 @@ await PublishScriptDiagnosticsAsync(
17921795

17931796
private static async Task PublishScriptDiagnosticsAsync(
17941797
ScriptFile scriptFile,
1795-
ScriptFileMarker[] markers,
1798+
List<ScriptFileMarker> markers,
17961799
Dictionary<string, Dictionary<string, MarkerCorrection>> correctionIndex,
17971800
Func<NotificationType<PublishDiagnosticsNotification, object>, PublishDiagnosticsNotification, Task> eventSender)
17981801
{

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class AnalysisService : IDisposable
5555
/// <summary>
5656
/// An empty script marker result to return when no script markers can be returned.
5757
/// </summary>
58-
private static readonly ScriptFileMarker[] s_emptyScriptMarkerResult = new ScriptFileMarker[0];
58+
private static readonly List<ScriptFileMarker> s_emptyScriptMarkerResult = new List<ScriptFileMarker>();
5959

6060
private static readonly string[] s_emptyGetRuleResult = new string[0];
6161

@@ -266,7 +266,7 @@ public static Hashtable GetPSSASettingsHashtable(IDictionary<string, Hashtable>
266266
/// </summary>
267267
/// <param name="file">The ScriptFile which will be analyzed for semantic markers.</param>
268268
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns>
269-
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file)
269+
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync(ScriptFile file)
270270
{
271271
return await GetSemanticMarkersAsync<string>(file, ActiveRules, SettingsPath);
272272
}
@@ -277,7 +277,7 @@ public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file)
277277
/// <param name="file">The ScriptFile to be analyzed.</param>
278278
/// <param name="settings">ScriptAnalyzer settings</param>
279279
/// <returns></returns>
280-
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file, Hashtable settings)
280+
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync(ScriptFile file, Hashtable settings)
281281
{
282282
return await GetSemanticMarkersAsync<Hashtable>(file, null, settings);
283283
}
@@ -288,7 +288,7 @@ public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file, H
288288
/// <param name="scriptContent">The script content to be analyzed.</param>
289289
/// <param name="settings">ScriptAnalyzer settings</param>
290290
/// <returns></returns>
291-
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(
291+
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync(
292292
string scriptContent,
293293
Hashtable settings)
294294
{
@@ -379,7 +379,7 @@ public async Task<string> FormatAsync(
379379

380380
#region Private Methods
381381

382-
private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
382+
private async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync<TSettings>(
383383
ScriptFile file,
384384
string[] rules,
385385
TSettings settings) where TSettings : class
@@ -398,7 +398,7 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
398398
}
399399
}
400400

401-
private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
401+
private async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync<TSettings>(
402402
string scriptContent,
403403
string[] rules,
404404
TSettings settings) where TSettings : class
@@ -407,7 +407,7 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
407407
&& (rules != null || settings != null))
408408
{
409409
var scriptFileMarkers = await GetDiagnosticRecordsAsync(scriptContent, rules, settings);
410-
return scriptFileMarkers.Select(ScriptFileMarker.FromDiagnosticRecord).ToArray();
410+
return scriptFileMarkers.Select(ScriptFileMarker.FromDiagnosticRecord).ToList();
411411
}
412412
else
413413
{

src/PowerShellEditorServices/Workspace/ScriptFile.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@ public string Contents
100100
/// Gets the list of syntax markers found by parsing this
101101
/// file's contents.
102102
/// </summary>
103-
public ScriptFileMarker[] SyntaxMarkers
104-
{
105-
get;
106-
private set;
107-
}
103+
public List<ScriptFileMarker> SyntaxMarkers { get; set; }
108104

109105
/// <summary>
110106
/// Gets the list of strings for each line of the file.
@@ -589,13 +585,11 @@ private void SetFileContents(string fileContents)
589585
}
590586

591587
/// <summary>
592-
/// Parses the current file contents to get the AST, tokens,
593-
/// and parse errors.
588+
/// Parses the current file contents to get the AST and tokens.
589+
/// Parse errors are ignored because PSScriptAnalyzer provides those to us.
594590
/// </summary>
595591
private void ParseFileContents()
596592
{
597-
ParseError[] parseErrors = null;
598-
599593
// First, get the updated file range
600594
int lineCount = this.FileLines.Count;
601595
if (lineCount > 0)
@@ -627,38 +621,25 @@ private void ParseFileContents()
627621
this.Contents,
628622
this.FilePath,
629623
out scriptTokens,
630-
out parseErrors);
624+
errors: out _);
631625
}
632626
else
633627
{
634628
this.ScriptAst =
635629
Parser.ParseInput(
636630
this.Contents,
637631
out scriptTokens,
638-
out parseErrors);
632+
errors: out _);
639633
}
640634

641635
this.ScriptTokens = scriptTokens;
642636
}
643-
catch (RuntimeException ex)
637+
catch (RuntimeException)
644638
{
645-
var parseError =
646-
new ParseError(
647-
null,
648-
ex.ErrorRecord.FullyQualifiedErrorId,
649-
ex.Message);
650-
651-
parseErrors = new[] { parseError };
652639
this.ScriptTokens = new Token[0];
653640
this.ScriptAst = null;
654641
}
655642

656-
// Translate parse errors into syntax markers
657-
this.SyntaxMarkers =
658-
parseErrors
659-
.Select(ScriptFileMarker.FromParseError)
660-
.ToArray();
661-
662643
// Untitled files have no directory
663644
// Discussed in https://github.com/PowerShell/PowerShellEditorServices/pull/815.
664645
// Rather than working hard to enable things for untitled files like a phantom directory,

src/PowerShellEditorServices/Workspace/ScriptFileMarker.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,31 +164,27 @@ internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject)
164164
};
165165
}
166166

167+
string severity = diagnosticRecord.Severity.ToString();
168+
if(!Enum.TryParse(severity, out ScriptFileMarkerLevel level))
169+
{
170+
throw new ArgumentException(
171+
string.Format(
172+
"The provided DiagnosticSeverity value '{0}' is unknown.",
173+
severity),
174+
"diagnosticSeverity");
175+
}
176+
167177
return new ScriptFileMarker
168178
{
169179
Message = $"{diagnosticRecord.Message as string}",
170180
RuleName = $"{diagnosticRecord.RuleName as string}",
171-
Level = GetMarkerLevelFromDiagnosticSeverity((diagnosticRecord.Severity as Enum).ToString()),
181+
Level = level,
172182
ScriptRegion = ScriptRegion.Create(diagnosticRecord.Extent as IScriptExtent),
173183
Correction = correction,
174184
Source = "PSScriptAnalyzer"
175185
};
176186
}
177187

178-
private static ScriptFileMarkerLevel GetMarkerLevelFromDiagnosticSeverity(
179-
string diagnosticSeverity)
180-
{
181-
if(Enum.TryParse(diagnosticSeverity, out ScriptFileMarkerLevel level))
182-
{
183-
return level;
184-
}
185-
186-
throw new ArgumentException(
187-
string.Format(
188-
"The provided DiagnosticSeverity value '{0}' is unknown.",
189-
diagnosticSeverity),
190-
"diagnosticSeverity");
191-
}
192188
#endregion
193189
}
194190
}

0 commit comments

Comments
 (0)