Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerLeonhardt committed Mar 22, 2019
1 parent b8f56fa commit c4ff248
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 54 deletions.
17 changes: 10 additions & 7 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ protected async Task HandleCommentHelpRequestAsync(
funcText = string.Join("\n", lines);
}

ScriptFileMarker[] analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync(
List<ScriptFileMarker> analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync(
funcText,
AnalysisService.GetCommentHelpRuleSettings(
enable: true,
Expand Down Expand Up @@ -1743,7 +1743,7 @@ await PublishScriptDiagnosticsAsync(
// Get the requested files
foreach (ScriptFile scriptFile in filesToAnalyze)
{
ScriptFileMarker[] semanticMarkers = null;
List<ScriptFileMarker> semanticMarkers = null;
if (isScriptAnalysisEnabled && editorSession.AnalysisService != null)
{
using (Logger.LogExecutionTime($"Script analysis of {scriptFile.FilePath} completed."))
Expand All @@ -1755,13 +1755,16 @@ await PublishScriptDiagnosticsAsync(
{
// Semantic markers aren't available if the AnalysisService
// isn't available
semanticMarkers = new ScriptFileMarker[0];
semanticMarkers = new List<ScriptFileMarker>();
}

// First we remove any
scriptFile.SyntaxMarkers = semanticMarkers;

await PublishScriptDiagnosticsAsync(
scriptFile,
// Concat script analysis errors to any existing parse errors
scriptFile.SyntaxMarkers.Concat(semanticMarkers).ToArray(),
scriptFile.SyntaxMarkers,
correctionIndex,
eventSender);
}
Expand All @@ -1772,14 +1775,14 @@ private async Task ClearMarkersAsync(ScriptFile scriptFile, EventContext eventCo
// send empty diagnostic markers to clear any markers associated with the given file
await PublishScriptDiagnosticsAsync(
scriptFile,
new ScriptFileMarker[0],
new List<ScriptFileMarker>(),
this.codeActionsPerFile,
eventContext);
}

private static async Task PublishScriptDiagnosticsAsync(
ScriptFile scriptFile,
ScriptFileMarker[] markers,
List<ScriptFileMarker> markers,
Dictionary<string, Dictionary<string, MarkerCorrection>> correctionIndex,
EventContext eventContext)
{
Expand All @@ -1792,7 +1795,7 @@ await PublishScriptDiagnosticsAsync(

private static async Task PublishScriptDiagnosticsAsync(
ScriptFile scriptFile,
ScriptFileMarker[] markers,
List<ScriptFileMarker> markers,
Dictionary<string, Dictionary<string, MarkerCorrection>> correctionIndex,
Func<NotificationType<PublishDiagnosticsNotification, object>, PublishDiagnosticsNotification, Task> eventSender)
{
Expand Down
14 changes: 7 additions & 7 deletions src/PowerShellEditorServices/Analysis/AnalysisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class AnalysisService : IDisposable
/// <summary>
/// An empty script marker result to return when no script markers can be returned.
/// </summary>
private static readonly ScriptFileMarker[] s_emptyScriptMarkerResult = new ScriptFileMarker[0];
private static readonly List<ScriptFileMarker> s_emptyScriptMarkerResult = new List<ScriptFileMarker>();

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

Expand Down Expand Up @@ -266,7 +266,7 @@ public static Hashtable GetPSSASettingsHashtable(IDictionary<string, Hashtable>
/// </summary>
/// <param name="file">The ScriptFile which will be analyzed for semantic markers.</param>
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns>
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file)
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync(ScriptFile file)
{
return await GetSemanticMarkersAsync<string>(file, ActiveRules, SettingsPath);
}
Expand All @@ -277,7 +277,7 @@ public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file)
/// <param name="file">The ScriptFile to be analyzed.</param>
/// <param name="settings">ScriptAnalyzer settings</param>
/// <returns></returns>
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file, Hashtable settings)
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync(ScriptFile file, Hashtable settings)
{
return await GetSemanticMarkersAsync<Hashtable>(file, null, settings);
}
Expand All @@ -288,7 +288,7 @@ public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file, H
/// <param name="scriptContent">The script content to be analyzed.</param>
/// <param name="settings">ScriptAnalyzer settings</param>
/// <returns></returns>
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync(
string scriptContent,
Hashtable settings)
{
Expand Down Expand Up @@ -379,7 +379,7 @@ public async Task<string> FormatAsync(

#region Private Methods

private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
private async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync<TSettings>(
ScriptFile file,
string[] rules,
TSettings settings) where TSettings : class
Expand All @@ -398,7 +398,7 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
}
}

private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
private async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync<TSettings>(
string scriptContent,
string[] rules,
TSettings settings) where TSettings : class
Expand All @@ -407,7 +407,7 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
&& (rules != null || settings != null))
{
var scriptFileMarkers = await GetDiagnosticRecordsAsync(scriptContent, rules, settings);
return scriptFileMarkers.Select(ScriptFileMarker.FromDiagnosticRecord).ToArray();
return scriptFileMarkers.Select(ScriptFileMarker.FromDiagnosticRecord).ToList();
}
else
{
Expand Down
31 changes: 6 additions & 25 deletions src/PowerShellEditorServices/Workspace/ScriptFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ public string Contents
/// Gets the list of syntax markers found by parsing this
/// file's contents.
/// </summary>
public ScriptFileMarker[] SyntaxMarkers
{
get;
private set;
}
public List<ScriptFileMarker> SyntaxMarkers { get; set; }

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

/// <summary>
/// Parses the current file contents to get the AST, tokens,
/// and parse errors.
/// Parses the current file contents to get the AST and tokens.
/// Parse errors are ignored because PSScriptAnalyzer provides those to us.
/// </summary>
private void ParseFileContents()
{
ParseError[] parseErrors = null;

// First, get the updated file range
int lineCount = this.FileLines.Count;
if (lineCount > 0)
Expand Down Expand Up @@ -627,38 +621,25 @@ private void ParseFileContents()
this.Contents,
this.FilePath,
out scriptTokens,
out parseErrors);
errors: out _);
}
else
{
this.ScriptAst =
Parser.ParseInput(
this.Contents,
out scriptTokens,
out parseErrors);
errors: out _);
}

this.ScriptTokens = scriptTokens;
}
catch (RuntimeException ex)
catch (RuntimeException)
{
var parseError =
new ParseError(
null,
ex.ErrorRecord.FullyQualifiedErrorId,
ex.Message);

parseErrors = new[] { parseError };
this.ScriptTokens = new Token[0];
this.ScriptAst = null;
}

// Translate parse errors into syntax markers
this.SyntaxMarkers =
parseErrors
.Select(ScriptFileMarker.FromParseError)
.ToArray();

// Untitled files have no directory
// Discussed in https://github.com/PowerShell/PowerShellEditorServices/pull/815.
// Rather than working hard to enable things for untitled files like a phantom directory,
Expand Down
26 changes: 11 additions & 15 deletions src/PowerShellEditorServices/Workspace/ScriptFileMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,31 +164,27 @@ internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject)
};
}

string severity = diagnosticRecord.Severity.ToString();
if(!Enum.TryParse(severity, out ScriptFileMarkerLevel level))
{
throw new ArgumentException(
string.Format(
"The provided DiagnosticSeverity value '{0}' is unknown.",
severity),
"diagnosticSeverity");
}

return new ScriptFileMarker
{
Message = $"{diagnosticRecord.Message as string}",
RuleName = $"{diagnosticRecord.RuleName as string}",
Level = GetMarkerLevelFromDiagnosticSeverity((diagnosticRecord.Severity as Enum).ToString()),
Level = level,
ScriptRegion = ScriptRegion.Create(diagnosticRecord.Extent as IScriptExtent),
Correction = correction,
Source = "PSScriptAnalyzer"
};
}

private static ScriptFileMarkerLevel GetMarkerLevelFromDiagnosticSeverity(
string diagnosticSeverity)
{
if(Enum.TryParse(diagnosticSeverity, out ScriptFileMarkerLevel level))
{
return level;
}

throw new ArgumentException(
string.Format(
"The provided DiagnosticSeverity value '{0}' is unknown.",
diagnosticSeverity),
"diagnosticSeverity");
}
#endregion
}
}
Expand Down

0 comments on commit c4ff248

Please sign in to comment.