方法 1
添加snippet 以按照建议的here 自动完成f-string。
您可以通过编辑在%USERPROFILE%/.atom 目录中找到的snippets.cson 文件来添加一个sn-p。也可以通过在Edit 菜单下选择Snippets... 进行编辑。
在编辑文件时,输入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', ->
# Get the active text editor instance
editor = atom.workspace.getActiveTextEditor()
# Get the character under the current position of the cursor
currentCharacterPrefix = editor.getLastCursor().getCurrentWordPrefix().slice(-1)
# Check if the character prefix is 'f'
if(currentCharacterPrefix == 'f')
# Insert double quotes with cursor position set in between the double quotes
snippetBody = '\"$1\"'
atom.packages.activePackages.snippets?.mainModule?.insert snippetBody
else
# If prefix is not 'f', then insert a double quote
# as if entering it manually in the text editor
# (so bracket-matcher does it's autocomplete job)
editor.insertText("\"")
keymap.cson
# Set a key binding for double quote to trigger the command in the init script
'atom-text-editor[data-grammar="source python"]':
'\"': 'custom:insert-double-quotes'
配置文件init.coffee可以通过选择Edit菜单下的Init Script...选项来编辑,keymap.cson文件可以通过选择Edit菜单下的Keymap...选项来编辑。这些配置文件位于%USERPROFILE%/.atom 目录下。
编辑配置文件后关闭并重新打开原子编辑器。在编辑器中(对于 python 特定文件),输入f",它应该会自动完成为f""。光标位置应该在两个双引号之间。
这种方法的灵感来自这个答案here。
在方法2中,还有另一种方法可以让bracket-matcher 包认为它只是添加普通的括号对。 (无需禁用括号匹配器中 "" 的自动完成功能)
将以下行添加到init.coffee 配置文件:
atom.commands.add 'atom-text-editor', 'custom:insert-double-quotes', ->
# Get the active text editor instance
editor = atom.workspace.getActiveTextEditor()
# Get the character under the current position of the cursor
currentCharacterPrefix = editor.getLastCursor().getCurrentWordPrefix().slice(-1)
# Check if the character prefix is 'f'
if(currentCharacterPrefix == 'f')
# Fool the Bracket Matcher package by inserting a space
editor.insertText(" ")
# Insert a double quote for bracket matcher to handle auto-complete job
editor.insertText("\"")
# Set cursor position to remove the space
editor.getLastCursor().moveLeft(1)
editor.backspace()
# Set cursor position in between the double quotes
editor.getLastCursor().moveRight(1)
else
# If prefix is not 'f', then insert a double quote
# as if entering it manually in the text editor
# (so bracket-matcher does it's autocomplete job)
editor.insertText("\"")