Improve Pygments Syntax Highlighting Speed for Tkinter Text
I solved the problem! Instead of going from the beginning to the end, I checked the line in which the user was typing, and only parsed the content within that line. This sped up the syntax highlighting a LOT.
def deafultHighlight(self, argument): self.content = self.text.get("1.0", tk.END) self.lines = self.content.split("\n") if (self.previousContent != self.content): self.text.mark_set("range_start", self.row + ".0") data = self.text.get(self.row + ".0", self.row + "." + str(len(self.lines[int(self.row) - 1]))) for token, content in lex(data, PythonLexer()): self.text.mark_set("range_end", "range_start + %dc" % len(content)) self.text.tag_add(str(token), "range_start", "range_end") self.text.mark_set("range_start", "range_end") self.previousContent = self.text.get("1.0", tk.END)