Mapping parts of LSP calls and responses to classes
This commit is contained in:
@@ -8,7 +8,7 @@ from gi.repository import Gtk
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
|
||||
from libs.dto.lsp_message_structs import MessageEncoder, LSPRequest, LSPNotification, LSPResponse
|
||||
|
||||
|
||||
class LogList(Gtk.ListBox):
|
||||
@@ -52,13 +52,13 @@ class LogList(Gtk.ListBox):
|
||||
def _load_widgets(self):
|
||||
...
|
||||
|
||||
def add_log_entry(self, m_type: str, data: dict):
|
||||
message = json.dumps(data, separators = (',', ':'), indent = 4)
|
||||
def add_log_entry(self, m_type: str, data: LSPRequest or LSPNotification or LSPResponse):
|
||||
message = json.dumps(data, separators = (',', ':'), indent = 4, cls = MessageEncoder)
|
||||
|
||||
frame = Gtk.Frame()
|
||||
revealer = Gtk.Revealer()
|
||||
label = Gtk.Label(label=message)
|
||||
lbl_str = f"> {m_type} Message: {data['id']}" if "id" in data.keys() else f"> {m_type} Notification"
|
||||
lbl_str = f"> {m_type} Message: {data.id}" if hasattr(data, "id") else f"> {m_type} Notification"
|
||||
|
||||
label.set_xalign(0.010)
|
||||
frame.set_label(lbl_str)
|
||||
@@ -80,4 +80,4 @@ class LogList(Gtk.ListBox):
|
||||
|
||||
def scroll_down(self):
|
||||
vadjustment = self.get_parent().get_vadjustment()
|
||||
vadjustment.set_value( vadjustment.get_upper())
|
||||
vadjustment.set_value( vadjustment.get_upper())
|
@@ -14,6 +14,8 @@ from gi.repository import GLib
|
||||
from gi.repository import GtkSource
|
||||
|
||||
# Application imports
|
||||
# from libs.dto.lsp_structs import TextDocumentItem
|
||||
from libs.dto.lsp_message_structs import MessageEncoder, LSPRequest, LSPNotification, LSPResponse
|
||||
from .buttons.top_button_box import TopButtonBox
|
||||
from .enteries.lsp_message_source_view import LspMessageSourceView
|
||||
from .buttons.bottom_button_box import BottomButtonBox
|
||||
@@ -29,15 +31,15 @@ class LSPMessageBox(Gtk.Box):
|
||||
def __init__(self):
|
||||
super(LSPMessageBox, self).__init__()
|
||||
|
||||
self._lsp_pid = -1
|
||||
self._message_id = 0
|
||||
self._lsp_pid = -1
|
||||
self._message_id = 0
|
||||
|
||||
# https://github.com/microsoft/multilspy/tree/main/src/multilspy/language_servers
|
||||
# initialize-params-slim.json was created off of jedi_language_server one
|
||||
self._lsp_init_data = settings_manager.get_lsp_init_data()
|
||||
self._lsp_init_ops = settings_manager.get_lsp_init_data()
|
||||
|
||||
self.read_lock = threading.Lock()
|
||||
self.write_lock = threading.Lock()
|
||||
self.read_lock = threading.Lock()
|
||||
self.write_lock = threading.Lock()
|
||||
|
||||
self._setup_styling()
|
||||
self._setup_signals()
|
||||
@@ -86,18 +88,18 @@ class LSPMessageBox(Gtk.Box):
|
||||
workspace_uri = folder_choser_btn.get_uri()
|
||||
folder_name = os.path.basename(workspace_file)
|
||||
|
||||
self._lsp_init_data["processId"] = settings_manager.get_app_pid()
|
||||
self._lsp_init_data["rootPath"] = workspace_file
|
||||
self._lsp_init_data["rootUri"] = workspace_uri
|
||||
self._lsp_init_data["workspaceFolders"] = [
|
||||
self._lsp_init_ops["processId"] = settings_manager.get_app_pid()
|
||||
self._lsp_init_ops["rootPath"] = workspace_file
|
||||
self._lsp_init_ops["rootUri"] = workspace_uri
|
||||
self._lsp_init_ops["workspaceFolders"] = [
|
||||
{
|
||||
"name": folder_name,
|
||||
"uri": workspace_uri
|
||||
}
|
||||
]
|
||||
|
||||
self._lsp_init_data["initializationOptions"] = json.loads(init_ops)
|
||||
self.send_request("initialize", self._lsp_init_data)
|
||||
self._lsp_init_ops["initializationOptions"] = json.loads(init_ops)
|
||||
self.send_request("initialize", self._lsp_init_ops)
|
||||
|
||||
del init_ops
|
||||
del workspace_file
|
||||
@@ -109,35 +111,18 @@ class LSPMessageBox(Gtk.Box):
|
||||
self.send_notification("initialized")
|
||||
|
||||
def send_notification(self, method: str, params: {} = {}):
|
||||
data = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"params": params
|
||||
}
|
||||
|
||||
self._send_message(data)
|
||||
|
||||
del data
|
||||
self._send_message( LSPNotification(method, params) )
|
||||
|
||||
def send_request(self, method: str, params: {} = {}):
|
||||
self._monitor_lsp_response()
|
||||
|
||||
data = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": self._message_id,
|
||||
"method": method,
|
||||
"params": params
|
||||
}
|
||||
|
||||
self._message_id += 1
|
||||
self._send_message(data)
|
||||
self._send_message( LSPRequest(self._message_id, method, params) )
|
||||
|
||||
del data
|
||||
|
||||
def _send_message(self, data: dict):
|
||||
def _send_message(self, data: LSPRequest or LSPNotification):
|
||||
if not data: return
|
||||
|
||||
message_str = json.dumps(data)
|
||||
message_str = json.dumps(data, cls = MessageEncoder)
|
||||
message_size = len(message_str)
|
||||
message = f"Content-Length: {message_size}\r\n\r\n{message_str}"
|
||||
|
||||
@@ -234,43 +219,35 @@ class LSPMessageBox(Gtk.Box):
|
||||
|
||||
data = self.lsp_process.stdout.read(message_size)
|
||||
jsonrpc_res = data.decode("utf-8")
|
||||
lsp_result = json.loads( jsonrpc_res )
|
||||
lsp_response = json.loads( jsonrpc_res )
|
||||
response_id = -1
|
||||
|
||||
if not lsp_result: return
|
||||
if "id" in lsp_result.keys(): response_id = lsp_result["id"]
|
||||
if not lsp_response: return
|
||||
if "id" in lsp_response.keys(): response_id = lsp_response["id"]
|
||||
|
||||
GLib.idle_add(self.handle_lsp_response, response_id, lsp_result)
|
||||
GLib.idle_add(self.handle_lsp_response, LSPResponse(response_id, lsp_response))
|
||||
|
||||
def handle_lsp_response(self, id: int, lsp_result: {}):
|
||||
self.get_parent().log_list.add_log_entry("LSP Response", lsp_result)
|
||||
def handle_lsp_response(self, lsp_response: LSPResponse):
|
||||
self.get_parent().log_list.add_log_entry("LSP Response", lsp_response)
|
||||
result = lsp_response.result
|
||||
keys = result.keys()
|
||||
|
||||
keys = lsp_result.keys()
|
||||
if "error" in keys:
|
||||
lsp_result = lsp_result["error"]
|
||||
logger.debug(f"LSP Error Code: {lsp_result['code']}")
|
||||
logger.debug(f"LSP Error Message:\n{lsp_result['message']}")
|
||||
lsp_response = result["error"]
|
||||
logger.debug(f"LSP Error Code: {result['code']}")
|
||||
logger.debug(f"LSP Error Message:\n{result['message']}")
|
||||
return
|
||||
|
||||
if "result" in keys:
|
||||
lsp_result = lsp_result["result"]
|
||||
result = result["result"]
|
||||
|
||||
if isinstance(lsp_result, dict):
|
||||
keys = lsp_result.keys()
|
||||
if isinstance(result, dict):
|
||||
keys = result.keys()
|
||||
if "capabilities" in keys:
|
||||
logger.debug(f"LSP Capabilities Response:\n{lsp_result}")
|
||||
logger.debug(f"LSP Capabilities Response:\n{result}")
|
||||
|
||||
if isinstance(result, list):
|
||||
...
|
||||
|
||||
if isinstance(lsp_result, list):
|
||||
print()
|
||||
print( str(id) )
|
||||
print(lsp_result)
|
||||
print()
|
||||
print()
|
||||
|
||||
if isinstance(lsp_result, tuple):
|
||||
print()
|
||||
print( str(id) )
|
||||
print(lsp_result)
|
||||
print()
|
||||
print()
|
||||
if isinstance(result, tuple):
|
||||
...
|
Reference in New Issue
Block a user