[gnome-builder/1151-vala-pass-dependencies-to-lsp: 3/3] GVls: implement runtime configuration notification



commit 502a46025425c730feb795d67c9f42f66d19d24c
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Thu Mar 5 09:31:31 2020 -0600

    GVls: implement runtime configuration notification
    
    Plugin now takes current runtime to know vala API
    version and use 'pkg-config' to find libvala's VAPI
    directory and generic VAPI at ${datadir}/vala/vapi,
    both are provided to the server to find packages
    in use for current runtime
    
    Plugin now report the Vala API version in use

 src/plugins/gvls/gvls_plugin.py | 138 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 121 insertions(+), 17 deletions(-)
---
diff --git a/src/plugins/gvls/gvls_plugin.py b/src/plugins/gvls/gvls_plugin.py
index df8f47030..9d3f3392b 100644
--- a/src/plugins/gvls/gvls_plugin.py
+++ b/src/plugins/gvls/gvls_plugin.py
@@ -1,3 +1,4 @@
+
 #!/usr/bin/env python
 
 # gvls_plugin.py
@@ -24,18 +25,10 @@ It builds off the generic language service components in libide
 by bridging them to our supervised Vala Language Server.
 """
 
-import gi
-import os
-import sys
-
 from gi.repository import GLib
 from gi.repository import Gio
 from gi.repository import GObject
 from gi.repository import Ide
-from gi.repository import Gdk
-from gi.repository import Gtk
-from gi.repository import GtkSource
-from gi.repository import Json
 
 DEV_MODE = True
 
@@ -50,10 +43,13 @@ class GVlsService(Ide.Object):
     default_vapi_dirs = True
     scan_work_space = True
     add_using_namespaces = True
+    library_vapidir = ""
+    system_vapidir = ""
     files = []
     packages = []
     vala_args = {}
     options = []
+    vala_api_version = ""
     build_system = None
     pipeline = None
     build_args = None
@@ -81,10 +77,6 @@ class GVlsService(Ide.Object):
         if parent is None:
             return
 
-        context = self.get_context()
-        workdir = context.ref_workdir()
-
-
     def do_stop(self):
         """
         Stops the Vala Language Server upon request to shutdown the
@@ -92,8 +84,8 @@ class GVlsService(Ide.Object):
         """
         if self._client is not None:
             print ("Shutting down server")
-            _client.stop()
-            _client.destroy()
+            self._client.stop()
+            self._client.destroy()
 
         if self._supervisor is not None:
             supervisor, self._supervisor = self._supervisor, None
@@ -166,6 +158,11 @@ class GVlsService(Ide.Object):
         vv = GLib.Variant.new_variant(GLib.Variant.new_boolean(val))
         return GLib.Variant.new_dict_entry(vk, vv)
 
+    def create_dict_entry_string(self, key, val):
+        vk = GLib.Variant.new_string (key)
+        vv = GLib.Variant.new_variant(GLib.Variant.new_string(val))
+        return GLib.Variant.new_dict_entry(vk, vv)
+
     def create_configuration_variant(self):
         try:
             b = GLib.VariantBuilder(GLib.VariantType.new('a{sv}'))
@@ -175,6 +172,9 @@ class GVlsService(Ide.Object):
             b.add_value(self.create_dict_entry_boolean('scanWorkspace', self.scan_work_space))
             b.add_value(self.create_dict_entry_boolean('addUsingNamespaces', self.add_using_namespaces))
             b.add_value(self.create_dict_entry_boolean('mesonBuildSystem', self.meson_build_system))
+            b.add_value(self.create_dict_entry_string('libraryVapi', self.library_vapidir))
+            b.add_value(self.create_dict_entry_string('systemVapi', self.system_vapidir))
+            b.add_value(self.create_dict_entry_string('valaApiVersion', self.vala_api_version))
             ad = GLib.Variant.new_string ('valaArgs')
             vadi = self.dict_to_array_variant(self.vala_args)
             adi = GLib.Variant.new_dict_entry(ad, GLib.Variant.new_variant (vadi))
@@ -229,7 +229,6 @@ class GVlsService(Ide.Object):
             self.packages = []
             self.vala_args = {}
             self.options = []
-            current = ''
             found_package = False
             found_arg = False
             arg_name = ''
@@ -348,7 +347,7 @@ class GVlsService(Ide.Object):
 
     def _did_change_configuration(self, source_object, result, user_data):
         try:
-            res = self._client.send_notification_finish(result)
+            self._client.send_notification_finish(result)
         except BaseException as exc:
             print('\n\nChange Configuration Notification error:\n')
             print(exc.args)
@@ -389,7 +388,109 @@ class GVlsService(Ide.Object):
             print('\n\nOn Pipeline Loaded start get build flags error:\n')
             print(exc.args)
             print('\n\n\n')
-                     
+
+    def on_get_vala_data_dir(self, vdp, cancellable, data):
+        try:
+            if self.pipeline == None:
+                return
+            rt = self.pipeline.get_runtime()
+            if rt == None:
+                return
+            vdpio = vdp.get_stdout_pipe()
+            ddpp = Gio.DataInputStream.new(vdpio)
+            ldp = ddpp.read_line()
+            fgdvapi = Gio.File.new_for_path(str(ldp[0],encoding='utf8'))
+            rtfgdvapi = rt.translate_file(fgdvapi)
+            self.system_vapidir = rtfgdvapi.get_uri() + "/vala/vapi"
+        except BaseException as exc:
+            print('\n\nOn get Vala DATA VAPI DIR:\n')
+            print(str(exc))
+            print('\n\n\n')
+    
+    def on_get_vapidir(self, vpkgp, cancellable, data):
+        try:
+            if self.pipeline == None:
+                return
+            rt = self.pipeline.get_runtime()
+            if rt == None:
+                return
+            vpstdio = vpkgp.get_stdout_pipe()
+            dpp = Gio.DataInputStream.new(vpstdio)
+            lp = dpp.read_line()
+            flvapi = Gio.File.new_for_path(str(lp[0],encoding='utf8'))
+            rtfvapi = rt.translate_file(flvapi)
+            self.library_vapidir = rtfvapi.get_uri()
+            flags = Gio.SubprocessFlags.STDOUT_PIPE
+            launcher = rt.create_launcher()
+            if launcher == None:
+                return
+            launcher.set_cwd(GLib.get_home_dir())
+            launcher.set_flags(flags)
+            launcher.set_argv (['pkg-config','--variable','datadir','libvala-'+self.vala_api_version])
+            vdp = launcher.spawn(None)
+            vdp.wait_async(None, self.on_get_vala_data_dir, None)
+        except BaseException as exc:
+            print('\n\nOn get Vala VAPI DIR:\n')
+            print(str(exc))
+            print('\n\n\n')
+    
+    def on_get_vala_api_version(self, valacp, cancellable, data):
+        try:
+            vstdio = valacp.get_stdout_pipe()
+            dp = Gio.DataInputStream.new(vstdio)
+            l = dp.read_line()
+            self.vala_api_version = str(l[0],encoding='utf8')
+            if self.pipeline == None:
+                return
+            rt = self.pipeline.get_runtime()
+            if rt == None:
+                return
+            flags = Gio.SubprocessFlags.STDOUT_PIPE
+            launcher = rt.create_launcher()
+            if launcher == None:
+                return
+            launcher.set_cwd(GLib.get_home_dir())
+            launcher.set_flags(flags)
+            launcher.set_argv (['pkg-config','--variable','vapidir','libvala-'+self.vala_api_version])
+            vpkgp = launcher.spawn(None)
+            vpkgp.wait_async(None, self.on_get_vapidir, None)
+        except BaseException as exc:
+            print('\n\nOn get Vala API VERSION Runtime Configuration:\n')
+            print(str(exc))
+            print('\n\n\n')
+    
+    def _update_config_from_runtime(self):
+        try:
+            if self.pipeline == None:
+                return
+            rt = self.pipeline.get_runtime()
+            if rt == None:
+                return
+            flags = Gio.SubprocessFlags.STDOUT_PIPE
+            launcher = rt.create_launcher()
+            if launcher == None:
+                return
+            launcher.set_cwd(GLib.get_home_dir())
+            launcher.set_flags(flags)
+            launcher.set_argv (['valac','--api-version'])
+            valacp = launcher.spawn(None)
+            valacp.wait_async(None, self.on_get_vala_api_version, None)
+        except BaseException as exc:
+            print('\n\nOn Update Runtime Configuration:\n')
+            print(str(exc))
+            print('\n\n\n')
+    
+    def on_config_changed_cb(self, data):
+        try:
+            ctx = self._client.ref_context()
+            buildmgr = Ide.BuildManager.from_context (ctx)
+            self.pipeline = buildmgr.get_pipeline ()
+            self.pipeline.connect('diagnostic', self._on_pipeline_diagnostic)
+        except BaseException as exc:
+            print('\n\nOn Config Changed CB:\n')
+            print(str(exc))
+            print('\n\n\n')
+
     def _gvls_spawned(self, supervisor, subprocess):
         """
         This callback is executed when the `org.gnome.gvls.stdio.Server` process is spawned.
@@ -420,6 +521,9 @@ class GVlsService(Ide.Object):
             buildmgr = Ide.BuildManager.from_context (ctx)
             self.pipeline = buildmgr.get_pipeline ()
             self.pipeline.connect('diagnostic', self._on_pipeline_diagnostic)
+            cfgmgr = Ide.ConfigManager.from_context(ctx)
+            cfgmgr.connect('notify::current', self.on_config_changed_cb)
+            self._update_config_from_runtime()
         except BaseException as exc:
             print('\n\n\n Exception Arguments: \n')
             print(exc.args)


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