From 5ea246adbc7e9ecd78e5fa7ce731309a5adc1102 Mon Sep 17 00:00:00 2001 From: WillLillis Date: Mon, 29 Dec 2025 14:12:25 -0500 Subject: [PATCH] feat(cli): fill in missing fields to tree-sitter.json when running `tree-sitter init -u` (cherry picked from commit dd60d5cff079dbae8db798ce7272879dbd2ac9e8) --- crates/cli/src/init.rs | 2 +- crates/cli/src/main.rs | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/crates/cli/src/init.rs b/crates/cli/src/init.rs index 2d37f8dd..00b7657b 100644 --- a/crates/cli/src/init.rs +++ b/crates/cli/src/init.rs @@ -123,7 +123,7 @@ const BUILD_ZIG_ZON_TEMPLATE: &str = include_str!("./templates/build.zig.zon"); const ROOT_ZIG_TEMPLATE: &str = include_str!("./templates/root.zig"); const TEST_ZIG_TEMPLATE: &str = include_str!("./templates/test.zig"); -const TREE_SITTER_JSON_SCHEMA: &str = +pub const TREE_SITTER_JSON_SCHEMA: &str = "https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json"; #[derive(Serialize, Deserialize, Clone)] diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 35d8fcb7..fbf75b8e 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -20,7 +20,7 @@ use tree_sitter_cli::{ LOG_GRAPH_ENABLED, START_SEED, }, highlight::{self, HighlightOptions}, - init::{generate_grammar_files, JsonConfigOpts}, + init::{generate_grammar_files, JsonConfigOpts, TREE_SITTER_JSON_SCHEMA}, input::{get_input, get_tmp_source_file, CliInput}, logger, parse::{self, ParseDebugType, ParseFileOptions, ParseOutput, ParseTheme}, @@ -867,10 +867,26 @@ impl Init { (opts.name.clone(), Some(opts)) } else { - let mut json = serde_json::from_str::( - &fs::read_to_string(current_dir.join("tree-sitter.json")) - .with_context(|| "Failed to read tree-sitter.json")?, - )?; + let old_config = fs::read_to_string(current_dir.join("tree-sitter.json")) + .with_context(|| "Failed to read tree-sitter.json")?; + + let mut json = serde_json::from_str::(&old_config)?; + if json.schema.is_none() { + json.schema = Some(TREE_SITTER_JSON_SCHEMA.to_string()); + } + + let new_config = format!("{}\n", serde_json::to_string_pretty(&json)?); + // Write the re-serialized config back, as newly added optional boolean fields + // will be included with explicit `false`s rather than implict `null`s + if self.update && !old_config.trim().eq(new_config.trim()) { + info!("Updating tree-sitter.json"); + fs::write( + current_dir.join("tree-sitter.json"), + serde_json::to_string_pretty(&json)?, + ) + .with_context(|| "Failed to write tree-sitter.json")?; + } + (json.grammars.swap_remove(0).name, None) };