|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import filedialog |
| 3 | +from tkinter import messagebox |
| 4 | +from tkinter import simpledialog |
| 5 | + |
| 6 | +from prompt_toolkit import prompt |
| 7 | +from prompt_toolkit.completion import WordCompleter |
| 8 | +def apply_syntax_cat(self, language): |
| 9 | + # Remove existing tags |
| 10 | + self.text.tag_remove("syntax", "1.0", tk.END) |
| 11 | + |
| 12 | + if language == "python": |
| 13 | + # Configure Pygments lexer and formatter |
| 14 | + lexer = PythonLexer() |
| 15 | + formatter = get_formatter_by_name("html", linenos=False) |
| 16 | + |
| 17 | + # Apply syntax highlighting |
| 18 | + code = self.text.get("1.0", tk.END) |
| 19 | + highlighted_code = pygments.highlight(code, lexer, formatter) |
| 20 | + |
| 21 | + # Insert the highlighted code |
| 22 | + self.text.delete("1.0", tk.END) |
| 23 | + self.text.insert(tk.END, highlighted_code, "syntax") |
| 24 | + |
| 25 | + # Configure the tag for syntax highlighting |
| 26 | + self.text.tag_configure("syntax", font=("Courier New", 10)) |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +class TextEditorApp: |
| 31 | + def __init__(self, root): |
| 32 | + self.root = root |
| 33 | + self.root.title("PANNO!") |
| 34 | + self.text = tk.Text(root) |
| 35 | + self.text.pack(fill=tk.BOTH, expand=True) |
| 36 | + self.undo_stack = [] |
| 37 | + self.redo_stack = [] |
| 38 | + self.bind_keyboard_shortcuts() |
| 39 | + self.current_file = None # Variable to store the current file name |
| 40 | + |
| 41 | + # Create the menu bar |
| 42 | + menu_bar = tk.Menu(root) |
| 43 | + file_menu = tk.Menu(menu_bar, tearoff=0) |
| 44 | + file_menu.add_command(label="Open", command=self.load_file) |
| 45 | + file_menu.add_command(label="Save", command=self.save_file) |
| 46 | + menu_bar.add_cascade(label="File", menu=file_menu) |
| 47 | + root.config(menu=menu_bar) |
| 48 | + |
| 49 | + # Create the toolbar |
| 50 | + toolbar = tk.Frame(root) |
| 51 | + toolbar.pack(side=tk.TOP, fill=tk.X) |
| 52 | + |
| 53 | + load_button = tk.Button(toolbar, text="Load", command=self.load_file) |
| 54 | + load_button.pack(side=tk.LEFT) |
| 55 | + |
| 56 | + save_button = tk.Button(toolbar, text="Save", command=self.save_file) |
| 57 | + save_button.pack(side=tk.LEFT) |
| 58 | + |
| 59 | + undo_button = tk.Button(toolbar, text="highlight", command=apply_syntax_cat(self,python)) |
| 60 | + undo_button.pack(side=tk.LEFT) |
| 61 | + |
| 62 | + redo_button = tk.Button(toolbar, text="Redo", command=self.redo) |
| 63 | + redo_button.pack(side=tk.LEFT) |
| 64 | + |
| 65 | + self.root.bind("<Control-space>", self.complete) |
| 66 | + |
| 67 | + def bind_keyboard_shortcuts(self): |
| 68 | + self.root.bind("<Control-s>", self.save_file) |
| 69 | + self.root.bind("<Control-z>", self.undo) |
| 70 | + self.root.bind("<Control-y>", self.redo) |
| 71 | + |
| 72 | + def load_file(self, event=None): |
| 73 | + file_path = filedialog.askopenfilename() |
| 74 | + if file_path: |
| 75 | + try: |
| 76 | + with open(file_path, "r") as file: |
| 77 | + self.text.delete("1.0", tk.END) |
| 78 | + self.text.insert(tk.END, file.read()) |
| 79 | + self.current_file = file_path # Update the current file name |
| 80 | + self.update_title() # Update the title |
| 81 | + except FileNotFoundError: |
| 82 | + messagebox.showerror("Error", "File not found.") |
| 83 | + except Exception as e: |
| 84 | + messagebox.showerror("Error", f"An error occurred: {str(e)}") |
| 85 | + |
| 86 | + def save_file(self, event=None): |
| 87 | + if self.current_file: |
| 88 | + try: |
| 89 | + with open(self.current_file, "w") as file: |
| 90 | + file.write(self.text.get("1.0", tk.END)) |
| 91 | + messagebox.showinfo("Save", "File saved successfully.") |
| 92 | + except Exception as e: |
| 93 | + messagebox.showerror("Error", f"An error occurred: {str(e)}") |
| 94 | + else: |
| 95 | + file_path = filedialog.asksaveasfilename(defaultextension=".txt") |
| 96 | + if file_path: |
| 97 | + try: |
| 98 | + with open(file_path, "w") as file: |
| 99 | + file.write(self.text.get("1.0", tk.END)) |
| 100 | + self.current_file = file_path # Update the current file name |
| 101 | + self.update_title() # Update the title |
| 102 | + messagebox.showinfo("Save", "File saved successfully.") |
| 103 | + except Exception as e: |
| 104 | + messagebox.showerror("Error", f"An error occurred: {str(e)}") |
| 105 | + |
| 106 | + def undo(self, event=None): |
| 107 | + if self.undo_stack: |
| 108 | + text = self.undo_stack.pop() |
| 109 | + self.redo_stack.append(self.text.get("1.0", tk.END)) |
| 110 | + self.text.delete("1.0",) |
| 111 | + self.text.insert(tk.END, text) |
| 112 | + |
| 113 | + def redo(self, event=None): |
| 114 | + if self.redo_stack: |
| 115 | + text = self.redo_stack.pop() |
| 116 | + self.undo_stack.append(self.text.get("1.0", tk.END)) |
| 117 | + self.text.delete("1.0", tk.END) |
| 118 | + self.text.insert(tk.END, text) |
| 119 | + |
| 120 | + def complete(self, event=None): |
| 121 | + # Check if a file is loaded |
| 122 | + if self.current_file: |
| 123 | + file_extension = self.current_file[self.current_file.rfind('.'):] |
| 124 | + |
| 125 | + completer = self.get_completer(file_extension) |
| 126 | + |
| 127 | + # Get the current cursor position |
| 128 | + cursor_position = self.text.index(tk.INSERT) |
| 129 | + |
| 130 | + # Split the code into lines |
| 131 | + lines = self.text.get("1.0", tk.END).split("\n") |
| 132 | + current_line = int(cursor_position.split(".")[0]) - 1 |
| 133 | + |
| 134 | + # Get the code from the current line |
| 135 | + current_code = lines[current_line] |
| 136 | + |
| 137 | + # Get the code before the cursor position on the current line |
| 138 | + code_before_cursor = current_code[:int(cursor_position.split(".")[1]) - 1] |
| 139 | + |
| 140 | + # Calculate the starting position of the current code |
| 141 | + start_position = sum(len(line) + 1 for line in lines[:current_line]) + len(code_before_cursor) + 1 |
| 142 | + |
| 143 | + # Set the start and end positions for the prompt_toolkit completion |
| 144 | + completer.position = len(code_before_cursor) + 1 |
| 145 | + completer.document = prompt_toolkit.document.Document(code_before_cursor, cursor_position=start_position) |
| 146 | + |
| 147 | + # Run the prompt_toolkit completion |
| 148 | + completion = prompt("", completer=completer) |
| 149 | + |
| 150 | + # Insert the completed code at the cursor position |
| 151 | + self.text.insert(tk.INSERT, completion) |
| 152 | + |
| 153 | + def get_completer(self, file_extension): |
| 154 | + if file_extension == '.py': |
| 155 | + return WordCompleter([ |
| 156 | + 'print', |
| 157 | + 'if', |
| 158 | + 'for', |
| 159 | + 'while', |
| 160 | + 'def', |
| 161 | + 'class', |
| 162 | + 'import', |
| 163 | + 'from', |
| 164 | + 'True', |
| 165 | + 'False', |
| 166 | + 'None', |
| 167 | + ]) |
| 168 | + elif file_extension == '.html': |
| 169 | + return WordCompleter([ |
| 170 | + 'html', |
| 171 | + 'head', |
| 172 | + 'body', |
| 173 | + 'div', |
| 174 | + 'p', |
| 175 | + 'a', |
| 176 | + 'img', |
| 177 | + 'table', |
| 178 | + 'tr', |
| 179 | + 'td', |
| 180 | + 'span', |
| 181 | + 'style', |
| 182 | + 'script', |
| 183 | + 'link', |
| 184 | + ]) |
| 185 | + elif file_extension == '.css': |
| 186 | + return WordCompleter([ |
| 187 | + 'color', |
| 188 | + 'background-color', |
| 189 | + 'font-size', |
| 190 | + 'font-family', |
| 191 | + 'margin', |
| 192 | + 'padding', |
| 193 | + 'width', |
| 194 | + 'height', |
| 195 | + 'border', |
| 196 | + 'display', |
| 197 | + 'position', |
| 198 | + 'float', |
| 199 | + 'text-align', |
| 200 | + 'border-radius', |
| 201 | + 'box-shadow', |
| 202 | + ]) |
| 203 | + else: |
| 204 | + return WordCompleter([]) # Default empty completer |
| 205 | + |
| 206 | + def update_title(self): |
| 207 | + if self.current_file: |
| 208 | + self.root.title(f"Text Editor - {self.current_file}") |
| 209 | + else: |
| 210 | + self.root.title("Text Editor") |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == "__main__": |
| 217 | + root = tk.Tk() |
| 218 | + app = TextEditorApp(root) |
| 219 | + root.mainloop() |
| 220 | + |
0 commit comments