Skip to content

Commit

Permalink
Apply requested changes round 3
Browse files Browse the repository at this point in the history
  • Loading branch information
alibektas committed Apr 29, 2024
1 parent 09c9963 commit 861713e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 31 deletions.
6 changes: 4 additions & 2 deletions crates/rust-analyzer/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,10 @@ fn run_server() -> anyhow::Result<()> {
if let Some(json) = initialization_options {
let mut change = ConfigChange::default();
change.change_client_config(json);
let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);

let error_sink: ConfigError;
(config, error_sink, _) = config.apply_change(change);

if !error_sink.is_empty() {
use lsp_types::{
notification::{Notification, ShowMessage},
Expand Down
8 changes: 5 additions & 3 deletions crates/rust-analyzer/src/cli/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tracing::error;

use crate::{
cli::flags,
config::{ConfigChange, ConfigError},
config::ConfigChange,
line_index::{LineEndings, LineIndex, PositionEncoding},
};

Expand Down Expand Up @@ -45,8 +45,10 @@ impl flags::Scip {
let json = serde_json::from_reader(&mut file)?;
let mut change = ConfigChange::default();
change.change_client_config(json);
let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);

let error_sink;
(config, error_sink, _) = config.apply_change(change);

// FIXME @alibektas : What happens to errors without logging?
error!(?error_sink, "Config Error(s)");
}
Expand Down
29 changes: 13 additions & 16 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ impl Config {
// 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.
pub fn apply_change(
fn apply_change_with_sink(
&self,
change: ConfigChange,
error_sink: &mut ConfigError,
Expand Down Expand Up @@ -805,10 +805,13 @@ impl Config {
(config, should_update)
}

pub fn apply_change_whatever(&self, change: ConfigChange) -> (Config, ConfigError) {
/// Given `change` this generates a new `Config`, thereby collecting errors of type `ConfigError`.
/// If there are changes that have global/client level effect, the last component of the return type
/// will be set to `true`, which should be used by the `GlobalState` to update itself.
pub fn apply_change(&self, change: ConfigChange) -> (Config, ConfigError, bool) {
let mut e = ConfigError(vec![]);
let (config, _) = self.apply_change(change, &mut e);
(config, e)
let (config, should_update) = self.apply_change_with_sink(change, &mut e);
(config, e, should_update)
}
}

Expand Down Expand Up @@ -3293,8 +3296,7 @@ mod tests {
"server": null,
}}));

let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);
(config, _, _) = config.apply_change(change);
assert_eq!(config.proc_macro_srv(), None);
}

Expand All @@ -3313,8 +3315,7 @@ mod tests {
"server": project_root().display().to_string(),
}}));

let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);
(config, _, _) = config.apply_change(change);
assert_eq!(config.proc_macro_srv(), Some(AbsPathBuf::try_from(project_root()).unwrap()));
}

Expand All @@ -3335,8 +3336,7 @@ mod tests {
"server": "./server"
}}));

let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);
(config, _, _) = config.apply_change(change);

assert_eq!(
config.proc_macro_srv(),
Expand All @@ -3360,8 +3360,7 @@ mod tests {
"rust" : { "analyzerTargetDir" : null }
}));

let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);
(config, _, _) = config.apply_change(change);
assert_eq!(config.cargo_targetDir(), &None);
assert!(
matches!(config.flycheck(), FlycheckConfig::CargoCommand { options, .. } if options.target_dir.is_none())
Expand All @@ -3383,8 +3382,7 @@ mod tests {
"rust" : { "analyzerTargetDir" : true }
}));

let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);
(config, _, _) = config.apply_change(change);

assert_eq!(config.cargo_targetDir(), &Some(TargetDirectory::UseSubdirectory(true)));
assert!(
Expand All @@ -3407,8 +3405,7 @@ mod tests {
"rust" : { "analyzerTargetDir" : "other_folder" }
}));

let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);
(config, _, _) = config.apply_change(change);

assert_eq!(
config.cargo_targetDir(),
Expand Down
4 changes: 2 additions & 2 deletions crates/rust-analyzer/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ impl GlobalState {

change
};
let mut error_sink = ConfigError::default();
let (config, should_update) = self.config.apply_change(config_change, &mut error_sink);

let (config, _, should_update) = self.config.apply_change(config_change);

if should_update {
self.update_configuration(config);
Expand Down
7 changes: 4 additions & 3 deletions crates/rust-analyzer/src/handlers/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use triomphe::Arc;
use vfs::{AbsPathBuf, ChangeKind, VfsPath};

use crate::{
config::{Config, ConfigChange, ConfigError},
config::{Config, ConfigChange},
global_state::GlobalState,
lsp::{from_proto, utils::apply_document_changes},
lsp_ext::{self, RunFlycheckParams},
Expand Down Expand Up @@ -200,8 +200,9 @@ pub(crate) fn handle_did_change_configuration(
let mut config = Config::clone(&*this.config);
let mut change = ConfigChange::default();
change.change_client_config(json.take());
let mut error_sink = ConfigError::default();
(config, _) = config.apply_change(change, &mut error_sink);

(config, _, _) = config.apply_change(change);

// Client config changes neccesitates .update_config method to be called.
this.update_configuration(config);
}
Expand Down
7 changes: 4 additions & 3 deletions crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use triomphe::Arc;
use vfs::{AbsPath, AbsPathBuf, ChangeKind};

use crate::{
config::{Config, ConfigChange, ConfigError, FilesWatcher, LinkedProject},
config::{Config, ConfigChange, FilesWatcher, LinkedProject},
global_state::GlobalState,
lsp_ext,
main_loop::Task,
Expand Down Expand Up @@ -621,8 +621,9 @@ impl GlobalState {

let mut config_change = ConfigChange::default();
config_change.change_source_root_parent_map(self.local_roots_parent_map.clone());
let mut error_sink = ConfigError::default();
let (config, _) = self.config.apply_change(config_change, &mut error_sink);

let (config, _, _) = self.config.apply_change(config_change);

self.config = Arc::new(config);

self.recreate_crate_graph(cause);
Expand Down
6 changes: 4 additions & 2 deletions crates/rust-analyzer/tests/slow-tests/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,11 @@ impl Project<'_> {
let mut change = ConfigChange::default();

change.change_client_config(self.config);
let mut error_sink = ConfigError::default();

let error_sink: ConfigError;
(config, error_sink, _) = config.apply_change(change);
assert!(error_sink.is_empty(), "{error_sink:?}");
(config, _) = config.apply_change(change, &mut error_sink);

config.rediscover_workspaces();

Server::new(tmp_dir.keep(), config)
Expand Down

0 comments on commit 861713e

Please sign in to comment.