Skip to content

Commit

Permalink
Fix processing of ratoml files
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jun 7, 2024
1 parent 3178e5f commit 4c2b832
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 189 deletions.
11 changes: 5 additions & 6 deletions crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,25 +272,24 @@ impl SourceRootConfig {
/// If a `SourceRoot` doesn't have a parent and is local then it is not contained in this mapping but it can be asserted that it is a root `SourceRoot`.
pub fn source_root_parent_map(&self) -> FxHashMap<SourceRootId, SourceRootId> {
let roots = self.fsc.roots();
let mut map = FxHashMap::<SourceRootId, SourceRootId>::default();
let mut i = 0;
roots
.iter()
.enumerate()
.filter(|(_, (_, id))| self.local_filesets.contains(id))
.filter_map(|(idx, (root, root_id))| {
// We are interested in parents if they are also local source roots.
// So instead of a non-local parent we may take a local ancestor as a parent to a node.
roots.iter().take(idx).find_map(|(root2, root2_id)| {
roots[..idx].iter().find_map(|(root2, root2_id)| {
i += 1;
if self.local_filesets.contains(root2_id) && root.starts_with(root2) {
return Some((root_id, root2_id));
}
None
})
})
.for_each(|(child, parent)| {
map.insert(SourceRootId(*child as u32), SourceRootId(*parent as u32));
});
map
.map(|(&child, &parent)| (SourceRootId(child as u32), SourceRootId(parent as u32)))
.collect()
}
}

Expand Down
11 changes: 9 additions & 2 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,13 @@ impl Config {
&self.user_config_path
}

pub fn same_source_root_parent_map(
&self,
other: &Arc<FxHashMap<SourceRootId, SourceRootId>>,
) -> bool {
Arc::ptr_eq(&self.source_root_parent_map, other)
}

// FIXME @alibektas : Server's health uses error sink but in other places it is not used atm.
/// Changes made to client and global configurations will partially not be reflected even after `.apply_change()` was called.
/// The return tuple's bool component signals whether the `GlobalState` should call its `update_configuration()` method.
Expand Down Expand Up @@ -927,7 +934,7 @@ impl ConfigChange {
}

pub fn change_root_ratoml(&mut self, content: Option<Arc<str>>) {
assert!(self.user_config_change.is_none()); // Otherwise it is a double write.
assert!(self.root_ratoml_change.is_none()); // Otherwise it is a double write.
self.root_ratoml_change = content;
}

Expand Down Expand Up @@ -1204,10 +1211,10 @@ impl Config {
workspace_roots,
visual_studio_code_version,
client_config: (FullConfigInput::default(), ConfigErrors(vec![])),
user_config: None,
ratoml_files: FxHashMap::default(),
default_config: DEFAULT_CONFIG_DATA.get_or_init(|| Box::leak(Box::default())),
source_root_parent_map: Arc::new(FxHashMap::default()),
user_config: None,
user_config_path,
root_ratoml: None,
root_ratoml_path,
Expand Down
6 changes: 4 additions & 2 deletions crates/rust-analyzer/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ impl GlobalState {

let _p = span!(Level::INFO, "GlobalState::process_changes/apply_change").entered();
self.analysis_host.apply_change(change);
if !modified_ratoml_files.is_empty() {
if !modified_ratoml_files.is_empty()
|| !self.config.same_source_root_parent_map(&self.local_roots_parent_map)
{
let config_change = {
let user_config_path = self.config.user_config_path();
let root_ratoml_path = self.config.root_ratoml_path();
Expand Down Expand Up @@ -386,7 +388,7 @@ impl GlobalState {
span!(Level::ERROR, "Mapping to SourceRootId failed.");
}
}

change.change_source_root_parent_map(self.local_roots_parent_map.clone());
change
};

Expand Down
25 changes: 25 additions & 0 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use crate::{
hack_recover_crate_name,
line_index::LineEndings,
lsp::{
ext::InternalTestingFetchConfigParams,
from_proto, to_proto,
utils::{all_edits_are_disjoint, invalid_params_error},
LspError,
Expand Down Expand Up @@ -2231,6 +2232,30 @@ pub(crate) fn fetch_dependency_list(
Ok(FetchDependencyListResult { crates: crate_infos })
}

pub(crate) fn internal_testing_fetch_config(
state: GlobalStateSnapshot,
params: InternalTestingFetchConfigParams,
) -> anyhow::Result<serde_json::Value> {
let source_root = params
.text_document
.map(|it| {
state
.analysis
.source_root_id(from_proto::file_id(&state, &it.uri)?)
.map_err(anyhow::Error::from)
})
.transpose()?;
serde_json::to_value(match &*params.config {
"local" => state.config.assist(source_root).assist_emit_must_use,
"global" => matches!(
state.config.rustfmt(),
RustfmtConfig::Rustfmt { enable_range_formatting: true, .. }
),
_ => return Err(anyhow::anyhow!("Unknown test config key: {}", params.config)),
})
.map_err(Into::into)
}

/// Searches for the directory of a Rust crate given this crate's root file path.
///
/// # Arguments
Expand Down
14 changes: 14 additions & 0 deletions crates/rust-analyzer/src/lsp/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ use serde::{Deserialize, Serialize};

use crate::line_index::PositionEncoding;

pub enum InternalTestingFetchConfig {}

impl Request for InternalTestingFetchConfig {
type Params = InternalTestingFetchConfigParams;
type Result = serde_json::Value;
const METHOD: &'static str = "rust-analyzer-internal/internalTestingFetchConfig";
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct InternalTestingFetchConfigParams {
pub text_document: Option<TextDocumentIdentifier>,
pub config: String,
}
pub enum AnalyzerStatus {}

impl Request for AnalyzerStatus {
Expand Down
3 changes: 3 additions & 0 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ impl GlobalState {

fn update_diagnostics(&mut self) {
let db = self.analysis_host.raw_database();
// spawn a task per subscription?
let subscriptions = {
let vfs = &self.vfs.read().0;
self.mem_docs
Expand Down Expand Up @@ -986,6 +987,8 @@ impl GlobalState {
.on::<NO_RETRY, lsp_ext::ExternalDocs>(handlers::handle_open_docs)
.on::<NO_RETRY, lsp_ext::OpenCargoToml>(handlers::handle_open_cargo_toml)
.on::<NO_RETRY, lsp_ext::MoveItem>(handlers::handle_move_item)
//
.on::<NO_RETRY, lsp_ext::InternalTestingFetchConfig>(handlers::internal_testing_fetch_config)
.finish();
}

Expand Down
10 changes: 4 additions & 6 deletions crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,11 @@ impl GlobalState {
// When they're not, integrate the base to make them into absolute patterns
filter
.flat_map(|root| {
root.include.into_iter().flat_map(|it| {
root.include.into_iter().flat_map(|base| {
[
format!("{it}/**/*.rs"),
// FIXME @alibektas : Following dbarsky's recomm I merged toml and lock patterns into one.
// Is this correct?
format!("{it}/**/Cargo.{{toml,lock}}"),
format!("{it}/**/rust-analyzer.toml"),
format!("{base}/**/*.rs"),
format!("{base}/**/Cargo.{{toml,lock}}"),
format!("{base}/**/rust-analyzer.toml"),
]
})
})
Expand Down
Loading

0 comments on commit 4c2b832

Please sign in to comment.