Skip to content

Commit 840d0d0

Browse files
Kapil Borledaviwil
Kapil Borle
authored andcommitted
Move rule settings related code to AnalysisService
1 parent e442fb8 commit 840d0d0

File tree

3 files changed

+61
-33
lines changed

3 files changed

+61
-33
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,20 +1089,15 @@ protected async Task HandleCommentHelpRequest(
10891089

10901090
if (functionDefinitionAst != null)
10911091
{
1092-
var settings = new Dictionary<string, Hashtable>();
1093-
var ruleSettings = new Hashtable();
1094-
ruleSettings.Add("ExportedOnly", false);
1095-
ruleSettings.Add("Enable", true);
1096-
ruleSettings.Add("BlockComment", requestParams.BlockComment);
1097-
ruleSettings.Add("VSCodeSnippetCorrection", true);
1098-
ruleSettings.Add("Placement", "before");
1099-
settings.Add("PSProvideCommentHelp", ruleSettings);
1100-
var pssaSettings = AnalysisService.GetPSSASettingsHashtable(settings);
1101-
1102-
// todo create a semantic marker api that take only string
1092+
// todo create a semantic marker api that take only string
11031093
var analysisResults = await EditorSession.AnalysisService.GetSemanticMarkersAsync(
11041094
scriptFile,
1105-
pssaSettings);
1095+
AnalysisService.GetCommentHelpRuleSettings(
1096+
true,
1097+
false,
1098+
requestParams.BlockComment,
1099+
true,
1100+
"before"));
11061101

11071102
var analysisResult = analysisResults?.FirstOrDefault(x =>
11081103
{

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,54 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
141141

142142
#region Public Methods
143143

144+
/// <summary>
145+
/// Get PSScriptAnalyzer settings hashtable for PSProvideCommentHelp rule.
146+
/// </summary>
147+
/// <param name="enable">Enable the rule.</param>
148+
/// <param name="exportedOnly">Analyze only exported functions/cmdlets.</param>
149+
/// <param name="blockComment">Use block comment or line comment.</param>
150+
/// <param name="vscodeSnippetCorrection">Return a vscode snipped correction should be returned.</param>
151+
/// <param name="placement">Place comment help at the given location relative to the function definition.</param>
152+
/// <returns>A PSScriptAnalyzer settings hashtable.</returns>
153+
public static Hashtable GetCommentHelpRuleSettings(
154+
bool enable,
155+
bool exportedOnly,
156+
bool blockComment,
157+
bool vscodeSnippetCorrection,
158+
string placement)
159+
{
160+
var settings = new Dictionary<string, Hashtable>();
161+
var ruleSettings = new Hashtable();
162+
ruleSettings.Add("Enable", enable);
163+
ruleSettings.Add("ExportedOnly", exportedOnly);
164+
ruleSettings.Add("BlockComment", blockComment);
165+
ruleSettings.Add("VSCodeSnippetCorrection", vscodeSnippetCorrection);
166+
ruleSettings.Add("Placement", placement);
167+
settings.Add("PSProvideCommentHelp", ruleSettings);
168+
return GetPSSASettingsHashtable(settings);
169+
}
170+
171+
/// <summary>
172+
/// Construct a PSScriptAnalyzer settings hashtable
173+
/// </summary>
174+
/// <param name="ruleSettingsMap">A settings hashtable</param>
175+
/// <returns></returns>
176+
public static Hashtable GetPSSASettingsHashtable(IDictionary<string, Hashtable> ruleSettingsMap)
177+
{
178+
var hashtable = new Hashtable();
179+
var ruleSettingsHashtable = new Hashtable();
180+
181+
hashtable["IncludeRules"] = ruleSettingsMap.Keys.ToArray<object>();
182+
hashtable["Rules"] = ruleSettingsHashtable;
183+
184+
foreach (var kvp in ruleSettingsMap)
185+
{
186+
ruleSettingsHashtable.Add(kvp.Key, kvp.Value);
187+
}
188+
189+
return hashtable;
190+
}
191+
144192
/// <summary>
145193
/// Perform semantic analysis on the given ScriptFile and returns
146194
/// an array of ScriptFileMarkers.
@@ -181,27 +229,6 @@ public IEnumerable<string> GetPSScriptAnalyzerRules()
181229
return ruleNames;
182230
}
183231

184-
/// <summary>
185-
/// Construct a PSScriptAnalyzer settings hashtable
186-
/// </summary>
187-
/// <param name="ruleSettingsMap">A settings hashtable</param>
188-
/// <returns></returns>
189-
public static Hashtable GetPSSASettingsHashtable(IDictionary<string, Hashtable> ruleSettingsMap)
190-
{
191-
var hashtable = new Hashtable();
192-
var ruleSettingsHashtable = new Hashtable();
193-
194-
hashtable["IncludeRules"] = ruleSettingsMap.Keys.ToArray<object>();
195-
hashtable["Rules"] = ruleSettingsHashtable;
196-
197-
foreach (var kvp in ruleSettingsMap)
198-
{
199-
ruleSettingsHashtable.Add(kvp.Key, kvp.Value);
200-
}
201-
202-
return hashtable;
203-
}
204-
205232
/// <summary>
206233
/// Disposes the runspace being used by the analysis service.
207234
/// </summary>

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ public ScriptRegion FindSmallestStatementAstRegion(
522522
return ScriptRegion.Create(ast.Extent);
523523
}
524524

525+
/// <summary>
526+
/// Gets the function defined on a given line.
527+
/// </summary>
528+
/// <param name="scriptFile">Open script file.</param>
529+
/// <param name="lineNumber">The 1 based line on which to look for function definition.</param>
530+
/// <returns>If found, returns the function definition on the given line. Otherwise, returns null.</returns>
525531
public FunctionDefinitionAst GetFunctionDefinitionAtLine(
526532
ScriptFile scriptFile,
527533
int lineNumber)

0 commit comments

Comments
 (0)