Skip to content

Commit f1fb3ff

Browse files
committed
meson: support settings overrides from editor
This introduces settings overrides (`EditorOptions`) passed from the editor to the server in the initialize() call. These overrides are just currently for the Meson backend. The intent is to have the vala-vscode extension send these options from the Meson VSCode extension to VLS. Closes #304 and #305.
1 parent a492927 commit f1fb3ff

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

src/projects/mesonproject.vala

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Vls.MesonProject : Project {
2727
private string build_dir;
2828
private bool configured_once;
2929
private bool requires_general_build;
30+
private string[] extra_setup_args = {};
31+
private string[] extra_compile_args = {};
3032

3133
/**
3234
* Substitute special arguments like `@INPUT@` and `@OUTPUT@` as they
@@ -209,6 +211,9 @@ class Vls.MesonProject : Project {
209211
string proc_stdout, proc_stderr;
210212
int proc_status;
211213

214+
foreach (string arg in extra_setup_args)
215+
spawn_args += arg;
216+
212217
string command_str = "";
213218
foreach (string part in spawn_args) {
214219
if (command_str != "")
@@ -761,9 +766,13 @@ class Vls.MesonProject : Project {
761766
if (requires_general_build) {
762767
int proc_status;
763768
string proc_stdout, proc_stderr;
769+
string[] spawn_args = {"meson", "compile"};
770+
771+
foreach (string arg in extra_compile_args)
772+
spawn_args += arg;
764773

765774
Process.spawn_sync (build_dir,
766-
{"meson", "compile"},
775+
spawn_args,
767776
null,
768777
SpawnFlags.SEARCH_PATH,
769778
null,
@@ -781,9 +790,28 @@ class Vls.MesonProject : Project {
781790
base.build_if_stale (cancellable);
782791
}
783792

784-
public MesonProject (string root_path, FileCache file_cache, Cancellable? cancellable = null) throws Error {
793+
public MesonProject (string root_path, FileCache file_cache,
794+
Lsp.EditorOptions? editor_options,
795+
Cancellable? cancellable = null) throws Error {
785796
base (root_path, file_cache);
786-
this.build_dir = DirUtils.make_tmp (@"vls-meson-$(str_hash (root_path))-XXXXXX");
797+
var default_build_dir_template = @"vls-meson-$(str_hash (root_path))-XXXXXX";
798+
if (editor_options != null) {
799+
debug ("overriding with VSCode Meson plugin settings: %s",
800+
Json.gobject_to_data (editor_options, null));
801+
this.extra_setup_args = editor_options.mesonConfigureOptions;
802+
this.extra_compile_args = editor_options.mesonCompileOptions;
803+
if (editor_options.mesonBuildDir != null) {
804+
var meson_build_dir = File.new_for_path (editor_options.mesonBuildDir);
805+
try {
806+
meson_build_dir.make_directory_with_parents (cancellable);
807+
this.build_dir = editor_options.mesonBuildDir;
808+
} catch (IOError.EXISTS e) {
809+
// ignore whether the build folder exists
810+
}
811+
}
812+
}
813+
if (this.build_dir == null)
814+
this.build_dir = DirUtils.make_tmp (default_build_dir_template);
787815
reconfigure_if_stale (cancellable);
788816
}
789817

src/protocol.vala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,38 @@ namespace Lsp {
693693
public TextDocumentClientCapabilities textDocument { get; set; default = new TextDocumentClientCapabilities (); }
694694
}
695695

696+
/**
697+
* (Custom) Editor type for {@link EditorOptions}
698+
*/
699+
enum EditorType {
700+
UNKNOWN = 0,
701+
VSCODE = 1
702+
}
703+
704+
/**
705+
* (Custom) extra settings and capabilities of the editor.
706+
* These may be overrides for projects. This is not LSP-specific.
707+
*/
708+
class EditorOptions : Object {
709+
public EditorType editorType { get; set; }
710+
public string[] mesonConfigureOptions { get; set; }
711+
public string[] mesonCompileOptions { get; set; }
712+
private string? _mesonBuildDir;
713+
public string? mesonBuildDir {
714+
get { return _mesonBuildDir; }
715+
set {
716+
if (value != null && value.length > 0)
717+
_mesonBuildDir = value;
718+
}
719+
}
720+
}
721+
696722
class InitializeParams : Object {
697723
public int processId { get; set; }
698724
public string? rootPath { get; set; }
699725
public string? rootUri { get; set; }
700726
public ClientCapabilities capabilities { get; set; default = new ClientCapabilities (); }
727+
public EditorOptions? initializationOptions { get; set; }
701728
}
702729

703730
class SignatureInformation : Object, Json.Serializable {

src/server.vala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,13 @@ class Vls.Server : Jsonrpc.Server {
400400
// TODO: autotools, make(?), cmake(?)
401401
if (meson_file.query_exists (cancellable)) {
402402
try {
403-
backend_project = new MesonProject (root_path, file_cache, cancellable);
403+
backend_project = new MesonProject (root_path, file_cache,
404+
init_params.initializationOptions, cancellable);
405+
} catch (ProjectError.VERSION_UNSUPPORTED e) {
406+
// ignore and skip to the next project
407+
show_message (client, @"Won't use Meson project - $(e.message)", MessageType.Warning);
404408
} catch (Error e) {
405-
if (!(e is ProjectError.VERSION_UNSUPPORTED)) {
406-
show_message (client, @"Failed to initialize Meson project - $(e.message)", MessageType.Error);
407-
}
409+
show_message (client, @"Failed to initialize Meson project - $(e.message)", MessageType.Error);
408410
}
409411
}
410412

0 commit comments

Comments
 (0)