Sublime Text 中的自定义自动套用格式

Custom AutoFormat in Sublime Text

有没有一种方法可以使用 Regex 进行格式化,但在当前文档上绑定一些替换所有表达式以进行格式化?我经常使用一大堆正则表达式来清理我的 SQL 脚本,我希望不必每次都手动完成。有没有办法一次将所有这些都热键到 运行?

这无关紧要,但这是我使用的 SQL Regex 脚本,其中 -- 是注释,第二行是替换变量。您可以将其用作参考,也可以根据需要将其用于您自己的脚本。

-- Add a space between = or + or * or / phrases
([@\w\d'")\]])([+=*\/]|<>|<=|>=|>|<)(['"@\w\d(-\[])
  

-- Remove unnessisary space at the end of a line
([-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/\d\w])\h+$


-- Add space between minus sign. Doesn't work after a letter because it could be mistaken with a hyphan.
([\d)\]])([-])([\w\d(\[])
  

-- Add a space after a , that comes after a letter, ), ', or "
([)\w\d'")])([,])([\w\d'"(@])
 

-- Fix dates in comments that are messed up from adding spacing to /
\* *(Created on: )?(\d{1,2}) *\/ *(\d{1,2}) *\/ *(\d)
* //

正如 Keith Hall 指出的那样,packagecontrol.io/packages/RegReplace 会很好地完成这项工作。

但是,仅供参考,如果您想直接创建这样的自定义命令,它会像这样:

  1. 下拉Preferences > Browse Packages...然后进入用户文件夹。

  2. 创建一个名为 sql_cleanup.py 的文件并粘贴以下内容:


from sublime_plugin import TextCommand
import re

# Sublime automatically recognizes this class name as being the "sql_fixup" command
class SqlFixupCommand(TextCommand):
    def run(self, edit):
        self._edit = edit
        self._process_text()

    def _get_file_content(self):
        return self.view.substr(sublime.Region(0, self.view.size()))

    def _update_file(self, doc):
        self.view.replace(self._edit, sublime.Region(0, self.view.size()), doc)

    def _process_text(self):
        txt = self._get_file_content()

        # Add a space between = or + or * or / phrases
        txt = re.sub(r"([@\w\d'\")\]])([+=*\/]|<>|<=|>=|>|<)(['\"@\w\d(-\[])", r"  ", txt)

        # Remove unnecessary space at the end of a line
        txt = re.sub(r"([-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/\d\w])\h+$", r"", txt)

        # Add space between minus sign. Doesn't work after a letter because it could be mistaken with a hyphen.
        txt = re.sub(r"([\d)\]])([-])([\w\d(\[])", r"  ", txt)

        # Add a space after a , that comes after a letter, ), ', or "
        txt = re.sub(r"([)\w\d'\")])([,])([\w\d'\"(@])", r" ", txt)

        # Fix dates in comments that are messed up from adding spacing to /
        txt = re.sub(r"\* *(Created on: )?(\d{1,2}) *\/ *(\d{1,2}) *\/ *(\d)", r"* //", txt)

        self._update_file(txt)
  1. 要选择性地将新命令添加到命令选项板,请创建一个名为 sql_cleanup.sublime-commands 的文件并粘贴以下内容:
[
  { "caption": "SQL: Fix Up Code","command": "sql_fixup" },
]

  1. 要选择将此命令添加到 Sublime 的工具菜单(在名为 SQL 的子菜单中),创建一个名为 Main.sublime-menu 的文件,其中包含以下内容:(如果已经有一个名为 SQL,然后这会将它添加到已经存在的任何内容中。)
[
  {
    "id": "tools",
    "children": [
      {
        "caption": "SQL",
        "id": "sql-tools",
        "mnemonic": "S",
        "children": [
          {
            "caption": "Fix Up Code",
            "command": "sql_fixup" },
        ]
      }
    ]
  },
]
  1. 要有选择地将此命令绑定到击键,请创建一个名为 Default (Windows).sublime-keymap(或您所在的任何平台)的文件,其中包含类似以下内容的内容:(此示例将其绑定到按住 CTRL 键和输入 UQ。)
[
   { "keys": ["ctrl+u", "ctrl+q"], "command": "sql_fixup" },
]