-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Code debugging support (built-in python debugger) #292
from tomlin7/debugger
- Loading branch information
Showing
12 changed files
with
218 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from __future__ import annotations | ||
|
||
import typing | ||
|
||
if typing.TYPE_CHECKING: | ||
from biscuit.core.components.editors import TextEditor | ||
|
||
from .output import DebuggerInfo | ||
from .python_dbd import PythonDebugger | ||
|
||
|
||
def get_debugger(editor: TextEditor): | ||
if editor.text.language == "Python": | ||
return PythonDebugger(editor) | ||
|
||
# others are not supported yet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from biscuit.core.utils import Text, Toplevel | ||
|
||
|
||
class DebuggerInfo(Toplevel): | ||
"""A Toplevel window that displays information about the debugger.""" | ||
|
||
def __init__(self, master, *args, **kwargs): | ||
super().__init__(master, *args, **kwargs) | ||
self.title("Debugger Information") | ||
self.text_widget = Text(self, wrap='word', width=60, height=50) | ||
self.text_widget.config(font=self.base.settings.font, **self.base.theme.editors.text) | ||
self.text_widget.pack(fill='both', expand=True) | ||
self.withdraw() | ||
|
||
self.wm_protocol("WM_DELETE_WINDOW", self.withdraw) | ||
|
||
def clear(self): | ||
"""Clear the output.""" | ||
|
||
try: | ||
self.text_widget.delete('1.0', 'end') | ||
except: | ||
pass | ||
|
||
def show_variables(self, frame): | ||
"""Show the local variables in the given frame. | ||
Args: | ||
frame (frame): The frame to show the local variables of.""" | ||
|
||
locals_str = "Local Variables:\n" | ||
for var, val in frame.f_locals.items(): | ||
locals_str += f"{var}: {val}\n" | ||
self.text_widget.insert('end', locals_str) | ||
|
||
def show_callstack(self, frame): | ||
"""Show the call stack from the given frame. | ||
Args: | ||
frame (frame): The frame to show the call stack from.""" | ||
|
||
callstack_str = "\nCall Stack:\n" | ||
while frame: | ||
callstack_str += f"{frame.f_code.co_name} at {frame.f_code.co_filename}, line {frame.f_lineno}\n" | ||
frame = frame.f_back | ||
self.text_widget.insert('end', callstack_str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from __future__ import annotations | ||
|
||
import bdb | ||
import typing | ||
|
||
if typing.TYPE_CHECKING: | ||
from biscuit.core.components.editors import TextEditor | ||
|
||
|
||
class PythonDebugger(bdb.Bdb): | ||
"""Python debugger class. | ||
This class is used to debug Python code. It uses python's standard bdb and | ||
overrides some of its methods to provide custom functionality. The debugger | ||
information is displayed in a DebuggerInfo window. | ||
Args: | ||
editor: The TextEditor instance that is being debugged.""" | ||
|
||
def __init__(self, editor: TextEditor): | ||
super().__init__() | ||
self.editor = self.master = editor | ||
self.base = editor.base | ||
self.file_path = self.editor.path | ||
self.output = self.base.debugger_info | ||
|
||
def run(self, *_): | ||
"""Debug the code in the editor.""" | ||
self.reset() | ||
|
||
with open(self.file_path, 'r') as f: | ||
code = compile(f.read(), self.file_path, 'exec') | ||
|
||
self.clear_all_breaks() | ||
for line_number in self.editor.breakpoints: | ||
self.set_break(self.file_path, line_number) | ||
|
||
self.set_trace() | ||
exec(code, globals(), locals()) | ||
|
||
def user_line(self, frame): | ||
self.output.clear() | ||
self.output.show_variables(frame) | ||
self.output.show_callstack(frame) | ||
self.output.deiconify() | ||
|
||
self.set_continue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import tkinter as tk | ||
|
||
from biscuit.core.utils import Canvas, Menubutton | ||
|
||
|
||
class LineNumbers(Canvas): | ||
def __init__(self, master, text=None, font=None, *args, **kwargs) -> None: | ||
super().__init__(master, *args, **kwargs) | ||
self.font = font | ||
self.config(width=65, bd=0, highlightthickness=0, **self.base.theme.editors.linenumbers) | ||
self.text = text | ||
|
||
self.bg, self.fg, _, self.hfg = self.base.theme.editors.linenumbers.number.values() | ||
self.bp_hover_color, _, self.bp_enabled_color, _ = self.base.theme.editors.linenumbers.breakpoint.values() | ||
self.breakpoints = set() | ||
|
||
def attach(self, text): | ||
self.text = text | ||
|
||
def mark_line(self, line): | ||
dline = self.text.dlineinfo(line) | ||
|
||
if not dline: | ||
return | ||
|
||
y = dline[1] | ||
btn = Menubutton(self, | ||
text=">", font=self.font, cursor="hand2", borderwidth=0, | ||
width=2, height=1, pady=0, padx=0, relief=tk.FLAT, **self.base.theme.linenumbers) | ||
self.create_window(70, y-2, anchor=tk.NE, window=btn) | ||
|
||
def set_bar_width(self, width): | ||
self.configure(width=width) | ||
|
||
def toggle_breakpoint(self, line): | ||
if line in self.breakpoints: | ||
self.breakpoints.remove(line) | ||
else: | ||
self.breakpoints.add(line) | ||
self.redraw() | ||
|
||
def redraw(self, *_): | ||
self.delete(tk.ALL) | ||
|
||
if not self.text: | ||
return | ||
|
||
i = self.text.index("@0,0") | ||
while True: | ||
dline = self.text.dlineinfo(i) | ||
if dline is None: | ||
break | ||
|
||
y = dline[1] | ||
linenum = int(float(i)) | ||
curline = self.text.dlineinfo(tk.INSERT) | ||
cur_y = curline[1] if curline else None | ||
|
||
# Render breakpoint | ||
has_breakpoint = linenum in self.breakpoints | ||
breakpoint_id = self.create_oval(5, y + 3, 15, y + 13, outline="", fill=self.bg if not has_breakpoint else self.bp_enabled_color) | ||
|
||
# Bind hover and click events for breakpoint | ||
self.tag_bind(breakpoint_id, "<Enter>", | ||
lambda _, breakpoint_id=breakpoint_id, flag=has_breakpoint: self.on_breakpoint_enter(breakpoint_id, flag)) | ||
self.tag_bind(breakpoint_id, "<Leave>", | ||
lambda _, breakpoint_id=breakpoint_id, flag=has_breakpoint: self.on_breakpoint_leave(breakpoint_id, flag)) | ||
self.tag_bind(breakpoint_id, "<Button-1>", | ||
lambda _, linenum=linenum: self.toggle_breakpoint(linenum)) | ||
|
||
self.create_text(40, y, anchor=tk.NE, text=linenum, font=self.font, tag=i, fill=self.hfg if y == cur_y else self.fg) | ||
i = self.text.index(f"{i}+1line") | ||
|
||
def on_breakpoint_enter(self, id, flag): | ||
self.itemconfig(id, fill=self.bp_enabled_color if flag else self.bp_hover_color) | ||
|
||
def on_breakpoint_leave(self, id, flag): | ||
self.itemconfig(id, fill=self.bp_enabled_color if flag else self.bg) |
63 changes: 0 additions & 63 deletions
63
biscuit/core/components/editors/texteditor/linenumbers/__init__.py
This file was deleted.
Oops, something went wrong.
43 changes: 0 additions & 43 deletions
43
biscuit/core/components/editors/texteditor/linenumbers/breakpoint.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.