[gnome-builder/wip/tingping/python-langserv] Create python-langserv plugin



commit 2dd49f75a5256bb13b829acda6cd4037937824bc
Author: Patrick Griffis <tingping tingping se>
Date:   Tue Apr 18 18:56:19 2017 -0400

    Create python-langserv plugin

 configure.ac                                   |    2 +
 meson_options.txt                              |    1 +
 plugins/Makefile.am                            |    1 +
 plugins/meson.build                            |    1 +
 plugins/python-langserv/Makefile.am            |   14 ++
 plugins/python-langserv/configure.ac           |   12 ++
 plugins/python-langserv/meson.build            |    6 +
 plugins/python-langserv/python-langserv.plugin |   12 ++
 plugins/python-langserv/python_langserv.py     |  159 ++++++++++++++++++++++++
 9 files changed, 208 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 194fd7d..affe901 100644
--- a/configure.ac
+++ b/configure.ac
@@ -322,6 +322,7 @@ m4_include([plugins/notification/configure.ac])
 m4_include([plugins/phpize/configure.ac])
 m4_include([plugins/project-tree/configure.ac])
 m4_include([plugins/python-gi-imports-completion/configure.ac])
+m4_include([plugins/python-langserv/configure.ac])
 m4_include([plugins/python-pack/configure.ac])
 m4_include([plugins/quick-highlight/configure.ac])
 m4_include([plugins/retab/configure.ac])
@@ -611,6 +612,7 @@ echo "  Project Tree ......................... : ${enable_project_tree_plugin}"
 echo "  Python GObject Introspection ......... : ${enable_python_gi_imports_completion_plugin}"
 echo "  Python Jedi Autocompletion ........... : ${enable_jedi_plugin}"
 echo "  Python Language Pack ................. : ${enable_python_pack_plugin}"
+echo "  Python Language Server ............... : ${enable_python_langserv_plugin}"
 echo "  Quick Highlight ...................... : ${enable_quick_highlight_plugin}"
 echo "  Retab................................. : ${enable_retab_plugin}"
 echo "  Rust Language Server ................. : ${enable_rust_langserv_plugin}"
diff --git a/meson_options.txt b/meson_options.txt
index c7067bb..c665810 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -50,6 +50,7 @@ option('with_notification', type: 'boolean')
 option('with_phpize', type: 'boolean')
 option('with_project_tree', type: 'boolean')
 option('with_python_gi_imports_completion', type: 'boolean')
+option('with_python_langserv', type: 'boolean')
 option('with_python_pack', type: 'boolean')
 option('with_quick_highlight', type: 'boolean')
 option('with_retab', type: 'boolean')
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index ffe0710..adaeac1 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -33,6 +33,7 @@ SUBDIRS =                            \
        phpize                       \
        project-tree                 \
        python-gi-imports-completion \
+       python-langserv              \
        python-pack                  \
        quick-highlight              \
        retab                        \
diff --git a/plugins/meson.build b/plugins/meson.build
index 4c9406d..584ea2e 100644
--- a/plugins/meson.build
+++ b/plugins/meson.build
@@ -44,6 +44,7 @@ subdir('notification')
 subdir('phpize')
 subdir('project-tree')
 subdir('python-gi-imports-completion')
+subdir('python-langserv')
 subdir('python-pack')
 subdir('quick-highlight')
 subdir('retab')
diff --git a/plugins/python-langserv/Makefile.am b/plugins/python-langserv/Makefile.am
new file mode 100644
index 0000000..515a8b9
--- /dev/null
+++ b/plugins/python-langserv/Makefile.am
@@ -0,0 +1,14 @@
+if ENABLE_PYTHON_LANGSERV_PLUGIN
+
+EXTRA_DIST = $(plugin_DATA)
+
+plugindir = $(libdir)/gnome-builder/plugins
+dist_plugin_DATA =           \
+       python-langserv.plugin \
+       python_langserv.py
+
+endif
+
+include $(top_srcdir)/plugins/Makefile.plugin
+
+-include $(top_srcdir)/git.mk
diff --git a/plugins/python-langserv/configure.ac b/plugins/python-langserv/configure.ac
new file mode 100644
index 0000000..3cf8ece
--- /dev/null
+++ b/plugins/python-langserv/configure.ac
@@ -0,0 +1,12 @@
+# --enable-python-langserv-plugin=yes/no
+AC_ARG_ENABLE([python-langserv-plugin],
+              [AS_HELP_STRING([--enable-python-langserv-plugin=@<:@yes/no@:>@],
+                              [Build with support for Python Language Server integration.])],
+              [enable_python_langserv_plugin=$enableval],
+              [enable_python_langserv_plugin=yes])
+
+# for if ENABLE_PYTHON_LANGSERV_PLUGIN in Makefile.am
+AM_CONDITIONAL(ENABLE_PYTHON_LANGSERV_PLUGIN, test x$enable_python_langserv_plugin = xyes)
+
+# Ensure our makefile is generated by autoconf
+AC_CONFIG_FILES([plugins/python-langserv/Makefile])
diff --git a/plugins/python-langserv/meson.build b/plugins/python-langserv/meson.build
new file mode 100644
index 0000000..6c739b0
--- /dev/null
+++ b/plugins/python-langserv/meson.build
@@ -0,0 +1,6 @@
+if get_option('with_python_langserv')
+
+install_data('python-langserv.plugin', install_dir: plugindir)
+install_data('python_langserv.py', install_dir: plugindir)
+
+endif
diff --git a/plugins/python-langserv/python-langserv.plugin b/plugins/python-langserv/python-langserv.plugin
new file mode 100644
index 0000000..2eedda7
--- /dev/null
+++ b/plugins/python-langserv/python-langserv.plugin
@@ -0,0 +1,12 @@
+[Plugin]
+Module=python_langserv
+Loader=python3
+Name=Python Language Server Integration
+Description=Provides auto-completion, diagnostics, and other IDE features
+Authors=Patrick Griffis <tingping tingping se>
+Copyright=Copyright © 2017 Patrick Griffis
+Builtin=true
+X-Completion-Provider-Languages=python3,python
+X-Diagnostic-Provider-Languages=python3,python
+X-Formatter-Languages=python3,python
+X-Symbol-Resolver-Languages=python3,python
diff --git a/plugins/python-langserv/python_langserv.py b/plugins/python-langserv/python_langserv.py
new file mode 100644
index 0000000..40db76e
--- /dev/null
+++ b/plugins/python-langserv/python_langserv.py
@@ -0,0 +1,159 @@
+#!/usr/bin/env python3
+
+# python_langserv.py
+#
+# Copyright (C) 2016 Christian Hergert <chergert redhat com>
+# Copyright (C) 2017 Patrick Griffis <tingping tingping se>
+#
+# 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 Python Language Server.
+It builds off the generic language service components in libide
+by bridging them to our supervised Python Language Server.
+"""
+
+import gi
+import os
+
+gi.require_version('Ide', '1.0')
+gi.require_version('GtkSource', '3.0')
+
+from gi.repository import GLib
+from gi.repository import Gio
+from gi.repository import GObject
+from gi.repository import GtkSource
+from gi.repository import Ide
+
+DEV_MODE = True
+
+class PythonService(Ide.Object, Ide.Service):
+    _client = None
+    _has_started = False
+    _supervisor = None
+
+    @GObject.Property(type=Ide.LangservClient)
+    def client(self):
+        return self._client
+
+    @client.setter
+    def client(self, value):
+        self._client = value
+        self.notify('client')
+
+    def do_stop(self):
+        """
+        Stops the Python Language Server upon request to shutdown the
+        PythonService.
+        """
+        if self._supervisor:
+            supervisor, self._supervisor = self._supervisor, None
+            supervisor.stop()
+
+    def _ensure_started(self):
+        """
+        Start the python service which provides communication with the
+        Python 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 PythonService to access the python components they need.
+        """
+        # To avoid starting the `pyls` 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 python language server
+            launcher = self._create_launcher()
+            launcher.set_clear_env(False)
+
+            # Locate the directory of the project and run pyls from there.
+            workdir = self.get_context().get_vcs().get_working_directory()
+            launcher.set_cwd(workdir.get_path())
+
+            launcher.push_argv('pyls')
+
+            # 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._pyls_spawned)
+            self._supervisor.set_launcher(launcher)
+            self._supervisor.start()
+
+    def _pyls_spawned(self, supervisor, subprocess):
+        """
+        This callback is executed when the `pyls` process is spawned.
+        We can use the stdin/stdout to create a channel for our
+        LangservClient.
+        """
+        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 = Ide.LangservClient.new(self.get_context(), io_stream)
+        self._client.add_language('python3')
+        self._client.add_language('python')
+        self._client.start()
+        self.notify('client')
+
+    def _create_launcher(self):
+        """
+        Creates a launcher to be used by the python service. This needs
+        to be run on the host because we do not currently bundle python
+        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_run_on_host(True)
+        return launcher
+
+    @classmethod
+    def bind_client(klass, provider):
+        """
+        This helper tracks changes to our client as it might happen when
+        our `pyls` process has crashed.
+        """
+        context = provider.get_context()
+        self = context.get_service_typed(PythonService)
+        self._ensure_started()
+        self.bind_property('client', provider, 'client', GObject.BindingFlags.SYNC_CREATE)
+
+class PythonDiagnosticProvider(Ide.LangservDiagnosticProvider):
+    def do_load(self):
+        PythonService.bind_client(self)
+
+class PythonCompletionProvider(Ide.LangservCompletionProvider):
+    def do_load(self, context):
+        PythonService.bind_client(self)
+
+class PythonSymbolResolver(Ide.LangservSymbolResolver):
+    def do_load(self):
+        PythonService.bind_client(self)
+
+class PythonFormatter(Ide.LangservFormatter):
+    def do_load(self):
+        PythonService.bind_client(self)


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