方法 1
添加代码片段以自动完成建议在此处使用的f-string
。
您可以通过编辑%USERPROFILE%/.atom
目录下的snippets.cson
文件来添加片段。也可以通过选择编辑
菜单下的片段...
进行编辑。
在编辑文件时,键入snip
并按TAB键。它应该生成一个像这样的示例配置:
'.source.js':
'Snippet Name':
'prefix': 'Snippet Trigger'
'body': 'Hello World!'
将上面的内容编辑为以下内容:
'.source.python':
'f-string':
'prefix': 'f"'
'body': 'f"$1"'
这个方法中的
f-string
自动完成只有在键入
f"
后按下
TAB才会触发。
方法2
将以下行添加到您的Atom编辑器的相应配置文件中:
init.coffee
atom.commands.add 'atom-text-editor', 'custom:insert-double-quotes', ->
editor = atom.workspace.getActiveTextEditor()
currentCharacterPrefix = editor.getLastCursor().getCurrentWordPrefix().slice(-1)
if(currentCharacterPrefix == 'f')
snippetBody = '\"$1\"'
atom.packages.activePackages.snippets?.mainModule?.insert snippetBody
else
editor.insertText("\"")
keymap.cson
'atom-text-editor[data-grammar="source python"]':
'\"': 'custom:insert-double-quotes'
要编辑 init.coffee
文件,可以在 Edit
菜单下选择 Init Script...
选项;要编辑 keymap.cson
文件,可以在 Edit
菜单下选择 Keymap...
选项。这些配置文件位于 %USERPROFILE%/.atom
目录下。
编辑配置文件后,关闭并重新打开 atom 编辑器。对于特定的 python 文件,在编辑器中输入 f"
,它应该自动补全为 f""
。光标位置应该在双引号之间。
这种方法的灵感来自于这里的答案(链接)。
方法二 中,还有另一种方法使括号匹配插件认为它只是添加普通的括号对。(无需禁用括号匹配器中的 ""
自动完成)
在 init.coffee
配置文件中添加以下行:
atom.commands.add 'atom-text-editor', 'custom:insert-double-quotes', ->
editor = atom.workspace.getActiveTextEditor()
currentCharacterPrefix = editor.getLastCursor().getCurrentWordPrefix().slice(-1)
if(currentCharacterPrefix == 'f')
editor.insertText(" ")
editor.insertText("\"")
editor.getLastCursor().moveLeft(1)
editor.backspace()
editor.getLastCursor().moveRight(1)
else
editor.insertText("\"")
print(x)
或者 你能告诉我你用的是哪个 Python 编辑器吗? - Kalanaprint(x)
可以得到所期望的结果。 - Jinzu