Slight refactor; initial event call from client structure

This commit is contained in:
itdominator 2024-09-09 21:50:52 -05:00
parent 6df84e9f23
commit f85b3e37a3
5 changed files with 137 additions and 72 deletions

View File

@ -1,7 +1,6 @@
# Python imports
import os
import signal
import json
import subprocess
import threading
@ -9,19 +8,17 @@ import threading
from gi.repository import GLib
# Application imports
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 MessageEncoder, LSPRequest, LSPNotification, LSPResponse
LEN_HEADER = "Content-Length: "
TYPE_HEADER = "Content-Type: "
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()
@ -33,11 +30,38 @@ class LSPController:
self.read_lock = threading.Lock()
self.write_lock = threading.Lock()
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_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,7 +80,7 @@ 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):
@ -74,7 +98,7 @@ class LSPController:
def _send_message(self, data: LSPRequest or LSPNotification):
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,7 +194,7 @@ class LSPController:
data = self.lsp_process.stdout.read(message_size)
jsonrpc_res = data.decode("utf-8")
lsp_response = json.loads( jsonrpc_res )
lsp_response = get_message_obj( jsonrpc_res )
response_id = -1
if not lsp_response: return

View File

@ -6,64 +6,7 @@ gi.require_version('GtkSource', '4')
from gi.repository import GtkSource
# Application imports
# Request type formatting
# https://github.com/microsoft/multilspy/blob/main/src/multilspy/language_server.py#L417
content_part = """{
"method": "textDocument/definition",
"params": {
"textDocument": {
"uri": "file:///home/abaddon/Coding/Projects/Active/Python_Projects/000_Usable/gtk/LSP-Manager/src/core/widgets/lsp_message_box.py",
"languageId": "python3",
"version": 1,
"text": ""
},
"position": {
"line": 5,
"character": 12,
"offset": 0
}
}
}
"""
references_query = """{
"method": "textDocument/references",
"params": {
"context": {
"includeDeclaration": false
},
"textDocument": {
"uri": "file:///home/abaddon/Coding/Projects/Active/Python_Projects/000_Usable/gtk/LSP-Manager/src/core/widgets/lsp_message_box.py",
"languageId": "python3",
"version": 1,
"text": ""
},
"position": {
"line": 30,
"character": 13,
"offset": 0
}
}
}
"""
symbols_query = """{
"method": "textDocument/documentSymbol",
"params": {
"textDocument": {
"uri": "file:///home/abaddon/Coding/Projects/Active/Python_Projects/000_Usable/gtk/LSP-Manager/src/core/widgets/lsp_message_box.py",
"languageId": "python3",
"version": 1,
"text": ""
}
}
}
"""
from libs.dto.lsp_messages import get_message_str, content_part
@ -97,7 +40,7 @@ class LspMessageSourceView(GtkSource.View):
buffer.set_language( language_manager.get_language("json") )
buffer.set_style_scheme(style_scheme)
buffer.set_text(content_part)
buffer.set_text( get_message_str(content_part) )
def _setup_signals(self):

View File

@ -1,5 +1,4 @@
# Python imports
import json
# Lib imports
import gi
@ -7,6 +6,7 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
from libs.dto.lsp_messages import get_message_obj
from core.controllers.lsp_controller import LSPController
from .buttons.top_button_box import TopButtonBox
from .enteries.lsp_message_source_view import LspMessageSourceView
@ -74,11 +74,11 @@ class LSPMessageBox(Gtk.Box):
self.lsp_controller.send_initialized_message()
def button_send_notification(self):
message = json.loads( self.lsp_msg_src_vw.get_text_str() )
message = get_message_obj( self.lsp_msg_src_vw.get_text_str() )
self.lsp_controller.send_notification(message["method"], message["params"])
def button_send_request(self):
message = json.loads( self.lsp_msg_src_vw.get_text_str() )
message = get_message_obj( self.lsp_msg_src_vw.get_text_str() )
self.lsp_controller.send_request(message["method"], message["params"])
self.update_message_id_label()

View File

@ -0,0 +1,93 @@
# Python imports
import json
# Lib imports
# Application imports
from .lsp_message_structs import MessageEncoder
LEN_HEADER = "Content-Length: "
TYPE_HEADER = "Content-Type: "
def get_message_str(data: dict) -> str:
return json.dumps(data, separators = (',', ':'), indent = 4, cls = MessageEncoder)
def get_message_obj(data: str) -> str:
return json.loads(data)
# Request type formatting
# https://github.com/microsoft/multilspy/blob/main/src/multilspy/language_server.py#L417
content_part = {
"method": "textDocument/definition",
"params": {
"textDocument": {
"uri": "file:///",
"languageId": "python3",
"version": 1,
"text": ""
},
"position": {
"line": 5,
"character": 12,
"offset": 0
}
}
}
definition_query = {
"method": "textDocument/definition",
"params": {
"textDocument": {
"uri": "file:///home/abaddon/Coding/Projects/Active/Python_Projects/000_Usable/gtk/LSP-Manager/src/core/widgets/lsp_message_box.py",
"languageId": "python3",
"version": 1,
"text": ""
},
"position": {
"line": 5,
"character": 12,
"offset": 0
}
}
}
references_query = {
"method": "textDocument/references",
"params": {
"context": {
"includeDeclaration": False
},
"textDocument": {
"uri": "file:///home/abaddon/Coding/Projects/Active/Python_Projects/000_Usable/gtk/LSP-Manager/src/core/widgets/lsp_message_box.py",
"languageId": "python3",
"version": 1,
"text": ""
},
"position": {
"line": 30,
"character": 13,
"offset": 0
}
}
}
symbols_query = {
"method": "textDocument/documentSymbol",
"params": {
"textDocument": {
"uri": "file:///home/abaddon/Coding/Projects/Active/Python_Projects/000_Usable/gtk/LSP-Manager/src/core/widgets/lsp_message_box.py",
"languageId": "python3",
"version": 1,
"text": ""
}
}
}

View File

@ -1,5 +1,10 @@
# Python imports
import enum
# Lib imports
# Application imports
def to_type(o, new_type):