def tokens(self, event=None):
"""
Highlight tokens as rendered by Pygments. Seems to only work after textarea is updated, though calling update_idletasks has no effect.
The problem can be solved by recalling the function if there is no bbox, (as with update_linenumbers), or figure out what is not updated
when running this function (bbox was the case in update_linenumbers).
"""
# http://stackoverflow.com/a/30199105
from pygments import lex, highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
# don't use because multiline strings can start at beginning and end in visible view
#tv = self.mainframe.texthelper.top_visible(self.textarea)
# use since highlight works if multiline str not properly closed
bv = self.mainframe.texthelper.bottom_visible(self.textarea)
data = self.textarea.get("1.0", bv) # "end-1c"
if data == self.prevdata:
return
self.clear_tokens()
#print( highlight(data, PythonLexer(), HtmlFormatter()))
prev_content = ''
i = 0
for token, content in lex(data, PythonLexer()):
lencontent = len(content)
# this happens sometimes in lubuntu
if not content:
#print('no content in HighLight.tokens() loop')
continue
#str(token) == 'Token.Literal.String.Doc' \
if self.mainframe.texthelper.visible(self.textarea, '1.0 + %dc' % i) \
or self.mainframe.texthelper.visible(self.textarea, '1.0 + %dc' % (i+lencontent)):
self.textarea.mark_set("range_start", "1.0 + %dc" %i )
self.textarea.mark_set("range_end", "range_start + %dc" % lencontent)
self.textarea.tag_add(str(token), "range_start", "range_end")
i += lencontent
self.prevdata = data
评论列表
文章目录