message refactoring; initial event call from client structure

This commit is contained in:
2024-09-09 21:50:52 -05:00
parent 6df84e9f23
commit 13b126ef6e
8 changed files with 206 additions and 114 deletions

View File

@@ -1,7 +1,6 @@
# Python imports
import os
import signal
import json
import subprocess
import threading
@@ -9,12 +8,17 @@ import threading
from gi.repository import GLib
# Application imports
from libs.dto.lsp_message_structs import MessageEncoder, LSPRequest, LSPNotification, LSPResponse
from libs.dto.lsp_messages import LEN_HEADER, TYPE_HEADER, get_message_str, get_message_obj, definition_query, references_query, symbols_query
from libs.dto.lsp_message_structs import ClientRequest, ClientNotification, LSPResponse
LEN_HEADER = "Content-Length: "
TYPE_HEADER = "Content-Type: "
def _log_list():
def add_log_entry(title: str, message: any):
...
def clear():
...
@@ -22,6 +26,8 @@ class LSPController:
def __init__(self):
super(LSPController).__init__()
self._language = "python3"
# 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()
@@ -32,12 +38,44 @@ class LSPController:
self.read_lock = threading.Lock()
self.write_lock = threading.Lock()
self.log_list = _log_list
self.request_list = {}
self._subscribe_to_events()
def _subscribe_to_events(self):
event_system.subscribe("client-send-request", self._client_send_request)
event_system.subscribe("client-send-notification", self._client_send_notification)
def _client_send_request(self, method: str, uri: str, line: int, character: int):
if not method: return
if "textDocument/definition":
params = symbols_query
elif "textDocument/references":
params = symbols_query
elif "textDocument/documentSymbol":
params = symbols_query
params["textDocument"]["uri"] = uri
params["textDocument"]["languageId"] = self._language
params["position"]["line"] = line
params["position"]["character"] = character
self.send_request(method, params)
def _client_send_notification(self, method: str, line: int, char: int):
if not method: return
self.send_notification(method, params)
def set_language(self, language):
self._language = language
def set_log_list(self, log_list):
self.log_list = log_list
def set_start_command(self, start_command: []):
def set_start_command(self, start_command: list):
self._start_command = start_command
def unset_start_command(self):
@@ -56,25 +94,26 @@ class LSPController:
}
]
self._init_params["initializationOptions"] = json.loads(init_ops)
self._init_params["initializationOptions"] = get_message_obj(init_ops)
self.send_request("initialize", self._init_params)
def send_initialized_message(self):
self.send_notification("initialized")
def send_notification(self, method: str, params: {} = {}):
self._send_message( LSPNotification(method, params) )
self._send_message( ClientNotification(method, params) )
def send_request(self, method: str, params: {} = {}):
self._monitor_lsp_response()
self._send_message( LSPRequest(self._message_id, method, params) )
self._send_message( ClientRequest(self._message_id, method, params) )
self.request_list[self._message_id] = {"method": method}
self._message_id += 1
def _send_message(self, data: LSPRequest or LSPNotification):
def _send_message(self, data: ClientRequest or ClientNotification):
if not data or not hasattr(self, "lsp_process"): return
message_str = json.dumps(data, cls = MessageEncoder)
message_str = get_message_str(data)
message_size = len(message_str)
message = f"Content-Length: {message_size}\r\n\r\n{message_str}"
@@ -170,33 +209,36 @@ class LSPController:
data = self.lsp_process.stdout.read(message_size)
jsonrpc_res = data.decode("utf-8")
lsp_response = json.loads( jsonrpc_res )
lsp_response = LSPResponse(**get_message_obj(jsonrpc_res))
response_id = -1
if not lsp_response: return
if "id" in lsp_response.keys(): response_id = lsp_response["id"]
GLib.idle_add(self.handle_lsp_response, LSPResponse(response_id, lsp_response))
GLib.idle_add(self.handle_lsp_response, lsp_response)
def handle_lsp_response(self, lsp_response: LSPResponse):
self.log_list.add_log_entry("LSP Response", lsp_response)
method = self.request_list[lsp_response.id]
result = lsp_response.result
keys = result.keys()
if "error" in keys:
error = result["error"]
logger.debug(f"LSP Error Code: {error['code']}")
logger.debug(f"LSP Error Message:\n{error['message']}")
return
if "result" in keys:
result = result["result"]
self.log_list.add_log_entry("LSP Response", lsp_response)
self.process_lsp_response(method, result)
def process_lsp_response(self, method, result):
if isinstance(result, dict):
keys = result.keys()
if "capabilities" in keys:
...
return
if "error" in keys:
error = result["error"]
logger.debug(f"LSP Error Code: {error['code']}")
logger.debug(f"LSP Error Message:\n{error['message']}")
return
if "result" in keys:
result = result["result"]
if isinstance(result, dict):
keys = result.keys()
if "capabilities" in keys:
...
if isinstance(result, list):
...