Add support for local language configuration (#1249)

* add local configuration

* move config loading to Application::new

* simplify find_root_impl
pull/2156/head
Kirawi 3 years ago committed by GitHub
parent be656c14e3
commit c2a40d9d52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

1
Cargo.lock generated

@ -404,6 +404,7 @@ dependencies = [
"cc", "cc",
"etcetera", "etcetera",
"libloading", "libloading",
"log",
"once_cell", "once_cell",
"serde", "serde",
"threadpool", "threadpool",

@ -2,6 +2,8 @@
Language-specific settings and settings for particular language servers can be configured in a `languages.toml` file placed in your [configuration directory](./configuration.md). Helix actually uses two `languages.toml` files, the [first one](https://github.com/helix-editor/helix/blob/master/languages.toml) is in the main helix repository; it contains the default settings for each language and is included in the helix binary at compile time. Users who want to see the available settings and options can either reference the helix repo's `languages.toml` file, or consult the table in the [adding languages](./guides/adding_languages.md) section. Language-specific settings and settings for particular language servers can be configured in a `languages.toml` file placed in your [configuration directory](./configuration.md). Helix actually uses two `languages.toml` files, the [first one](https://github.com/helix-editor/helix/blob/master/languages.toml) is in the main helix repository; it contains the default settings for each language and is included in the helix binary at compile time. Users who want to see the available settings and options can either reference the helix repo's `languages.toml` file, or consult the table in the [adding languages](./guides/adding_languages.md) section.
A local `languages.toml` can be created within a `.helix` directory. Its settings will be merged with both the global and default configs.
Changes made to the `languages.toml` file in a user's [configuration directory](./configuration.md) are merged with helix's defaults on start-up, such that a user's settings will take precedence over defaults in the event of a collision. For example, the default `languages.toml` sets rust's `auto-format` to `true`. If a user wants to disable auto-format, they can change the `languages.toml` in their [configuration directory](./configuration.md) to make the rust entry read like the example below; the new key/value pair `auto-format = false` will override the default when the two sets of settings are merged on start-up: Changes made to the `languages.toml` file in a user's [configuration directory](./configuration.md) are merged with helix's defaults on start-up, such that a user's settings will take precedence over defaults in the event of a collision. For example, the default `languages.toml` sets rust's `auto-format` to `true`. If a user wants to disable auto-format, they can change the `languages.toml` in their [configuration directory](./configuration.md) to make the rust entry read like the example below; the new key/value pair `auto-format = false` will override the default when the two sets of settings are merged on start-up:
```toml ```toml

@ -1,10 +1,10 @@
/// Syntax configuration loader based on built-in languages.toml. /// Syntax configuration loader based on built-in languages.toml.
pub fn default_syntax_loader() -> crate::syntax::Configuration { pub fn default_syntax_loader() -> crate::syntax::Configuration {
helix_loader::default_lang_config() helix_loader::config::default_lang_config()
.try_into() .try_into()
.expect("Could not serialize built-in languages.toml") .expect("Could not serialize built-in languages.toml")
} }
/// Syntax configuration loader based on user configured languages.toml. /// Syntax configuration loader based on user configured languages.toml.
pub fn user_syntax_loader() -> Result<crate::syntax::Configuration, toml::de::Error> { pub fn user_syntax_loader() -> Result<crate::syntax::Configuration, toml::de::Error> {
helix_loader::user_lang_config()?.try_into() helix_loader::config::user_lang_config()?.try_into()
} }

@ -46,41 +46,9 @@ pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> {
/// * Top-most folder containing a root marker if not git repository detected /// * Top-most folder containing a root marker if not git repository detected
/// * Current working directory as fallback /// * Current working directory as fallback
pub fn find_root(root: Option<&str>, root_markers: &[String]) -> Option<std::path::PathBuf> { pub fn find_root(root: Option<&str>, root_markers: &[String]) -> Option<std::path::PathBuf> {
let current_dir = std::env::current_dir().expect("unable to determine current directory"); helix_loader::find_root_impl(root, root_markers)
.first()
let root = match root { .cloned()
Some(root) => {
let root = std::path::Path::new(root);
if root.is_absolute() {
root.to_path_buf()
} else {
current_dir.join(root)
}
}
None => current_dir.clone(),
};
let mut top_marker = None;
for ancestor in root.ancestors() {
for marker in root_markers {
if ancestor.join(marker).exists() {
top_marker = Some(ancestor);
break;
}
}
// don't go higher than repo
if ancestor.join(".git").is_dir() {
// Use workspace if detected from marker
return Some(top_marker.unwrap_or(ancestor).to_path_buf());
}
}
// In absence of git repo, use workspace if detected
if top_marker.is_some() {
top_marker.map(|a| a.to_path_buf())
} else {
Some(current_dir)
}
} }
pub use ropey::{Rope, RopeBuilder, RopeSlice}; pub use ropey::{Rope, RopeBuilder, RopeSlice};

@ -18,6 +18,8 @@ tree-sitter = "0.20"
libloading = "0.7" libloading = "0.7"
once_cell = "1.9" once_cell = "1.9"
log = "0.4"
# cloning/compiling tree-sitter grammars # cloning/compiling tree-sitter grammars
cc = { version = "1" } cc = { version = "1" }
threadpool = { version = "1.0" } threadpool = { version = "1.0" }

@ -0,0 +1,26 @@
/// Default bultin-in languages.toml.
pub fn default_lang_config() -> toml::Value {
toml::from_slice(include_bytes!("../../languages.toml"))
.expect("Could not parse bultin-in languages.toml to valid toml")
}
/// 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()
.into_iter()
.chain([crate::config_dir()].into_iter())
.map(|path| path.join("languages.toml"))
.filter_map(|file| {
std::fs::read(&file)
.map(|config| toml::from_slice(&config))
.ok()
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.chain([default_lang_config()].into_iter())
.fold(toml::Value::Table(toml::value::Table::default()), |a, b| {
crate::merge_toml_values(b, a)
});
Ok(config)
}

@ -92,7 +92,7 @@ pub fn build_grammars() -> Result<()> {
// merged. The `grammar_selection` key of the config is then used to filter // merged. The `grammar_selection` key of the config is then used to filter
// down all grammars into a subset of the user's choosing. // down all grammars into a subset of the user's choosing.
fn get_grammar_configs() -> Result<Vec<GrammarConfiguration>> { fn get_grammar_configs() -> Result<Vec<GrammarConfiguration>> {
let config: Configuration = crate::user_lang_config() let config: Configuration = crate::config::user_lang_config()
.context("Could not parse languages.toml")? .context("Could not parse languages.toml")?
.try_into()?; .try_into()?;

@ -1,3 +1,4 @@
pub mod config;
pub mod grammar; pub mod grammar;
use etcetera::base_strategy::{choose_base_strategy, BaseStrategy}; use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
@ -36,6 +37,15 @@ pub fn config_dir() -> std::path::PathBuf {
path path
} }
pub fn local_config_dirs() -> Vec<std::path::PathBuf> {
let directories = find_root_impl(None, &[".helix".to_string()])
.into_iter()
.map(|path| path.join(".helix"))
.collect();
log::debug!("Located configuration folders: {:?}", directories);
directories
}
pub fn cache_dir() -> std::path::PathBuf { pub fn cache_dir() -> std::path::PathBuf {
// TODO: allow env var override // TODO: allow env var override
let strategy = choose_base_strategy().expect("Unable to find the config directory!"); let strategy = choose_base_strategy().expect("Unable to find the config directory!");
@ -56,25 +66,36 @@ pub fn log_file() -> std::path::PathBuf {
cache_dir().join("helix.log") cache_dir().join("helix.log")
} }
/// Default bultin-in languages.toml. pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<std::path::PathBuf> {
pub fn default_lang_config() -> toml::Value { let current_dir = std::env::current_dir().expect("unable to determine current directory");
toml::from_slice(include_bytes!("../../languages.toml")) let mut directories = Vec::new();
.expect("Could not parse bultin-in languages.toml to valid toml")
} let root = match root {
Some(root) => {
/// User configured languages.toml file, merged with the default config. let root = std::path::Path::new(root);
pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> { if root.is_absolute() {
let def_lang_conf = default_lang_config(); root.to_path_buf()
let data = std::fs::read(crate::config_dir().join("languages.toml")); } else {
let user_lang_conf = match data { current_dir.join(root)
Ok(raw) => {
let value = toml::from_slice(&raw)?;
merge_toml_values(def_lang_conf, value)
} }
Err(_) => def_lang_conf, }
None => current_dir,
}; };
Ok(user_lang_conf) for ancestor in root.ancestors() {
// don't go higher than repo
if ancestor.join(".git").is_dir() {
// Use workspace if detected from marker
directories.push(ancestor.to_path_buf());
break;
} else if root_markers
.iter()
.any(|marker| ancestor.join(marker).exists())
{
directories.push(ancestor.to_path_buf());
}
}
directories
} }
// right overrides left // right overrides left

@ -56,15 +56,33 @@ pub struct Application {
} }
impl Application { impl Application {
pub fn new(args: Args, config: Config) -> Result<Self, Error> { pub fn new(args: Args) -> Result<Self, Error> {
use helix_view::editor::Action; use helix_view::editor::Action;
let mut compositor = Compositor::new()?;
let size = compositor.size();
let conf_dir = helix_loader::config_dir(); let config_dir = helix_loader::config_dir();
if !config_dir.exists() {
std::fs::create_dir_all(&config_dir).ok();
}
let theme_loader = let config = match std::fs::read_to_string(config_dir.join("config.toml")) {
std::sync::Arc::new(theme::Loader::new(&conf_dir, &helix_loader::runtime_dir())); Ok(config) => toml::from_str(&config)
.map(crate::keymap::merge_keys)
.unwrap_or_else(|err| {
eprintln!("Bad config: {}", err);
eprintln!("Press <ENTER> to continue with default config");
use std::io::Read;
// This waits for an enter press.
let _ = std::io::stdin().read(&mut []);
Config::default()
}),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
Err(err) => return Err(Error::new(err)),
};
let theme_loader = std::sync::Arc::new(theme::Loader::new(
&config_dir,
&helix_loader::runtime_dir(),
));
let true_color = config.editor.true_color || crate::true_color(); let true_color = config.editor.true_color || crate::true_color();
let theme = config let theme = config
@ -98,9 +116,10 @@ impl Application {
}); });
let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf)); let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
let mut compositor = Compositor::new()?;
let config = Arc::new(ArcSwap::from_pointee(config)); let config = Arc::new(ArcSwap::from_pointee(config));
let mut editor = Editor::new( let mut editor = Editor::new(
size, compositor.size(),
theme_loader.clone(), theme_loader.clone(),
syn_loader.clone(), syn_loader.clone(),
Box::new(Map::new(Arc::clone(&config), |config: &Config| { Box::new(Map::new(Arc::clone(&config), |config: &Config| {

@ -1,7 +1,6 @@
use anyhow::{Context, Error, Result}; use anyhow::{Context, Result};
use helix_term::application::Application; use helix_term::application::Application;
use helix_term::args::Args; use helix_term::args::Args;
use helix_term::config::{Config, ConfigLoadError};
use std::path::PathBuf; use std::path::PathBuf;
fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> { fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
@ -109,35 +108,10 @@ FLAGS:
return Ok(0); return Ok(0);
} }
let conf_dir = helix_loader::config_dir();
if !conf_dir.exists() {
std::fs::create_dir_all(&conf_dir).ok();
}
let config = match Config::load_default() {
Ok(config) => config,
Err(err) => {
match err {
ConfigLoadError::BadConfig(err) => {
eprintln!("Bad config: {}", err);
eprintln!("Press <ENTER> to continue with default config");
use std::io::Read;
// This waits for an enter press.
let _ = std::io::stdin().read(&mut []);
Config::default()
}
ConfigLoadError::Error(err) if err.kind() == std::io::ErrorKind::NotFound => {
Config::default()
}
ConfigLoadError::Error(err) => return Err(Error::new(err)),
}
}
};
setup_logging(logpath, args.verbosity).context("failed to initialize logging")?; setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
// TODO: use the thread local executor to spawn the application task separately from the work pool // TODO: use the thread local executor to spawn the application task separately from the work pool
let mut app = Application::new(args, config).context("unable to create new application")?; let mut app = Application::new(args).context("unable to create new application")?;
let exit_code = app.run().await?; let exit_code = app.run().await?;

Loading…
Cancel
Save