Navigation

    Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    How to get correct syntax highlighting for Python 3 formatted strings

    Help wanted · · · – – – · · ·
    4
    9
    1615
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Giulio.Portioli
      Giulio.Portioli last edited by

      Hi there,

      Python 3 recently introduced a new feature: formatted strings, which are ordinary strings with an ‘f’ or ‘F’ in front, for example:

      f'Hello World in a formatted string'
      

      much like raw strings are ordinary strings with an ‘r’ in front:

      r'Hello World raw'
      

      The problem is that Notepad++ correctly recognizes raw strings and highlights them all in the chosen color, including the leading ‘r’, while it doesn’t seem to recognize formatted strings, and as soon as you put an ‘f’ in front of a string, the whole string loses its colour and is rendered as ordinary code (e.g. same colour as any user-defined variable).

      I’m using the latest version of Notepad++; has this worked before? Is there a quick way to add a rule to syntax highlighting to take formatted strings into account? I tried to make sense of the parser definition file but gave up after a while.

      Thanks!

      1 Reply Last reply Reply Quote 1
      • PeterJones
        PeterJones last edited by PeterJones

        @giulioportioli, Welcome to the Notepad++ Community. You said:

        Is there a quick way to add a rule to syntax highlighting to take formatted strings into account?

        Not native to Notepad++. The Python lexer is compiled as part of the scintilla component (the underlying editor component, which is a separate codebase from Notepad++), and it would have to be recompiled to add that recognition. (If it were an older Notepad++ I was actually surprised that Notepad++ v7.7, with it’s new Scintilla 4.1.4, didn’t recognize/highlight formatted strings.)

        However, all is not lost. @Ekopalypse has shared the EnhanceAnyBuiltinLexer.py, which is a script for the PythonScript plugin, which allows adding highlights based on regular expressions – the nice thing about this is that it can be used in conjunction with another active lexer, so it can add more highlighting than the active lexer supports. Thus, you should be able to come up with a regex (first guess would be something like f(['"]).*?\1, untested: my intention is to find f, followed by either a single or double quote mark, followed by other characters, until the next instance of the same style of quote; it’s not perfect (doesn’t handle escaped quotes in the string, for example), but it’s a starting place).

        Alan Kilborn 1 Reply Last reply Reply Quote 3
        • Alan Kilborn
          Alan Kilborn @PeterJones last edited by Alan Kilborn

          Also, see here. And here for why you might care.

          dail 1 Reply Last reply Reply Quote 4
          • dail
            dail @Alan Kilborn last edited by

            @Alan-Kilborn said:

            Also, see here. And here for why you might care.

            @giulioportioli As Alan pointed out, there has been a fix for it and it will be in the next release. And actually it doesn’t require any code changes, just a few additions to an XML file. If you feel adventurous take a look here and add those changes to your theme.

            Alan Kilborn 1 Reply Last reply Reply Quote 5
            • Alan Kilborn
              Alan Kilborn @dail last edited by

              @dail said

              …add those changes to your theme.

              I went ahead and did that. It worked great. As I am using a themeless portable version of Notepad++, the file I edited was ...\npp.7.7.bin\stylers.xml.

              Of course, as a Python 3 user, I’d heard of f-strings, but to this point never really tried them out in real code, favoring 'foo {} bar'.format(blah) instead of the simpler f'foo {blah} bar'. Going forward, I’ll probably remember to use f-strings more, thanks to this thread and @dail’s commit.

              1 Reply Last reply Reply Quote 3
              • PeterJones
                PeterJones last edited by

                @dail said:

                And actually it doesn’t require any code changes, just a few additions to an XML file.

                Ah… so it was in the lexer, but because those styles weren’t defined in the stylers.xml, I wasn’t seeing the highlighting. Yes, once I added those to my v7.7, the f-strings get highlighted, and I can change the colors in the Style Configurator as well. (But if I make the same changes in v7.6.6, with the ancient scintilla component, it still doesn’t highlight the f, because the old scintilla doesn’t understand those extra styles) Thanks for that.

                @Alan-Kilborn said:

                as a Python 3 user, I’d heard of f-strings, … Going forward, I’ll probably remember to use f-strings more,

                Since my only Python programming is for Notepad++ PythonScript automation, I was bummed when I saw those f-strings in various tutorials, but then learned that for PythonScript’s 2.7 interpreter they were not available to me, and I was stuck with 'string'.format(...).

                Alan Kilborn 1 Reply Last reply Reply Quote 5
                • Alan Kilborn
                  Alan Kilborn @PeterJones last edited by

                  @PeterJones said

                  …bummed…

                  Since you were apparently up to @dail 's earlier “adventurous” challenge, maybe you could put the Indiana Jones hat back on and do a sequel: https://github.com/asottile/future-fstrings

                  Disclaimer: I can live without f-strings in my Pythonscript efforts, so I have NOT pursued this myself.

                  1 Reply Last reply Reply Quote 4
                  • PeterJones
                    PeterJones last edited by

                    @Alan-Kilborn said:

                    Since you were apparently up to @dail 's earlier “adventurous” challenge

                    Copying four lines of XML wasn’t overly adventurous for me. I think trying to properly install that, and get it working with the python27.dll in PythonScript is beyond what I want to tackle for a “bummer”. :-)

                    But thanks.

                    1 Reply Last reply Reply Quote 2
                    • Giulio.Portioli
                      Giulio.Portioli last edited by

                      Thanks all for the comments. Yes, adding those four lines to stylers.xml worked perfectly.

                      I’m glad it will be fixed in the next release. I knew the issue must have already been picked up by someone.

                      1 Reply Last reply Reply Quote 5
                      • First post
                        Last post
                      Copyright © 2014 NodeBB Forums | Contributors