From 84369e594cb3a417c94e928240617d6147f8a0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Barbero=20Rodr=C3=ADguez?= Date: Tue, 1 Nov 2011 21:10:40 +0100 Subject: [PATCH] Added basic symbol completion --- sourcecodebrowser/completion.py | 77 +++++++++++++++++++++++++++++++++ sourcecodebrowser/plugin.py | 12 ++++- 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 sourcecodebrowser/completion.py diff --git a/sourcecodebrowser/completion.py b/sourcecodebrowser/completion.py new file mode 100644 index 0000000..33ab98c --- /dev/null +++ b/sourcecodebrowser/completion.py @@ -0,0 +1,77 @@ +from gi.repository import GObject, Gtk, GtkSource, Gedit + +class Proposal(GObject.Object, GtkSource.CompletionProposal): + __gtype_name__ = "GeditSourceCodeProposal" + + + def __init__(self, tag): + GObject.Object.__init__(self) + self._tag = tag + + # Interface implementation + def do_get_markup(self): + return self._tag.name + + """ + def do_get_info(self): + return self._tag.name + """ + +class Provider(GObject.Object, GtkSource.CompletionProvider): + __gtype_name__ = "GeditSourceCodeProvider" + + def __init__(self, sourcetree): + GObject.Object.__init__(self) + self.name = 'SourceCodeProvider' + self.sourcetree = sourcetree + """ + theme = Gtk.IconTheme.get_default() + f, w, h = Gtk.icon_size_lookup(Gtk.IconSize.MENU) + + try: + self.icon = theme.load_icon(Gtk.STOCK_JUSTIFY_LEFT, w, 0) + except: + self.icon = None + """ + self.icon = self.sourcetree.get_pixbuf('source-code-browser') + + def get_word(self, context): + it = context.get_iter() + + if it.starts_word() or it.starts_line() or not it.ends_word(): + return None + + start = it.copy() + + if start.backward_word_start(): + return start.get_text(it) + else: + return None + + def do_match(self, context): + return True + + def do_populate(self, context): + word = self.get_word(context) + proposals = [] + print word + for tag in self.sourcetree.tags: + if not word or tag.name.startswith(word): + proposals.append(Proposal(tag)) + context.add_proposals(self, proposals, True) + + def do_get_name(self): + return self.name + + def do_activate_proposal(self, proposal, piter): + buf = piter.get_buffer() + buf.insert_at_cursor(proposal._tag.name, -1) + return True + + def do_get_icon(self): + return self.icon + + def do_get_activation(self): + return GtkSource.CompletionActivation.USER_REQUESTED + +# ex:ts=8:et: diff --git a/sourcecodebrowser/plugin.py b/sourcecodebrowser/plugin.py index 19123fc..4dec07a 100644 --- a/sourcecodebrowser/plugin.py +++ b/sourcecodebrowser/plugin.py @@ -3,6 +3,7 @@ import logging import tempfile import ctags +import completion from gi.repository import GObject, GdkPixbuf, Gedit, Gtk, PeasGtk, Gio logging.basicConfig() @@ -179,9 +180,9 @@ def load(self, kinds, tags, uri): # TODO: We need to go at least one more level to deal with the inline # classes used in many python projects (eg. Models in Django) # Recursion would be even better. - + self.tags = tags # sort - if self.sort_list: + if self.sort_list: self._store.set_sort_column_id(1, Gtk.SortType.ASCENDING) # expand @@ -393,8 +394,11 @@ def _load_active_document_symbols(self): self._is_loaded = False # do not load if not the active tab in the panel panel = self.window.get_side_panel() + #Disabled because completion needs the symbols + """ if not panel.item_is_active(self._sourcetree): return + """ document = self.window.get_active_document() if document: @@ -424,6 +428,10 @@ def _load_active_document_symbols(self): def on_active_tab_changed(self, window, tab, data=None): self._load_active_document_symbols() + + if not hasattr(self.window.get_active_view(), "sc_completion"): + self.window.get_active_view().get_completion().add_provider(completion.Provider(self._sourcetree)) + self.window.get_active_view().sc_completion = True def on_setting_changed(self, settings, key, data=None): """