4use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
7pub static RUNTIME_DIR: once_cell::sync::Lazy<PathBuf> = once_cell::sync::Lazy::new(runtime_dir);
9static CONFIG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
11pub fn initialize_config_file(specified_file: Option<PathBuf>) {
12 let config_file = specified_file.unwrap_or_else(|| {
13 let config_dir = config_dir();
15 if !config_dir.exists() {
16 std::fs::create_dir_all(&config_dir).ok();
19 config_dir.join("config.toml")
22 // We should only initialize this value once.
23 CONFIG_FILE.set(config_file).ok();
26pub fn runtime_dir() -> PathBuf {
27 if let Ok(dir) = std::env::var("HELIX_RUNTIME") {
31 if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") {
32 // this is the directory of the crate being run by cargo, we need the workspace path so we take the parent
33 let path = std::path::PathBuf::from(dir).parent().unwrap().join(RT_DIR);
34 log::debug!("runtime dir: {}", path.to_string_lossy());
38 const RT_DIR: &str = "runtime";
39 let conf_dir = config_dir().join(RT_DIR);
40 if conf_dir.exists() {
44 // fallback to location of the executable being run
45 std::env::current_exe()
47 .and_then(|path| path.parent().map(|path| path.to_path_buf().join(RT_DIR)))
51pub fn config_dir() -> PathBuf {
52 // TODO: allow env var override
53 let strategy = choose_base_strategy().expect("Unable to find the config directory!");
54 let mut path = strategy.config_dir();
59pub fn local_config_dirs() -> Vec<PathBuf> {
60 let directories = find_root_impl(None, &[".helix".to_string()])
62 .map(|path| path.join(".helix"))
64 log::debug!("Located configuration folders: {:?}", directories);
68pub fn cache_dir() -> PathBuf {
69 // TODO: allow env var override
70 let strategy = choose_base_strategy().expect("Unable to find the config directory!");
71 let mut path = strategy.cache_dir();
76pub fn config_file() -> PathBuf {
79 .map(|path| path.to_path_buf())
80 .unwrap_or_else(|| config_dir().join("config.toml"))
83pub fn lang_config_file() -> PathBuf {
84 config_dir().join("languages.toml")
87pub fn log_file() -> PathBuf {
88 cache_dir().join("helix.log")
91pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<PathBuf> {
92 let current_dir = std::env::current_dir().expect("unable to determine current directory");
93 let mut directories = Vec::new();
95 let root = match root {
97 let root = std::path::Path::new(root);
98 if root.is_absolute() {
101 current_dir.join(root)
107 for ancestor in root.ancestors() {
108 // don't go higher than repo
109 if ancestor.join(".git").is_dir() {
110 // Use workspace if detected from marker
111 directories.push(ancestor.to_path_buf());
113 } else if root_markers
115 .any(|marker| ancestor.join(marker).exists())
117 directories.push(ancestor.to_path_buf());
123/// Merge two TOML documents, merging values from `right` onto `left`
125/// When an array exists in both `left` and `right`, `right`'s array is
126/// used. When a table exists in both `left` and `right`, the merged table
127/// consists of all keys in `left`'s table unioned with all keys in `right`
128/// with the values of `right` being merged recursively onto values of
131/// `merge_toplevel_arrays` controls whether a top-level array in the TOML
132/// document is merged instead of overridden. This is useful for TOML
133/// documents that use a top-level array of values like the `languages.toml`,
134/// where one usually wants to override or add to the array instead of
135/// replacing it altogether.
136pub fn merge_toml_values(left: toml::Value, right: toml::Value, merge_depth: usize) -> toml::Value {
139 fn get_name(v: &Value) -> Option<&str> {
140 v.get("name").and_then(Value::as_str)
143 match (left, right) {
144 (Value::Array(mut left_items), Value::Array(right_items)) => {
145 // The top-level arrays should be merged but nested arrays should
146 // act as overrides. For the `languages.toml` config, this means
147 // that you can specify a sub-set of languages in an overriding
148 // `languages.toml` but that nested arrays like Language Server
149 // arguments are replaced instead of merged.
151 left_items.reserve(right_items.len());
152 for rvalue in right_items {
153 let lvalue = get_name(&rvalue)
155 left_items.iter().position(|v| get_name(v) == Some(rname))
157 .map(|lpos| left_items.remove(lpos));
158 let mvalue = match lvalue {
159 Some(lvalue) => merge_toml_values(lvalue, rvalue, merge_depth - 1),
162 left_items.push(mvalue);
164 Value::Array(left_items)
166 Value::Array(right_items)
169 (Value::Table(mut left_map), Value::Table(right_map)) => {
171 for (rname, rvalue) in right_map {
172 match left_map.remove(&rname) {
174 let merged_value = merge_toml_values(lvalue, rvalue, merge_depth - 1);
175 left_map.insert(rname, merged_value);
178 left_map.insert(rname, rvalue);
182 Value::Table(left_map)
184 Value::Table(right_map)
187 // Catch everything else we didn't handle, and use the right value
193mod merge_toml_tests {
194 use super::merge_toml_values;
198 fn language_toml_map_merges() {
199 const USER: &str = r#"
203 indent = { tab-width = 4, unit = " ", test = "aaa" }
206 let base: Value = toml::from_slice(include_bytes!("../../languages.toml"))
207 .expect("Couldn't parse built-in languages config");
208 let user: Value = toml::from_str(USER).unwrap();
210 let merged = merge_toml_values(base, user, 3);
211 let languages = merged.get("language").unwrap().as_array().unwrap();
214 .find(|v| v.get("name").unwrap().as_str().unwrap() == "nix")
216 let nix_indent = nix.get("indent").unwrap();
218 // We changed tab-width and unit in indent so check them if they are the new values
220 nix_indent.get("tab-width").unwrap().as_integer().unwrap(),
223 assert_eq!(nix_indent.get("unit").unwrap().as_str().unwrap(), " ");
224 // We added a new keys, so check them
225 assert_eq!(nix.get("test").unwrap().as_str().unwrap(), "bbb");
226 assert_eq!(nix_indent.get("test").unwrap().as_str().unwrap(), "aaa");
227 // We didn't change comment-token so it should be same
228 assert_eq!(nix.get("comment-token").unwrap().as_str().unwrap(), "#");
232 fn language_toml_nested_array_merges() {
233 const USER: &str = r#"
236 language-server = { command = "deno", args = ["lsp"] }
239 let base: Value = toml::from_slice(include_bytes!("../../languages.toml"))
240 .expect("Couldn't parse built-in languages config");
241 let user: Value = toml::from_str(USER).unwrap();
243 let merged = merge_toml_values(base, user, 3);
244 let languages = merged.get("language").unwrap().as_array().unwrap();
247 .find(|v| v.get("name").unwrap().as_str().unwrap() == "typescript")
250 ts.get("language-server")
256 &vec![Value::String("lsp".into())]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list