Refactor LSP manager and file handling
- Refactored LSPClient and LSPClientEvents to use workspace path and initialization options. - Updated LSPManager to pass workspace path to client creation. - Modified LSPManagerUI to handle user home paths and workspace configurations. - Enhanced file handling to manage buffer and directory types. - Introduced CreatedSourceViewEvent for source view creation event tracking. - Added better error handling and logging for file operations. Other minor UI and widget adjustments for improved layout and drag-and-drop handling.
This commit is contained in:
@@ -19,13 +19,10 @@ class LSPClient(LSPClientWebsocket):
|
||||
def __init__(self):
|
||||
super(LSPClient, self).__init__()
|
||||
|
||||
# https://github.com/microsoft/multilspy/tree/main/src/multilspy/language_servers
|
||||
# initialize-params-slim.json was created off of jedi_language_server one
|
||||
# self._init_params = settings_manager.get_lsp_init_data()
|
||||
|
||||
self._language: str = ""
|
||||
self._workspace_path: str = ""
|
||||
self._init_params: dict = {}
|
||||
self._event_history: dict[int, str] = {}
|
||||
self._init_opts: dict = {}
|
||||
|
||||
try:
|
||||
_USER_HOME = path.expanduser('~')
|
||||
@@ -33,19 +30,29 @@ class LSPClient(LSPClientWebsocket):
|
||||
_LSP_INIT_CONFIG = f"{_SCRIPT_PTH}/../configs/initialize-params-slim.json"
|
||||
|
||||
with open(_LSP_INIT_CONFIG) as file:
|
||||
data = file.read().replace("{user.home}", _USER_HOME)
|
||||
data = file.read()
|
||||
self._init_params = json.loads(data)
|
||||
except Exception as e:
|
||||
logger.error( f"LSP Controller: {_LSP_INIT_CONFIG}\n\t\t{repr(e)}" )
|
||||
|
||||
self._message_id: int = -1
|
||||
self._socket = None
|
||||
self.read_lock = threading.Lock()
|
||||
self.write_lock = threading.Lock()
|
||||
|
||||
self._socket = None
|
||||
self._message_id: int = -1
|
||||
self._event_history: dict[int, str] = {}
|
||||
|
||||
self.read_lock = threading.Lock()
|
||||
self.write_lock = threading.Lock()
|
||||
|
||||
|
||||
def set_language(self, language: str):
|
||||
self._language = language
|
||||
|
||||
def set_workspace_path(self, workspace_path: str):
|
||||
self._workspace_path = workspace_path
|
||||
|
||||
def set_init_opts(self, init_opts: dict[str, str]):
|
||||
self._init_opts = init_opts
|
||||
|
||||
def set_socket(self, socket: str):
|
||||
self._socket = socket
|
||||
|
||||
|
||||
@@ -17,11 +17,12 @@ from ..dto.code.lsp.lsp_messages import symbols_request
|
||||
|
||||
|
||||
class LSPClientEvents:
|
||||
def send_initialize_message(self, init_ops: dict, workspace_file: str, workspace_uri: str):
|
||||
folder_name = os.path.basename(workspace_file)
|
||||
def send_initialize_message(self):
|
||||
folder_name = os.path.basename(self._workspace_path)
|
||||
workspace_uri = f"file://{self._workspace_path}"
|
||||
|
||||
self._init_params["processId"] = None
|
||||
self._init_params["rootPath"] = workspace_file
|
||||
self._init_params["rootPath"] = self._workspace_path
|
||||
self._init_params["rootUri"] = workspace_uri
|
||||
self._init_params["workspaceFolders"] = [
|
||||
{
|
||||
@@ -30,7 +31,7 @@ class LSPClientEvents:
|
||||
}
|
||||
]
|
||||
|
||||
self._init_params["initializationOptions"] = init_ops
|
||||
self._init_params["initializationOptions"] = self._init_opts
|
||||
self.send_request("initialize", self._init_params)
|
||||
|
||||
def send_initialized_message(self):
|
||||
|
||||
@@ -54,17 +54,21 @@ class LSPManager(ControllerBase):
|
||||
self.response_registry.unregister_handler(event.lang_id)
|
||||
self.lsp_manager_ui.remove_client_listing(event.lang_id)
|
||||
|
||||
def _on_create_client(self, ui, lang_id: str, workspace_uri: str) -> bool:
|
||||
def _on_create_client(self, ui, lang_id: str, workspace_path: str) -> bool:
|
||||
init_opts = ui.get_init_opts(lang_id)
|
||||
result = self.create_client(lang_id, workspace_uri, init_opts)
|
||||
result = self.create_client(lang_id, workspace_path, init_opts)
|
||||
|
||||
if result:
|
||||
ui.toggle_client_buttons(show_close=True)
|
||||
ui.toggle_client_buttons(show_close = True)
|
||||
|
||||
return result
|
||||
|
||||
def _on_close_client(self, ui, lang_id: str) -> bool:
|
||||
result = self.close_client(lang_id)
|
||||
|
||||
if result:
|
||||
ui.toggle_client_buttons(show_close=False)
|
||||
ui.toggle_client_buttons(show_close = False)
|
||||
|
||||
return result
|
||||
|
||||
def handle_destroy(self):
|
||||
@@ -73,12 +77,12 @@ class LSPManager(ControllerBase):
|
||||
|
||||
def create_client(
|
||||
self,
|
||||
lang_id: str = "python",
|
||||
workspace_uri: str = "",
|
||||
init_opts: dict = {}
|
||||
lang_id: str,
|
||||
workspace_path: str,
|
||||
init_opts: dict[str, str]
|
||||
) -> bool:
|
||||
client = self.lsp_manager_client.create_client(
|
||||
lang_id, workspace_uri, init_opts
|
||||
lang_id, workspace_path, init_opts
|
||||
)
|
||||
handler = self.response_registry.get_handler(lang_id)
|
||||
self.lsp_manager_client.active_language_id = lang_id
|
||||
@@ -92,7 +96,7 @@ class LSPManager(ControllerBase):
|
||||
handler.set_response_cache(self.response_cache)
|
||||
|
||||
client.handle_lsp_response = self.server_response
|
||||
client.send_initialize_message(init_opts, "", f"file://{workspace_uri}")
|
||||
client.send_initialize_message()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@@ -22,9 +22,9 @@ class LSPManagerClient(LSPClientEventsMixin):
|
||||
|
||||
def create_client(
|
||||
self,
|
||||
lang_id: str = "python",
|
||||
workspace_uri: str = "",
|
||||
init_opts: dict = {}
|
||||
lang_id: str,
|
||||
workspace_path: str,
|
||||
init_opts: dict[str, str]
|
||||
) -> LSPClient:
|
||||
if lang_id in self.clients: return None
|
||||
|
||||
@@ -33,8 +33,10 @@ class LSPManagerClient(LSPClientEventsMixin):
|
||||
uri = f"ws://{address}:{port}/{lang_id}"
|
||||
client = LSPClient()
|
||||
|
||||
client.set_language(lang_id)
|
||||
client.set_socket(uri)
|
||||
client.set_language(lang_id)
|
||||
client.set_workspace_path(workspace_path)
|
||||
client.set_init_opts(init_opts)
|
||||
client.start_client()
|
||||
|
||||
if not client.ws_client.wait_for_connection(timeout = 5.0):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# Python imports
|
||||
from os import path
|
||||
import json
|
||||
|
||||
# Lib imports
|
||||
@@ -24,9 +25,11 @@ class LSPManagerUI(Gtk.Dialog):
|
||||
def __init__(self):
|
||||
super(LSPManagerUI, self).__init__()
|
||||
|
||||
self._USER_HOME = path.expanduser('~')
|
||||
|
||||
self.client_configs: dict[str, str] = {}
|
||||
|
||||
self.source_view = None
|
||||
self.source_view = None
|
||||
|
||||
self._setup_styling()
|
||||
self._setup_signals()
|
||||
@@ -167,9 +170,11 @@ class LSPManagerUI(Gtk.Dialog):
|
||||
lang_id = self.combo_box.get_active_text()
|
||||
if not lang_id: return
|
||||
|
||||
json_str = self.client_configs[lang_id].replace("{workspace.folder}", workspace_dir)
|
||||
buffer = self.source_view.get_buffer()
|
||||
json_str = self.client_configs[lang_id] \
|
||||
.replace("{workspace.folder}", workspace_dir) \
|
||||
.replace("{user.home}", self._USER_HOME)
|
||||
|
||||
buffer = self.source_view.get_buffer()
|
||||
buffer.set_text(json_str, -1)
|
||||
|
||||
def map_parent_resize_event(self, parent):
|
||||
@@ -204,7 +209,7 @@ class LSPManagerUI(Gtk.Dialog):
|
||||
model = self.combo_box.get_model()
|
||||
|
||||
for i, row in enumerate(model):
|
||||
if row[0] == lang_id: # assuming text is in column 0
|
||||
if row[0] == lang_id:
|
||||
self.combo_box.remove(i)
|
||||
break
|
||||
|
||||
@@ -215,7 +220,9 @@ class LSPManagerUI(Gtk.Dialog):
|
||||
if not lang_id or lang_id not in self.client_configs: return {}
|
||||
|
||||
try:
|
||||
lang_config = json.loads(self.client_configs[lang_id])
|
||||
buffer = self.source_view.get_buffer()
|
||||
json_str = buffer.get_text(*buffer.get_bounds(), -1)
|
||||
lang_config = json.loads(json_str)
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"Invalid JSON for {lang_id}: {e}")
|
||||
return {}
|
||||
|
||||
Reference in New Issue
Block a user