From 6ac966899853f03ab572ccd2cee8bf5b2a66aaea Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Apr 2020 13:31:12 +0200 Subject: Generalize rustfmt config --- crates/rust-analyzer/src/main_loop.rs | 4 ++-- crates/rust-analyzer/src/main_loop/handlers.rs | 27 ++++++++++++++++++-------- crates/rust-analyzer/src/world.rs | 20 ++++++++++++++++++- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index a89ea86ea..753dc7d50 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -38,7 +38,7 @@ use crate::{ subscriptions::Subscriptions, }, req, - world::{Config, WorldSnapshot, WorldState}, + world::{Config, RustfmtConfig, WorldSnapshot, WorldState}, Result, ServerConfig, }; use req::ConfigurationParams; @@ -110,7 +110,7 @@ fn get_config( } else { None }, - rustfmt_args: config.rustfmt_args.clone(), + rustfmt: RustfmtConfig::Rustfmt { extra_args: config.rustfmt_args.clone() }, vscode_lldb: config.vscode_lldb, proc_macro_srv: None, // FIXME: get this from config } diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index d5cb5d137..80d96f89e 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -39,7 +39,7 @@ use crate::{ from_json, req::{self, Decoration, InlayHint, InlayHintsParams}, semantic_tokens::SemanticTokensBuilder, - world::WorldSnapshot, + world::{RustfmtConfig, WorldSnapshot}, LspError, Result, }; @@ -610,13 +610,24 @@ pub fn handle_formatting( let file_line_index = world.analysis().file_line_index(file_id)?; let end_position = TextUnit::of_str(&file).conv_with(&file_line_index); - let mut rustfmt = process::Command::new("rustfmt"); - rustfmt.args(&world.config.rustfmt_args); - if let Some(&crate_id) = crate_ids.first() { - // Assume all crates are in the same edition - let edition = world.analysis().crate_edition(crate_id)?; - rustfmt.args(&["--edition", &edition.to_string()]); - } + let mut rustfmt = match &world.config.rustfmt { + RustfmtConfig::Rustfmt { extra_args } => { + let mut cmd = process::Command::new("rustfmt"); + cmd.args(extra_args); + if let Some(&crate_id) = crate_ids.first() { + // Assume all crates are in the same edition + let edition = world.analysis().crate_edition(crate_id)?; + cmd.arg("--edition"); + cmd.arg(edition.to_string()); + } + cmd + } + RustfmtConfig::CustomCommand { command, args } => { + let mut cmd = process::Command::new(command); + cmd.args(args); + cmd + } + }; if let Ok(path) = params.text_document.uri.to_file_path() { if let Some(parent) = path.parent() { diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index 2db058eb1..ccdf3710c 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -57,12 +57,30 @@ pub struct Config { pub supports_location_link: bool, pub line_folding_only: bool, pub inlay_hints: InlayHintsConfig, - pub rustfmt_args: Vec, + pub rustfmt: RustfmtConfig, pub check: Option, pub vscode_lldb: bool, pub proc_macro_srv: Option, } +#[derive(Debug, Clone)] +pub enum RustfmtConfig { + Rustfmt { + extra_args: Vec, + }, + #[allow(unused)] + CustomCommand { + command: String, + args: Vec, + }, +} + +impl Default for RustfmtConfig { + fn default() -> Self { + RustfmtConfig::Rustfmt { extra_args: Vec::new() } + } +} + /// `WorldState` is the primary mutable state of the language server /// /// The most interesting components are `vfs`, which stores a consistent -- cgit v1.2.3 From b9bf29019d1dd733d45bae8aed986ecd330565bc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Apr 2020 14:32:04 +0200 Subject: Move config to config.rs --- crates/rust-analyzer/src/config.rs | 68 ++++++++++++++++++++++++++ crates/rust-analyzer/src/main_loop.rs | 42 ++-------------- crates/rust-analyzer/src/main_loop/handlers.rs | 3 +- crates/rust-analyzer/src/world.rs | 36 ++------------ 4 files changed, 77 insertions(+), 72 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 628ed107e..fb7895ce0 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -9,9 +9,77 @@ use rustc_hash::FxHashMap; +use lsp_types::TextDocumentClientCapabilities; +use ra_flycheck::FlycheckConfig; +use ra_ide::InlayHintsConfig; use ra_project_model::CargoFeatures; use serde::{Deserialize, Deserializer}; +#[derive(Debug, Clone)] +pub struct Config { + pub publish_decorations: bool, + pub supports_location_link: bool, + pub line_folding_only: bool, + pub inlay_hints: InlayHintsConfig, + pub rustfmt: RustfmtConfig, + pub check: Option, + pub vscode_lldb: bool, + pub proc_macro_srv: Option, +} + +#[derive(Debug, Clone)] +pub enum RustfmtConfig { + Rustfmt { + extra_args: Vec, + }, + #[allow(unused)] + CustomCommand { + command: String, + args: Vec, + }, +} + +impl Default for RustfmtConfig { + fn default() -> Self { + RustfmtConfig::Rustfmt { extra_args: Vec::new() } + } +} + +pub(crate) fn get_config( + config: &ServerConfig, + text_document_caps: Option<&TextDocumentClientCapabilities>, +) -> Config { + Config { + publish_decorations: config.publish_decorations, + supports_location_link: text_document_caps + .and_then(|it| it.definition) + .and_then(|it| it.link_support) + .unwrap_or(false), + line_folding_only: text_document_caps + .and_then(|it| it.folding_range.as_ref()) + .and_then(|it| it.line_folding_only) + .unwrap_or(false), + inlay_hints: InlayHintsConfig { + type_hints: config.inlay_hints_type, + parameter_hints: config.inlay_hints_parameter, + chaining_hints: config.inlay_hints_chaining, + max_length: config.inlay_hints_max_length, + }, + check: if config.cargo_watch_enable { + Some(FlycheckConfig::CargoCommand { + command: config.cargo_watch_command.clone(), + all_targets: config.cargo_watch_all_targets, + extra_args: config.cargo_watch_args.clone(), + }) + } else { + None + }, + rustfmt: RustfmtConfig::Rustfmt { extra_args: config.rustfmt_args.clone() }, + vscode_lldb: config.vscode_lldb, + proc_macro_srv: None, // FIXME: get this from config + } +} + /// Client provided initialization options #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase", default)] diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 753dc7d50..de40e2ac2 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -21,8 +21,8 @@ use lsp_types::{ WorkDoneProgressBegin, WorkDoneProgressCreateParams, WorkDoneProgressEnd, WorkDoneProgressReport, }; -use ra_flycheck::{url_from_path_with_drive_lowercasing, CheckTask, FlycheckConfig}; -use ra_ide::{Canceled, FileId, InlayHintsConfig, LibraryData, SourceRootId}; +use ra_flycheck::{url_from_path_with_drive_lowercasing, CheckTask}; +use ra_ide::{Canceled, FileId, LibraryData, SourceRootId}; use ra_prof::profile; use ra_vfs::{VfsFile, VfsTask, Watch}; use relative_path::RelativePathBuf; @@ -31,6 +31,7 @@ use serde::{de::DeserializeOwned, Serialize}; use threadpool::ThreadPool; use crate::{ + config::get_config, diagnostics::DiagnosticTask, feature_flags::FeatureFlags, main_loop::{ @@ -38,7 +39,7 @@ use crate::{ subscriptions::Subscriptions, }, req, - world::{Config, RustfmtConfig, WorldSnapshot, WorldState}, + world::{WorldSnapshot, WorldState}, Result, ServerConfig, }; use req::ConfigurationParams; @@ -81,41 +82,6 @@ fn get_feature_flags(config: &ServerConfig, connection: &Connection) -> FeatureF ff } -fn get_config( - config: &ServerConfig, - text_document_caps: Option<&TextDocumentClientCapabilities>, -) -> Config { - Config { - publish_decorations: config.publish_decorations, - supports_location_link: text_document_caps - .and_then(|it| it.definition) - .and_then(|it| it.link_support) - .unwrap_or(false), - line_folding_only: text_document_caps - .and_then(|it| it.folding_range.as_ref()) - .and_then(|it| it.line_folding_only) - .unwrap_or(false), - inlay_hints: InlayHintsConfig { - type_hints: config.inlay_hints_type, - parameter_hints: config.inlay_hints_parameter, - chaining_hints: config.inlay_hints_chaining, - max_length: config.inlay_hints_max_length, - }, - check: if config.cargo_watch_enable { - Some(FlycheckConfig::CargoCommand { - command: config.cargo_watch_command.clone(), - all_targets: config.cargo_watch_all_targets, - extra_args: config.cargo_watch_args.clone(), - }) - } else { - None - }, - rustfmt: RustfmtConfig::Rustfmt { extra_args: config.rustfmt_args.clone() }, - vscode_lldb: config.vscode_lldb, - proc_macro_srv: None, // FIXME: get this from config - } -} - pub fn main_loop( ws_roots: Vec, client_caps: ClientCapabilities, diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 80d96f89e..bb99b38a8 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -31,6 +31,7 @@ use stdx::format_to; use crate::{ cargo_target_spec::CargoTargetSpec, + config::RustfmtConfig, conv::{ to_call_hierarchy_item, to_location, Conv, ConvWith, FoldConvCtx, MapConvWith, TryConvWith, TryConvWithToVec, @@ -39,7 +40,7 @@ use crate::{ from_json, req::{self, Decoration, InlayHint, InlayHintsParams}, semantic_tokens::SemanticTokensBuilder, - world::{RustfmtConfig, WorldSnapshot}, + world::WorldSnapshot, LspError, Result, }; diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index ccdf3710c..124de4d8e 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -11,10 +11,9 @@ use std::{ use crossbeam_channel::{unbounded, Receiver}; use lsp_types::Url; use parking_lot::RwLock; -use ra_flycheck::{url_from_path_with_drive_lowercasing, Flycheck, FlycheckConfig}; +use ra_flycheck::{url_from_path_with_drive_lowercasing, Flycheck}; use ra_ide::{ - Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, InlayHintsConfig, LibraryData, - SourceRootId, + Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, }; use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace}; use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; @@ -22,6 +21,7 @@ use relative_path::RelativePathBuf; use stdx::format_to; use crate::{ + config::Config, diagnostics::{CheckFixes, DiagnosticCollection}, feature_flags::FeatureFlags, main_loop::pending_requests::{CompletedRequest, LatestRequests}, @@ -51,36 +51,6 @@ fn create_flycheck(workspaces: &[ProjectWorkspace], config: &Config) -> Option, - pub vscode_lldb: bool, - pub proc_macro_srv: Option, -} - -#[derive(Debug, Clone)] -pub enum RustfmtConfig { - Rustfmt { - extra_args: Vec, - }, - #[allow(unused)] - CustomCommand { - command: String, - args: Vec, - }, -} - -impl Default for RustfmtConfig { - fn default() -> Self { - RustfmtConfig::Rustfmt { extra_args: Vec::new() } - } -} - /// `WorldState` is the primary mutable state of the language server /// /// The most interesting components are `vfs`, which stores a consistent -- cgit v1.2.3