Skip to content

Commit

Permalink
fix: Fixed app crashes due to hover renderer issues #411
Browse files Browse the repository at this point in the history
 from tomlin7/hover-fix
  • Loading branch information
tomlin7 authored Oct 11, 2024
2 parents ae9b181 + 9485a49 commit c75d59f
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 88 deletions.
Binary file added resources/linux/icon.ico
Binary file not shown.
10 changes: 7 additions & 3 deletions src/biscuit/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def open_recent_file(self, *_):

def open_recent_dir(self, *_):
self.base.palette.show("recentd:")

def restore_last_closed_editor(self, *_) -> None:
self.base.editorsmanager.restore_last_closed_editor()

Expand Down Expand Up @@ -134,8 +134,12 @@ def minimize_biscuit(self, *_) -> None:
if platform.system() == "Windows":
from ctypes import windll

hwnd = windll.user32.GetParent(self.base.winfo_id())
windll.user32.ShowWindow(hwnd, 6)
try:
hwnd = windll.user32.GetParent(self.base.winfo_id())
windll.user32.ShowWindow(hwnd, 6)
except Exception as e:
print(e)
self.base.withdraw()
else:
self.base.withdraw()
self.base.notifications.hide()
Expand Down
15 changes: 9 additions & 6 deletions src/biscuit/common/ui/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ def set_callback(self, event) -> None:

def onclick(self, *args) -> None:
try:
self.event(*args)
except:
self.event()

self.v_onclick()
self.toggle_icon()
try:
self.event(*args)
except:
self.event()

self.v_onclick()
self.toggle_icon()
except Exception as e:
print(e)

def v_onclick(self) -> None: ...

Expand Down
57 changes: 28 additions & 29 deletions src/biscuit/editor/hover/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import tkinter as tk
import typing

from biscuit.common.ui import Frame, Toplevel
from biscuit.common.ui import Frame
from biscuit.common.ui import Text as TextW
from biscuit.common.ui import Toplevel

from .renderer import HoverRenderer

Expand All @@ -14,26 +16,20 @@
from ..text import Text


def codeblock(text: str, language: str) -> str:
return f"```{language}\n{text}\n```\n\n---\n\n"


class Hover(Toplevel):
def __init__(self, master: App, bd: int = 2, *args, **kw) -> None:
super().__init__(master, *args, **kw)
self.overrideredirect(True)
self.maxsize(400, 200)
self.maxsize(600, 200)
self.config(bg=self.base.theme.primary_background_highlight, bd=bd)

container = Frame(self, bg=self.base.theme.border)
container.pack(fill=tk.BOTH, expand=True)

self.label = tk.Label(
container,
padx=6,
font=self.base.settings.font,
anchor=tk.W,
justify=tk.LEFT,
**self.base.theme.editors.hover.text,
)
self.label.pack(fill=tk.BOTH, expand=True)

self.renderer = HoverRenderer(container)
self.renderer.pack(fill=tk.BOTH, expand=True)

Expand All @@ -55,26 +51,29 @@ def update_position(self, pos: str, tab: Text):
)

def show(self, tab: Text, response: HoverResponse) -> None:
if response.text:
if response.docs:
self.renderer.pack_forget()
self.label.config(text=response.text[1])
self.label.pack(fill=tk.BOTH, expand=True)
else:
self.label.pack_forget()

if response.docs:
self.renderer.render_markdown(response.docs)
self.renderer.pack(fill=tk.BOTH, pady=(1, 0) if response.text else 0)
else:
self.renderer.pack_forget()
if not response or not (response.text or response.docs):
self.hide()
return

self.update()
self.deiconify()
docs = ""
try:
self.update_position(response.location, tab)
except:
if response.text[1].strip():
docs += codeblock(response.text[1].strip(), tab.language_alias)
except IndexError:
pass

if response.docs and response.docs.strip():
docs += response.docs

if not docs or not docs.strip():
self.hide()
return

self.renderer.render_markdown(docs)
self.update_idletasks()
self.deiconify()
self.update()
self.update_position(response.location, tab)

def hide(self, *_) -> None:
self.withdraw()
24 changes: 18 additions & 6 deletions src/biscuit/editor/hover/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def __iter__(self):

class HoverRenderer(HtmlFrame):
def __init__(self, master: Hover, *args, **kwargs) -> None:
super().__init__(
master, messages_enabled=False, vertical_scrollbar=False, *args, **kwargs
)
super().__init__(master, messages_enabled=False, *args, **kwargs)
self.base = master.base
self.html.shrink(True)

# NOTE: Causing app to crash
# self.html.shrink(True)

self.style = BiscuitStyle(self.base.theme)

Expand All @@ -102,12 +102,23 @@ def __init__(self, master: Hover, *args, **kwargs) -> None:
pygments_css = self.formatter.get_style_defs(".highlight")
self.css = f"""
{pygments_css}
HTML {{
width: fit-content;
height: fit-content;
}}
CODE, PRE {{
font-family: {self.base.settings.font['family']};
font-size: {self.base.settings.font['size']}pt;
background-color: {t.border};
padding: 2px;
}}
.highlight {{
width: 100%;
}}
BODY {{
background-color: {t.secondary_background};
color: {t.secondary_foreground};
Expand Down Expand Up @@ -144,7 +155,8 @@ def __init__(self, master: Hover, *args, **kwargs) -> None:
}}
"""

def render_markdown(self, rawmd):
self.load_html(self.markdown(rawmd))
def render_markdown(self, docs) -> None:
self.load_html(self.markdown(docs))
print(self.markdown(docs))
self.add_css(self.css)
self.update_idletasks()
2 changes: 0 additions & 2 deletions src/biscuit/language/NOTICE

This file was deleted.

82 changes: 41 additions & 41 deletions src/biscuit/language/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@
import tarts as lsp


# Copyright (c) 2021-2024 tomlin7
def to_document_symbol(infos: list[lsp.SymbolInformation]) -> list[lsp.DocumentSymbol]:
if not infos:
return []

infos.sort(
key=lambda x: (
(x.location.range.start.line, x.location.range.start.character),
(-x.location.range.end.line, -x.location.range.end.character),
)
)
res: list[lsp.DocumentSymbol] = []
parents: list[lsp.DocumentSymbol] = []

for info in infos:
element = lsp.DocumentSymbol(
name=info.name or "Error Symbol",
kind=info.kind,
children=[],
range=info.location.range,
selectionRange=info.location.range,
)

while True:
if not parents:
parents.append(element)
res.append(element)
break
parent = parents[-1]
if contains_range(parent.range, element.range) and not equals_range(
parent.range, element.range
):
# TODO avoid adding the same named element twice to same parent
parent.children.append(element)
parents.append(element)
break
parents.pop()

return res


def get_completion_item_doc(item: lsp.CompletionItem) -> str:
if not item.documentation:
return item.label
Expand Down Expand Up @@ -116,44 +157,3 @@ def equals_range(a: lsp.Range, b: lsp.Range) -> bool:
and a.end.line == b.end.line
and a.end.character == b.end.character
)


# Copyright (c) 2021-2024 tomlin7
def to_document_symbol(infos: list[lsp.SymbolInformation]) -> list[lsp.DocumentSymbol]:
if not infos:
return []

infos.sort(
key=lambda x: (
(x.location.range.start.line, x.location.range.start.character),
(-x.location.range.end.line, -x.location.range.end.character),
)
)
res: list[lsp.DocumentSymbol] = []
parents: list[lsp.DocumentSymbol] = []

for info in infos:
element = lsp.DocumentSymbol(
name=info.name or "Error Symbol",
kind=info.kind,
children=[],
range=info.location.range,
selectionRange=info.location.range,
)

while True:
if not parents:
parents.append(element)
res.append(element)
break
parent = parents[-1]
if contains_range(parent.range, element.range) and not equals_range(
parent.range, element.range
):
# TODO avoid adding the same named element twice to same parent
parent.children.append(element)
parents.append(element)
break
parents.pop()

return res
2 changes: 1 addition & 1 deletion src/biscuit/views/debug/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def open_config_editor(self, *_):
if self.base.active_directory
else ".biscuit/launch.toml"
)
self.base.open_editor(path)
self.base.open_editor(str(path))

def refresh(self):
if self.manager.config_loader.config_found or self.running:
Expand Down

0 comments on commit c75d59f

Please sign in to comment.