Python中的F字符串自动完成。

4
使用 Python 3 中的 f 字符串在 Atom 编辑器中时,它无法正确自动完成字符串。键入:
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."""

如何使结束引号按预期自动完成?

我访问了atom.io网站,但是Atom仍然在我输入结束引号时打印了额外的引号,而不只是给出结束引号。


我不确定那是否是真正的问题,我认为Atom将“in”识别为保留关键字而不是变量。 - Alexander Santos
在Bracket Matcher设置中,您可以尝试启用“始终跳过闭合对”选项。它不能完全解决问题,但可以在一定程度上有所帮助,但会带来一些缺点,例如三重引号无法自动完成,但您可以使用代码片段解决。 - wjandrea
你尝试过 print(x) 或者 你能告诉我你用的是哪个 Python 编辑器吗? - Kalana
使用Atom编译器,打印语句print(x)可以得到所期望的结果。 - Jinzu
2个回答

6

方法 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编辑器的相应配置文件中:
  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("\"") 
  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 文件,可以在 Edit 菜单下选择 Keymap... 选项。这些配置文件位于 %USERPROFILE%/.atom 目录下。

编辑配置文件后,关闭并重新打开 atom 编辑器。对于特定的 python 文件,在编辑器中输入 f",它应该自动补全为 f""。光标位置应该在双引号之间。

这种方法的灵感来自于这里的答案(链接)


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

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("\"") 

如果您可以在 2. keymap.cson 下进行编辑,并且有 'atom-text-editor...,则在 atom-text-editor 之前应该有一个单引号。此外,这并不能解决输入结束双引号时的问题。我需要的是当按下结束双引号时,它不会添加两个新的双引号。 - Jinzu
我找到了如何禁用括号匹配器自动完成双引号的方法,有没有一个链接可以解释如何在init.config中设置自动完成?我不知道该放什么来编辑这个文件。 - Jinzu
我的意思是,您必须使用atom api编写自己的实现,这不适合于init.coffee配置文件,而应该创建一个新的软件包或仅解决现有括号匹配器软件包的增强请求 - Saurabh P Bhandari
1
@Jinzu,糟糕!!有一种方法,请查看我的更新答案。 - Saurabh P Bhandari
@Jinzu,更新的方法二答案有帮助吗? - Saurabh P Bhandari
显示剩余3条评论

-1

试一下这个

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

我认为问题出在您将in作为对象使用。请尽量避免这样做。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接