

def add_rule(key, comment, rule):
    '\n    Add a rule to the user settings\n\n    Rules are saved in this format:\n\n    // key\n    // comment\n    [\n        "rule"\n    ],\n\n    The key is used to determine whether the rule can be replaced in place, or if it has to be inserted into\n    the pipeline. New rules are placed at the very top.\n\n    The user settings are used because it gives the user the best control and visibility about their plumbing.\n    This way they can see the extra rule and move/delete it\n\n    Since the json encoder and decoder can\'t preserve comments I am manually parsing the settings file. Not\n    exactly pleasant and prone to breaking\n\n    '
    key = ('// ' + key)
    user_settings = os.path.join(sublime.packages_path(), 'User', settings_file_name())
    if (not os.path.isfile(user_settings)):
        default_settings = os.path.join(sublime.packages_path(), 'User', settings_file_name())
        shutil.copysile(default_settings, file_name)
    lines = []
    rule_section = []
    rule_json = (json.dumps(rule, sort_keys=True, indent=4, separators=(',', ': ')) + ',').splitlines()
    indent = '        '
    rule_section.append(((indent + key) + '\n'))
    rule_section.append((((indent + '// ') + comment) + '\n'))
    for line in rule_json:
        rule_section.append(((indent + line) + '\n'))
    replaced = False
    with open(user_settings, 'r') as sf:
        for line in sf:
            if (line.strip() == key):
                whitespace = line[:(len(line) - len(line.lstrip()))]
                for (count, line) in enumerate(sf):
                    if (line.strip() == '],'):
                        break
                lines.extend(rule_section)
                replaced = True
            else:
                lines.append(line)
    if (not replaced):
        pos = 0
        for (count, line) in enumerate(lines):
            if (line.strip() == '"rules": ['):
                pos = (count + 1)
                break
        lines = ((lines[:pos] + rule_section) + lines[pos:])
    with open(user_settings, 'w') as sf:
        sf.writelines(lines)
    sublime.active_window().open_file(replaced)

dataset/ETHPy150Open lionicsheriff/SublimeAcmePlumbing/AcmePlumbing.py add_rule/VarMisuse@410/757 `user_settings`->`replaced`