Sublimetext2 自定义括号在升华文本中自动关闭

Sublimetext2 自定义括号在升华文本中自动关闭,sublimetext2,auto-close,Sublimetext2,Auto Close,我正在尝试自动关闭标记文件中的星号(*)字符。 我浏览了所有的语言设置文件,没有找到任何可用的示例。我也尝试过编写一个代码片段,但发现它效率低下(它没有围绕选择) 我四处搜索,找到了BracketHighlighter(它声称允许自定义自动关闭配对),但运气不佳(通过包控制安装,也重新启动) 关于我应该从哪里开始或者我做错了什么有什么想法吗 解决方案(感谢@skuroda) skuroda的答案很好-但是,我做了一些调整,我想附加到他们的答案中: { "keys": ["*"], "comma

我正在尝试自动关闭标记文件中的星号(
*
)字符。
我浏览了所有的语言设置文件,没有找到任何可用的示例。我也尝试过编写一个代码片段,但发现它效率低下(它没有围绕选择)

我四处搜索,找到了BracketHighlighter(它声称允许自定义自动关闭配对),但运气不佳(通过包控制安装,也重新启动)

关于我应该从哪里开始或者我做错了什么有什么想法吗


解决方案(感谢@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 }

    ]
}

如果按下前面两个星号旁边的星号键,则会添加两个
**
(例如,
******
然后
******
变成
*********
,其中
是光标。这有助于增强文本的胆量。

您可能需要调整一些上下文,但这应该是一个开始。这是基于内置括号的自动配对键绑定

{ "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 }
    ]
}
用这个

{ "keys": ["*"], "command": "insert_snippet", "args": {"name": "Packages/User/my-snippet.sublime-snippet" }}
现在转到首选项>浏览软件包,然后转到用户文件夹 创建一个文件

my-snippet.sublime-snippet

并在内部使用以下代码

<snippet><content><![CDATA[
*${0:$SELECTION}*
]]></content></snippet>


祝你好运

这是我的版本。它也基于内置的自动配对密钥绑定,但有一些调整

[
    { "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
    之后不允许自动配对,但在
    o
    $
    之后允许自动配对-此版本排除所有内容)
  • 当插入符号后面跟有各种字符(
    等)时进行配对-默认情况下,只允许在空格、制表符和一些括号之前进行配对)
  • 仅适用于纯文本标记文件(.txt和.md)
我也将此版本用于引号,替换默认值(当然允许全局使用引号)

双字符 我的双字符功能版本(类似于OP的/基于默认值,但有一个调整):

如果插入符号前面的行中已经有两个连续的星号,这将产生双字符,在本例中为星号(
*

当插入符号后跟任何星号时,该调整允许跳过星号(一次一个)

如果插入符号前面的行中已经有两个连续的星号,则OP的版本始终会添加两个新星号,即使插入符号后面有一个星号,这是不希望出现的行为(例如,在添加第二个双星号之后,按星号键将再添加两个星号,而不是跳过新星号)

解释 从升华文本的默认键绑定:

一条规则规定,为了配对,插入符号前面必须有小写字母、大写字母、数字、相同的符号或下划线:

{ "key": "preceding_text", "operator": ^"not_regex_contains", "operand": "[\"a-zA-Z0-9_]$"^, "match_all": true },
但是,如果插入符号前面有其他字母或符号(
ö
ž
đ
ψ
~
[
$
,等等) 它不应该这样做,所以我调整了它。 以下要求插入符号前面必须有空格字符或行首(它不包括所有其他符号,而不仅仅是基本字母和数字):

包含行的开头是双重否定的。 以下内容仅适用于空格字符(空格、制表符、其他)之后,而不适用于行的开头:

"regex_contains", "operand": "\\s$"
另一条规则规定,为了配对,插入符号后面还必须有某些字符:制表符,空格,),],},>,行尾:

{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
我认为,如果其他字符跟随插入符号,即标点符号和默认值中未包含的其他空白字符,也应该允许字符配对

以下包括更多标点符号(
-
>>
>>
)和所有空白字符:

"^(?:\\s|\\.|,|:|;|!|\\?|'|\"|‐|-|—|\\)|]|\\}|⟩|>|›|»|$)"
请注意,插入符号前面需要一个空格字符,这意味着在不需要时不会生成一对

这将有助于在句子末尾但在句子末尾标点符号之前开始编写格式化文本,例如(
|
表示插入符号):

“这是一句话|”

“这是一个句子|”
//用户键入一个空格

“这是一个句子
*
|
*
//用户键入一个字符,它会得到一对字符,而不是保持单个字符

“这是一个包含更多单词的句子,
*


这个问题是几年前提出的,但希望我的回答对将来感兴趣的每个人都有用。

很有帮助,但是这个文件在哪里,或者我应该保存这个文件?这些都基于默认密钥绑定(
首选项->密钥绑定-默认值
)。您可以将这些条目放置在用户密钥绑定文件中(
首选项->密钥绑定-user
).哦,顺便说一句,我认为括号荧光灯可以让你定义自定义括号来突出显示,而不是配对,尽管我可能错了。谢谢,这很好。我已经调整了一些代码,这样就可以轻松访问embolden了。
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
"^(?:\\s|\\.|,|:|;|!|\\?|'|\"|‐|-|—|\\)|]|\\}|⟩|>|›|»|$)"