F字符串自动完成python

2024-10-02 12:31:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在python3foratom中使用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."""

我怎样才能让结束报价自动完成?在

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

atom.io site


Tags: of字符串io键入链接sitepeopleare
2条回答

方法1

添加一个snippet以自动完成f-string,如建议的here。在

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

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

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

将上述内容编辑为:

^{pr2}$

在这种方法中,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""。光标位置应该在两个双引号之间。在

这个方法的灵感来自于这个答案here。在


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

试试这个

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

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

相关问题 更多 >

    热门问题