3807: Generalize rustfmt config r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-04-01 12:34:31 +00:00 committed by GitHub
commit facdf56cf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 60 deletions

View file

@ -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<FlycheckConfig>,
pub vscode_lldb: bool,
pub proc_macro_srv: Option<String>,
}
#[derive(Debug, Clone)]
pub enum RustfmtConfig {
Rustfmt {
extra_args: Vec<String>,
},
#[allow(unused)]
CustomCommand {
command: String,
args: Vec<String>,
},
}
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)]

View file

@ -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, 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_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<PathBuf>,
client_caps: ClientCapabilities,

View file

@ -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,
@ -610,13 +611,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() {

View file

@ -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,18 +51,6 @@ fn create_flycheck(workspaces: &[ProjectWorkspace], config: &Config) -> Option<F
})
}
#[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_args: Vec<String>,
pub check: Option<FlycheckConfig>,
pub vscode_lldb: bool,
pub proc_macro_srv: Option<String>,
}
/// `WorldState` is the primary mutable state of the language server
///
/// The most interesting components are `vfs`, which stores a consistent