How to adjust the lexer used by pygments highlighting

Tom Tromey tom@tromey.com
Fri Feb 10 17:07:32 GMT 2023


>>>>> "Simon" == Simon Sobisch via Gdb <gdb@sourceware.org> writes:

Simon> The highlighting via pygments actually works quite fine in general,
Simon> but in some cases it uses the wrong lexer and then has bad results.

Simon> It _seems_ that pygments' "filename (extension) based" language lookup
Simon> is not used, because tested files using `pygmentize` are always well
Simon> colored, the ones in GDB sometimes use the wrong lexer.

Simon> Question: How can we adjust the lexer used by pygments syntax
Simon> higlighting in GDB for a given file?

There's no customization to be done here, as far as I know, but the code
is in gdb/python/lib/gdb/styling.py.

Simon> Is it possible to setup a filename extension-based selection when GDB
Simon> does not know of the language ("set language " _seems_ to have no
Simon> effect on highlighting, but that would only work if the language is
Simon> known to GDB)? If not can the "normal" extension list [1] be added to
Simon> GDB or the decision which lexer to be used passed on to pygments?

>From what I can see it does appear to use just the filename and not the
current language:

    def colorize(filename, contents):
        # Don't want any errors.
        try:
            lexer = lexers.get_lexer_for_filename(filename, stripnl=False)
            formatter = get_formatter()
            return highlight(contents, lexer, formatter).encode(
                gdb.host_charset(), "backslashreplace"
            )
        except:
            return None

We're definitely open to accepting improvements here.

One thing is to be sure you're using pygments and not Source Highlight.

Tom


More information about the Gdb mailing list