0

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.

  • fixed formatting on code – Hunter Kepley Jun 09 '17 at 21:41
  • The formatting is still not right. And please provide a [mcve]. – Bryan Oakley Jun 09 '17 at 21:55
  • Rewriting because long comment; Fixed formatting for real, some reason it didn't save my edit, and *all* the code is needed to reproduce and solve the issue, I can't cut it out. I've seen much larger code snippets before, not sure what the problem is. That's as 'Minimal, Complete, and Verified' I can get with my specific problem. – Hunter Kepley Jun 09 '17 at 22:24
  • If we can't reproduce the problem, we can't help you. I seriously doubt you need hundreds of lines, if all you're doing is applying the formatting to some code. – Bryan Oakley Jun 09 '17 at 22:36
  • By the way, there's no need to call `tag_configure` in a loop. You only need to configure a tag once. It probably adds an imperceptible performance hit, but I doubt it's the root of your problem. – Bryan Oakley Jun 09 '17 at 22:38
  • Bryan, it's about 27 lines, not 100s. And you **can** reproduce the problem, what are you talking about? And I will try it, thanks. Oh, also, it's not being looped, as stated in the original post.... – Hunter Kepley Jun 09 '17 at 22:39
  • I just tried your code. I was able to highlight 59 lines of code in about 27ms. – Bryan Oakley Jun 09 '17 at 22:44
  • I have a slow laptop, so 400 lines took about 1 second, but taking out the `tag_configure` lines actually solved it, now it takes absolutely no time at all, at least not visibly. Also, I'm still confused why you brought up the minimal example, and saying it was '100s of lines'. Either way, thanks for the answer man. Have a good day. [Kinda funny how simple some fixes are that ya don't even think it's a fix, like that :P] – Hunter Kepley Jun 09 '17 at 22:46
  • When you said "all the code", I inferred that you had hundreds of lines since, if it was just a few lines more you would have gone ahead and added the code to your example. The point being, if you want help it behooves you to make it as easy on the helpers as possible and not make us guess about the missing code. I was forced to look up on the internet to see how to import pygments, and to figure out what `lex` was. – Bryan Oakley Jun 09 '17 at 22:52
  • OH I meant all the code as in the Open File function or Switch File function, that's what I meant, that's all the code for highlighting itself. – Hunter Kepley Jun 09 '17 at 22:54
  • The "M" in [mcve] means we don't need code that doesn't actually contribute to the problem. In this case an mcve is the code you posted plus just enough to create a root window, a text widget, insert some text, and call your function. – Bryan Oakley Jun 09 '17 at 22:58
  • Alright, thanks. I've never really seen people post a root window, text widget, etc. in the example, that's why I didn't know. Thanks! – Hunter Kepley Jun 09 '17 at 23:00
  • 1
    While it is true that many people don't, they should. There's a reason that stackoverflow created the [mcve] page in order to explain the process to people. But beyond that, this is just good programming practice. As you gain more experience you'll find that one of the first steps in debugging is to reduce the problem down as small as you possibly can while still being able to reproduce the problem. Often, the very action of creating an mcve will shed enough light on the problem for you to solve it yourself. – Bryan Oakley Jun 09 '17 at 23:09

1 Answers1

1

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.