Skip to content

Commit fac7632

Browse files
author
Kapil Borle
committed
Move formatting to its own class
1 parent f0b5fea commit fac7632

File tree

1 file changed

+2
-79
lines changed

1 file changed

+2
-79
lines changed

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ protected override void BeginProcessing()
6666
}
6767
#endif
6868

69-
// var settings = PSSASettings.Create(Settings);
70-
// formatter = new Formatter(settings);
71-
72-
// todo move to a common initalize session method
7369
Helper.Instance = new Helper(SessionState.InvokeCommand, this);
7470
Helper.Instance.Initialize();
7571

@@ -90,83 +86,10 @@ protected override void BeginProcessing()
9086
}
9187
}
9288

93-
9489
protected override void ProcessRecord()
9590
{
96-
// this.WriteObject(formatter.Format(ScriptDefinition));
97-
98-
var ruleOrder = new string[]
99-
{
100-
"PSPlaceCloseBrace",
101-
"PSPlaceOpenBrace",
102-
"PSUseConsistentWhitespace",
103-
"PSUseConsistentIndentation",
104-
"PSAlignAssignmentStatement"
105-
};
106-
107-
var text = new EditableText(ScriptDefinition);
108-
foreach (var rule in ruleOrder)
109-
{
110-
if (!inputSettings.RuleArguments.ContainsKey(rule))
111-
{
112-
continue;
113-
}
114-
115-
this.WriteVerbose("Running " + rule);
116-
var currentSettingsHashtable = new Hashtable();
117-
currentSettingsHashtable.Add("IncludeRules", new string[] { rule });
118-
var ruleSettings = new Hashtable();
119-
ruleSettings.Add(rule, new Hashtable(inputSettings.RuleArguments[rule]));
120-
currentSettingsHashtable.Add("Rules", ruleSettings);
121-
var currentSettings = new Settings(currentSettingsHashtable);
122-
ScriptAnalyzer.Instance.UpdateSettings(currentSettings);
123-
ScriptAnalyzer.Instance.Initialize(this, null, null, null, null, true, false);
124-
125-
var corrections = new List<CorrectionExtent>();
126-
var records = Enumerable.Empty<DiagnosticRecord>();
127-
var numPreviousCorrections = corrections.Count;
128-
129-
do
130-
{
131-
var correctionApplied = new HashSet<int>();
132-
foreach (var correction in corrections)
133-
{
134-
// apply only one edit per line
135-
if (correctionApplied.Contains(correction.StartLineNumber))
136-
{
137-
continue;
138-
}
139-
140-
correctionApplied.Add(correction.StartLineNumber);
141-
text.ApplyEdit(correction);
142-
}
143-
144-
records = ScriptAnalyzer.Instance.AnalyzeScriptDefinition(text.ToString());
145-
corrections = records.Select(r => r.SuggestedCorrections.ElementAt(0)).ToList();
146-
if (numPreviousCorrections > 0 && numPreviousCorrections == corrections.Count)
147-
{
148-
this.ThrowTerminatingError(new ErrorRecord(
149-
new InvalidOperationException(),
150-
"FORMATTER_ERROR",
151-
ErrorCategory.InvalidOperation,
152-
corrections));
153-
}
154-
155-
numPreviousCorrections = corrections.Count;
156-
157-
// get unique correction instances
158-
// sort them by line numbers
159-
corrections.Sort((x, y) =>
160-
{
161-
return x.StartLineNumber < x.StartLineNumber ?
162-
1 :
163-
(x.StartLineNumber == x.StartLineNumber ? 0 : -1);
164-
});
165-
166-
} while (numPreviousCorrections > 0);
167-
}
168-
169-
this.WriteObject(text.ToString());
91+
var text = Formatter.Format(ScriptDefinition, inputSettings, this);
92+
this.WriteObject(text);
17093
}
17194

17295
private void ValidateInputSettings()

0 commit comments

Comments
 (0)