From b44270efab1edd4bba0ee0d4aa6a2c493adb4dbd Mon Sep 17 00:00:00 2001 From: Douglas Creager Date: Thu, 10 Jun 2021 09:43:07 -0400 Subject: [PATCH] cli: Missing config file shouldn't be an error Just fall back on the default values for each configuration option. --- cli/config/src/lib.rs | 22 +++++++++++----------- cli/loader/src/lib.rs | 7 +++++++ cli/src/main.rs | 4 ++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cli/config/src/lib.rs b/cli/config/src/lib.rs index dfe84b7a..cc52ea6e 100644 --- a/cli/config/src/lib.rs +++ b/cli/config/src/lib.rs @@ -15,34 +15,31 @@ use std::{env, fs}; /// components will use the [`get`][] method to parse that JSON to extract configuration fields /// that are specific to that component. pub struct Config { - location: PathBuf, - config: Value, + pub location: PathBuf, + pub config: Value, } impl Config { - fn find_config_file() -> Result { + fn find_config_file() -> Result> { if let Ok(path) = env::var("TREE_SITTER_DIR") { let mut path = PathBuf::from(path); path.push("config.json"); - return Ok(path); + return Ok(Some(path)); } let xdg_path = Self::xdg_config_file()?; if xdg_path.is_file() { - return Ok(xdg_path); + return Ok(Some(xdg_path)); } let legacy_path = dirs::home_dir() .ok_or(anyhow!("Cannot determine home directory"))? .join(".tree-sitter/config.json"); if legacy_path.is_file() { - return Ok(legacy_path); + return Ok(Some(legacy_path)); } - Err(anyhow!(concat!( - "Cannot find a tree-sitter configuration file.\n", - "Please run `tree-sitter init-config` to create one!" - ))) + Ok(None) } fn xdg_config_file() -> Result { @@ -61,7 +58,10 @@ impl Config { /// - `$HOME/.tree-sitter/config.json` as a fallback from where tree-sitter _used_ to store /// its configuration pub fn load() -> Result { - let location = Self::find_config_file()?; + let location = match Self::find_config_file()? { + Some(location) => location, + None => return Config::initial(), + }; let content = fs::read_to_string(&location)?; let config = serde_json::from_str(&content)?; Ok(Config { location, config }) diff --git a/cli/loader/src/lib.rs b/cli/loader/src/lib.rs index 80f0b1df..4e9af25f 100644 --- a/cli/loader/src/lib.rs +++ b/cli/loader/src/lib.rs @@ -104,6 +104,13 @@ impl Loader { } pub fn find_all_languages(&mut self, config: &Config) -> Result<()> { + if config.parser_directories.is_empty() { + eprintln!("Warning: You have not configured any parser directories!"); + eprintln!("Please run `tree-sitter init-config` and edit the resulting"); + eprintln!("configuration file to indicate where we should look for"); + eprintln!("language grammars."); + eprintln!(""); + } for parser_container_dir in &config.parser_directories { if let Ok(entries) = fs::read_dir(parser_container_dir) { for entry in entries { diff --git a/cli/src/main.rs b/cli/src/main.rs index 06a8f6ac..5c39190b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -183,6 +183,10 @@ fn run() -> Result<()> { config.add(tree_sitter_loader::Config::initial())?; config.add(tree_sitter_cli::highlight::ThemeConfig::default())?; config.save()?; + println!( + "Saved initial configuration to {}", + config.location.display() + ); } else if let Some(matches) = matches.subcommand_matches("generate") { let grammar_path = matches.value_of("grammar-path"); let report_symbol_name = matches.value_of("report-states-for-rule").or_else(|| {