F 字符串自动完成 python

F string autocomplete python

在 Python 3 中为 Atom 使用 f 字符串时,它不会正确地自动完成字符串。输入

types_of_people = 10
x = f"There are {types_of_people} types_of_people."

当我开始输入时我得到 x = f"... 但结束引号没有自动完成

当我输入结束语时,我得到 x = f"There are {types_of_people} types_of_people."""

如何根据需要自动完成结尾引语?

我去了这个link。但是当我输入结束引号时,atom 仍然打印额外的引号,而不是只给出结束引号。

atom.io site

试试这个

y =  1
x = f"I am {y} years old"

我认为问题的发生是因为您使用 in 作为对象。尽量避免这种情况。

方法一

加一个snippet to autocomplete the f-string as suggested here

您可以通过编辑 %USERPROFILE%/.atom 目录中的 snippets.cson 文件来添加片段。也可以通过选择 Edit 菜单下的 Snippets... 进行编辑。

编辑文件时,键入 snip 并按 TAB。它应该生成这样的示例配置:

'.source.js':
  'Snippet Name':
    'prefix': 'Snippet Trigger'
    'body': 'Hello World!'

将上面的内容修改为:

'.source.python':
  'f-string':
    'prefix': 'f"'
    'body': 'f""'

此方法中 f-string 的自动完成仅在键入 f"

后按 TAB 时触发

方法二

将以下行添加到原子编辑器的相应配置文件中:

  1. 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 = '\"\"' 
    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("\"") 
  1. 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 文件可以通过选择 [=29] 编辑=] Edit 菜单下的选项。这些配置文件位于 %USERPROFILE%/.atom 目录下。

编辑配置文件后关闭并重新打开原子编辑器。在编辑器中(对于 python 个特定文件),键入 f",它应该会自动完成 f""。光标位置应在两个双引号之间。

这种方法的灵感来自这个答案


方法 2 中,还有另一种方法可以使括号匹配器包认为它只是添加普通的括号对。 (无需为括号匹配器中的""禁用自动完成)

将以下行添加到 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("\"")