Populating completion items list with manual trigger

This commit is contained in:
itdominator 2024-09-16 23:00:39 -05:00
parent bc47275e57
commit 28b618ea0b
6 changed files with 31 additions and 167 deletions

View File

@ -53,28 +53,13 @@ class EditorControllerMixin(KeyInputController, EditorEventsMixin):
keys = message.result.keys() keys = message.result.keys()
if "items" in keys: if "items" in keys:
buffer = source_view.get_buffer()
completion = source_view.get_completion() completion = source_view.get_completion()
providers = completion.get_providers() providers = completion.get_providers()
for provider in providers: for provider in providers:
if provider.__class__.__name__ == 'LSPCompletionProvider': if provider.__class__.__name__ == 'LSPCompletionProvider':
# context = completion.create_context( buffer.get_iter_at_mark( buffer.get_insert() ) ) source_view.completion_items = message.result["items"]
# context = completion.create_context( None ) source_view.emit("show-completion")
# provider.do_populate(context, message.result["items"])
box = Gtk.Box()
box.set_homogeneous(True)
# iter = buffer.get_iter_at_mark( buffer.get_insert() )
# rects, recte = source_view.get_cursor_locations(iter)
rect = source_view.get_allocation()
box.set_orientation( Gtk.Orientation.VERTICAL )
for item in message.result["items"]:
box.add( provider.create_completion_item_button(item) )
box.show_all()
source_view.add_child_in_window(box, Gtk.TextWindowType.WIDGET, rect.width - 200, rect.height / 2)
if "result" in keys: if "result" in keys:
... ...

View File

@ -1,98 +0,0 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GtkSource', '4')
from gi.repository import Gtk
from gi.repository import GtkSource
from gi.repository import GObject
# Application imports
class LSPCompletionProvider(GObject.Object, GtkSource.CompletionProvider):
"""
This code is an LSP code completion plugin for Newton.
# NOTE: Some code pulled/referenced from here --> https://github.com/isamert/gedi
"""
__gtype_name__ = 'LSPProvider'
def __init__(self, source_view):
GObject.Object.__init__(self)
self._theme = Gtk.IconTheme.get_default()
self._source_view = source_view
def do_get_name(self):
return "LSP Code Completion"
def get_iter_correctly(self, context):
return context.get_iter()[1] if isinstance(context.get_iter(), tuple) else context.get_iter()
def do_match(self, context):
iter = self.get_iter_correctly(context)
buffer = iter.get_buffer()
if buffer.get_context_classes_at_iter(iter) != ['no-spell-check']:
return False
ch = iter.get_char()
# NOTE: Look to re-add or apply supprting logic to use spaces
# As is it slows down the editor in certain contexts...
if not (ch in ('_', '.', ' ') or ch.isalnum()):
# if not (ch in ('_', '.') or ch.isalnum()):
return False
return True
def do_get_priority(self):
return 1
def do_get_activation(self):
return GtkSource.CompletionActivation.INTERACTIVE
def do_populate(self, context, result = None):
result = event_system.emit_and_await("textDocument/completion", (self._source_view,))
proposals = []
if result:
if not result.items is None:
for item in result.items:
proposals.append( self.create_completion_item(item) )
else:
proposals.append( self.create_completion_item(result) )
context.add_proposals(self, proposals, True)
def get_icon_for_type(self, _type):
try:
return self._theme.load_icon(icon_names[_type.lower()], 16, 0)
except:
...
try:
return self._theme.load_icon(Gtk.STOCK_ADD, 16, 0)
except:
...
return None
def create_completion_item(self, item):
comp_item = GtkSource.CompletionItem.new()
comp_item.set_label(item.label)
if item.textEdit:
if isinstance(item.textEdit, dict):
comp_item.set_text(item.textEdit["newText"])
else:
comp_item.set_text(item.textEdit)
else:
comp_item.set_text(item.insertText)
comp_item.set_icon( self.get_icon_for_type(item.kind) )
comp_item.set_info(item.documentation)
return comp_item

View File

@ -34,31 +34,19 @@ class LSPCompletionProvider(GObject.Object, GtkSource.CompletionProvider):
return context.get_iter()[1] if isinstance(context.get_iter(), tuple) else context.get_iter() return context.get_iter()[1] if isinstance(context.get_iter(), tuple) else context.get_iter()
def do_match(self, context): def do_match(self, context):
iter = self.get_iter_correctly(context)
buffer = iter.get_buffer()
if buffer.get_context_classes_at_iter(iter) != ['no-spell-check']:
return False
ch = iter.get_char()
# NOTE: Look to re-add or apply supprting logic to use spaces
# As is it slows down the editor in certain contexts...
if not (ch in ('_', '.', ' ') or ch.isalnum()):
# if not (ch in ('_', '.') or ch.isalnum()):
return False
return True return True
def do_get_priority(self): def do_get_priority(self):
return 1 return 1
def do_get_activation(self): def do_get_activation(self):
return GtkSource.CompletionActivation.INTERACTIVE return GtkSource.CompletionActivation.USER_REQUESTED
def do_populate(self, context, items = []):
if hasattr(self._source_view, "completion_items"):
items = self._source_view.completion_items
def do_populate(self, context, items = None):
# result = event_system.emit_and_await("textDocument/completion", (self._source_view,))
proposals = [] proposals = []
if items:
for item in items: for item in items:
proposals.append( self.create_completion_item(item) ) proposals.append( self.create_completion_item(item) )
@ -77,36 +65,30 @@ class LSPCompletionProvider(GObject.Object, GtkSource.CompletionProvider):
return None return None
def create_completion_item(self, item):
comp_item = GtkSource.CompletionItem.new()
def create_completion_item_button(self, item):
comp_item = Gtk.Box()
button = Gtk.Button(label = item["label"])
keys = item.keys() keys = item.keys()
comp_item.set_label(item["label"])
if "insertText" in keys: if "insertText" in keys:
button.set_tooltip_text( item["insertText"] ) comp_item.set_text(item["insertText"])
button.set_hexpand(True)
comp_item.add(button)
return comp_item
def _create_completion_item(self, item):
comp_item = GtkSource.CompletionItem.new()
comp_item.set_label(item.label)
if item.textEdit:
if isinstance(item.textEdit, dict):
comp_item.set_text(item.textEdit["newText"])
else:
comp_item.set_text(item.textEdit)
else:
comp_item.set_text(item.insertText)
comp_item.set_icon( self.get_icon_for_type(item.kind) )
comp_item.set_info(item.documentation)
return comp_item return comp_item
# def create_completion_item(self, item):
# comp_item = GtkSource.CompletionItem.new()
# comp_item.set_label(item.label)
# if item.textEdit:
# if isinstance(item.textEdit, dict):
# comp_item.set_text(item.textEdit["newText"])
# else:
# comp_item.set_text(item.textEdit)
# else:
# comp_item.set_text(item.insertText)
# comp_item.set_icon( self.get_icon_for_type(item.kind) )
# comp_item.set_info(item.documentation)
# return comp_item

View File

@ -81,7 +81,7 @@ class KeyInputController:
return True return True
if keyname in ["z", "y", "m", "s", "h", "g", "d", "k", "u", "equal", "minus", "Up", "Down"]: if keyname in ["z", "y", "m", "s", "h", "g", "d", "k", "u", "space", "equal", "minus", "Up", "Down"]:
if keyname == "z": if keyname == "z":
self.keyboard_undo() self.keyboard_undo()
if keyname == "y": if keyname == "y":
@ -100,6 +100,8 @@ class KeyInputController:
self.cut_to_buffer() self.cut_to_buffer()
if keyname == "u": if keyname == "u":
self.paste_cut_buffer() self.paste_cut_buffer()
if keyname == "space":
event_system.emit("textDocument/completion", (self, ))
if keyname == "equal": if keyname == "equal":
self.scale_up_text() self.scale_up_text()

View File

@ -156,9 +156,5 @@ class FileEventsMixin:
lsp_completion_provider = LSPCompletionProvider(self) lsp_completion_provider = LSPCompletionProvider(self)
self._completion.add_provider(lsp_completion_provider) self._completion.add_provider(lsp_completion_provider)
# if self._current_filetype in ("python", "python3"):
# py_lsp_completion_provider = PythonCompletionProvider(uri)
# self._completion.add_provider(py_lsp_completion_provider)
self.got_to_line(buffer, line) self.got_to_line(buffer, line)
event_system.emit("buffer_changed_first_load", (buffer, )) event_system.emit("buffer_changed_first_load", (buffer, ))

View File

@ -46,6 +46,7 @@ class SourceView(SourceViewControllerMixin, GtkSource.View):
self._px_value = settings.theming.default_zoom self._px_value = settings.theming.default_zoom
self._multi_insert_marks = [] self._multi_insert_marks = []
self.completion_items = []
self.freeze_multi_line_insert = False self.freeze_multi_line_insert = False
self._setup_styling() self._setup_styling()
@ -88,7 +89,6 @@ class SourceView(SourceViewControllerMixin, GtkSource.View):
self.connect("key-release-event", self._key_release_event) self.connect("key-release-event", self._key_release_event)
self.connect("button-press-event", self._button_press_event) self.connect("button-press-event", self._button_press_event)
self.connect("scroll-event", self._scroll_event) self.connect("scroll-event", self._scroll_event)
self.connect("show-completion", self._show_completion)
buffer = self.get_buffer() buffer = self.get_buffer()
buffer.connect('changed', self._is_modified) buffer.connect('changed', self._is_modified)
@ -96,9 +96,6 @@ class SourceView(SourceViewControllerMixin, GtkSource.View):
buffer.connect('insert-text', self._insert_text) buffer.connect('insert-text', self._insert_text)
buffer.connect('modified-changed', self._buffer_modified_changed) buffer.connect('modified-changed', self._buffer_modified_changed)
def _show_completion(self, source_view):
event_system.emit("textDocument/completion", (source_view, ))
def _subscribe_to_events(self): def _subscribe_to_events(self):
... ...