Skip to content

Fix help completion performance for large files #459

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
merged 3 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 9 additions & 8 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,22 +1091,23 @@ protected async Task HandleCommentHelpRequest(
{
// todo create a semantic marker api that take only string
var analysisResults = await EditorSession.AnalysisService.GetSemanticMarkersAsync(
scriptFile,
functionDefinitionAst.Extent.Text,
AnalysisService.GetCommentHelpRuleSettings(
true,
false,
requestParams.BlockComment,
true,
"before"));

var analysisResult = analysisResults?.FirstOrDefault(x =>
{
return x.Correction != null
&& x.Correction.Edits[0].StartLineNumber == expectedFunctionLine;
});

// find the analysis result whose correction starts on
result.Content = analysisResult?.Correction.Edits[0].Text.Split('\n').Select(x => x.Trim('\r')).ToArray();
result.Content = analysisResults?
.FirstOrDefault()?
.Correction?
.Edits[0]
.Text
.Split('\n')
.Select(x => x.Trim('\r'))
.ToArray();
}

await requestContext.SendResult(result);
Expand Down
51 changes: 42 additions & 9 deletions src/PowerShellEditorServices/Analysis/AnalysisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static Hashtable GetPSSASettingsHashtable(IDictionary<string, Hashtable>
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns>
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file)
{
return await GetSemanticMarkersAsync(file, activeRules, settingsPath);
return await GetSemanticMarkersAsync<string>(file, activeRules, settingsPath);
}

/// <summary>
Expand All @@ -211,6 +211,19 @@ public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file, H
return await GetSemanticMarkersAsync<Hashtable>(file, null, settings);
}

/// <summary>
/// Perform semantic analysis on the given script with the given settings.
/// </summary>
/// <param name="scriptContent">The script content to be analyzed.</param>
/// <param name="settings">ScriptAnalyzer settings</param>
/// <returns></returns>
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(
string scriptContent,
Hashtable settings)
{
return await GetSemanticMarkersAsync<Hashtable>(scriptContent, null, settings);
}

/// <summary>
/// Returns a list of builtin-in PSScriptAnalyzer rules
/// </summary>
Expand Down Expand Up @@ -252,11 +265,29 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
TSettings settings) where TSettings : class
{
if (hasScriptAnalyzerModule
&& file.IsAnalysisEnabled
&& (typeof(TSettings) == typeof(string) || typeof(TSettings) == typeof(Hashtable))
&& file.IsAnalysisEnabled)
{
return await GetSemanticMarkersAsync<TSettings>(
file.Contents,
rules,
settings);
}
else
{
// Return an empty marker list
return new ScriptFileMarker[0];
}
}

private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>(
string scriptContent,
string[] rules,
TSettings settings) where TSettings : class
{
if ((typeof(TSettings) == typeof(string) || typeof(TSettings) == typeof(Hashtable))
&& (rules != null || settings != null))
{
var scriptFileMarkers = await GetDiagnosticRecordsAsync(file, rules, settings);
var scriptFileMarkers = await GetDiagnosticRecordsAsync(scriptContent, rules, settings);
return scriptFileMarkers.Select(ScriptFileMarker.FromDiagnosticRecord).ToArray();
}
else
Expand Down Expand Up @@ -318,9 +349,9 @@ private void EnumeratePSScriptAnalyzerRules()
}

private async Task<PSObject[]> GetDiagnosticRecordsAsync<TSettings>(
ScriptFile file,
string[] rules,
TSettings settings) where TSettings : class
string scriptContent,
string[] rules,
TSettings settings) where TSettings : class
{
var diagnosticRecords = new PSObject[0];

Expand All @@ -346,7 +377,7 @@ private async Task<PSObject[]> GetDiagnosticRecordsAsync<TSettings>(
"Invoke-ScriptAnalyzer",
new Dictionary<string, object>
{
{ "ScriptDefinition", file.Contents },
{ "ScriptDefinition", scriptContent },
{ settingParameter, settingArgument }
});
}
Expand All @@ -358,6 +389,7 @@ private async Task<PSObject[]> GetDiagnosticRecordsAsync<TSettings>(
return diagnosticRecords;
}


private PSObject[] InvokePowerShell(string command, IDictionary<string, object> paramArgMap)
{
using (var powerShell = System.Management.Automation.PowerShell.Create())
Expand Down Expand Up @@ -389,7 +421,8 @@ private PSObject[] InvokePowerShell(string command, IDictionary<string, object>

private async Task<PSObject[]> InvokePowerShellAsync(string command, IDictionary<string, object> paramArgMap)
{
var task = Task.Run(() => {
var task = Task.Run(() =>
{
return InvokePowerShell(command, paramArgMap);
});

Expand Down