【问题标题】:Commands Not Showing in Command Palette with RegReplace (Sublime Text 3)使用 RegReplace(Sublime Text 3)在命令面板中未显示的命令
【发布时间】:2018-06-05 20:15:56
【问题描述】:

我正在尝试使用 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"
            ]
        }
    }
]

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

非常感谢任何帮助

【问题讨论】:

    标签: regex sublimetext3 sublimetext


    【解决方案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 并经常使用它的人来说可能是显而易见的,也许有一天我会成为其中之一。

    【讨论】:

      【解决方案2】:

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

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

      因此,您的条目应该看起来更像:

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

      【讨论】:

      • 哇。花了大约一个小时试图找到问题。非常感谢!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 2016-04-13
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多