命令调色板中没有显示的命令与RegReplace(Sublime Text 3)

时间:2018-06-05 20:15:57

标签: regex sublimetext3 sublimetext

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

采取的措施:

  • 已安装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"
            ]
        }
    }
]

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

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

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

添加两个由 the examples at the developer's site 一起使用的替换时,我无法让命令显示在命令面板中。我可以让一个键绑定工作,但它给出了错误消息,说找不到第一个替代品……在成功使用它之后。罪魁祸首是格式错误的 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 并经常使用它的人来说,这可能是显而易见的,也许有一天我会成为其中之一。

答案 1 :(得分:0)

这对您不起作用的原因是您的command文件中存在Default.sublime-commands错误。特别是,命令extract_ers_variables不存在,因此命令面板中的条目被隐藏,因为选择它不会做任何事情。从视觉上讲,如果此命令位于sublime-menu文件中,则菜单中的条目将显示为禁用。

如果从菜单中选择Preferences > Package Settings > RegReplace > Quick Start Guide并按照显示的示例进行操作,请注意当涉及在Default.sublime-commands中创建命令条目的部分时,它会告诉您使用{{1}作为命令,reg_replace参数的名称告诉命令要执行哪个替换。

因此,您的参赛作品应该更像:

replacements