我正在尝试自动关闭Markdown文件中的星号(*
)字符
我一直在浏览所有的语言设置文件,并且没有任何东西可以用来作为例子。我也试过写一个片段,但发现它效率低下(它没有包围选择)。
我四处搜索并发现BracketHighlighter(声称允许自定义自动关闭配对)但没有运气(通过Package Control安装,也重新启动)。
关于我应该从哪里开始或者我做错了什么想法?
解决方案(感谢@skuroda)
skuroda的答案会很好 - 但是,我做了一些调整,我想补充一下他们的答案:
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "$0**"}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\*\\*", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
如果在两个前面的星号旁边按下星号键,则会添加两个**
(例如**|
,然后***|
变为**|**
,其中|
是游标这对于鼓励文本很有帮助。
答案 0 :(得分:3)
您可能需要调整一些上下文,但这应该是一个开始。这基于内置括号的自动对键绑定。
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*$0*"}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
},
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*${0:$SELECTION}*"}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
},
{ "keys": ["*"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
答案 1 :(得分:1)
使用此
{ "keys": ["*"], "command": "insert_snippet", "args": {"name": "Packages/User/my-snippet.sublime-snippet" }}
现在转到Preference>浏览包,然后转到用户文件夹 创建文件
MY-snippet.sublime-片段
并在
中使用以下代码<snippet><content><![CDATA[
*${0:$SELECTION}*
]]></content></snippet>
祝你好运
答案 2 :(得分:1)
这是我的版本。它也基于内置的自动配对键绑定,但有一些调整。
[
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*$0*"}, "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": "^(?:\\s|\\.|,|:|;|!|\\?|'|\"|‐|-|—|\\)|]|\\}|⟩|>|›|»|$)", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "\\S$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "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 },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "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 },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "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 },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "match_all": true },
]
},
]
与默认设置不同,此版本将:
o
之后自动配对,但在ö
或{{1之后)自动配对}}-此版本不包含所有内容)$
,.
,:
,!
等)时进行配对-默认情况下仅允许在空格,制表符和一些括号)我也将此版本用于引号,代替了默认版本(当然,全局允许使用引号)。
我的双字符功能版本(与OP相似,基于默认值,但有所调整):
"
如果在插入号前面的行中的任何地方已经有两个连续的星号,则将生成双字符,在本例中为星号([
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "$0**"}, "context":
[
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\*\\*", "match_all": true },
{ "key": "following_text", "operator": "not_regex_contains", "operand": "\\*", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.plain, text.html.markdown", "match_all": true },
]
},
]
)。
该调整允许在插入符号后接任何星号时跳过星号(一次跳过一个星号)。
如果插入号前面的行中已经有两个连续的星号,则OP的版本总是添加两个新的星号,即使插入号后面紧跟着一个星号也是不希望的行为(例如,仅在添加第二个双星号之后,按星号键会增加两个,而不是跳过新的星号。
通过Sublime Text的默认键绑定:
一个规则指出,要配对,必须在插入符号前加上小写字母,大写字母,数字,相同符号或下划线:
*
但是,如果插入号前面有其他字母或符号({ "key": "preceding_text", "operator": ^"not_regex_contains", "operand": "[\"a-zA-Z0-9_]$"^, "match_all": true },
,ö
,ž
,đ
,ç
,蘋
,Ψ
,?
,~
,[
,•
等)
它不应该这样做,所以我对其进行了调整。
以下要求插入符号前面必须有空格字符或一行的开头(它不包括所有其他符号,而不仅仅是基本字母和数字):
$
包含行首是双重否定。 以下内容仅适用于空格字符(空格,制表符和其他字符),但不适用于行首:
"not_regex_contains", "operand": "\\S$"
另一条规则指出,为了配对,插入号还必须跟随某些字符:制表符,空格,),],},>,行尾:
"regex_contains", "operand": "\\s$"
我认为,如果其他字符跟在插入记号之后,也应该允许字符配对,即标点符号和其他默认未包含的空白字符。
以下内容包括更多的标点符号({ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
,.
,,
,:
,;
,!
,{{1} },?
,'
,"
,‐
,-
,—
,⟩
)和所有空白字符:< / p>
›
请注意,插入符号前必须有一个空格字符,这意味着在不需要时将不会创建一对。
这在以下情况下会有所帮助:例如,您想在句子末尾但在该句子末尾的标点之前开始编写格式文本,例如(»
代表插入号)
“这是一个句子|”
“这是一个句子|”
"^(?:\\s|\\.|,|:|;|!|\\?|'|\"|‐|-|—|\\)|]|\\}|⟩|>|›|»|$)"
“这是一个句子
|
|// user types a space
”*
“这是句子
*
,其中包含更多单词|// User types a character and it gets a pair instead of staying single
”
这个问题是几年前提出的,但希望我的回答对将来有兴趣的每个人有用。