Reputation: 37
I am trying to get syntax highlighting working in a text editor I am making, it is now highlighting [a few things improperly, but not important right now] using Pygments and obviously tkinter. The problem is that when I run it, even just once, it is EXTREMELY slow, my laptop is cheap so that is obviously a factor, but vim, IDLE, etc. run just fine.
I've read a few SO posts about slow highlighting, but those are for just when to update it, like instead of updating, for example, after every word. My problem is with just one time running the syntax highlighting, it lags.
Here is the highlighting code first:
def highlight(t):
t.mark_set("range_start", "1.0")
data = t.get("1.0", "end-1c")
for token, content in lex(data, PythonLexer()):
t.mark_set("range_end", "range_start + %dc" % len(content))
t.tag_add(str(token), "range_start", "range_end")
t.tag_configure("Token.Keyword", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Constant", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Declaration", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Namespace", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Pseudo", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Reserved", foreground="#CC7A00")
t.tag_configure("Token.Keyword.Type", foreground="#CC7A00")
t.tag_configure("Token.Name.Class", foreground="#003D99")
t.tag_configure("Token.Name.Exception", foreground="#003D99")
t.tag_configure("Token.Name.Function", foreground="#003D99")
t.tag_configure("Token.Operator.Word", foreground="#CC7A00")
t.tag_configure("Token.Comment", foreground="#B80000")
t.tag_configure("Token.Literal.String", foreground="#248F24")
t.mark_set("range_start", "range_end")
Now, I run it just by calling highlight(text)
in my main file, so there's no real point to provide the code from there unless requested.
It ONLY runs when I either open a file, or switch a file using the GUI [like a normal editor with multiple file support]. The larger the file, the longer it takes to run the highlighting [effectively stopping the program for a short time.]
Is there a way to make it faster, or am I limited because of Python and Pygments? [I assume not since IDLE isnt!]
Thanks! If you'd want more code [I can't see why], it can be provided.
Edit: The highlighting that isn't working [the only ones I've noticed] is when I call a function [defining a function highlights the name, like def printStuff():
, but printStuff()
isnt. Also comments, like [obviously] #hi
or """hi"""
, just incase anyone can help with that as well.
Edit2: Other useful info: It originally ran constantly, but it was very slow then, so I decided to only make it run when files open or get switched, thinking it would be at least fast if I did that, but it now takes a few seconds to open a file or switch a file... So the problem seems to lie in the highlight function itself.
Upvotes: 0
Views: 477
Reputation: 37
Bryan Oakley provided the answer in a comment, it was the tag_configure
lines being called every time I updated syntax highlighting. Moving those to a separate function and calling it once fixed the problem.
Upvotes: 1