2023-11-04 19:38:20 +00:00
|
|
|
# Python imports
|
2023-11-05 05:36:55 +00:00
|
|
|
import os
|
|
|
|
import json
|
2023-11-04 19:38:20 +00:00
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
from plugins.plugin_base import PluginBase
|
|
|
|
from .lsp_controller import LSPController
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-06 05:22:41 +00:00
|
|
|
|
|
|
|
class LSPPliginException(Exception):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-04 19:38:20 +00:00
|
|
|
class Plugin(PluginBase):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
2023-11-05 05:36:55 +00:00
|
|
|
self.name = "LSP Client" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
|
|
|
|
# where self.name should not be needed for message comms
|
|
|
|
self.lsp_config_path: str = os.path.dirname(os.path.realpath(__file__)) + "/../../lsp_servers_config.json"
|
|
|
|
self.lsp_servers_config: dict = {}
|
|
|
|
self.lsp_controller = None
|
2023-11-04 19:38:20 +00:00
|
|
|
|
|
|
|
def generate_reference_ui_element(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
def run(self):
|
2023-11-05 05:36:55 +00:00
|
|
|
if os.path.exists(self.lsp_config_path):
|
|
|
|
with open(self.lsp_config_path, "r") as f:
|
|
|
|
self.lsp_servers_config = json.load(f)
|
|
|
|
else:
|
|
|
|
text = f"LSP NOT Enabled.\nFile:\n\t{self.lsp_config_path}\ndoes no exsist..."
|
2023-11-04 19:38:20 +00:00
|
|
|
self._event_system.emit("bubble_message", ("warning", self.name, text,))
|
2023-11-07 05:01:18 +00:00
|
|
|
return
|
2023-11-05 05:36:55 +00:00
|
|
|
|
2023-11-07 05:01:18 +00:00
|
|
|
self.lsp_controller = LSPController(self.lsp_servers_config)
|
|
|
|
self.inner_subscribe_to_events()
|
2023-11-04 19:38:20 +00:00
|
|
|
|
|
|
|
def subscribe_to_events(self):
|
2023-11-07 05:01:18 +00:00
|
|
|
...
|
|
|
|
|
|
|
|
def inner_subscribe_to_events(self):
|
2023-11-04 19:38:20 +00:00
|
|
|
self._event_system.subscribe("shutting_down", self._shutting_down)
|
|
|
|
self._event_system.subscribe("buffer_changed", self._buffer_changed)
|
|
|
|
|
2023-11-07 05:01:18 +00:00
|
|
|
self._event_system.subscribe("textDocument/didOpen", self.lsp_controller.do_open)
|
|
|
|
self._event_system.subscribe("textDocument/didSave", self.lsp_controller.do_save)
|
|
|
|
self._event_system.subscribe("textDocument/didClose", self.lsp_controller.do_close)
|
|
|
|
self._event_system.subscribe("textDocument/definition", self._do_goto)
|
2023-11-04 19:38:20 +00:00
|
|
|
|
|
|
|
def _shutting_down(self):
|
2023-11-05 05:36:55 +00:00
|
|
|
if self.lsp_controller:
|
|
|
|
self.lsp_controller._shutting_down()
|
2023-11-04 19:38:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _buffer_changed(self, buffer):
|
2023-11-07 05:01:18 +00:00
|
|
|
# self._do_completion()
|
|
|
|
...
|
2023-11-04 19:38:20 +00:00
|
|
|
|
2023-11-06 05:22:41 +00:00
|
|
|
|
|
|
|
def _do_completion(self, is_invoked = False):
|
2023-11-07 05:01:18 +00:00
|
|
|
fpath = self._active_src_view.get_current_filepath()
|
2023-11-05 05:36:55 +00:00
|
|
|
|
2023-11-07 05:01:18 +00:00
|
|
|
if not fpath: return
|
|
|
|
|
|
|
|
uri = fpath.get_uri()
|
2023-11-06 05:22:41 +00:00
|
|
|
iter = self._buffer.get_iter_at_mark( self._buffer.get_insert() )
|
|
|
|
line = iter.get_line()
|
|
|
|
offset = iter.get_line_offset()
|
|
|
|
trigger = pylspclient.lsp_structs.CompletionTriggerKind.TriggerCharacter
|
|
|
|
_char = iter.get_char()
|
|
|
|
trigger = None
|
|
|
|
|
|
|
|
if _char in [".", " "]:
|
|
|
|
trigger = pylspclient.lsp_structs.CompletionTriggerKind.TriggerCharacter
|
|
|
|
elif is_invoked:
|
|
|
|
trigger = pylspclient.lsp_structs.CompletionTriggerKind.Invoked
|
|
|
|
else:
|
|
|
|
trigger = pylspclient.lsp_structs.CompletionTriggerKind.TriggerForIncompleteCompletions
|
2023-11-05 05:36:55 +00:00
|
|
|
|
2023-11-07 05:01:18 +00:00
|
|
|
result = self.lsp_controller.completion(
|
2023-11-06 05:22:41 +00:00
|
|
|
pylspclient.lsp_structs.TextDocumentIdentifier(uri),
|
|
|
|
pylspclient.lsp_structs.Position(line, offset),
|
|
|
|
pylspclient.lsp_structs.CompletionContext(trigger, _char)
|
|
|
|
)
|
|
|
|
|
|
|
|
if result.items:
|
|
|
|
for item in result.items:
|
|
|
|
print(item.label)
|
|
|
|
else:
|
|
|
|
print(result.label)
|
2023-11-05 05:36:55 +00:00
|
|
|
|
2023-11-07 05:01:18 +00:00
|
|
|
def _do_goto(self, language_id, uri, line, offset):
|
|
|
|
results = self.lsp_controller.do_goto(language_id, uri, line, offset)
|
2023-11-06 05:22:41 +00:00
|
|
|
|
|
|
|
if len(results) == 1:
|
|
|
|
result = results[0]
|
|
|
|
file = result.uri[7:]
|
|
|
|
line = result.range.end.line
|
|
|
|
message = f"FILE|{file}:{line}"
|
|
|
|
self._event_system.emit("post_file_to_ipc", message)
|
2023-11-05 05:36:55 +00:00
|
|
|
|