Skip to content

Commit 4820d50

Browse files
committed
fix(init): handle parser name replacements better when a kebab-case name is expected
1 parent bde94ae commit 4820d50

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

cli/src/init.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const PARSER_NAME_PLACEHOLDER: &str = "PARSER_NAME";
2424
const CAMEL_PARSER_NAME_PLACEHOLDER: &str = "CAMEL_PARSER_NAME";
2525
const UPPER_PARSER_NAME_PLACEHOLDER: &str = "UPPER_PARSER_NAME";
2626
const LOWER_PARSER_NAME_PLACEHOLDER: &str = "LOWER_PARSER_NAME";
27+
const KEBAB_PARSER_NAME_PLACEHOLDER: &str = "KEBAB_PARSER_NAME";
2728
const PARSER_CLASS_NAME_PLACEHOLDER: &str = "PARSER_CLASS_NAME";
2829

2930
const PARSER_DESCRIPTION_PLACEHOLDER: &str = "PARSER_DESCRIPTION";
@@ -403,13 +404,13 @@ pub fn generate_grammar_files(
403404
// Generate C bindings
404405
if tree_sitter_config.bindings.c {
405406
missing_path(bindings_dir.join("c"), create_dir)?.apply(|path| {
406-
let old_file = &path.join(format!("tree-sitter-{language_name}.h"));
407+
let old_file = &path.join(format!("tree-sitter-{}.h", language_name.to_kebab_case()));
407408
if allow_update && fs::exists(old_file).unwrap_or(false) {
408409
fs::remove_file(old_file)?;
409410
}
410411
missing_path(path.join("tree_sitter"), create_dir)?.apply(|include_path| {
411412
missing_path(
412-
include_path.join(format!("tree-sitter-{language_name}.h")),
413+
include_path.join(format!("tree-sitter-{}.h", language_name.to_kebab_case())),
413414
|path| {
414415
generate_file(path, PARSER_NAME_H_TEMPLATE, language_name, &generate_opts)
415416
},
@@ -418,7 +419,7 @@ pub fn generate_grammar_files(
418419
})?;
419420

420421
missing_path(
421-
path.join(format!("tree-sitter-{language_name}.pc.in")),
422+
path.join(format!("tree-sitter-{}.pc.in", language_name.to_kebab_case())),
422423
|path| {
423424
generate_file(
424425
path,
@@ -709,6 +710,10 @@ fn generate_file(
709710
LOWER_PARSER_NAME_PLACEHOLDER,
710711
&language_name.to_snake_case(),
711712
)
713+
.replace(
714+
KEBAB_PARSER_NAME_PLACEHOLDER,
715+
&language_name.to_kebab_case(),
716+
)
712717
.replace(PARSER_NAME_PLACEHOLDER, language_name)
713718
.replace(CLI_VERSION_PLACEHOLDER, CLI_VERSION)
714719
.replace(RUST_BINDING_VERSION_PLACEHOLDER, RUST_BINDING_VERSION)

cli/src/templates/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ fn main() {
1717
println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
1818
}
1919

20-
c_config.compile("tree-sitter-PARSER_NAME");
20+
c_config.compile("tree-sitter-KEBAB_PARSER_NAME");
2121
}

cli/src/templates/cmakelists.cmake

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.13)
22

3-
project(tree-sitter-PARSER_NAME
3+
project(tree-sitter-KEBAB_PARSER_NAME
44
VERSION "PARSER_VERSION"
55
DESCRIPTION "PARSER_DESCRIPTION"
66
HOMEPAGE_URL "PARSER_URL"
@@ -24,42 +24,42 @@ add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c"
2424
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
2525
COMMENT "Generating parser.c")
2626

27-
add_library(tree-sitter-PARSER_NAME src/parser.c)
27+
add_library(tree-sitter-KEBAB_PARSER_NAME src/parser.c)
2828
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scanner.c)
29-
target_sources(tree-sitter-PARSER_NAME PRIVATE src/scanner.c)
29+
target_sources(tree-sitter-KEBAB_PARSER_NAME PRIVATE src/scanner.c)
3030
endif()
31-
target_include_directories(tree-sitter-PARSER_NAME
31+
target_include_directories(tree-sitter-KEBAB_PARSER_NAME
3232
PRIVATE src
3333
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/bindings/c>
3434
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
3535

36-
target_compile_definitions(tree-sitter-PARSER_NAME PRIVATE
36+
target_compile_definitions(tree-sitter-KEBAB_PARSER_NAME PRIVATE
3737
$<$<BOOL:${TREE_SITTER_REUSE_ALLOCATOR}>:TREE_SITTER_REUSE_ALLOCATOR>
3838
$<$<CONFIG:Debug>:TREE_SITTER_DEBUG>)
3939

40-
set_target_properties(tree-sitter-PARSER_NAME
40+
set_target_properties(tree-sitter-KEBAB_PARSER_NAME
4141
PROPERTIES
4242
C_STANDARD 11
4343
POSITION_INDEPENDENT_CODE ON
4444
SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}"
4545
DEFINE_SYMBOL "")
4646

47-
configure_file(bindings/c/tree-sitter-PARSER_NAME.pc.in
48-
"${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-PARSER_NAME.pc" @ONLY)
47+
configure_file(bindings/c/tree-sitter-KEBAB_PARSER_NAME.pc.in
48+
"${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-KEBAB_PARSER_NAME.pc" @ONLY)
4949

5050
include(GNUInstallDirs)
5151

5252
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bindings/c/tree_sitter"
5353
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
5454
FILES_MATCHING PATTERN "*.h")
55-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-PARSER_NAME.pc"
55+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-KEBAB_PARSER_NAME.pc"
5656
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig")
57-
install(TARGETS tree-sitter-PARSER_NAME
57+
install(TARGETS tree-sitter-KEBAB_PARSER_NAME
5858
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
5959

6060
file(GLOB QUERIES queries/*.scm)
6161
install(FILES ${QUERIES}
62-
DESTINATION "${CMAKE_INSTALL_DATADIR}/tree-sitter/queries/PARSER_NAME")
62+
DESTINATION "${CMAKE_INSTALL_DATADIR}/tree-sitter/queries/KEBAB_PARSER_NAME")
6363

6464
add_custom_target(ts-test "${TREE_SITTER_CLI}" test
6565
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"

cli/src/templates/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const root = require("path").join(__dirname, "..", "..");
33
module.exports =
44
typeof process.versions.bun === "string"
55
// Support `bun build --compile` by being statically analyzable enough to find the .node file at build-time
6-
? require(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-PARSER_NAME.node`)
6+
? require(`../../prebuilds/${process.platform}-${process.arch}/tree-sitter-KEBAB_PARSER_NAME.node`)
77
: require("node-gyp-build")(root);
88

99
try {

cli/src/templates/makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ifeq ($(OS),Windows_NT)
22
$(error Windows is not supported)
33
endif
44

5-
LANGUAGE_NAME := tree-sitter-PARSER_NAME
5+
LANGUAGE_NAME := tree-sitter-KEBAB_PARSER_NAME
66
HOMEPAGE_URL := PARSER_URL
77
VERSION := PARSER_VERSION
88

@@ -70,14 +70,14 @@ $(PARSER): $(SRC_DIR)/grammar.json
7070
$(TS) generate $^
7171

7272
install: all
73-
install -d '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/PARSER_NAME '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)'
73+
install -d '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/KEBAB_PARSER_NAME '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)'
7474
install -m644 bindings/c/tree_sitter/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
7575
install -m644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
7676
install -m644 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
7777
install -m755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER)
7878
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
7979
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT)
80-
install -m644 queries/*.scm '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/PARSER_NAME
80+
install -m644 queries/*.scm '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/KEBAB_PARSER_NAME
8181

8282
uninstall:
8383
$(RM) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a \
@@ -86,7 +86,7 @@ uninstall:
8686
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT) \
8787
'$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h \
8888
'$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
89-
$(RM) -r '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/PARSER_NAME
89+
$(RM) -r '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/KEBAB_PARSER_NAME
9090

9191
clean:
9292
$(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT)

0 commit comments

Comments
 (0)