Python pygments 和 urwid.Edit 小部件(包含元组的列表)

发布于 2024-12-28 12:24:40 字数 1934 浏览 0 评论 0原文

我有一个问题。我尝试为终端制作一个编辑器。所以我使用 urwid,因为它支持按键和鼠标事件。我的设置是使用自定义 listwalker 和一行编辑小部件,并将标题设置为行号。问题是当它突出显示代码时,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/

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/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文