F字符串自动完成Python [英] F string autocomplete python

查看:20
本文介绍了F字符串自动完成Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于Atom的Python3中使用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

推荐答案

方法1

添加snippet以按照建议的here自动完成f-string

您可以通过编辑%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"$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'
通过选择Edit菜单下的Init Script...选项可以编辑配置文件init.coffee文件,通过选择Edit菜单下的Keymap...选项可以编辑keymap.cson文件。这些配置文件位于%USERPROFILE%/.atom目录下。

编辑配置文件后,关闭并重新打开ATOM编辑器。在编辑器中(针对特定的文件),输入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(""") 

这篇关于F字符串自动完成Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆