Sublime Text的Python 3中带有f字符串的引号完成

时间:2018-11-12 08:19:43

标签: python python-3.x autocomplete sublimetext3

当我在Sublime Text 3(3176)中输入引号时,它自动以结束引号引起来。

例如我输入",我得到"<cursor>"

这太好了,我现在一直期待着。但是,如果在我输入f"时将f字符串引入Python,则会得到f"<cursor>而不是f"<cursor>"。无论如何这都不是一个大问题,但它并没有我感觉的那么流畅。

我认为,如果光标左侧有一个字符,则自动补全规则不会添加多余的引号,这通常是在您尝试输入结束引号时。

是否有一种方法可以修改规则,以便如果左边的字符是“ f”,它将输入右引号?为避免使用怪异的用例,当您真正尝试以“ f”结尾的字符串结尾时,可能会有一个and条件来检查左括号,空格或等号。对于print(f"string"foo = f"string"foo =f"string"的高使用情况,为“ f”。

1 个答案:

答案 0 :(得分:2)

是的,这是可能的。只需将以下内容添加到您的用户键映射文件中(“首选项”菜单->“键绑定”。用户键映射在右侧):

// Auto-pair quotes even after string modifiers.
// Copied over from the default bindings with modifications to `preceding_text`
// and an added selector condition.
{ "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": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\\b[bfru]+$", "match_all": true },
        { "key": "selector", "operator": "equal", "operand": "source.python" },
        { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true }
    ]
},
{ "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": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\\b[bfru]+$", "match_all": true },
        { "key": "selector", "operator": "equal", "operand": "source.python" },
        { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true }
    ]
},

This will be shipped by default in a future build of ST.

它使用范围选择器来确定插入符号是否已经在字符串中,因此用f字符完成字符串的情况不成问题。