More changes to completion logic

This commit is contained in:
itdominator 2023-03-25 01:00:39 -05:00
parent 16543dbd92
commit 1268449824
2 changed files with 31 additions and 29 deletions

View File

@ -9,6 +9,7 @@ from gi.repository import Gtk
from gi.repository import GtkSource from gi.repository import GtkSource
from gi.repository import GObject from gi.repository import GObject
import jedi
from jedi.api import Script from jedi.api import Script
# Application imports # Application imports
@ -27,20 +28,16 @@ icon_names = {
class Jedi: class Jedi:
def get_script(file, buffer): def get_script(file, doc_text):
doc_text = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter(), False)
iter_cursor = buffer.get_iter_at_mark(buffer.get_insert())
linenum = iter_cursor.get_line() + 1
charnum = iter_cursor.get_line_index()
return Script(code = doc_text, path = file) return Script(code = doc_text, path = file)
class GediCompletionProvider(GObject.Object, GtkSource.CompletionProvider): class PythonCompletionProvider(GObject.Object, GtkSource.CompletionProvider):
""" """
This code is A python code completion plugin for Gedit that's been modified accordingly to work for Newton. This code is A python code completion plugin for Newton.
# NOTE: Code pulled from here --> https://github.com/isamert/gedi # NOTE: Some code pulled/referenced from here --> https://github.com/isamert/gedi
""" """
__gtype_name__ = 'GediProvider' __gtype_name__ = 'PythonProvider'
def __init__(self, file): def __init__(self, file):
GObject.Object.__init__(self) GObject.Object.__init__(self)
@ -48,20 +45,19 @@ class GediCompletionProvider(GObject.Object, GtkSource.CompletionProvider):
self._file = file self._file = file
def do_get_name(self): def do_get_name(self):
return _("Gedi Python Code Completion") return "Python Code Completion"
def get_iter_correctly(self, context): def get_iter_correctly(self, context):
if isinstance(context.get_iter(), tuple): return context.get_iter()[1] if isinstance(context.get_iter(), tuple) else context.get_iter()
return context.get_iter()[1];
else:
return context.get_iter()
def do_match(self, context): def do_match(self, context):
iter = self.get_iter_correctly(context) iter = self.get_iter_correctly(context)
iter.backward_char() iter.backward_char()
buffer = iter.get_buffer() buffer = iter.get_buffer()
if buffer.get_context_classes_at_iter(iter) != ['no-spell-check']: if buffer.get_context_classes_at_iter(iter) != ['no-spell-check']:
return False return False
ch = iter.get_char() ch = iter.get_char()
if not (ch in ('_', '.') or ch.isalnum()): if not (ch in ('_', '.') or ch.isalnum()):
return False return False
@ -75,20 +71,27 @@ class GediCompletionProvider(GObject.Object, GtkSource.CompletionProvider):
return GtkSource.CompletionActivation.INTERACTIVE return GtkSource.CompletionActivation.INTERACTIVE
def do_populate(self, context): def do_populate(self, context):
# TODO: Convert async maybe? # TODO: Maybe convert async?
it = self.get_iter_correctly(context) it = self.get_iter_correctly(context)
document = it.get_buffer() buffer = it.get_buffer()
proposals = [] proposals = []
# for completion in Jedi.get_script(document).completions(): doc_text = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter(), False)
for completion in Jedi.get_script(self._file, document).complete(): iter_cursor = buffer.get_iter_at_mark(buffer.get_insert())
complete = completion.name linenum = iter_cursor.get_line() + 1
doc = completion.doc if jedi.__version__ <= (0,7,0) else completion.docstring() charnum = iter_cursor.get_line_index()
proposals.append(GtkSource.CompletionItem.new(completion.name, def create_generator():
completion.name, for completion in Jedi.get_script(self._file, doc_text).complete(line = linenum, column = None, fuzzy = False):
self.get_icon_for_type(completion.type), comp_item = GtkSource.CompletionItem.new()
doc)) comp_item.set_label(completion.name)
comp_item.set_text(completion.name)
comp_item.set_icon(self.get_icon_for_type(completion.type))
comp_item.set_info(completion.docstring())
yield comp_item
for item in create_generator():
proposals.append(item)
context.add_proposals(self, proposals, True) context.add_proposals(self, proposals, True)

View File

@ -13,7 +13,7 @@ from gi.repository import GtkSource
# Application imports # Application imports
from .source_view_events import SourceViewEventsMixin from .source_view_events import SourceViewEventsMixin
from .custom_completion_providers.example_completion_provider import ExampleCompletionProvider from .custom_completion_providers.example_completion_provider import ExampleCompletionProvider
from .custom_completion_providers.gedi_completion_provider import GediCompletionProvider from .custom_completion_providers.python_completion_provider import PythonCompletionProvider
@ -79,7 +79,6 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
self.connect("drag-data-received", self._on_drag_data_received) self.connect("drag-data-received", self._on_drag_data_received)
self._buffer.connect("mark-set", self._on_cursor_move) self._buffer.connect("mark-set", self._on_cursor_move)
self._buffer.connect('changed', self._is_modified) self._buffer.connect('changed', self._is_modified)
# self._buffer.connect("loaded", self.on_document_load)
def _document_loaded(self): def _document_loaded(self):
for provider in self._completion.get_providers(): for provider in self._completion.get_providers():
@ -94,8 +93,8 @@ class SourceView(SourceViewEventsMixin, GtkSource.View):
# example_completion_provider = ExampleCompletionProvider() # example_completion_provider = ExampleCompletionProvider()
# self._completion.add_provider(example_completion_provider) # self._completion.add_provider(example_completion_provider)
gedi_completion_provider = GediCompletionProvider(file) py_completion_provider = PythonCompletionProvider(file)
self._completion.add_provider(gedi_completion_provider) self._completion.add_provider(py_completion_provider)
def _subscribe_to_events(self): def _subscribe_to_events(self):