键盘映射文件中的字符转义
Character escaping in keymap file
这是一个自动配对星号(用于 Markdown 和 AsciiDoc)文件的键盘映射。有用。问题:
1) 我是否应该转义第一个块的第 6 行中的星号?
2) 在我的测试中,这一行中的转义没有任何区别。但为什么?如果您尝试删除最后一个块的第 5 行和第 6 行(以及第 5 个块中的行)中的转义,将导致不正确的行为。因此,那里需要转义 是 。但似乎在第一个街区不需要它。这让我感到困惑。
[
// Auto-pair *
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*[=10=]*"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\*a-zA-Z0-9_]$", "match_all": true }, // --- THIS line ---
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true }
]
},
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*${0:$SELECTION}*"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},
{ "keys": ["*"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\*", "match_all": true }, // --- THIS line ---
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true },
]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\*$", "match_all": true }, // --- THIS line ---
{ "key": "following_text", "operator": "regex_contains", "operand": "^\*", "match_all": true }, // --- THIS line ---
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true },
]
},
]
更新:实际上,使用另一个正则表达式会更好:
- [*a-zA-Z0-9_]$
+ (^[*\s]*$)|([*a-zA-Z0-9_]$)
这样当您使用项目符号列表时,星号将不会在行的开头重复。
在正则表达式中,*
运算符是特殊的,表示 零次或多次重复前一个原子 。因此,为了匹配文字 *
字符,您需要将其转义为 \*
(或 JSON \*
)以向正则表达式引擎表明您的意思是文字 *
而不是应用特殊含义。
构造 []
表示 character set,它匹配集合中的任何字符。在一个字符集中,唯一对正则表达式引擎有特殊意义的字符是 ]
(关闭字符集),\
(仍然需要能够转义),-
(指定一系列字符)和 ^
(否定集合并且仅当它是第一个字符时才特殊),因此只有那些字符需要在集合内转义。
由于字符集意味着匹配以下任何一个字符 *
的特殊含义不适用,因此在字符集内部它不适用需要转义(尽管如果你愿意,你仍然可以这样做)。
这是一个自动配对星号(用于 Markdown 和 AsciiDoc)文件的键盘映射。有用。问题:
1) 我是否应该转义第一个块的第 6 行中的星号?
2) 在我的测试中,这一行中的转义没有任何区别。但为什么?如果您尝试删除最后一个块的第 5 行和第 6 行(以及第 5 个块中的行)中的转义,将导致不正确的行为。因此,那里需要转义 是 。但似乎在第一个街区不需要它。这让我感到困惑。
[
// Auto-pair *
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*[=10=]*"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\*a-zA-Z0-9_]$", "match_all": true }, // --- THIS line ---
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true }
]
},
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*${0:$SELECTION}*"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},
{ "keys": ["*"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\*", "match_all": true }, // --- THIS line ---
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true },
]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\*$", "match_all": true }, // --- THIS line ---
{ "key": "following_text", "operator": "regex_contains", "operand": "^\*", "match_all": true }, // --- THIS line ---
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true },
]
},
]
更新:实际上,使用另一个正则表达式会更好:
- [*a-zA-Z0-9_]$
+ (^[*\s]*$)|([*a-zA-Z0-9_]$)
这样当您使用项目符号列表时,星号将不会在行的开头重复。
在正则表达式中,*
运算符是特殊的,表示 零次或多次重复前一个原子 。因此,为了匹配文字 *
字符,您需要将其转义为 \*
(或 JSON \*
)以向正则表达式引擎表明您的意思是文字 *
而不是应用特殊含义。
构造 []
表示 character set,它匹配集合中的任何字符。在一个字符集中,唯一对正则表达式引擎有特殊意义的字符是 ]
(关闭字符集),\
(仍然需要能够转义),-
(指定一系列字符)和 ^
(否定集合并且仅当它是第一个字符时才特殊),因此只有那些字符需要在集合内转义。
由于字符集意味着匹配以下任何一个字符 *
的特殊含义不适用,因此在字符集内部它不适用需要转义(尽管如果你愿意,你仍然可以这样做)。