Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
42 changes: 4 additions & 38 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,14 +31,15 @@ use serde::{de::DeserializeOwned, Serialize};
use threadpool::ThreadPool;

use crate::{
config::get_config,
diagnostics::DiagnosticTask,
feature_flags::FeatureFlags,
main_loop::{
pending_requests::{PendingRequest, PendingRequests},
subscriptions::Subscriptions,
},
req,
world::{Config, WorldSnapshot, WorldState},
world::{WorldSnapshot, WorldState},
Result, ServerConfig,
};
use req::ConfigurationParams;
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 19 additions & 7 deletions crates/rust-analyzer/src/main_loop/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
18 changes: 3 additions & 15 deletions crates/rust-analyzer/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ 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};
use relative_path::RelativePathBuf;
use stdx::format_to;

use crate::{
config::Config,
diagnostics::{CheckFixes, DiagnosticCollection},
feature_flags::FeatureFlags,
main_loop::pending_requests::{CompletedRequest, LatestRequests},
Expand Down Expand Up @@ -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
Expand Down