Skip to content

Commit c051a90

Browse files
rooneyObserverOfTime
authored andcommitted
fix: insert "tree-sitter" section in current directory's package.json if it exists
Co-authored-by: ObserverOfTime <[email protected]>
1 parent 63babea commit c051a90

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

cli/src/generate/grammar_files.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ pub fn generate_grammar_files(
214214
updated = true;
215215
}
216216

217+
// insert `tree-sitter` at the end
218+
if !package_json.contains_key("tree-sitter") {
219+
eprintln!("Adding a `tree-sitter` section to package.json");
220+
package_json.insert(
221+
"tree-sitter".to_string(),
222+
json!([{
223+
"scope": format!("source.{language_name}"),
224+
"injection-regex": format!("^{language_name}$"),
225+
}]),
226+
);
227+
updated = true;
228+
}
229+
217230
if updated {
218231
let mut package_json_str = serde_json::to_string_pretty(&package_json)?;
219232
package_json_str.push('\n');
@@ -450,8 +463,7 @@ fn lookup_package_json_for_path(path: &Path) -> Result<(PathBuf, PackageJSON)> {
450463
.then(|| -> Result<PackageJSON> {
451464
let file =
452465
File::open(pathbuf.as_path()).with_context(|| "Failed to open package.json")?;
453-
let package_json: PackageJSON = serde_json::from_reader(BufReader::new(file))?;
454-
Ok(package_json)
466+
Ok(serde_json::from_reader(BufReader::new(file))?)
455467
})
456468
.transpose()?;
457469
if let Some(package_json) = package_json {

0 commit comments

Comments
 (0)