<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
{
    public class Formatter
    {
        public static string Format<TCmdlet>(
            string scriptDefinition,
            Settings inputSettings,
            TCmdlet cmdlet) where TCmdlet : PSCmdlet, IOutputWriter
        {
            var ruleOrder = new string[]
            {
                "PSPlaceCloseBrace",
                "PSPlaceOpenBrace",
                "PSUseConsistentWhitespace",
                "PSUseConsistentIndentation",
                "PSAlignAssignmentStatement"
            };

            var text = new EditableText(scriptDefinition);
            foreach (var rule in ruleOrder)
            {
                if (!inputSettings.RuleArguments.ContainsKey(rule))
                {
                    continue;
                }

                cmdlet.WriteVerbose("Running " + rule);
                var currentSettingsHashtable = new Hashtable();
                currentSettingsHashtable.Add("IncludeRules", new string[] { rule });
                var ruleSettings = new Hashtable();
                ruleSettings.Add(rule, new Hashtable(inputSettings.RuleArguments[rule]));
                currentSettingsHashtable.Add("Rules", ruleSettings);
                var currentSettings = new Settings(currentSettingsHashtable);
                ScriptAnalyzer.Instance.UpdateSettings(inputSettings);
                ScriptAnalyzer.Instance.Initialize(cmdlet, null, null, null, null, true, false);

                var corrections = new List<CorrectionExtent>();
                var records = Enumerable.Empty<DiagnosticRecord>();
                var numPreviousCorrections = corrections.Count;

                do
                {
                    var correctionApplied = new HashSet<int>();
                    foreach (var correction in corrections)
                    {
                        // apply only one edit per line
                        if (correctionApplied.Contains(correction.StartLineNumber))
                        {
                            continue;
                        }

                        correctionApplied.Add(correction.StartLineNumber);
                        text.ApplyEdit(correction);
                    }

                    records = ScriptAnalyzer.Instance.AnalyzeScriptDefinition(text.ToString());
                    corrections = records.Select(r => r.SuggestedCorrections.ElementAt(0)).ToList();
                    if (numPreviousCorrections > 0 && numPreviousCorrections == corrections.Count)
                    {
                        cmdlet.ThrowTerminatingError(new ErrorRecord(
                            new InvalidOperationException(),
                            "FORMATTER_ERROR",
                            ErrorCategory.InvalidOperation,
                            corrections));<commit_msg>Fix updating settings in invoke-formatter<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
{
    public class Formatter
    {
        public static string Format<TCmdlet>(
            string scriptDefinition,
            Settings inputSettings,
            TCmdlet cmdlet) where TCmdlet : PSCmdlet, IOutputWriter
        {
            var ruleOrder = new string[]
            {
                "PSPlaceCloseBrace",
                "PSPlaceOpenBrace",
                "PSUseConsistentWhitespace",
                "PSUseConsistentIndentation",
                "PSAlignAssignmentStatement"
            };

            var text = new EditableText(scriptDefinition);
            foreach (var rule in ruleOrder)
            {
                if (!inputSettings.RuleArguments.ContainsKey(rule))
                {
                    continue;
                }

                cmdlet.WriteVerbose("Running " + rule);
                var currentSettingsHashtable = new Hashtable();
                currentSettingsHashtable.Add("IncludeRules", new string[] { rule });
                var ruleSettings = new Hashtable();
                ruleSettings.Add(rule, new Hashtable(inputSettings.RuleArguments[rule]));
                currentSettingsHashtable.Add("Rules", ruleSettings);
                var currentSettings = new Settings(currentSettingsHashtable);
                ScriptAnalyzer.Instance.UpdateSettings(currentSettings);
                ScriptAnalyzer.Instance.Initialize(cmdlet, null, null, null, null, true, false);

                var corrections = new List<CorrectionExtent>();
                var records = Enumerable.Empty<DiagnosticRecord>();
                var numPreviousCorrections = corrections.Count;

                do
                {
                    var correctionApplied = new HashSet<int>();
                    foreach (var correction in corrections)
                    {
                        // apply only one edit per line
                        if (correctionApplied.Contains(correction.StartLineNumber))
                        {
                            continue;
                        }

                        correctionApplied.Add(correction.StartLineNumber);
                        text.ApplyEdit(correction);
                    }

                    records = ScriptAnalyzer.Instance.AnalyzeScriptDefinition(text.ToString());
                    corrections = records.Select(r => r.SuggestedCorrections.ElementAt(0)).ToList();
                    if (numPreviousCorrections > 0 && numPreviousCorrections == corrections.Count)
                    {
                        cmdlet.ThrowTerminatingError(new ErrorRecord(
                            new InvalidOperationException(),
                            "FORMATTER_ERROR",
                            ErrorCategory.InvalidOperation,
                            corrections));
{'fraction_non_alphanumeric': 0.07394931023419955, 'fraction_numerical': 0.0006416426050689766, 'mean_word_length': 1.608786610878661, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5730031', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1312, 'n_words': 269}