Skip to content

Commit 89ba69f

Browse files
committed
1 parent aad6ed0 commit 89ba69f

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

Engine/HostedAnalyzer.cs

+61
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,19 @@ public string Fix(string scriptDefinition, Settings settings)
353353
/// <param name="settings">The settings to use when fixing.</param>
354354
public Task<string> FixAsync(string scriptDefinition, Settings settings) => Task.Run(() => Fix(scriptDefinition, settings));
355355

356+
/// <summary>
357+
/// Create a standard settings object for Script Analyzer
358+
/// This is the object used by analyzer internally
359+
/// It is more functional than the AnalyzerSettings object because
360+
/// it contains the Rule Arguments which are not passable to the Initialize method
361+
/// </summary>
362+
public Settings CreateSettings(string[] ruleNames)
363+
{
364+
var s = CreateSettings();
365+
s.IncludeRules.AddRange(ruleNames);
366+
return s;
367+
}
368+
356369
/// <summary>
357370
/// Create a standard settings object for Script Analyzer
358371
/// This is the object used by analyzer internally
@@ -437,6 +450,52 @@ public string Format(string scriptDefinition)
437450
public Task<string> FormatAsync(string scriptDefinition) =>
438451
Task.Run(() => Format(scriptDefinition));
439452

453+
/// <summary>
454+
/// Format a script according to the formatting rules
455+
/// PSPlaceCloseBrace
456+
/// PSPlaceOpenBrace
457+
/// PSUseConsistentWhitespace
458+
/// PSUseConsistentIndentation
459+
/// PSAlignAssignmentStatement
460+
/// PSUseCorrectCasing
461+
/// </summary>
462+
/// <param name="scriptDefinition">The script to format.</param>
463+
/// <param name="range">The range of the script to format</param>
464+
/// <returns>The formatted script.</returns>
465+
public string Format(string scriptDefinition, Range range)
466+
{
467+
Settings settings = CreateSettings("CodeFormatting");
468+
string formattedScript;
469+
lock(hostedAnalyzerLock)
470+
{
471+
try {
472+
formattedScript = Formatter.Format(scriptDefinition, settings, range, ps.Runspace, writer);
473+
}
474+
finally {
475+
analyzer.CleanUp();
476+
// Reset is required because formatting leaves a number of settings behind which
477+
// should be cleared.
478+
Reset();
479+
}
480+
}
481+
return formattedScript;
482+
}
483+
484+
/// <summary>
485+
/// Format a script according to the formatting rules
486+
/// PSPlaceCloseBrace
487+
/// PSPlaceOpenBrace
488+
/// PSUseConsistentWhitespace
489+
/// PSUseConsistentIndentation
490+
/// PSAlignAssignmentStatement
491+
/// PSUseCorrectCasing
492+
/// </summary>
493+
/// <param name="scriptDefinition">The script to format.</param>
494+
/// <param name="range">The range of the script to format</param>
495+
/// <returns>The formatted script.</returns>
496+
public Task<string> FormatAsync(string scriptDefinition, Range range) =>
497+
Task.Run(() => Format(scriptDefinition, range));
498+
440499
/// <summary>
441500
/// Format a script based on settings
442501
/// </summary>
@@ -497,6 +556,8 @@ internal class hostedWriter : IOutputWriter
497556
public IList<string> Debug;
498557
/// <summary>The warning messages emitted during the invocation of the analyzer.</summary>
499558
public IList<string> Warning;
559+
/// <summary>Add a nonterminating error the ccollection.</summary>
560+
public void WriteError(SMA.ErrorRecord errorRecord) { TerminatingErrors.Add(errorRecord); }
500561
/// <summary>Add a terminating error the ccollection.</summary>
501562
public void ThrowTerminatingError(SMA.ErrorRecord errorRecord) { TerminatingErrors.Add(errorRecord); }
502563
/// <summary>Add a verbose message to the verbose collection.</summary>

Engine/Settings.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public bool IncludeDefaultRules
4242
set { includeDefaultRules = value; }
4343
}
4444
public string FilePath => filePath;
45-
public IEnumerable<string> IncludeRules => includeRules;
46-
public IEnumerable<string> ExcludeRules => excludeRules;
47-
public IEnumerable<string> Severities => severities;
48-
public IEnumerable<string> CustomRulePath => customRulePath;
45+
public List<string> IncludeRules => includeRules;
46+
public List<string> ExcludeRules => excludeRules;
47+
public List<string> Severities => severities;
48+
public List<string> CustomRulePath => customRulePath;
4949
public Dictionary<string, Dictionary<string, object>> RuleArguments => ruleArguments;
5050

5151
/// <summary>

0 commit comments

Comments
 (0)