【问题标题】:F string autocomplete pythonF字符串自动完成python
【发布时间】:2020-04-13 22:26:48
【问题描述】:

在 Python 3 for 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."""

我怎样才能让结束报价根据需要自动完成?

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

atom.io site

【问题讨论】:

  • 我不确定这是否是真正的问题,我认为 Atom 将 in 识别为保留关键字,而不是 var。
  • 在括号匹配器设置中,您可以尝试启用“始终跳过关闭对”。它不能解决问题,但它有点帮助,有一些缺点,比如三引号不能自动完成,尽管你可以使用 sn-ps。
  • 你试过print(x) 或者你能告诉你的python编辑器是什么
  • print(x) 使用 atom 给出了想要的结果

标签: python python-3.x atom-editor f-string


【解决方案1】:

方法 1

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

您可以通过编辑在%USERPROFILE%/.atom 目录中找到的snippets.cson 文件来添加一个sn-p。也可以通过在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'

配置文件init.coffee可以通过选择Edit菜单下的Init Script...选项来编辑,keymap.cson文件可以通过选择Edit菜单下的Keymap...选项来编辑。这些配置文件位于%USERPROFILE%/.atom 目录下。

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

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


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

将以下行添加到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,避免这种情况的一种方法是从括号匹配器包的自动完成字符配置中删除"",并在初始化配置中管理""的括号匹配
  • @Jinzu,只有当用于 atom 的 Bracket Matcher 包支持多个字符时,该问题的解决方案才会更容易,自 2017 年以来有一个增强 request 待定
  • 我想出了如何为双引号禁用括号匹配器自动完成,在哪里有一个链接解释了如何在init.config 中设置自动完成?我不知道该放什么来编辑这个文件。
  • @Jinzu,哎呀!!有办法,检查我更新的答案
【解决方案2】:

试试这个

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2014-04-21
    • 1970-01-01
    • 2019-04-14
    • 2016-05-03
    相关资源
    最近更新 更多