Skip to content

Commit

Permalink
fix: picking language for non-existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlin7 committed Dec 31, 2023
1 parent 5df4e3d commit d3e49f1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
7 changes: 6 additions & 1 deletion biscuit/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@


class App(tk.Tk):
"""BISCUIT CORE
"""
BISCUIT CORE
------------
Main point of having this class is to have a single point of access to all the important parts of the app. This class
holds reference to all the components of Biscuit and every class of biscuit have a reference back to this `base` class.
i.e. `self.base` is the reference to this class from any other class of biscuit.
Usage
-----
Example: In order to access the open editor from the git:
class Git:
Expand Down
3 changes: 1 addition & 2 deletions biscuit/core/components/editors/texteditor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def __init__(self, master, path=None, exists=True, language=None, minimalist=Fal
self.text.bind("<<Change>>", self.on_change)
self.text.bind("<<Scroll>>", self.on_scroll)

self.on_change()
self.on_scroll()
if not self.minimalist:
self.minimap.redraw_cursor()

if self.base.settings.config.auto_save_enabled:
self.auto_save()
Expand Down
41 changes: 13 additions & 28 deletions biscuit/core/components/editors/texteditor/highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __init__(self, text: Text, language: str=None, *args, **kwargs) -> None:
self.lexer = None
self.text.language = "Plain Text"
self.base.notifications.info("Selected lexer is not available.")
return
else:
try:
if os.path.basename(text.path).endswith("txt"):
Expand All @@ -50,8 +49,6 @@ def __init__(self, text: Text, language: str=None, *args, **kwargs) -> None:
if self.text.exists:
self.base.notifications.info("Unrecognized file type opened")

return

self.tag_colors = self.base.theme.syntax
self.setup_highlight_tags()

Expand All @@ -65,30 +62,18 @@ def change_language(self, language: str) -> None:
language : str
Language to highlight
"""
if language:
try:
self.lexer = get_lexer_by_name(language)
self.text.language = self.lexer.name
except:
self.lexer = None
self.text.language = "Plain Text"
self.base.notifications.info("Selected lexer is not available.")
return
else:
try:
if os.path.basename(self.text.path).endswith("txt"):
raise Exception()

self.lexer = get_lexer_for_filename(os.path.basename(self.text.path), encoding=self.text.encoding)
self.text.language = self.lexer.name
except:
self.lexer = None
self.text.language = "Plain Text"
if self.text.exists:
self.base.notifications.info("Unrecognized file type opened")
return

threading.Thread(target=self.text.refresh, daemon=True).start()
try:
self.lexer = get_lexer_by_name(language)
except:
self.lexer = None
self.text.language = "Plain Text"
self.base.notifications.info("Selected lexer is not available.")
return

self.text.language = self.lexer.name
self.tag_colors = self.base.theme.syntax
self.text.master.on_change()
self.base.statusbar.on_open_file(self.text)

def setup_highlight_tags(self) -> None:
"Setup the tags for highlighting"
Expand All @@ -97,7 +82,7 @@ def setup_highlight_tags(self) -> None:

def highlight(self) -> None:
"Highlights the text content of attached Editor instance"
if not self.lexer:
if not self.lexer or not self.tag_colors:
return

for token, _ in self.tag_colors.items():
Expand Down
13 changes: 9 additions & 4 deletions biscuit/core/components/editors/texteditor/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,26 @@ def __init__(self, master: TextEditor, path: str=None, exists: bool=True, minima
self.current_word = None
self.words: list[str] = []
self.lsp: bool = False

self.hover_after = None
self.last_hovered = None

if self.exists:
self.load_file()
self.update_idletasks()

self.last_change = Change(None, None, None, None, None)
# self.last_change = Change(None, None, None, None, None)
self.highlighter = Highlighter(self, language)
self.base.statusbar.on_open_file(self)
self.autocomplete = self.base.autocomplete
self.definitions = self.base.definitions
self.hover = self.base.hover
self.base.statusbar.on_open_file(self)

self.focus_set()
self.config_tags()
self.create_proxy()
self.config_bindings()
self.update_idletasks()
self.configure(wrap=tk.NONE, relief=tk.FLAT, highlightthickness=0, bd=0, **self.base.theme.editors.text)

# modified event
Expand All @@ -62,8 +66,6 @@ def __init__(self, master: TextEditor, path: str=None, exists: bool=True, minima
self._edit_stack_index = -1
self._last_selection: list[str, str] = [None, None]
self._last_cursor: list[str, str] = [None, None]
self.hover_after = None
self.last_hovered = None

def config_tags(self):
self.tag_config(tk.SEL, background=self.base.theme.editors.selection)
Expand Down Expand Up @@ -371,6 +373,9 @@ def lsp_show_autocomplete(self, response: Completions) -> None:

def lsp_diagnostics(self, response: Underlines) -> None:
print("LSP <<< ", response)
for i in response.underline_list:
# self.tag_add("error", f"{i.start[0]}.{i.start[1]}", f"{i.end[0]}.{i.end[1]}")
print(i.start, i.color, i.tooltip_text)

def lsp_goto_definition(self, response: Jump) -> None:
if not response.locations:
Expand Down
1 change: 1 addition & 0 deletions biscuit/core/layout/base/content/editors/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def set_active_tab(self, selected_tab: Tab) -> None:
for tab in self.tabs:
if tab != selected_tab:
tab.deselect()
self.master.master.refresh()

def clear_all_tabs(self) -> None:
for tab in self.tabs:
Expand Down

0 comments on commit d3e49f1

Please sign in to comment.