here2share

# syntax_highlighter.py

Jan 27th, 2022 (edited)
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.01 KB | None | 0 0
  1. # syntax_highlighter.py
  2.  
  3. from tkinter import *
  4. from collections import deque
  5.  
  6.  
  7. class Window:
  8.     def __init__(self, master):
  9.         self.master = master
  10.         self.master.option_add("*Font", "Verdana 12")
  11.  
  12.         self.Main = Frame(self.master)
  13.  
  14.         self.stack = deque(maxlen = 10)
  15.         self.stackcursor = 0
  16.  
  17.         self.L1 = Label(self.Main, text = "To Start... Paste Code Into Entry Box")
  18.         self.L1.pack(padx = 5, pady = 5)
  19.  
  20.  
  21.         #---------
  22.  
  23.         self.T1 = Text(self.Main, width = 124, height = 29)
  24.  
  25.         self.T1.tag_configure("orange", foreground = "orange", font = "Verdana 12")
  26.         self.T1.tag_configure("blue", foreground = "blue", font = "Verdana 12")
  27.         self.T1.tag_configure("purple", foreground = "purple", font = "Verdana 12")
  28.         self.T1.tag_configure("grey", foreground = "grey", font = "Verdana 12")
  29.         self.T1.tag_configure("red", foreground = "red", font = "Verdana 12")
  30.         self.T1.tag_configure("green", foreground = "green", font = "Verdana 12")
  31.  
  32.         self.tags = ["orange", "blue", "purple", "red", "grey", "green"]
  33.  
  34.         self.wordlist = [["and", "or", "not", "assert", "finally", "find", "break", "for", "pass", "class", "from", "print", "continue", "global", "raise", "def", "if","return", "del", "import", "try", "elif", "in", "while", "else", "is", "with", "except", "lambda", ".append", ".extend", "yield", ".insert", ".delete", "del", ".update", ".zfill", ".bind", ".config", ".tag_configure", ".tag_add", ".index", "True", "False", "Frame", "Label", "Entry", "Button", "Radiobutton", "Checkbutton", "reverse", "Text", ".create_oval", ".create_rectangle", ".create_image", ".create_polygon", "text", "height", "width", "fill", "fg", "bg", "color", "font", "pack", "grid", "place"],
  35.         ["int", "str", "float", "list", "dict", "tuple", ".set", ".get", "bool", "zip", "input", "__init__", "+", "+=", "-", "-=", "*", "*=", "/","/=", "==", "!=", "**", "<", ">", "^", "%"],
  36.         ["pygame", "tkinter", "sys", "os", "mysql", ":"],
  37.         ["self", "exec", "eval", "open", "system", "fork", "Tkinter", ";"],
  38.         ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] ]
  39.  
  40.         self.T1.bind("<Return>", lambda event: self.indent(event.widget))
  41.          
  42.         self.T1.pack(padx = 5, pady = 5)
  43.  
  44.         #---------
  45.  
  46.         self.menu = Menu(self.Main)
  47.         self.menu.add_command(label = "Print", command = self.print_stack)
  48.         self.menu.add_command(label = "Undo", command = self.undo)
  49.         self.menu.add_command(label = "Redo", command = self.redo)
  50.  
  51.         self.master.config(menu = self.menu)
  52.  
  53.         self.B1 = Button(self.Main, text = "Print", width = 8, command = self.display)
  54.         self.B1.pack(padx = 5, pady = 5, side = LEFT)
  55.  
  56.         self.B2 = Button(self.Main, text = "Clear", width = 8, command = self.clear)
  57.         self.B2.pack(padx = 5, pady = 5, side = LEFT)
  58.  
  59.         self.B3 = Button(self.Main, text = "Undo", width = 8, command = self.undo)
  60.         self.B3.pack(padx = 5, pady = 5, side = LEFT)
  61.  
  62.         self.B4 = Button(self.Main, text = "Redo", width = 8, command = self.redo)
  63.         self.B4.pack(padx = 5, pady = 5, side = LEFT)
  64.  
  65.         self.Main.pack(padx = 5, pady = 5)
  66.  
  67.  
  68.     def tagHighlight(self):
  69.         start = "1.0"
  70.         end = "end"
  71.          
  72.         for mylist in self.wordlist:
  73.             num = int(self.wordlist.index(mylist))
  74.  
  75.             for word in mylist:
  76.                 self.T1.mark_set("matchStart", start)
  77.                 self.T1.mark_set("matchEnd", start)
  78.                 self.T1.mark_set("SearchLimit", end)
  79.  
  80.                 mycount = IntVar()
  81.                  
  82.                 while True:
  83.                     index= self.T1.search(word,"matchEnd","SearchLimit", count=mycount, regexp = False)
  84.  
  85.                     if index == "": break
  86.                     if mycount.get() == 0: break
  87.  
  88.                     self.T1.mark_set("matchStart", index)
  89.                     self.T1.mark_set("matchEnd", "%s+%sc" % (index, mycount.get()))
  90.  
  91.                     preIndex = "%s-%sc" % (index, 1)
  92.                     postIndex = "%s+%sc" % (index, mycount.get())
  93.                      
  94.                     if self.check(index, preIndex, postIndex):
  95.                         self.T1.tag_add(self.tags[num], "matchStart", "matchEnd")
  96.                          
  97.  
  98.     def check(self, index, pre, post):
  99.         letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
  100.                    "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
  101.  
  102.         if self.T1.get(pre) == self.T1.get(index):
  103.             pre = index
  104.         else:
  105.             if self.T1.get(pre) in letters:
  106.                 return 0
  107.  
  108.         if self.T1.get(post) in letters:
  109.             return 0
  110.  
  111.         return 1
  112.  
  113.  
  114.     def scan(self):
  115.         start = "1.0"
  116.         end = "end"
  117.         mycount = IntVar()
  118.  
  119.         regex_patterns = [r'".*"', r'#.*']
  120.  
  121.         for pattern in regex_patterns:
  122.             self.T1.mark_set("start", start)
  123.             self.T1.mark_set("end", end)
  124.  
  125.             num = int(regex_patterns.index(pattern))
  126.  
  127.             while True:
  128.                 index = self.T1.search(pattern, "start", "end", count=mycount, regexp = True)
  129.  
  130.                 if index == "": break
  131.  
  132.                 if (num == 1):
  133.                     self.T1.tag_add(self.tags[5], index, index + " lineend")
  134.                 elif (num == 0):
  135.                     self.T1.tag_add(self.tags[4], index, "%s+%sc" % (index, mycount.get()))
  136.  
  137.                 self.T1.mark_set("start", "%s+%sc" % (index, mycount.get()))
  138.  
  139.  
  140.     def indent(self, widget):
  141.  
  142.         index1 = widget.index("insert")
  143.         index2 = "%s-%sc" % (index1, 1)
  144.         prevIndex = widget.get(index2, index1)
  145.  
  146.         prevIndentLine = widget.index(index1 + "linestart")
  147.         print("prevIndentLine ",prevIndentLine)
  148.         prevIndent = self.getIndex(prevIndentLine)
  149.         print("prevIndent ", prevIndent)
  150.  
  151.  
  152.         if prevIndex == ":":
  153.             widget.insert("insert", "\n" + "     ")
  154.             widget.mark_set("insert", "insert + 1 line + 5char")
  155.  
  156.             while widget.compare(prevIndent, ">", prevIndentLine):
  157.                 widget.insert("insert", "     ")
  158.                 widget.mark_set("insert", "insert + 5 chars")
  159.                 prevIndentLine += "+5c"
  160.             return "break"
  161.          
  162.         elif prevIndent != prevIndentLine:
  163.             widget.insert("insert", "\n")
  164.             widget.mark_set("insert", "insert + 1 line")
  165.  
  166.             while widget.compare(prevIndent, ">", prevIndentLine):
  167.                 widget.insert("insert", "     ")
  168.                 widget.mark_set("insert", "insert + 5 chars")
  169.                 prevIndentLine += "+5c"
  170.             return "break"
  171.  
  172.  
  173.     def getIndex(self, index):
  174.         while True:
  175.             if self.T1.get(index) == " ":
  176.                 index = "%s+%sc" % (index, 1)
  177.             else:
  178.                 return self.T1.index(index)
  179.            
  180.                    
  181.     def update(self):
  182.         self.stackify()
  183.         self.tagHighlight()
  184.         self.scan()
  185.  
  186.     def display(self):
  187.         print(self.T1.get("1.0", "end"))    
  188.  
  189.     def clear(self):
  190.         self.T1.delete("1.0", "end")
  191.  
  192.     def stackify(self):
  193.         self.stack.append(self.T1.get("1.0", "end - 1c"))
  194.         if self.stackcursor < 9: self.stackcursor += 1
  195.  
  196.     def undo(self):
  197.         if self.stackcursor != 0:
  198.             self.clear()
  199.             if self.stackcursor > 0: self.stackcursor -= 1
  200.             self.T1.insert("0.0", self.stack[self.stackcursor])
  201.  
  202.     def redo(self):
  203.         if len(self.stack) > self.stackcursor + 1:
  204.             self.clear()
  205.             if self.stackcursor < 9: self.stackcursor += 1
  206.             self.T1.insert("0.0", self.stack[self.stackcursor])
  207.  
  208.     def print_stack(self):
  209.         i = 0
  210.         for stack in self.stack:
  211.             print(str(i) + " " + stack)
  212.             i += 1
  213.  
  214.                      
  215. root = Tk()
  216. window = Window(root)
  217. root.bind("<Key>", lambda event: window.update())
  218. root.mainloop()
Add Comment
Please, Sign In to add comment