tree-sitter-config-0.19.0/.cargo_vcs_info.json0000644000000001120000000000100146270ustar { "git": { "sha1": "23b28f6f36ae0b9056fbb23802c3300e50802069" } } tree-sitter-config-0.19.0/Cargo.toml0000644000000021100000000000100126250ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "tree-sitter-config" version = "0.19.0" authors = ["Max Brunsfeld "] description = "User configuration of tree-sitter's command line programs" readme = "README.md" keywords = ["incremental", "parsing"] categories = ["command-line-utilities", "parsing"] license = "MIT" repository = "https://github.com/tree-sitter/tree-sitter" [dependencies.anyhow] version = "1.0" [dependencies.dirs] version = "3.0" [dependencies.serde] version = "1.0" [dependencies.serde_json] version = "1.0" features = ["preserve_order"] tree-sitter-config-0.19.0/Cargo.toml.orig000064400000000000000000000010040072674642500163370ustar 00000000000000[package] name = "tree-sitter-config" description = "User configuration of tree-sitter's command line programs" version = "0.19.0" authors = ["Max Brunsfeld "] edition = "2018" license = "MIT" readme = "README.md" keywords = ["incremental", "parsing"] categories = ["command-line-utilities", "parsing"] repository = "https://github.com/tree-sitter/tree-sitter" [dependencies] anyhow = "1.0" dirs = "3.0" serde = "1.0" [dependencies.serde_json] version = "1.0" features = ["preserve_order"] tree-sitter-config-0.19.0/README.md000064400000000000000000000003410072674642500147320ustar 00000000000000# `tree-sitter-config` You can use a configuration file to control the behavior of the `tree-sitter` command-line program. This crate implements the logic for finding and the parsing the contents of the configuration file. tree-sitter-config-0.19.0/src/lib.rs000064400000000000000000000110370072674642500153620ustar 00000000000000//! Manages tree-sitter's configuration file. use anyhow::{anyhow, Context, Result}; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::path::PathBuf; use std::{env, fs}; /// Holds the contents of tree-sitter's configuration file. /// /// The file typically lives at `~/.config/tree-sitter/config.json`, but see the [`load`][] method /// for the full details on where it might be located. /// /// This type holds the generic JSON content of the configuration file. Individual tree-sitter /// components will use the [`get`][] method to parse that JSON to extract configuration fields /// that are specific to that component. #[derive(Debug)] pub struct Config { pub location: PathBuf, pub config: Value, } impl Config { pub fn find_config_file() -> Result> { if let Ok(path) = env::var("TREE_SITTER_DIR") { let mut path = PathBuf::from(path); path.push("config.json"); if path.is_file() { return Ok(Some(path)); } } let xdg_path = Self::xdg_config_file()?; if xdg_path.is_file() { 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(Some(legacy_path)); } Ok(None) } fn xdg_config_file() -> Result { let xdg_path = dirs::config_dir() .ok_or(anyhow!("Cannot determine config directory"))? .join("tree-sitter/config.json"); Ok(xdg_path) } /// Locates and loads in the user's configuration file. We search for the configuration file /// in the following locations, in order: /// /// - `$TREE_SITTER_DIR/config.json`, if the `TREE_SITTER_DIR` environment variable is set /// - `tree-sitter/config.json` in your default user configuration directory, as determined /// by [`dirs::config_dir`](https://docs.rs/dirs/*/dirs/fn.config_dir.html) /// - `$HOME/.tree-sitter/config.json` as a fallback from where tree-sitter _used_ to store /// its configuration pub fn load() -> Result { let location = match Self::find_config_file()? { Some(location) => location, None => return Config::initial(), }; let content = fs::read_to_string(&location) .with_context(|| format!("Failed to read {}", &location.to_string_lossy()))?; let config = serde_json::from_str(&content) .with_context(|| format!("Bad JSON config {}", &location.to_string_lossy()))?; Ok(Config { location, config }) } /// Creates an empty initial configuration file. You can then use the [`add`][] method to add /// the component-specific configuration types for any components that want to add content to /// the default file, and then use [`save`][] to write the configuration to disk. /// /// (Note that this is typically only done by the `tree-sitter init-config` command.) pub fn initial() -> Result { let location = Self::xdg_config_file()?; let config = serde_json::json!({}); Ok(Config { location, config }) } /// Saves this configuration to the file that it was originally loaded from. pub fn save(&self) -> Result<()> { let json = serde_json::to_string_pretty(&self.config)?; fs::create_dir_all(self.location.parent().unwrap())?; fs::write(&self.location, json)?; Ok(()) } /// Parses a component-specific configuration from the configuration file. The type `C` must /// be [deserializable](https://docs.rs/serde/*/serde/trait.Deserialize.html) from a JSON /// object, and must only include the fields relevant to that component. pub fn get(&self) -> Result where C: for<'de> Deserialize<'de>, { let config = serde_json::from_value(self.config.clone())?; Ok(config) } /// Adds a component-specific configuration to the configuration file. The type `C` must be /// [serializable](https://docs.rs/serde/*/serde/trait.Serialize.html) into a JSON object, and /// must only include the fields relevant to that component. pub fn add(&mut self, config: C) -> Result<()> where C: Serialize, { let mut config = serde_json::to_value(&config)?; self.config .as_object_mut() .unwrap() .append(config.as_object_mut().unwrap()); Ok(()) } }