regex - 使用 RegReplace 命令未显示在命令面板中的命令 (Sublime Text 3)

标签 regex sublimetext3 sublimetext

我正在尝试使用 Sublime Text 3 中的 RegReplace 插件运行一系列命令,但无法加载命令,也无法让键绑定(bind)工作。我不知道出了什么问题。

采取的步骤:

  • 已安装 RegReplace
  • 打开命令面板
  • 搜索“RegReplace:创建新的正则表达式”
  • 将规则修改为以下内容

    """
    If you don't need a setting, just leave it as None.
    When the rule is parsed, the default will be used.
    Each variable is evaluated separately, so you cannot substitute variables in other variables.
    """
    
    # name (str): Rule name.  Required.
    name = "extract_variables"
    
    # find (str): Regular expression pattern or literal string.
    #    Use (?i) for case insensitive. Use (?s) for dotall.
    #    See https://docs.python.org/3.4/library/re.html for more info on regex flags.
    #    Required unless "scope" is defined.
    find = r".*\[(.*[^(<|>)]*?)\].*"
    
    # replace (str - default=r'\g<0>'): Replace pattern.
    replace = r"\1"
    
    # literal (bool - default=False): Preform a non-regex, literal search and replace.
    literal = None
    
    # literal_ignorecase (bool - default=False): Ignore case when "literal" is true.
    literal_ignorecase = None
    
    # scope (str): Scope to search for and to apply optional regex to.
    #    Required unless "find" is defined.
    scope = None
    
    # scope_filter ([str] - default=[]): An array of scope qualifiers for the match.
    #    Only used when "scope" is not defined.
    #
    #    - Any instance of scope qualifies match: scope.name
    #    - Entire match of scope qualifies match: !scope.name
    #    - Any instance of scope disqualifies match: -scope.name
    #    - Entire match of scope disqualifies match: -!scope.name
    scope_filter = None
    
    # greedy (bool - default=True): Apply action to all instances (find all).
    #    Used when "find" is defined.
    greedy = None
    
    # greedy_scope (bool - default=True): Find all the scopes specified by "scope."
    greedy_scope = None
    
    # format_replace (bool - default=False): Use format string style replace templates.
    #    Works only for Regex (with and without Backrefs) and Re (with Backrefs).
    #    See http://facelessuser.github.io/backrefs/#format-replacements for more info.
    format_replace = None
    
    # selection_inputs (bool -default=False): Use selection for inputs into find pattern.
    #    Global setting "selection_only" must be disabled for this to work.
    selection_inputs = None
    
    # multi_pass (bool - default=False): Perform multiple sweeps on the scope region to find
    #    and replace all instances of the regex when regex cannot be formatted to find
    #    all instances. Since a replace can change a scope, this can be useful.
    multi_pass = None
    
    # plugin (str): Define replace plugin for more advanced replace logic.
    plugin = None
    
    # args (dict): Arguments for 'plugin'.
    args = None
    
    # ----------------------------------------------------------------------------------------
    # test: Here you can setup a test command.  This is not saved and is just used for this session.
    #     - replacements ([str]): A list of regex rules to sequence together.
    #     - find_only (bool): Highlight current find results and prompt for action.
    #     - action (str): Apply the given action (fold|unfold|mark|unmark|select).
    #       This overrides the default replace action.
    #     - options (dict): optional parameters for actions (see documentation for more info).
    #         - key (str): Unique name for highlighted region.
    #         - scope (str - default="invalid"): Scope name to use as the color.
    #         - style (str - default="outline"): Highlight style (solid|underline|outline).
    #     - multi_pass (bool): Repeatedly sweep with sequence to find all instances.
    #     - no_selection (bool): Overrides the "selection_only" setting and forces no selections.
    #     - regex_full_file_with_selections (bool): Apply regex search to full file then apply
    #       action to results under selections.
    test = {
        "replacements": ["extract_variables"],
        "find_only": True,
        "action": None,
        "options": {},
        "multi_pass": False,
        "no_selection": False,
        "regex_full_file_with_selections": False
    }
    

此代码在 AppData\Roaming\Sublime Text 3\Packages\User\reg_replace_rules.sublime-settings 中生成以下内容

{
    "replacements":
    {
        "extract_variables":
        {
            "find": ".*\\[(.*[^(<|>)]*?)\\].*",
            "name": "extract_variables",
            "replace": "\\1"
        }
    }
}

然后我在同一目录下创建了以下命令,文件名为 Default.sublime-commands

[   
    { 
        "caption": "Reg Replace: Extract ERS Variables",
        "command": "extract_ers_variables",
        "args": {
            "replacements": [
                "extract_variables"
            ]
        }
    }
]

保存所有这些后,我仍然没有在命令面板中看到该命令,并且当我尝试将其另存为键盘映射时它也没有显示。

非常感谢任何帮助

最佳答案

带着我自己的麻烦来到这里,不妨记录一下我愚蠢的错误。我对 JSON 一无所知。

当添加两个一起使用的替换时 the examples at the developer's site ,我无法让命令显示在命令面板中。我可以让一个键绑定(bind)工作,但它给出了错误消息,指出无法找到第一个替换...在成功使用它之后。罪魁祸首是一个格式错误的 reg_replace_rules.sublime-settings 文件:

//Wrong
{
    "replacements":
    {
        "rep_one":
        //stuff
    },
    "replacements":
    {
        "rep_two":
        //other stuff
    }
}

//Correct
{
    "replacements":
    {
        "rep_one":
        //stuff, comma
        "rep_two":
        //other stuff
    }
}

修复清除了错误消息,但该命令仍然不会出现在命令面板中。问题是 JSON 更糟糕,这次是在 Default.sublime-commands 中。

//Wrong
{
    "caption": "My Command",
    "command": "reg_replace",
    "args": {"replacements": ["rep_one", "rep_two"]}
}

//Correct
[
    {
        "caption": "My Command",
        "command": "reg_replace",
        "args": {"replacements": ["rep_one", "rep_two"]}
    }
]

对于正确学习 JSON 并经常使用它的人来说,这可能是显而易见的,也许有一天我也会成为其中之一。

关于regex - 使用 RegReplace 命令未显示在命令面板中的命令 (Sublime Text 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50708264/

相关文章:

macos - 如何在 Sublime Text (OSX) 中恢复我的输出面板?

java - 正则表达式作为通过控制台的输入

sublimetext3 - Sublime - 用于转到第一个选择 'Find in Files' 的键盘快捷键

regex - 如何使用正则表达式覆盖entity.name.function?

python - Sublime Text Python 自动完成

icons - Sublime Text 3 侧边栏图标

python - Sublime Text 2::Python 代码完成

ios - 解析和收缩俄语全名

Javascript regEx 电子邮件仅限 3 个域

Java 查找所有以字母开头的单词