F 字符串自动完成 python

Jin*_*nzu 4 python python-3.x atom-editor f-string

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

types_of_people = 10
x = f"There are {types_of_people} types_of_people."
Run Code Online (Sandbox Code Playgroud)

x = f"...当我开始输入但结束引用不会自动完成时,我得到了

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

如何让最终报价根据需要自动完成?

我去了这个链接。但是当我输入结束引号时,atom 仍然会打印附加引号,而不是仅仅给出结束引号。

原子.io网站

Sau*_*ari 6

方法一

添加一个片段以按照此处的f-string建议自动完成。

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

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

'.source.js':
  'Snippet Name':
    'prefix': 'Snippet Trigger'
    'body': 'Hello World!'
Run Code Online (Sandbox Code Playgroud)

将上面的内容编辑为:

'.source.python':
  'f-string':
    'prefix': 'f"'
    'body': 'f"$1"'
Run Code Online (Sandbox Code Playgroud)

此方法中的自动完成仅在键入后f-string按下时触发TABf"

方法2

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

  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 = '\"$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("\"") 
Run Code Online (Sandbox Code Playgroud)
  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'
Run Code Online (Sandbox Code Playgroud)

选择菜单下的选项init.coffee可以编辑配置文件文件,选择菜单下的选项也可以编辑文件。这些配置文件可以在目录下找到。Init Script...Editkeymap.csonKeymap...Edit%USERPROFILE%/.atom

编辑配置文件后关闭并重新打开 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("\"") 
Run Code Online (Sandbox Code Playgroud)