[gnome-builder] Upstream Vala Language Server plugin



commit 71a36195468a38057c6fa9495ce0b0a11c77a03d
Author: Princeton Ferro <princetonferro gmail com>
Date:   Tue Jul 6 12:18:24 2021 -0400

    Upstream Vala Language Server plugin
    
    This is to upstream the plugin for the language server at
    https://github.com/Prince781/vala-language-server

 meson_options.txt                    |   1 +
 src/plugins/meson.build              |   4 +-
 src/plugins/vls/meson.build          |  13 +++
 src/plugins/vls/vala_langserv.plugin |  16 +++
 src/plugins/vls/vala_langserv.py     | 191 +++++++++++++++++++++++++++++++++++
 5 files changed, 224 insertions(+), 1 deletion(-)
---
diff --git a/meson_options.txt b/meson_options.txt
index 315274648..6c246cb9d 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -76,6 +76,7 @@ option('plugin_update_manager', type: 'boolean')
 option('plugin_vala', type: 'boolean')
 option('plugin_vagrant', type: 'boolean', value: false)
 option('plugin_valgrind', type: 'boolean')
+option('plugin_vls', type: 'boolean')
 option('plugin_waf', type: 'boolean')
 option('plugin_words', type: 'boolean')
 option('plugin_xml_pack', type: 'boolean')
diff --git a/src/plugins/meson.build b/src/plugins/meson.build
index ebaa2bd26..6df310c80 100644
--- a/src/plugins/meson.build
+++ b/src/plugins/meson.build
@@ -132,6 +132,7 @@ subdir('vala-pack')
 subdir('valgrind')
 subdir('vcsui')
 subdir('vim')
+subdir('vls')
 subdir('waf')
 subdir('words')
 subdir('xml-pack')
@@ -199,7 +200,8 @@ status += [
   'Sysroot ............... : @0@'.format(get_option('plugin_sysroot')),
   'Todo .................. : @0@'.format(get_option('plugin_todo')),
   'Update Manager ........ : @0@'.format(get_option('plugin_update_manager')),
-  'Vala Language Server... : @0@'.format(get_option('plugin_gvls')),
+  'GNOME Vala LS ......... : @0@'.format(get_option('plugin_gvls')),
+  'Vala Language Server .. : @0@'.format(get_option('plugin_vls')),
   'Vala Pack ............. : @0@'.format(get_option('plugin_vala')),
   'Vagrant ............... : @0@'.format(get_option('plugin_vagrant')),
   'Valgrind .............. : @0@'.format(get_option('plugin_valgrind')),
diff --git a/src/plugins/vls/meson.build b/src/plugins/vls/meson.build
new file mode 100644
index 000000000..064a90ad3
--- /dev/null
+++ b/src/plugins/vls/meson.build
@@ -0,0 +1,13 @@
+if get_option('plugin_vls')
+
+install_data('vala_langserv.py', install_dir: plugindir)
+
+configure_file(
+          input: 'vala_langserv.plugin',
+         output: 'vala_langserv.plugin',
+  configuration: config_h,
+        install: true,
+    install_dir: plugindir,
+)
+
+endif
diff --git a/src/plugins/vls/vala_langserv.plugin b/src/plugins/vls/vala_langserv.plugin
new file mode 100644
index 000000000..7ab68b409
--- /dev/null
+++ b/src/plugins/vls/vala_langserv.plugin
@@ -0,0 +1,16 @@
+[Plugin]
+Name=Vala Language Server
+Description=Vala code intelligence provided by VLS.
+Authors=Princeton Ferro, Ben Iofel
+Copyright=Copyright © 2020
+
+Loader=python3
+Module=vala_langserv
+X-Completion-Provider-Languages=vala
+X-Diagnostic-Provider-Languages=vala
+X-Formatter-Languages=vala
+X-Highlighter-Languages=vala
+X-Hover-Provider-Languages=vala
+X-Rename-Provider-Languages=vala
+X-Symbol-Resolver-Languages=vala
+X-Builder-ABI=@PACKAGE_ABI@
diff --git a/src/plugins/vls/vala_langserv.py b/src/plugins/vls/vala_langserv.py
new file mode 100644
index 000000000..72a55e35f
--- /dev/null
+++ b/src/plugins/vls/vala_langserv.py
@@ -0,0 +1,191 @@
+
+#!/usr/bin/env python
+
+#   vala_langserv.py
+#
+# Copyright 2016 Christian Hergert <chergert redhat com>
+# Copyright 2020 Princeton Ferro <princetonferro gmail com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""
+This plugin provides integration with the Vala Language Server.
+It builds off the generic language service components in libide
+by bridging them to our supervised Vala Language Server.
+"""
+
+import gi
+import os
+
+from gi.repository import GLib
+from gi.repository import Gio
+from gi.repository import GObject
+from gi.repository import Ide
+
+DEV_MODE = False
+
+class VlsService(Ide.Object):
+    _client = None
+    _has_started = False
+    _supervisor = None
+    _monitor = None
+
+    @classmethod
+    def from_context(cls, context):
+        return context.ensure_child_typed(VlsService)
+
+    @GObject.Property(type=Ide.LspClient)
+    def client(self):
+        return self._client
+
+    @client.setter
+    def client(self, value):
+        self._client = value
+        self.notify('client')
+
+    def do_stop(self):
+        """
+        Stops the Vala Language Server upon request to shutdown the
+        VlsService.
+        """
+        if self._monitor is not None:
+            monitor, self._monitor = self._monitor, None
+            if monitor is not None:
+                monitor.cancel()
+
+        if self._supervisor is not None:
+            supervisor, self._supervisor = self._supervisor, None
+            supervisor.stop()
+
+    def _ensure_started(self):
+        """
+        Start the service which provides communication with the Vala Language
+        Server. We supervise our own instance of the language server and
+        restart it as necessary using the Ide.SubprocessSupervisor.
+
+        Various extension points (diagnostics, symbol providers, etc) use
+        the VlsService to access the rust components they need.
+        """
+        # To avoid starting the `vls` process unconditionally at startup,
+        # we lazily start it when the first provider tries to bind a client
+        # to its :client property.
+        if not self._has_started:
+            self._has_started = True
+
+            # Setup a launcher to spawn the rust language server
+            launcher = self._create_launcher()
+            launcher.set_clear_env(False)
+            if DEV_MODE:
+                launcher.setenv('G_MESSAGES_DEBUG', 'all', True)
+
+            # Locate the directory of the project and run vls from there.
+            workdir = self.get_context().ref_workdir()
+            launcher.set_cwd(workdir.get_path())
+
+            # Setup our Argv. We want to communicate over STDIN/STDOUT,
+            # so it does not require any command line options.
+            launcher.push_argv("vala-language-server")
+
+            # Spawn our peer process and monitor it for
+            # crashes. We may need to restart it occasionally.
+            self._supervisor = Ide.SubprocessSupervisor()
+            self._supervisor.connect('spawned', self._vls_spawned)
+            self._supervisor.set_launcher(launcher)
+            self._supervisor.start()
+
+    def _vls_spawned(self, supervisor, subprocess):
+        """
+        This callback is executed when the `vala-language-server` process is
+        spawned.  We can use the stdin/stdout to create a channel for our
+        LspClient.
+        """
+        stdin = subprocess.get_stdin_pipe()
+        stdout = subprocess.get_stdout_pipe()
+        io_stream = Gio.SimpleIOStream.new(stdout, stdin)
+
+        if self._client:
+            self._client.stop()
+            self._client.destroy()
+
+        self._client = Ide.LspClient.new(io_stream)
+        self.append(self._client)
+        self._client.add_language('vala')
+        self._client.start()
+        self.notify('client')
+
+    def _create_launcher(self):
+        """
+        Creates a launcher to be used by the rust service. This needs
+        to be run on the host because we do not currently bundle rust
+        inside our flatpak.
+
+        In the future, we might be able to rely on the runtime for
+        the tooling. Maybe even the program if flatpak-builder has
+        prebuilt our dependencies.
+        """
+        flags = Gio.SubprocessFlags.STDIN_PIPE | Gio.SubprocessFlags.STDOUT_PIPE
+        if not DEV_MODE:
+            flags |= Gio.SubprocessFlags.STDERR_SILENCE
+        launcher = Ide.SubprocessLauncher()
+        launcher.set_flags(flags)
+        launcher.set_cwd(GLib.get_home_dir())
+        launcher.set_run_on_host(True)
+        return launcher
+
+    @classmethod
+    def bind_client(cls, provider):
+        """
+        This helper tracks changes to our client as it might happen when
+        our `vls` process has crashed.
+        """
+        context = provider.get_context()
+        self = VlsService.from_context(context)
+        self._ensure_started()
+        self.bind_property('client', provider, 'client', GObject.BindingFlags.SYNC_CREATE)
+
+class VlsDiagnosticProvider(Ide.LspDiagnosticProvider):
+    def do_load(self):
+        VlsService.bind_client(self)
+
+class VlsCompletionProvider(Ide.LspCompletionProvider):
+    def do_load(self, context):
+        VlsService.bind_client(self)
+
+    def do_get_priority(self, context):
+        # This provider only activates when it is very likely that we
+        # want the results. So use high priority (negative is better).
+        return -1000
+
+class VlsRenameProvider(Ide.LspRenameProvider):
+    def do_load(self):
+        VlsService.bind_client(self)
+
+class VlsSymbolResolver(Ide.LspSymbolResolver):
+    def do_load(self):
+        VlsService.bind_client(self)
+
+class VlsHighlighter(Ide.LspHighlighter):
+    def do_load(self):
+        VlsService.bind_client(self)
+
+class VlsFormatter(Ide.LspFormatter):
+    def do_load(self):
+        VlsService.bind_client(self)
+
+class VlsHoverProvider(Ide.LspHoverProvider):
+    def do_prepare(self):
+        self.props.category = 'Vala'
+        self.props.priority = 200
+        VlsService.bind_client(self)
+


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