Python pygments and urwid.Edit widget(list with tuples)

529 views Asked by At

I have a problem. I try to make an editor for the terminal. So I use urwid, since it supports both key and mouse events. My setup is to use a custom listwalker and one row Edit widget, and the caption set to a line number. The problem is when it highlights code pygments does this line by line. Which is very efficient. But there is a catch. For example if you have a multi line doc string, it gets highlighted wrong.

  1 """This is a docstring and it is be highlighted this whole line.
  2 This  is not higlighted"""#This should be a comment but is a part of the doc string

I cannot wrap my head around it. To highlight the code I use

list(lex(self._edit_text, self.parent.language))

It is colored using a urwid palette.

palette = [(token.Token.Literal.String.Doc, "dark magenta", "black"),]
#                              |                 |             |
#                       the identifier          fg            bg
# if it isn't lined up, palette[0] is the identifier('"""*"""'),
# palette[1] is the foreground color and palette[2] is the background color.

the lines is stored in a smiple list

[urwid.Edit("  1 ",'"""This is a docstring and it's highlighted as a string.'),\
urwid.Edit("  2 ",\
    'This  is not higlighted"""#This should be a comment but is a part of the doc string')]

and the highlighting function produces

[(Token.Literal.String.Doc, '"""This is a docstring and it's highlighted as a string.')\
    (Token.Name,\
    'This  is not higlighted"""#This should be a comment but is a part of the doc string')]

And when it is processed it makes a string out of the text and is stored in self._edit_text. Then it makes a list of tuples stored as

self._attrib = [('ln_sel', 4), (Token.Name, 4), (Token.Text, 2), (Token.Operator.Word, 2),\
    (Token.Text, 1), (Token.Operator.Word, 3), (Token.Text, 1), (Token.Name, 10), \
    (Token.Literal.String, 61)]

Which means that the self._edit_text[0:4] is colored with "ln_sel" palette tuple, and that self._edit_text[4:8] is colored with "Token.Name" palette tuple.

How can I make this work?

Thanks :)

http://excess.org/urwid/
http://pygments.org/

0

There are 0 answers