Skip to content

Commit 71a54ff

Browse files
author
Kapil Borle
committed
Add Formatter class to provide code formatting capability
1 parent 45e7f93 commit 71a54ff

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

Engine/Formatter.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Management.Automation;
6+
using System.Management.Automation.Runspaces;
7+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
8+
9+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
10+
{
11+
public class Formatter
12+
{
13+
private Settings settings;
14+
15+
private Formatter(Settings settings)
16+
{
17+
this.settings = settings;
18+
}
19+
20+
public static string Format(string scriptDefinition, Settings settings)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
25+
public static string Format(
26+
string scriptDefinition,
27+
Hashtable settingsHashtable,
28+
Runspace runspace,
29+
IOutputWriter outputWriter)
30+
{
31+
var inputSettings = new Settings(settingsHashtable);
32+
var ruleOrder = new string[]
33+
{
34+
"PSPlaceCloseBrace",
35+
"PSPlaceOpenBrace",
36+
"PSUseConsistentWhitespace",
37+
"PSUseConsistentIndentation",
38+
"PSAlignAssignmentStatement"
39+
};
40+
41+
var text = new EditableText(scriptDefinition);
42+
foreach (var rule in ruleOrder)
43+
{
44+
if (!inputSettings.RuleArguments.ContainsKey(rule))
45+
{
46+
continue;
47+
}
48+
49+
outputWriter.WriteVerbose("Running " + rule);
50+
var currentSettingsHashtable = new Hashtable();
51+
currentSettingsHashtable.Add("IncludeRules", new string[] { rule });
52+
var ruleSettings = new Hashtable();
53+
ruleSettings.Add(rule, new Hashtable(inputSettings.RuleArguments[rule]));
54+
currentSettingsHashtable.Add("Rules", ruleSettings);
55+
var currentSettings = new Settings(currentSettingsHashtable);
56+
ScriptAnalyzer.Instance.UpdateSettings(inputSettings);
57+
ScriptAnalyzer.Instance.Initialize(runspace, outputWriter);
58+
59+
var corrections = new List<CorrectionExtent>();
60+
var records = Enumerable.Empty<DiagnosticRecord>();
61+
var numPreviousCorrections = corrections.Count;
62+
63+
do
64+
{
65+
var correctionApplied = new HashSet<int>();
66+
foreach (var correction in corrections)
67+
{
68+
// apply only one edit per line
69+
if (correctionApplied.Contains(correction.StartLineNumber))
70+
{
71+
continue;
72+
}
73+
74+
correctionApplied.Add(correction.StartLineNumber);
75+
text.ApplyEdit(correction);
76+
}
77+
78+
records = ScriptAnalyzer.Instance.AnalyzeScriptDefinition(text.ToString());
79+
corrections = records.Select(r => r.SuggestedCorrections.ElementAt(0)).ToList();
80+
if (numPreviousCorrections > 0 && numPreviousCorrections == corrections.Count)
81+
{
82+
outputWriter.ThrowTerminatingError(new ErrorRecord(
83+
new InvalidOperationException(),
84+
"FORMATTER_ERROR",
85+
ErrorCategory.InvalidOperation,
86+
corrections));
87+
}
88+
89+
numPreviousCorrections = corrections.Count;
90+
91+
// get unique correction instances
92+
// sort them by line numbers
93+
corrections.Sort((x, y) =>
94+
{
95+
return x.StartLineNumber < x.StartLineNumber ?
96+
1 :
97+
(x.StartLineNumber == x.StartLineNumber ? 0 : -1);
98+
});
99+
100+
} while (numPreviousCorrections > 0);
101+
}
102+
103+
return text.ToString();
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)