2

私は問題があります。端末用のエディターを作成しようとしています。キー イベントとマウス イベントの両方をサポートするので、urwid を使用します。私のセットアップでは、カスタム listwalker と 1 つの行編集ウィジェットを使用し、キャプションを行番号に設定します。問題は、コード pygments がこの行を 1 行ずつ強調表示するときです。これは非常に効率的です。しかし、落とし穴があります。たとえば、複数行のドキュメント文字列がある場合、間違って強調表示されます。

  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.

行はsmipleリストに保存されます

[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