Allow user config to not exist

This commit is contained in:
Lukas Wirth 2024-08-19 16:00:06 +02:00
parent fd3fce2600
commit 90e08d3c93
5 changed files with 10 additions and 15 deletions

View file

@ -794,19 +794,17 @@ impl Config {
/// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config |
/// | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
/// | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
pub fn user_config_path() -> &'static AbsPath {
static USER_CONFIG_PATH: LazyLock<AbsPathBuf> = LazyLock::new(|| {
pub fn user_config_path() -> Option<&'static AbsPath> {
static USER_CONFIG_PATH: LazyLock<Option<AbsPathBuf>> = LazyLock::new(|| {
let user_config_path = if let Some(path) = env::var_os("__TEST_RA_USER_CONFIG_DIR") {
std::path::PathBuf::from(path)
} else {
dirs::config_dir()
.expect("A config dir is expected to existed on all platforms ra supports.")
.join("rust-analyzer")
dirs::config_dir()?.join("rust-analyzer")
}
.join("rust-analyzer.toml");
AbsPathBuf::assert_utf8(user_config_path)
Some(AbsPathBuf::assert_utf8(user_config_path))
});
&USER_CONFIG_PATH
USER_CONFIG_PATH.as_deref()
}
pub fn same_source_root_parent_map(

View file

@ -399,7 +399,7 @@ impl GlobalState {
.collect_vec();
for (file_id, (_change_kind, vfs_path)) in modified_ratoml_files {
if vfs_path == *user_config_path {
if vfs_path.as_path() == user_config_path {
change.change_user_config(Some(db.file_text(file_id)));
continue;
}

View file

@ -551,11 +551,8 @@ impl GlobalState {
watchers.extend(
iter::once(Config::user_config_path())
.chain(
self.workspaces
.iter()
.filter_map(|ws| ws.manifest().map(ManifestPath::as_ref)),
)
.chain(self.workspaces.iter().map(|ws| ws.manifest().map(ManifestPath::as_ref)))
.flatten()
.map(|glob_pattern| lsp_types::FileSystemWatcher {
glob_pattern: lsp_types::GlobPattern::String(glob_pattern.to_string()),
kind: None,

View file

@ -77,7 +77,7 @@ impl RatomlTest {
let mut spl = spl.into_iter();
if let Some(first) = spl.next() {
if first == "$$CONFIG_DIR$$" {
path = Config::user_config_path().to_path_buf().into();
path = Config::user_config_path().unwrap().to_path_buf().into();
} else {
path = path.join(first);
}

View file

@ -123,7 +123,7 @@ impl Project<'_> {
if config_dir_guard.is_none() {
config_dir_guard = Some(CONFIG_DIR_LOCK.lock());
}
let path = Config::user_config_path().join(&pth['/'.len_utf8()..]);
let path = Config::user_config_path().unwrap().join(&pth['/'.len_utf8()..]);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path.as_path(), entry.text.as_bytes()).unwrap();
} else {