Skip to content

Instantly share code, notes, and snippets.

@phelipetls
Last active January 19, 2024 18:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phelipetls/8726d6cd68e66ad6b83586ae53f3b3d8 to your computer and use it in GitHub Desktop.
Save phelipetls/8726d6cd68e66ad6b83586ae53f3b3d8 to your computer and use it in GitHub Desktop.
" ~/.vim/after/syntax/python.vim or ~/.config/nvim/after/syntax/python.vim
syn match pythonEscape +{{+ contained containedin=pythonfString,pythonfDocstring
syn match pythonEscape +}}+ contained containedin=pythonfString,pythonfDocstring
syn region pythonfString matchgroup=pythonQuotes
\ start=+[fF]\@1<=\z(['"]\)+ end="\z1"
\ contains=@Spell,pythonEscape,pythonInterpolation
syn region pythonfDocstring matchgroup=pythonQuotes
\ start=+[fF]\@1<=\z('''\|"""\)+ end="\z1" keepend
\ contains=@Spell,pythonEscape,pythonSpaceError,pythonInterpolation,pythonDoctest
syn region pythonInterpolation contained
\ matchgroup=SpecialChar
\ start=+{{\@!+ end=+}}\@!+ skip=+{{+ keepend
\ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue,pythonDoctest
syn match pythonStringModifier /:\(.[<^=>]\)\?[-+ ]\?#\?0\?[0-9]*[_,]\?\(\.[0-9]*\)\?[bcdeEfFgGnosxX%]\?/ contained containedin=pythonInterpolation
syn match pythonStringModifier /![sra]/ contained containedin=pythonInterpolation
syn match pythonStringModifier /\zs *= *\ze[}:!]/ contained containedin=pythonInterpolation
hi link pythonfString String
hi link pythonfDocstring String
hi link pythonStringModifier PreProc
@finite-state-machine
Copy link

Thank you for posting this very useful gist, @phelipetls!

I found it useful to add the following after line 17, which enables highlighting the = in e.g. f"{foo=}":

syn match pythonStringModifier /\zs *= *\ze[}:!]/ contained containedin=pythonInterpolation

(See this change in context: https://gist.github.com/finite-state-machine/aa12c251fc4a7809d1484c4d82d9b064)

(I'd send a pull request, but those don't seem to be supported for gists.)

@phelipetls
Copy link
Author

Thanks! I added it to the gist :)

@mwchase
Copy link

mwchase commented Apr 21, 2022

Quick comment; I hope to look into this when I have more time, but it looks like something is missing, or my environment could be weird somehow.

Anyway, I don't think the highlighting is quite right for stuff like f"... {shlex.quote(SOURCEDIR)} ..." (where the rest of this string justifies why this isn't just using the value). It looks to me like quote(SOURCEDIR) isn't getting highlighted, although it's worth noting that I'm not getting the extra highlights at all without running https://github.com/jaxbot/semantic-highlight.vim, which I think is evidence towards "I did something weird with my environment".

@ninfueng
Copy link

For person who have a problem to use this script, put this file to ~/.config/nvim/after/syntax or ~/.vim/after/syntax. Then, rename this script from fstrings.vim to python.vim.

@phelipetls
Copy link
Author

Thanks, @ninfueng. I added this as a comment to the gist.

@phelipetls
Copy link
Author

@mwchase it seems to be working for me?

image

@mwchase
Copy link

mwchase commented Sep 22, 2022

I just experimented a little, and it looks like it's a result of using https://github.com/vim-python/python-syntax instead of the builtin python syntax. I can try to work out what's working differently later.

Actually, from looking at using the built-in syntax, I... can't tell what using the other version was getting me, so I could just not bother.

@phelipetls
Copy link
Author

That would make sense, this files assumes you're using the default highlight groups, bundled with Vim.

All groups will be highlighted, except things that aren't expressions (should probably contain more stuff here but I forgot):

syn region pythonInterpolation contained
      \ matchgroup=SpecialChar
      \ start=+{{\@!+ end=+}}\@!+ skip=+{{+ keepend
      \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue,pythonDoctest

After a quick look, it seems this would make sense if you're using python-syntax:

syn region pythonInterpolation contained
      \ matchgroup=SpecialChar
      \ start=+{{\@!+ end=+}}\@!+ skip=+{{+ keepend
      \ contains=pythonExpression

@Griffin-Brome
Copy link

Thank you! This should be part of the standard Vim distribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment