在Sublime Text的python3中用fstrings完成引号

2024-06-07 07:12:37 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在Sublime Text 3(3176)中键入引号时,它会自动以右引号结束。你知道吗

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

这是伟大的,我现在期待它的所有时间。然而,在Python中引入f字符串后,如果我键入f",我得到的是f"<cursor>,而不是f"<cursor>"。这不是一个大问题,但它不是一个流动性,因为我觉得它可以。你知道吗

我认为如果光标左边有一个字符,自动补全规则不会添加额外的引号,这通常是在您尝试输入右引号时。你知道吗

有没有办法修改规则,以便在左边的字符是“f”时输入右引号?为了避免在真正尝试以“f”结束字符串时出现奇怪的用例,对于print(f"string"foo = f"string"foo =f"string"的高用例,可能有一个and条件来检查“f”左边的括号、空格或等号。你知道吗


Tags: 字符串textstring键入foo规则时间用例
1条回答
网友
1楼 · 发布于 2024-06-07 07:12:37

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

// 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字符结束字符串的情况不是问题。你知道吗

相关问题 更多 >