the lsp kinda works but is very buggy and crashes often

This commit is contained in:
2026-05-17 15:37:38 +10:00
parent fe184e1c2a
commit 5817ca36e6
4 changed files with 395 additions and 11 deletions

89
main.py
View File

@@ -15,10 +15,13 @@ from plugin_loader import PluginLoader
from settings import SettingsScreen
from settings_store import ConfigHandler
from directory_tree_custom import CustomDirectoryTree
from completions_menu import CompletionsMenu
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from lsp_client import LSPClient
import os, sys
@@ -61,6 +64,13 @@ class Berry(App):
self.current_editor = None
self.file_tabs = {}
self.completions_menu = None
self.current_lsp = None
self.running_lsps = {
"py": LSPClient("jedi-language-server", [], "python")
}
self.last_change_pos = None
def compose(self) -> ComposeResult:
yield Header()
@@ -244,11 +254,25 @@ class Berry(App):
else:
tabs.active = self.file_tabs[str(event.path)].id
def open_text_editor(self, file_content: bytes, file_extension: str):
code_editor = TextArea.code_editor(placeholder="This file is empty.", theme="css", disabled=True, soft_wrap=bool(int(self.config_handler.get("editor", "word_wrap"))), show_line_numbers=bool(int(self.config_handler.get("editor", "line_numbers"))))
async def open_text_editor(self, file_content: bytes, file_extension: str):
code_editor = TextArea.code_editor(placeholder="This file is empty.", classes="code-editor", theme="css", disabled=True, soft_wrap=bool(int(self.config_handler.get("editor", "word_wrap"))), show_line_numbers=bool(int(self.config_handler.get("editor", "line_numbers"))))
self.current_editor.mount(code_editor)
await self.current_editor.mount(code_editor)
# start lsp
if file_extension in self.running_lsps:
lsp = self.running_lsps[file_extension]
self.current_lsp = file_extension
if not lsp.running:
await lsp.start(self.open_file)
else:
await lsp.open_file(self.open_file)
else:
self.current_lsp = None
# setup text editor
code_editor.language = theme_mappings.get(file_extension, None)
try:
@@ -261,17 +285,17 @@ class Berry(App):
code_editor.focus()
def open_editor(self, file_extension: str, file_content: bytes):
async def open_editor(self, file_extension: str, file_content: bytes):
default_editors: dict = eval(self.config_handler.get("editor", "default_editors"))
editor_for_file = default_editors.get(file_extension, None)
self.current_editor.remove_children()
if not editor_for_file:
self.open_text_editor(file_content, file_extension)
await self.open_text_editor(file_content, file_extension)
@on(Tabs.TabActivated)
def on_tab_shown(self, event: Tabs.TabActivated):
async def on_tab_shown(self, event: Tabs.TabActivated):
if self.file_clicked:
self.file_clicked = False
@@ -307,7 +331,7 @@ class Berry(App):
# close the file
f.close()
self.open_editor(file_extension, content)
await self.open_editor(file_extension, content)
@@ -316,7 +340,10 @@ class Berry(App):
def window_minimized(self, event: Window.Minimized):
event.window.remove_window()
def on_text_area_changed(self, event: TextArea.Changed):
async def on_text_area_changed(self, event: TextArea.Changed):
if not event.text_area.has_class("code-editor"):
return
if self.switching:
self.switching = False
return
@@ -329,7 +356,7 @@ class Berry(App):
with open(self.open_file, "r", encoding="utf-8") as f:
if f.read() == event.text_area.text: # TODO: figure out why im guetting what seems like a race conidition which is making this if statement needed
return
self.unsaved_files[self.open_file] = {"current": event.text_area.text, "original": f.read()}
self.unsaved_files[self.open_file] = {"current": event.text_area.text.encode(), "original": f.read()}
tabs.active_tab.tooltip = f"Unsaved changes in {tabs.active_tab.label}"
@@ -341,6 +368,48 @@ class Berry(App):
tabs.active_tab.label = os.path.basename(self.open_file)
tabs.active_tab.tooltip = str(tabs.active_tab.label)
self.unsaved_files.pop(self.open_file)
await self.run_completions()
async def run_completions(self):
code_editor: TextArea = self.current_editor.query_one(TextArea)
if len(code_editor.text.strip()) == 0: return
lsp: LSPClient = self.running_lsps[self.current_lsp]
line, col = code_editor.cursor_location
# ensure we're on the latest version of the file when we apply completions or else we will error
if self.last_change_pos:
await lsp.apply_change(code_editor.text)
self.last_change_pos = (line, col)
self.log("waiting for completions")
completions = await lsp.get_completions((line,col-1))
self.log("god completions")
self.notify(str(completions.get("error", None)))
if not completions["result"]:
if self.completions_menu:
self.completions_menu.hide()
return
else:
if self.completions_menu:
self.completions_menu.show()
completion_choices = [{"text": result["label"], "type": result["kind"]} for result in completions["result"]["items"]]
if not self.completions_menu:
self.completions_menu = CompletionsMenu(completion_choices, code_editor)
await self.current_editor.mount(self.completions_menu)
else:
self.completions_menu.align_to_cursor()
self.completions_menu.update(completion_choices)
def action_new(self):
tabs: Tabs = self.query_one("#file-tabs")
@@ -390,7 +459,7 @@ class Berry(App):
self.observer.join()
return super().action_quit()
def on_ready(self):
async def on_ready(self):
self.open_file = None
self.unsaved_files = {} # list of paths
self.switching = False