Skip to content

Allow formatting when ScriptAnalysis is off #1262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 11 additions & 25 deletions src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ public void RunScriptDiagnostics(
ScriptFile[] filesToAnalyze,
CancellationToken cancellationToken)
{
EnsureEngineSettingsCurrent();

if (AnalysisEngine == null)
if (_configurationService.CurrentSettings.ScriptAnalysis.Enable == false)
{
return;
}

EnsureEngineSettingsCurrent();

// Create a cancellation token source that will cancel if we do or if the caller does
var cancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

Expand Down Expand Up @@ -191,12 +191,6 @@ public void RunScriptDiagnostics(
public Task<string> FormatAsync(string scriptFileContents, Hashtable formatSettings, int[] formatRange = null)
{
EnsureEngineSettingsCurrent();

if (AnalysisEngine == null)
{
return null;
}

return AnalysisEngine.FormatAsync(scriptFileContents, formatSettings, formatRange);
}

Expand Down Expand Up @@ -263,32 +257,24 @@ public void ClearMarkers(ScriptFile file)
/// <param name="settings">The new language server settings.</param>
public void OnConfigurationUpdated(object sender, LanguageServerSettings settings)
{
InitializeAnalysisEngineToCurrentSettings();
if (settings.ScriptAnalysis.Enable ?? true)
{
InitializeAnalysisEngineToCurrentSettings();
}
}

private void EnsureEngineSettingsCurrent()
{
if (_pssaSettingsFilePath != null
&& !File.Exists(_pssaSettingsFilePath))
if (_analysisEngineLazy == null ||
(_pssaSettingsFilePath != null
&& !File.Exists(_pssaSettingsFilePath)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (_analysisEngineLazy == null ||
(_pssaSettingsFilePath != null
&& !File.Exists(_pssaSettingsFilePath)))
if (_analysisEngineLazy == null
|| (_pssaSettingsFilePath != null
&& !File.Exists(_pssaSettingsFilePath)))

{
InitializeAnalysisEngineToCurrentSettings();
}
}

private void InitializeAnalysisEngineToCurrentSettings()
{
// If script analysis has been disabled, just return null
if (_configurationService.CurrentSettings.ScriptAnalysis.Enable != true)
{
if (_analysisEngineLazy != null && _analysisEngineLazy.IsValueCreated)
{
_analysisEngineLazy.Value.Dispose();
}

_analysisEngineLazy = null;
return;
}

// We may be triggered after the lazy factory is set,
// but before it's been able to instantiate
if (_analysisEngineLazy == null)
Expand Down Expand Up @@ -357,7 +343,7 @@ private bool TryFindSettingsFile(out string settingsFilePath)
if (settingsFilePath == null
|| !File.Exists(settingsFilePath))
{
_logger.LogWarning($"Unable to find PSSA settings file at '{configuredPath}'. Loading default rules.");
_logger.LogInformation($"Unable to find PSSA settings file at '{configuredPath}'. Loading default rules.");
settingsFilePath = null;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Builder WithSettingsFile(string settingsPath)
_settingsParameter = settingsPath;
return this;
}

/// <summary>
/// Uses a settings hashtable for PSSA rule configuration.
/// </summary>
Expand Down Expand Up @@ -176,7 +176,8 @@ public async Task<string> FormatAsync(string scriptDefinition, Hashtable formatS
// Invoke-Formatter throws a ParameterBinderValidationException if the ScriptDefinition is an empty string.
if (string.IsNullOrEmpty(scriptDefinition))
{
return null;
_logger.LogDebug("Script Definition was: " + scriptDefinition == null ? "null" : "empty string");
return scriptDefinition;
}

var psCommand = new PSCommand()
Expand All @@ -194,7 +195,7 @@ public async Task<string> FormatAsync(string scriptDefinition, Hashtable formatS
if (result == null)
{
_logger.LogError("Formatter returned null result");
return null;
return scriptDefinition;
}

if (result.HasErrors)
Expand All @@ -205,7 +206,7 @@ public async Task<string> FormatAsync(string scriptDefinition, Hashtable formatS
errorBuilder.Append(err).Append(s_indentJoin);
}
_logger.LogWarning($"Errors found while formatting file: {errorBuilder}");
return null;
return scriptDefinition;
}

foreach (PSObject resultObj in result.Output)
Expand All @@ -216,7 +217,8 @@ public async Task<string> FormatAsync(string scriptDefinition, Hashtable formatS
}
}

return null;
_logger.LogError("Couldn't get result from output. Returning original script.");
return scriptDefinition;
}

/// <summary>
Expand Down