]> git.lizzy.rs Git - rust.git/commitdiff
Configuration plumbing for cargo watcher
authorEmil Lauridsen <mine809@gmail.com>
Wed, 25 Dec 2019 15:50:38 +0000 (16:50 +0100)
committerEmil Lauridsen <mine809@gmail.com>
Wed, 25 Dec 2019 16:37:40 +0000 (17:37 +0100)
crates/ra_lsp_server/src/cargo_check.rs
crates/ra_lsp_server/src/config.rs
crates/ra_lsp_server/src/main_loop.rs
crates/ra_lsp_server/src/world.rs
editors/code/package.json
editors/code/src/config.ts
editors/code/src/server.ts

index d5ff02154192d7307ea584706681aa8b3bb2f155..f98b4f69cd34e974180d163a4b0ebac927c79ba5 100644 (file)
@@ -1,3 +1,4 @@
+use crate::world::Options;
 use cargo_metadata::{
     diagnostic::{
         Applicability, Diagnostic as RustDiagnostic, DiagnosticLevel, DiagnosticSpan,
@@ -30,14 +31,17 @@ pub struct CheckWatcher {
 }
 
 impl CheckWatcher {
-    pub fn new(workspace_root: PathBuf) -> CheckWatcher {
+    pub fn new(options: &Options, workspace_root: PathBuf) -> CheckWatcher {
+        let check_command = options.cargo_check_command.clone();
+        let check_args = options.cargo_check_args.clone();
         let shared = Arc::new(RwLock::new(CheckWatcherSharedState::new()));
 
         let (task_send, task_recv) = unbounded::<CheckTask>();
         let (cmd_send, cmd_recv) = unbounded::<CheckCommand>();
         let shared_ = shared.clone();
         let handle = std::thread::spawn(move || {
-            let mut check = CheckWatcherState::new(shared_, workspace_root);
+            let mut check =
+                CheckWatcherState::new(check_command, check_args, workspace_root, shared_);
             check.run(&task_send, &cmd_recv);
         });
 
@@ -50,6 +54,8 @@ pub fn update(&self) {
 }
 
 pub struct CheckWatcherState {
+    check_command: Option<String>,
+    check_args: Vec<String>,
     workspace_root: PathBuf,
     running: bool,
     watcher: WatchThread,
@@ -134,11 +140,21 @@ pub enum CheckCommand {
 
 impl CheckWatcherState {
     pub fn new(
-        shared: Arc<RwLock<CheckWatcherSharedState>>,
+        check_command: Option<String>,
+        check_args: Vec<String>,
         workspace_root: PathBuf,
+        shared: Arc<RwLock<CheckWatcherSharedState>>,
     ) -> CheckWatcherState {
-        let watcher = WatchThread::new(&workspace_root);
-        CheckWatcherState { workspace_root, running: false, watcher, last_update_req: None, shared }
+        let watcher = WatchThread::new(check_command.as_ref(), &check_args, &workspace_root);
+        CheckWatcherState {
+            check_command,
+            check_args,
+            workspace_root,
+            running: false,
+            watcher,
+            last_update_req: None,
+            shared,
+        }
     }
 
     pub fn run(&mut self, task_send: &Sender<CheckTask>, cmd_recv: &Receiver<CheckCommand>) {
@@ -163,7 +179,11 @@ pub fn run(&mut self, task_send: &Sender<CheckTask>, cmd_recv: &Receiver<CheckCo
                 self.shared.write().clear(task_send);
 
                 self.watcher.cancel();
-                self.watcher = WatchThread::new(&self.workspace_root);
+                self.watcher = WatchThread::new(
+                    self.check_command.as_ref(),
+                    &self.check_args,
+                    &self.workspace_root,
+                );
             }
         }
     }
@@ -229,13 +249,25 @@ struct WatchThread {
 }
 
 impl WatchThread {
-    fn new(workspace_root: &PathBuf) -> WatchThread {
-        let manifest_path = format!("{}/Cargo.toml", workspace_root.to_string_lossy());
+    fn new(
+        check_command: Option<&String>,
+        check_args: &[String],
+        workspace_root: &PathBuf,
+    ) -> WatchThread {
+        let check_command = check_command.cloned().unwrap_or("check".to_string());
+        let mut args: Vec<String> = vec![
+            check_command,
+            "--message-format=json".to_string(),
+            "--manifest-path".to_string(),
+            format!("{}/Cargo.toml", workspace_root.to_string_lossy()),
+        ];
+        args.extend(check_args.iter().cloned());
+
         let (message_send, message_recv) = unbounded();
         let (cancel_send, cancel_recv) = unbounded();
         std::thread::spawn(move || {
             let mut command = Command::new("cargo")
-                .args(&["check", "--message-format=json", "--manifest-path", &manifest_path])
+                .args(&args)
                 .stdout(Stdio::piped())
                 .stderr(Stdio::null())
                 .spawn()
index 67942aa4147cd1f6bd273d87fe369dfdf04e142b..621f2238c51ad39ba5933c91876c3c5d885c5b8e 100644 (file)
@@ -32,6 +32,10 @@ pub struct ServerConfig {
 
     pub max_inlay_hint_length: Option<usize>,
 
+    pub cargo_check_enable: bool,
+    pub cargo_check_command: Option<String>,
+    pub cargo_check_args: Vec<String>,
+
     /// For internal usage to make integrated tests faster.
     #[serde(deserialize_with = "nullable_bool_true")]
     pub with_sysroot: bool,
@@ -51,6 +55,9 @@ fn default() -> ServerConfig {
             use_client_watching: false,
             lru_capacity: None,
             max_inlay_hint_length: None,
+            cargo_check_enable: true,
+            cargo_check_command: None,
+            cargo_check_args: vec![],
             with_sysroot: true,
             feature_flags: FxHashMap::default(),
             cargo_features: Default::default(),
index 943d38943f1cb6d9dd7518dc92c902a4b90b755f..1f6175699a0eb63a4ae33099d1f58d470cb3cc2a 100644 (file)
@@ -127,6 +127,9 @@ pub fn main_loop(
                     .and_then(|it| it.line_folding_only)
                     .unwrap_or(false),
                 max_inlay_hint_length: config.max_inlay_hint_length,
+                cargo_check_enable: config.cargo_check_enable,
+                cargo_check_command: config.cargo_check_command,
+                cargo_check_args: config.cargo_check_args,
             }
         };
 
index 8e9380ca07ecf7fb6ef9b514d467732b8deba6a3..235eb199d01ef7c76f0d40608d0c0a7aad256ceb 100644 (file)
@@ -35,6 +35,9 @@ pub struct Options {
     pub supports_location_link: bool,
     pub line_folding_only: bool,
     pub max_inlay_hint_length: Option<usize>,
+    pub cargo_check_enable: bool,
+    pub cargo_check_command: Option<String>,
+    pub cargo_check_args: Vec<String>,
 }
 
 /// `WorldState` is the primary mutable state of the language server
@@ -131,7 +134,7 @@ pub fn new(
         change.set_crate_graph(crate_graph);
 
         // FIXME: Figure out the multi-workspace situation
-        let check_watcher = CheckWatcher::new(folder_roots.first().cloned().unwrap());
+        let check_watcher = CheckWatcher::new(&options, folder_roots.first().cloned().unwrap());
 
         let mut analysis_host = AnalysisHost::new(lru_capacity, feature_flags);
         analysis_host.apply_change(change);
index 6cb24a3cec680bfab0084f82a1de91c5fd263d38..5f4123397b2e068c8d0e3b257c789ef6d7dd32be 100644 (file)
                     "default": "ra_lsp_server",
                     "description": "Path to ra_lsp_server executable"
                 },
-                "rust-analyzer.enableCargoWatchOnStartup": {
-                    "type": "string",
-                    "default": "ask",
-                    "enum": [
-                        "ask",
-                        "enabled",
-                        "disabled"
-                    ],
-                    "enumDescriptions": [
-                        "Asks each time whether to run `cargo watch`",
-                        "`cargo watch` is always started",
-                        "Don't start `cargo watch`"
-                    ],
-                    "description": "Whether to run `cargo watch` on startup"
+                "rust-analyzer.enableCargoCheck": {
+                    "type": "boolean",
+                    "default": true,
+                    "description": "Run `cargo check` for diagnostics on save"
                 },
                 "rust-analyzer.excludeGlobs": {
                     "type": "array",
                     "default": true,
                     "description": "client provided file watching instead of notify watching."
                 },
-                "rust-analyzer.cargo-watch.arguments": {
-                    "type": "string",
-                    "description": "`cargo-watch` arguments. (e.g: `--features=\"shumway,pdf\"` will run as `cargo watch -x \"check --features=\"shumway,pdf\"\"` )",
-                    "default": ""
-                },
-                "rust-analyzer.cargo-watch.command": {
-                    "type": "string",
-                    "description": "`cargo-watch` command. (e.g: `clippy` will run as `cargo watch -x clippy` )",
-                    "default": "check"
-                },
-                "rust-analyzer.cargo-watch.ignore": {
+                "rust-analyzer.cargo-check.arguments": {
                     "type": "array",
-                    "description": "A list of patterns for cargo-watch to ignore (will be passed as `--ignore`)",
+                    "description": "`cargo-check` arguments. (e.g: `--features=\"shumway,pdf\"` will run as `cargo check --features=\"shumway,pdf\"` )",
                     "default": []
                 },
-                "rust-analyzer.cargo-watch.allTargets": {
-                    "type": "boolean",
-                    "description": "Check all targets and tests (will be passed as `--all-targets`)",
-                    "default": true
+                "rust-analyzer.cargo-check.command": {
+                    "type": "string",
+                    "description": "`cargo-check` command. (e.g: `clippy` will run as `cargo clippy` )",
+                    "default": "check"
                 },
                 "rust-analyzer.trace.server": {
                     "type": "string",
index e131f09df69e89136d3c63936d903b8ac6a8a6f1..96532e2c9d541abb70fccf1f0e6d35f3c6ac79da 100644 (file)
@@ -4,16 +4,10 @@ import { Server } from './server';
 
 const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
 
-export type CargoWatchStartupOptions = 'ask' | 'enabled' | 'disabled';
-export type CargoWatchTraceOptions = 'off' | 'error' | 'verbose';
-
-export interface CargoWatchOptions {
-    enableOnStartup: CargoWatchStartupOptions;
-    arguments: string;
-    command: string;
-    trace: CargoWatchTraceOptions;
-    ignore: string[];
-    allTargets: boolean;
+export interface CargoCheckOptions {
+    enabled: boolean;
+    arguments: string[];
+    command: null | string;
 }
 
 export interface CargoFeatures {
@@ -35,13 +29,10 @@ export class Config {
     public featureFlags = {};
     // for internal use
     public withSysroot: null | boolean = null;
-    public cargoWatchOptions: CargoWatchOptions = {
-        enableOnStartup: 'ask',
-        trace: 'off',
-        arguments: '',
-        command: '',
-        ignore: [],
-        allTargets: true,
+    public cargoCheckOptions: CargoCheckOptions = {
+        enabled: true,
+        arguments: [],
+        command: null,
     };
     public cargoFeatures: CargoFeatures = {
         noDefaultFeatures: false,
@@ -100,47 +91,27 @@ export class Config {
                 RA_LSP_DEBUG || (config.get('raLspServerPath') as string);
         }
 
-        if (config.has('enableCargoWatchOnStartup')) {
-            this.cargoWatchOptions.enableOnStartup = config.get<
-                CargoWatchStartupOptions
-            >('enableCargoWatchOnStartup', 'ask');
-        }
-
-        if (config.has('trace.cargo-watch')) {
-            this.cargoWatchOptions.trace = config.get<CargoWatchTraceOptions>(
-                'trace.cargo-watch',
-                'off',
+        if (config.has('enableCargoCheck')) {
+            this.cargoCheckOptions.enabled = config.get<boolean>(
+                'enableCargoCheck',
+                true,
             );
         }
 
         if (config.has('cargo-watch.arguments')) {
-            this.cargoWatchOptions.arguments = config.get<string>(
+            this.cargoCheckOptions.arguments = config.get<string[]>(
                 'cargo-watch.arguments',
-                '',
+                [],
             );
         }
 
         if (config.has('cargo-watch.command')) {
-            this.cargoWatchOptions.command = config.get<string>(
+            this.cargoCheckOptions.command = config.get<string>(
                 'cargo-watch.command',
                 '',
             );
         }
 
-        if (config.has('cargo-watch.ignore')) {
-            this.cargoWatchOptions.ignore = config.get<string[]>(
-                'cargo-watch.ignore',
-                [],
-            );
-        }
-
-        if (config.has('cargo-watch.allTargets')) {
-            this.cargoWatchOptions.allTargets = config.get<boolean>(
-                'cargo-watch.allTargets',
-                true,
-            );
-        }
-
         if (config.has('lruCapacity')) {
             this.lruCapacity = config.get('lruCapacity') as number;
         }
index 5ace1d0faeb4c2b794e59276cc249748f84c6e43..409d3b4b7382049b9cfeab999bbbc082cbd7f9a0 100644 (file)
@@ -55,6 +55,9 @@ export class Server {
                 publishDecorations: true,
                 lruCapacity: Server.config.lruCapacity,
                 maxInlayHintLength: Server.config.maxInlayHintLength,
+                cargoCheckEnable: Server.config.cargoCheckOptions.enabled,
+                cargoCheckCommand: Server.config.cargoCheckOptions.command,
+                cargoCheckArgs: Server.config.cargoCheckOptions.arguments,
                 excludeGlobs: Server.config.excludeGlobs,
                 useClientWatching: Server.config.useClientWatching,
                 featureFlags: Server.config.featureFlags,