2

我有个问题。我尝试为终端制作一个编辑器。所以我使用 urwid,因为它同时支持键和鼠标事件。我的设置是使用自定义 listwalker 和一行 Edit 小部件,并将标题设置为行号。问题是当它突出显示代码时,pygments 会逐行执行此操作。这是非常有效的。但是有一个问题!例如,如果您有一个多行文档字符串,它会被错误地突出显示。

  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

我无法绕过它。突出显示我使用的代码

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

它使用 urwid 调色板着色。

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.

这些行存储在一个简单的列表中

[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')]

和高亮功能产生

[(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')]

当它被处理时,它会从文本中生成一个字符串并存储在 self._edit_text 中。然后它生成一个元组列表,存储为

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)]

这意味着 self._edit_text[0:4] 用“ln_sel”调色板元组着色,而 self._edit_text[4:8] 用“Token.Name”调色板元组着色。

我怎样才能使这项工作?

谢谢 :)

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

4

0 回答 0