feat(loader): add error message when a tree-sitter.json file is invalid

This commit is contained in:
Will Lillis 2024-11-09 20:46:29 -05:00 committed by GitHub
parent 0dc1044d0d
commit 7b90dbf189
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -147,9 +147,10 @@ pub struct TreeSitterJSON {
}
impl TreeSitterJSON {
#[must_use]
pub fn from_file(path: &Path) -> Option<Self> {
serde_json::from_str(&fs::read_to_string(path.join("tree-sitter.json")).ok()?).ok()
pub fn from_file(path: &Path) -> Result<Self> {
Ok(serde_json::from_str(&fs::read_to_string(
path.join("tree-sitter.json"),
)?)?)
}
#[must_use]
@ -1119,7 +1120,8 @@ impl Loader {
) -> Result<&[LanguageConfiguration]> {
let initial_language_configuration_count = self.language_configurations.len();
if let Some(config) = TreeSitterJSON::from_file(parser_path) {
let ts_json = TreeSitterJSON::from_file(parser_path);
if let Ok(config) = ts_json {
let language_count = self.languages_by_id.len();
for grammar in config.grammars {
// Determine the path to the parser directory. This can be specified in
@ -1210,6 +1212,11 @@ impl Loader {
Some(self.language_configurations.len() - 1);
}
}
} else if let Err(e) = ts_json {
eprintln!(
"Warning: Failed to read {} -- {e}",
parser_path.join("tree-sitter.json").display()
);
}
// If we didn't find any language configurations in the tree-sitter.json file,