From 6e7353cf1182712b53a7ed5de3b004ad5507911f Mon Sep 17 00:00:00 2001 From: tmpod Date: Sun, 28 Apr 2024 17:05:10 +0100 Subject: [PATCH] Implement -L flag for the LSP tool, similar to the viewer You can pass `-L foo=/path/to/foo` to add `@foo` as a library when starting the LSP server. Closes #5144 --- tools/lsp/main.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tools/lsp/main.rs b/tools/lsp/main.rs index 43a03c4373a..64f7467e2f9 100644 --- a/tools/lsp/main.rs +++ b/tools/lsp/main.rs @@ -24,6 +24,7 @@ use lsp_types::notification::{ use lsp_types::{DidChangeTextDocumentParams, DidOpenTextDocumentParams, InitializeParams, Url}; use clap::{Args, Parser, Subcommand}; +use itertools::Itertools; use lsp_server::{Connection, ErrorCode, IoThreads, Message, RequestId, Response}; use std::cell::RefCell; use std::collections::HashMap; @@ -36,14 +37,14 @@ use std::task::{Poll, Waker}; #[derive(Clone, clap::Parser)] #[command(author, version, about, long_about = None)] pub struct Cli { - #[arg( - short = 'I', - name = "Add include paths for the import statements", - number_of_values = 1, - action - )] + /// Add include paths for the import statements + #[arg(short = 'I', name = "/path/to/import", number_of_values = 1, action)] include_paths: Vec, + /// Specify library location of the '@library' in the form 'library=/path/to/library' + #[arg(short = 'L', value_name = "library=path", number_of_values = 1, action)] + library_paths: Vec, + /// The style name for the preview ('native' or 'fluent') #[arg(long, name = "style name", default_value_t, action)] style: String, @@ -280,6 +281,11 @@ fn main_loop(connection: Connection, init_param: InitializeParams, cli_args: Cli compiler_config.style = Some(if cli_args.style.is_empty() { "native".into() } else { cli_args.style }); compiler_config.include_paths = cli_args.include_paths; + compiler_config.library_paths = cli_args + .library_paths + .iter() + .filter_map(|entry| entry.split('=').collect_tuple().map(|(k, v)| (k.into(), v.into()))) + .collect(); let server_notifier_ = server_notifier.clone(); compiler_config.open_import_fallback = Some(Rc::new(move |path| { let server_notifier = server_notifier_.clone();