4

When using f strings in Python 3 for Atom, it doesn't autocomplete the string correctly. Typing in

types_of_people = 10
x = f"There are {types_of_people} types_of_people."

I get x = f"... when I start typing but the end quote doesn't autocomplete

When I type in the ending quote I get x = f"There are {types_of_people} types_of_people."""

How can I get the end quote to autocomplete as desired?

I went to this link. But atom still prints additional quotes when I am typing the end quote, instead of just giving the end quote.

atom.io site

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Jinzu
  • 1,325
  • 2
  • 10
  • 22
  • I am not sure if that's the real issue, i think Atom is recognizing `in` as the reserved keyword, not as a var. – Alexander Santos Dec 22 '19 at 04:42
  • In the Bracket Matcher settings, you could try enabling "Always skip closing pairs". It doesn't fix the problem, but it helps a bit, with some drawbacks like triple quotes don't complete automatically, though you could use snippets for that. – wjandrea Dec 25 '19 at 01:57
  • Did you try `print(x)` **or** can you tell what is your python editer – Kalana Dec 25 '19 at 05:04
  • `print(x)` gives the desired result, using atom – Jinzu Dec 27 '19 at 02:14

2 Answers2

6

Approach 1

Add a snippet to autocomplete the f-string as suggested here.

You can add a snippet by editing the snippets.cson file found in %USERPROFILE%/.atom directory. It can also be edited by selecting Snippets... under Edit menu.

While editing the file, type snip and press TAB. It should generate a sample config like this :

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

Edit the above to this :

'.source.python':
  'f-string':
    'prefix': 'f"'
    'body': 'f"$1"'

The auto-complete of the f-string in this approach is triggered only on pressing TAB after typing f"

Approach 2

Add the following lines to the respective config file for your atom editor :

  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'

The config file init.coffee file can be be edited by selecting the Init Script... option under Edit menu and keymap.cson file can be edited by selecting the Keymap... option under Edit menu. These config files are found under %USERPROFILE%/.atom directory.

Close and reopen the atom editor after editing the config files. In the editor (for python specific files), type f" and it should auto-complete to f"". The cursor position should be in between the two double quotes.

This approach is inspired by this answer here.


In Approach 2, there is another way to make bracket-matcher package think that it is just adding normal bracket pairs. (No need to disable the autocomplete for "" in bracket matcher)

Add following lines to the init.coffee config file:

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("\"") 
Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
  • If you could edit under `2. keymap.cson`. And have `'atom-text-editor...`, there should be a single quote before `atom-text-editor`. Also this doesn't solve the problem of when typing the end double quote. I need it to be so when pressing end double quote, it does not add two new double quotes. – Jinzu Dec 30 '19 at 20:45
  • @Jinzu, One way to avoid that would be to remove `""` from autocomplete characters configuration of Bracket Matcher package and manage bracket matching for `""` in init config – Saurabh P Bhandari Dec 31 '19 at 03:51
  • @Jinzu, The solution to the problem would have been easier only if the Bracket Matcher package for atom supported multiple characters, there is an enhancement [request](https://github.com/atom/bracket-matcher/issues/323) pending since 2017 – Saurabh P Bhandari Dec 31 '19 at 03:54
  • I figured how to disable the Bracket Matcher autocomplete for double-quotes, where is a link that explains how to setup autocomplete in `init.config`? I can't figure out what to put to edit this file. – Jinzu Dec 31 '19 at 04:14
  • I mean you have to write your own implementation using atom [api](https://flight-manual.atom.io/api/), and that would not be a good fit for the `init.coffee` config file, instead a new package should be created or just solve this [enhancement request](https://github.com/atom/bracket-matcher/issues/323) for the existing bracket-matcher package – Saurabh P Bhandari Dec 31 '19 at 04:19
  • 1
    @Jinzu, Oops!! There is a way, check my updated answer – Saurabh P Bhandari Dec 31 '19 at 04:51
  • @Jinzu, Does the updated answer for approach 2 help ? – Saurabh P Bhandari Jan 03 '20 at 14:18
  • This was a good starting point, but it'd be awesome if you updated your answer to address both single and double quotes. I'm not familiar with either of these config files so it took a fair bit of trial and error to figure it out. – David Scott Dec 02 '21 at 22:39
-1

try this

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

I think the problem happens because you use in as an object. Try to avoid that.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Inoejj
  • 32
  • 4