[gnome-builder/wip/elad/jedi-pygobject] jedi: Support completion for GObject Introspection modules



commit 04b0c3a953861871004df8754989d51dd230bd39
Author: Elad Alfassa <elad fedoraproject org>
Date:   Wed Aug 19 19:13:06 2015 +0300

    jedi: Support completion for GObject Introspection modules
    
    By monkey-patching jedi to understand GObject Introspection, we can get
    a decent-enough auto-completion support for GObject Introspection modules

 plugins/jedi/jedi_plugin.py |   30 +++++++++++++++++++++++++++++-
 1 files changed, 29 insertions(+), 1 deletions(-)
---
diff --git a/plugins/jedi/jedi_plugin.py b/plugins/jedi/jedi_plugin.py
index 3a15943..9305f71 100644
--- a/plugins/jedi/jedi_plugin.py
+++ b/plugins/jedi/jedi_plugin.py
@@ -19,14 +19,43 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+from gi.importer import DynamicImporter
+from gi.module import IntrospectionModule
 from gi.repository import GLib
 from gi.repository import GObject
 from gi.repository import Gtk
 from gi.repository import GtkSource
 from gi.repository import Ide
+gi_importer = DynamicImporter('gi.repository')
 
 try:
     import jedi
+    from jedi.evaluate.compiled import CompiledObject
+    from jedi.evaluate.imports import Importer
+
+    class PatchedJediCompiledObject(CompiledObject):
+        "A modified version of Jedi CompiledObject to work with GObject Introspection modules"
+        def _cls(self):
+            if self.obj.__class__ == IntrospectionModule:
+                return self
+            else:
+                return super()._cls()
+
+    class PatchedJediImporter(Importer):
+        "A modified version of Jedi Importer to work with GObject Introspection modules"
+        def follow(self):
+            module_list = super().follow()
+            if module_list == []:
+                import_path = '.'.join([str(i) for i in self.import_path])
+                if import_path.startswith('gi.repository'):
+                    try:
+                        module = gi_importer.load_module(import_path)
+                        module_list = [PatchedJediCompiledObject(module)]
+                    except ImportError:
+                        pass
+            return module_list
+
+    jedi.evaluate.imports.Importer = PatchedJediImporter
     HAS_JEDI = True
 except ImportError:
     HAS_JEDI = False
@@ -196,4 +225,3 @@ def load_icon(context, name):
     _icon_cache[name] = icon
 
     return icon
-


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]