add workspace config and manual LSP root management

fixup documentation

Co-authored-by: LeoniePhiline <22329650+LeoniePhiline@users.noreply.github.com>

fixup typo

Co-authored-by: LeoniePhiline <22329650+LeoniePhiline@users.noreply.github.com>
This commit is contained in:
Pascal Kuthe 2023-01-31 00:31:21 +01:00 committed by Blaž Hrastnik
parent d59b80514e
commit 2d10a429eb
16 changed files with 296 additions and 183 deletions

View file

@ -9,9 +9,8 @@ pub fn default_lang_config() -> toml::Value {
/// User configured languages.toml file, merged with the default config.
pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
let config = crate::local_config_dirs()
let config = [crate::config_dir(), crate::find_workspace().join(".helix")]
.into_iter()
.chain([crate::config_dir()].into_iter())
.map(|path| path.join("languages.toml"))
.filter_map(|file| {
std::fs::read_to_string(file)
@ -20,8 +19,7 @@ pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.chain([default_lang_config()].into_iter())
.fold(toml::Value::Table(toml::value::Table::default()), |a, b| {
.fold(default_lang_config(), |a, b| {
// combines for example
// b:
// [[language]]
@ -38,7 +36,7 @@ pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
// language-server = { command = "/usr/bin/taplo" }
//
// thus it overrides the third depth-level of b with values of a if they exist, but otherwise merges their values
crate::merge_toml_values(b, a, 3)
crate::merge_toml_values(a, b, 3)
});
Ok(config)

View file

@ -42,7 +42,7 @@ fn prioritize_runtime_dirs() -> Vec<PathBuf> {
let mut rt_dirs = Vec::new();
if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") {
// this is the directory of the crate being run by cargo, we need the workspace path so we take the parent
let path = std::path::PathBuf::from(dir).parent().unwrap().join(RT_DIR);
let path = PathBuf::from(dir).parent().unwrap().join(RT_DIR);
log::debug!("runtime dir: {}", path.to_string_lossy());
rt_dirs.push(path);
}
@ -113,15 +113,6 @@ pub fn config_dir() -> PathBuf {
path
}
pub fn local_config_dirs() -> Vec<PathBuf> {
let directories = find_local_config_dirs()
.into_iter()
.map(|path| path.join(".helix"))
.collect();
log::debug!("Located configuration folders: {:?}", directories);
directories
}
pub fn cache_dir() -> PathBuf {
// TODO: allow env var override
let strategy = choose_base_strategy().expect("Unable to find the config directory!");
@ -137,6 +128,10 @@ pub fn config_file() -> PathBuf {
.unwrap_or_else(|| config_dir().join("config.toml"))
}
pub fn workspace_config_file() -> PathBuf {
find_workspace().join(".helix").join("config.toml")
}
pub fn lang_config_file() -> PathBuf {
config_dir().join("languages.toml")
}
@ -145,22 +140,6 @@ pub fn log_file() -> PathBuf {
cache_dir().join("helix.log")
}
pub fn find_local_config_dirs() -> Vec<PathBuf> {
let current_dir = std::env::current_dir().expect("unable to determine current directory");
let mut directories = Vec::new();
for ancestor in current_dir.ancestors() {
if ancestor.join(".git").exists() {
directories.push(ancestor.to_path_buf());
// Don't go higher than repo if we're in one
break;
} else if ancestor.join(".helix").is_dir() {
directories.push(ancestor.to_path_buf());
}
}
directories
}
/// Merge two TOML documents, merging values from `right` onto `left`
///
/// When an array exists in both `left` and `right`, `right`'s array is
@ -302,3 +281,16 @@ mod merge_toml_tests {
)
}
}
/// Finds the current workspace folder.
/// Used as a ceiling dir for root resolve, for the filepicker and other related
pub fn find_workspace() -> PathBuf {
let current_dir = std::env::current_dir().expect("unable to determine current directory");
for ancestor in current_dir.ancestors() {
if ancestor.join(".git").exists() || ancestor.join(".helix").exists() {
return ancestor.to_owned();
}
}
current_dir
}