diff --git a/plugins/gvls/gvls-code-plugin.vala b/plugins/gvls/gvls-code-plugin.vala new file mode 100644 index 0000000000..eb01c02e4b --- /dev/null +++ b/plugins/gvls/gvls-code-plugin.vala @@ -0,0 +1,117 @@ +/* gvls-sourceview.vala + * + * Copyright 2021 Daniel Espinosa + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * Author: Daniel Espinosa + */ + +public class Scratch.Plugins.GVlsCompletion : Peas.ExtensionBase, Peas.Activatable { + private MainWindow main_window; + + public Object object { owned get; construct; } + Scratch.Services.Interface plugins; + + public void activate () { + plugins = (Scratch.Services.Interface) object; + this.main_window = plugins.manager.window; + + main_window.folder_opened.connect ((project)=>{ + var gvls_manager = project.get_data ("gvls-manager"); + if (gvls_manager == null) { + GLib.File f = project.file.file; + gvls_manager = new GVlsui.ProjectManager.for_meson (f); + project.set_data ("gvls-manager", gvls_manager); + gvls_manager.manager.initialize_stdio.begin ((obj, res)=>{ + try { + gvls_manager.manager.initialize_stdio.end (res); + debug ("gvls-plugin: Started GVls server"); + + main_window.destroy.connect (()=>{ + gvls_manager.manager.client.server_exit.begin (); + }); + } catch (GLib.Error e) { + warning ("Error Opening File: %s", e.message); + } + }); + } + }); + + main_window.document_opened.connect ((doc)=>{ + if (doc.source_view.project == null) { + return; + } + + var gvls_manager = doc.source_view.project.get_data ("gvls-manager"); + if (gvls_manager == null) { + GLib.File f = doc.source_view.project.file.file; + gvls_manager = new GVlsui.ProjectManager.for_meson (f); + doc.source_view.project.set_data ("gvls-manager", gvls_manager); + gvls_manager.manager.initialize_stdio.begin ((obj, res)=>{ + try { + gvls_manager.manager.initialize_stdio.end (res); + debug ("gvls-plugin: Started GVls server"); + gvls_manager.set_completion_provider (doc.source_view, doc.file); + gvls_manager.open_document (doc.source_view); + + main_window.destroy.connect (()=>{ + gvls_manager.manager.client.server_exit.begin (); + }); + } catch (GLib.Error e) { + warning ("Error Opening File: %s", e.message); + } + }); + } else { + gvls_manager.set_completion_provider (doc.source_view, doc.file); + gvls_manager.open_document (doc.source_view); + + main_window.destroy.connect (()=>{ + gvls_manager.manager.client.server_exit.begin (); + }); + } + }); + } + + + public void deactivate () { + if (main_window == null) { + message ("No MainWindow was set"); + return; + } + + foreach (Services.Document doc in main_window.document_view.docs) { + var p = doc.source_view.project; + var gvls_manager = p.get_data ("gvls-manager"); + if (gvls_manager == null) { + continue; + } + + gvls_manager.manager.client.server_exit.begin (()=>{ + p.set_data ("gvls-manager", null); + }); + + } + + } + + public void update_state () {} +} + +[ModuleInit] +public void peas_register_types (TypeModule module) { + var objmodule = module as Peas.ObjectModule; + objmodule.register_extension_type (typeof (Peas.Activatable), + typeof (Scratch.Plugins.GVlsCompletion)); +} diff --git a/plugins/gvls/gvls-code.plugin b/plugins/gvls/gvls-code.plugin new file mode 100644 index 0000000000..20407e5bde --- /dev/null +++ b/plugins/gvls/gvls-code.plugin @@ -0,0 +1,8 @@ +[Plugin] +Module=libgvls-code +IAge=0 +Name=GVls Vala Completion Plugin +Description= Provides completion for Vala sources +Authors=Daniel Espinosa +Copyright=Copyright © 2018-2020 Daniel Espinosa Ortiz +Website=https://gitlab.gnome.org/esodan/gvls/wikis/home diff --git a/plugins/gvls/meson.build b/plugins/gvls/meson.build new file mode 100644 index 0000000000..5399e45403 --- /dev/null +++ b/plugins/gvls/meson.build @@ -0,0 +1,40 @@ +module_name = 'gvls-code' + +gvls_plugin_sources = files([ + 'gvls-code-plugin.vala' + ]) + +gvls_install_dir=join_paths(pluginsdir, module_name) + +install_data('gvls-code.plugin', install_dir: gvls_install_dir) + +plugin_deps = [ + gvls_dep, + gvlsp_dep, + gvlsui_dep, + codecore_dep, + ] + +shared_module(module_name, gvls_plugin_sources, + dependencies: plugin_deps, + install: true, + install_dir: gvls_install_dir, + vala_args: [ + '--target-glib=2.52', + ] +) + +custom_target(module_name + '.plugin_merge', + input: module_name + '.plugin', + output: module_name + '.plugin', + command : [msgfmt, + '--desktop', + '--keyword=Description', + '--keyword=Name', + '-d' + join_paths(meson.source_root (), 'po', 'plugins'), + '--template=@INPUT@', + '-o@OUTPUT@', + ], + install : true, + install_dir: join_paths(pluginsdir, module_name), +) diff --git a/plugins/meson.build b/plugins/meson.build index 2e8855ee1d..f8ca6eabd3 100644 --- a/plugins/meson.build +++ b/plugins/meson.build @@ -13,3 +13,10 @@ subdir('strip-trailing-save') subdir('terminal') subdir('vim-emulation') subdir('word-completion') + +gvls_dep = dependency ('gvls-19', version: '>=19.0', required: false) +gvlsp_dep = dependency ('gvlsp-19', version: '>=19.0', required: false) +gvlsui_dep = dependency ('gvlsui-19', version: '>=19.0', required: false) +if gvls_dep.found() and gvlsp_dep.found() and gvlsui_dep.found() + subdir('gvls') +endif diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index f86f5a5069..e92da1f7ca 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -63,17 +63,18 @@ namespace Scratch.FolderManager { } } - public void open_folder (File folder) { + public ProjectFolderItem? open_folder (File folder) { if (is_open (folder)) { var existing = find_path (root, folder.path); if (existing is Granite.Widgets.SourceList.ExpandableItem) { ((Granite.Widgets.SourceList.ExpandableItem)existing).expanded = true; + return (Scratch.FolderManager.ProjectFolderItem) existing; } - return; + return null; } - add_folder (folder, true); + return add_folder (folder, true); } public void collapse_all () { @@ -196,7 +197,6 @@ namespace Scratch.FolderManager { } } - private void rename_items_with_same_name (Item item) { string item_name = item.file.name; foreach (var child in this.root.children) { @@ -220,10 +220,10 @@ namespace Scratch.FolderManager { private void add_folder (File folder, bool expand) { if (is_open (folder)) { warning ("Folder '%s' is already open.", folder.path); - return; + return null; } else if (!folder.is_valid_directory (true)) { // Allow hidden top-level folders warning ("Cannot open invalid directory."); - return; + return null; } var folder_root = new ProjectFolderItem (folder, this); // Constructor adds project to GitManager @@ -257,6 +257,8 @@ namespace Scratch.FolderManager { }); write_settings (); + + return folder_root; } private bool is_open (File folder) { diff --git a/src/MainWindow.vala b/src/MainWindow.vala index bb7f3142c3..7e4d70a984 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -20,6 +20,9 @@ namespace Scratch { public class MainWindow : Hdy.Window { + + public signal void document_opened (Scratch.Services.Document doc); + public signal void folder_opened (Scratch.FolderManager.ProjectFolderItem? project); public const int FONT_SIZE_MAX = 72; public const int FONT_SIZE_MIN = 7; private const uint MAX_SEARCH_TEXT_LENGTH = 255; @@ -556,13 +559,15 @@ namespace Scratch { public void open_folder (File folder) { var foldermanager_file = new FolderManager.File (folder.get_path ()); - folder_manager_view.open_folder (foldermanager_file); + Scratch.FolderManager.ProjectFolderItem? project = folder_manager_view.open_folder (foldermanager_file); + folder_opened (project); } public void open_document (Scratch.Services.Document doc, bool focus = true, int cursor_position = 0) { FolderManager.ProjectFolderItem? project = folder_manager_view.get_project_for_file (doc.file); doc.source_view.project = project; document_view.open_document (doc, focus, cursor_position); + document_opened (doc); } // Close a document diff --git a/src/Widgets/DocumentView.vala b/src/Widgets/DocumentView.vala index 2f0da5524d..0c2ccc65ac 100644 --- a/src/Widgets/DocumentView.vala +++ b/src/Widgets/DocumentView.vala @@ -26,7 +26,11 @@ public class Scratch.Widgets.DocumentView : Granite.Widgets.DynamicNotebook { public Services.Document current_document { get { - return (Services.Document) current; + unowned Services.Document doc = null; + if (current is Services.Document) { + doc = (Services.Document) current; + } + return doc; } set { current = value;