Skip to content

Commit 46f8d12

Browse files
committed
feat(init): add a title field to grammars
1 parent 4820d50 commit 46f8d12

File tree

7 files changed

+32
-6
lines changed

7 files changed

+32
-6
lines changed

cli/loader/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ pub struct Grammar {
167167
pub name: String,
168168
#[serde(skip_serializing_if = "Option::is_none")]
169169
pub camelcase: Option<String>,
170+
#[serde(skip_serializing_if = "Option::is_none")]
171+
pub title: Option<String>,
170172
pub scope: String,
171173
#[serde(skip_serializing_if = "Option::is_none")]
172174
pub path: Option<PathBuf>,

cli/src/init.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const ABI_VERSION_MAX_PLACEHOLDER: &str = "ABI_VERSION_MAX";
2222

2323
const PARSER_NAME_PLACEHOLDER: &str = "PARSER_NAME";
2424
const CAMEL_PARSER_NAME_PLACEHOLDER: &str = "CAMEL_PARSER_NAME";
25+
const TITLE_PARSER_NAME_PLACEHOLDER: &str = "TITLE_PARSER_NAME";
2526
const UPPER_PARSER_NAME_PLACEHOLDER: &str = "UPPER_PARSER_NAME";
2627
const LOWER_PARSER_NAME_PLACEHOLDER: &str = "LOWER_PARSER_NAME";
2728
const KEBAB_PARSER_NAME_PLACEHOLDER: &str = "KEBAB_PARSER_NAME";
@@ -119,6 +120,7 @@ pub fn path_in_ignore(repo_path: &Path) -> bool {
119120
pub struct JsonConfigOpts {
120121
pub name: String,
121122
pub camelcase: String,
123+
pub title: String,
122124
pub description: String,
123125
#[serde(skip_serializing_if = "Option::is_none")]
124126
pub repository: Option<Url>,
@@ -143,6 +145,7 @@ impl JsonConfigOpts {
143145
grammars: vec![Grammar {
144146
name: self.name.clone(),
145147
camelcase: Some(self.camelcase),
148+
title: Some(self.title),
146149
scope: self.scope,
147150
path: None,
148151
external_files: PathsJSON::Empty,
@@ -188,6 +191,7 @@ impl Default for JsonConfigOpts {
188191
Self {
189192
name: String::new(),
190193
camelcase: String::new(),
194+
title: String::new(),
191195
description: String::new(),
192196
repository: None,
193197
funding: None,
@@ -212,6 +216,7 @@ struct GenerateOpts<'a> {
212216
funding: Option<&'a str>,
213217
version: &'a Version,
214218
camel_parser_name: &'a str,
219+
title_parser_name: &'a str,
215220
class_name: &'a str,
216221
}
217222

@@ -254,6 +259,10 @@ pub fn generate_grammar_files(
254259
.camelcase
255260
.clone()
256261
.unwrap_or_else(|| language_name.to_upper_camel_case());
262+
let title_name = tree_sitter_config.grammars[0]
263+
.title
264+
.clone()
265+
.unwrap_or_else(|| language_name.to_upper_camel_case());
257266
let class_name = tree_sitter_config.grammars[0]
258267
.class_name
259268
.clone()
@@ -283,6 +292,7 @@ pub fn generate_grammar_files(
283292
.and_then(|l| l.funding.as_ref().map(|f| f.as_str())),
284293
version: &tree_sitter_config.metadata.version,
285294
camel_parser_name: &camel_name,
295+
title_parser_name: &title_name,
286296
class_name: &class_name,
287297
};
288298

@@ -702,6 +712,10 @@ fn generate_file(
702712
CAMEL_PARSER_NAME_PLACEHOLDER,
703713
generate_opts.camel_parser_name,
704714
)
715+
.replace(
716+
TITLE_PARSER_NAME_PLACEHOLDER,
717+
generate_opts.title_parser_name,
718+
)
705719
.replace(
706720
UPPER_PARSER_NAME_PLACEHOLDER,
707721
&language_name.to_shouty_snake_case(),

cli/src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,13 @@ impl Init {
534534
.interact_text()
535535
};
536536

537+
let title = |name: &str| {
538+
Input::<String>::with_theme(&ColorfulTheme::default())
539+
.with_prompt("Title (human-readable name)")
540+
.default(name.to_upper_camel_case())
541+
.interact_text()
542+
};
543+
537544
let description = |name: &str| {
538545
Input::<String>::with_theme(&ColorfulTheme::default())
539546
.with_prompt("Description")
@@ -656,6 +663,7 @@ impl Init {
656663
let choices = [
657664
"name",
658665
"camelcase",
666+
"title",
659667
"description",
660668
"repository",
661669
"funding",
@@ -674,6 +682,7 @@ impl Init {
674682
match $choice {
675683
"name" => opts.name = name()?,
676684
"camelcase" => opts.camelcase = camelcase_name(&opts.name)?,
685+
"title" => opts.title = title(&opts.name)?,
677686
"description" => opts.description = description(&opts.name)?,
678687
"repository" => opts.repository = Some(repository(&opts.name)?),
679688
"funding" => opts.funding = funding()?,

cli/src/templates/binding_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ import (
1010
func TestCanLoadGrammar(t *testing.T) {
1111
language := tree_sitter.NewLanguage(tree_sitter_LOWER_PARSER_NAME.Language())
1212
if language == nil {
13-
t.Errorf("Error loading CAMEL_PARSER_NAME grammar")
13+
t.Errorf("Error loading TITLE_PARSER_NAME grammar")
1414
}
1515
}

cli/src/templates/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! let language = tree_sitter_PARSER_NAME::LANGUAGE;
1111
//! parser
1212
//! .set_language(&language.into())
13-
//! .expect("Error loading CAMEL_PARSER_NAME parser");
13+
//! .expect("Error loading TITLE_PARSER_NAME parser");
1414
//! let tree = parser.parse(code, None).unwrap();
1515
//! assert!(!tree.root_node().has_error());
1616
//! ```
@@ -48,6 +48,6 @@ mod tests {
4848
let mut parser = tree_sitter::Parser::new();
4949
parser
5050
.set_language(&super::LANGUAGE.into())
51-
.expect("Error loading CAMEL_PARSER_NAME parser");
51+
.expect("Error loading TITLE_PARSER_NAME parser");
5252
}
5353
}

cli/src/templates/test_binding.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from unittest import TestCase
22

3-
import tree_sitter, tree_sitter_LOWER_PARSER_NAME
3+
import tree_sitter
4+
import tree_sitter_LOWER_PARSER_NAME
45

56

67
class TestLanguage(TestCase):
78
def test_can_load_grammar(self):
89
try:
910
tree_sitter.Language(tree_sitter_LOWER_PARSER_NAME.language())
1011
except Exception:
11-
self.fail("Error loading CAMEL_PARSER_NAME grammar")
12+
self.fail("Error loading TITLE_PARSER_NAME grammar")

cli/src/templates/tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ final class PARSER_CLASS_NAMETests: XCTestCase {
77
let parser = Parser()
88
let language = Language(language: tree_sitter_LOWER_PARSER_NAME())
99
XCTAssertNoThrow(try parser.setLanguage(language),
10-
"Error loading CAMEL_PARSER_NAME grammar")
10+
"Error loading TITLE_PARSER_NAME grammar")
1111
}
1212
}

0 commit comments

Comments
 (0)