generated from itdominator/Python-With-Gtk-Template
Refactored buttons and call structures
This commit is contained in:
parent
b7bac2d5ae
commit
4b47ba4120
|
@ -34,18 +34,18 @@ class BottomButtonBox(Gtk.ButtonBox):
|
|||
def _load_widgets(self):
|
||||
self.initialize_btn = Gtk.Button(label = "Send Initialize Message")
|
||||
self.notification_btn = Gtk.Button(label = "Send Notification")
|
||||
self.message_btn = Gtk.Button(label = "Send Message")
|
||||
self.request_btn = Gtk.Button(label = "Send Request")
|
||||
self.start_stop_lsp_btn = Gtk.Button(label = "Start LSP")
|
||||
|
||||
self.add(self.initialize_btn)
|
||||
self.add(self.notification_btn)
|
||||
self.add(self.message_btn)
|
||||
self.add(self.request_btn)
|
||||
self.add(self.start_stop_lsp_btn)
|
||||
|
||||
def connect_widget_signals(self, parent):
|
||||
if not parent: return
|
||||
|
||||
self.initialize_btn.connect("clicked", parent.send_initialize_message)
|
||||
self.notification_btn.connect("clicked", parent.send_notification)
|
||||
self.message_btn.connect("clicked", parent.send_message)
|
||||
self.notification_btn.connect("clicked", parent.button_send_notification)
|
||||
self.request_btn.connect("clicked", parent.button_send_request)
|
||||
self.start_stop_lsp_btn.connect("clicked", parent.start_stop_lsp)
|
|
@ -46,27 +46,16 @@ class LogList(Gtk.ListBox):
|
|||
def _load_widgets(self):
|
||||
...
|
||||
|
||||
def add_log_entry(self, m_type: str, data: str or dict):
|
||||
message = None
|
||||
json_data = None
|
||||
def add_log_entry(self, m_type: str, data: dict):
|
||||
message = json.dumps(data, separators = (',', ':'), indent = 4)
|
||||
|
||||
if isinstance(data, str):
|
||||
parts = data.split("\n", 2)
|
||||
jsonStr = parts[2]
|
||||
json_data = json.loads(jsonStr)
|
||||
messageStr = json.dumps(json_data, separators = (',', ':'), indent = 4)
|
||||
message = f"{parts[0]}\r\n\r\n{messageStr}"
|
||||
|
||||
if isinstance(data, dict):
|
||||
json_data = data
|
||||
message = json.dumps(data, separators = (',', ':'), indent = 4)
|
||||
|
||||
frame = Gtk.Frame()
|
||||
revealer = Gtk.Revealer()
|
||||
label = Gtk.Label(label=message)
|
||||
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"
|
||||
|
||||
label.set_xalign(0.010)
|
||||
frame.set_label( f"> {m_type}: {json_data['id']}" if "id" in json_data.keys() else f"> {m_type}" )
|
||||
frame.set_label(lbl_str)
|
||||
frame.set_label_align(0.010, 0.5)
|
||||
revealer.set_reveal_child(False)
|
||||
|
||||
|
@ -77,4 +66,4 @@ class LogList(Gtk.ListBox):
|
|||
frame.show_all()
|
||||
|
||||
del message
|
||||
del json_data
|
||||
del lbl_str
|
||||
|
|
|
@ -111,7 +111,7 @@ class LSPMessageBox(Gtk.Box):
|
|||
|
||||
self._lsp_init_data["initializationOptions"] = json.loads(init_ops)
|
||||
|
||||
self.make_request("initialize", self._lsp_init_data)
|
||||
self.send_request("initialize", self._lsp_init_data)
|
||||
button.set_sensitive(False)
|
||||
|
||||
del init_ops
|
||||
|
@ -121,27 +121,20 @@ class LSPMessageBox(Gtk.Box):
|
|||
del parent
|
||||
|
||||
def send_initialized_message(self):
|
||||
self.make_request("initialized")
|
||||
self.send_notification("initialized")
|
||||
|
||||
def make_notification(self, method: str, params: {} = {}):
|
||||
def send_notification(self, method: str, params: {} = {}):
|
||||
data = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"params": params
|
||||
}
|
||||
message_str = json.dumps(data)
|
||||
message_size = len(message_str)
|
||||
message = f"Content-Length: {message_size}\r\n\r\n{message_str}"
|
||||
|
||||
self.lsp_process.stdin.write( message.encode("utf-8") )
|
||||
self.lsp_process.stdin.flush()
|
||||
self._send_message(data)
|
||||
|
||||
del data
|
||||
del message_str
|
||||
del message_size
|
||||
del message
|
||||
|
||||
def make_request(self, method: str, params: {} = {}):
|
||||
def send_request(self, method: str, params: {} = {}):
|
||||
self._monitor_lsp_response()
|
||||
|
||||
data = {
|
||||
|
@ -150,30 +143,38 @@ class LSPMessageBox(Gtk.Box):
|
|||
"method": method,
|
||||
"params": params
|
||||
}
|
||||
|
||||
self._message_id += 1
|
||||
self._send_message(data)
|
||||
|
||||
del data
|
||||
|
||||
def _send_message(self, data: dict):
|
||||
if not data: return
|
||||
|
||||
message_str = json.dumps(data)
|
||||
message_size = len(message_str)
|
||||
message = f"Content-Length: {message_size}\r\n\r\n{message_str}"
|
||||
|
||||
self.get_parent().log_list.add_log_entry("Client Message", message)
|
||||
self.get_parent().log_list.add_log_entry("Client", data)
|
||||
|
||||
with self.write_lock:
|
||||
self.lsp_process.stdin.write( message.encode("utf-8") )
|
||||
self.lsp_process.stdin.flush()
|
||||
|
||||
self._message_id += 1
|
||||
self.update_message_id_label()
|
||||
|
||||
del data
|
||||
del message_str
|
||||
del message_size
|
||||
del message
|
||||
|
||||
def send_notification(self, button, eve = None):
|
||||
pass
|
||||
|
||||
def send_message(self, button, eve = None):
|
||||
def button_send_notification(self, button, eve = None):
|
||||
message = json.loads( self.lsp_msg_src_vw.get_text_str() )
|
||||
self.make_request(message["method"], message["params"])
|
||||
self.send_notification(message["method"], message["params"])
|
||||
|
||||
def button_send_request(self, button, eve = None):
|
||||
message = json.loads( self.lsp_msg_src_vw.get_text_str() )
|
||||
self.send_request(message["method"], message["params"])
|
||||
|
||||
def start_stop_lsp(self, button, eve = None):
|
||||
if self._lsp_pid == -1:
|
||||
|
@ -260,7 +261,7 @@ class LSPMessageBox(Gtk.Box):
|
|||
GLib.idle_add(self.handle_lsp_response, response_id, lsp_result)
|
||||
|
||||
def handle_lsp_response(self, id: int, lsp_result: {}):
|
||||
self.get_parent().log_list.add_log_entry("LSP Response Message", lsp_result)
|
||||
self.get_parent().log_list.add_log_entry("LSP Response", lsp_result)
|
||||
|
||||
keys = lsp_result.keys()
|
||||
if "error" in keys:
|
||||
|
|
Loading…
Reference in New Issue