Skip to content

Commit 49763bd

Browse files
authored
Merge pull request #2 from gbprod/fix-diff-injection
fix: use only one diff node for injection
2 parents f6e7f24 + 732e8af commit 49763bd

File tree

9 files changed

+1304
-1347
lines changed

9 files changed

+1304
-1347
lines changed

README.md

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
11
# tree-sitter-gitcommit
22

3-
**This grammar is under development**
3+
[![Build/test](https://github.com/gbprod/tree-sitter-gitcommit/actions/workflows/ci.yml/badge.svg)](https://github.com/gbprod/tree-sitter-gitcommit/actions/workflows/ci.yml)
44

55
`gitcommit` grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter).
66

77
Support [Conventionnal commit](https://www.conventionalcommits.org) specifications.
88

9-
[![Build/test](https://github.com/gbprod/tree-sitter-gitcommit/actions/workflows/ci.yml/badge.svg)](https://github.com/gbprod/tree-sitter-gitcommit/actions/workflows/ci.yml)
10-
11-
## Installation
12-
13-
### Neovim
14-
15-
As this grammar is not (yet) available throught [`nvim-treesitter`](https://github.com/nvim-treesitter/nvim-treesitter),
16-
you must install it manualy.
17-
18-
```lua
19-
local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
20-
21-
parser_config.gitcommit = {
22-
install_info = {
23-
url = "https://github.com/gbprod/tree-sitter-gitcommit",
24-
files = { "src/parser.c", "src/scanner.c" },
25-
branch = "main",
26-
},
27-
filetype = "gitcommit",
28-
maintainers = { "@gbprod" },
29-
}
30-
```
31-
32-
Then we need some queries for nvim-treesitter:
33-
34-
Edit file `~/.config/nvim/after/queries/gitcommit/highlights.scm` or execute `:TSEditQueryUserAfter highlights gitcommit`
35-
and copy the content of [`queries/highlights.scm`](https://github.com/gbprod/tree-sitter-gitcommit/blob/main/queries/highlights.scm).
36-
37-
Edit file `~/.config/nvim/after/queries/gitcommit/injections.scm` or execute `:TSEditQueryUserAfter injections gitcommit` with the content:
38-
39-
```scheme
40-
(diff) @diff
41-
(rebase_command) @git_rebase
42-
```
43-
44-
Now we can install the `gitcommit` parser via nvim-treesitter.
45-
46-
```vim
47-
:TSInstall gitcommit
48-
```
49-
50-
To have a complete highlight, you should have `git_rebase` and `diff` parsers installed :
51-
52-
```vim
53-
:TSInstall diff git_rebase
54-
```
55-
569
## Credits
5710

5811
- [https://github.com/the-mikedavis/tree-sitter-git-commit](https://github.com/the-mikedavis/tree-sitter-git-commit) good parser but [only works in english](https://github.com/the-mikedavis/tree-sitter-git-commit/issues/4).
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

corpus/scissor.txt

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,9 @@ index 457a414..94b9036 100644
2020
(subject)
2121
(comment)
2222
(scissor)
23-
(generated_comment)
24-
(generated_comment)
25-
(diff)
26-
(diff)
27-
(diff)
28-
(diff)
29-
(diff)
30-
(diff)
31-
(diff))
23+
(diff
24+
(generated_comment)
25+
(generated_comment)))
3226

3327
================================================================================
3428
Simple empty scissor
@@ -45,8 +39,9 @@ This is a simple subject
4539
(subject)
4640
(comment)
4741
(scissor)
48-
(generated_comment)
49-
(generated_comment))
42+
(diff
43+
(generated_comment)
44+
(generated_comment)))
5045

5146
================================================================================
5247
Scissor in commit verbose
@@ -69,15 +64,12 @@ index 9443e24..d8f88db 100644
6964
(source
7065
(subject)
7166
(scissor)
72-
(generated_comment)
73-
(generated_comment)
74-
(generated_comment)
75-
(generated_comment
76-
(title))
77-
(diff)
78-
(diff)
79-
(generated_comment)
80-
(generated_comment
81-
(title))
82-
(diff)
83-
(diff))
67+
(diff
68+
(generated_comment)
69+
(generated_comment)
70+
(generated_comment)
71+
(generated_comment
72+
(title))
73+
(generated_comment)
74+
(generated_comment
75+
(title))))

grammar.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,7 @@ module.exports = grammar({
184184
)
185185
),
186186

187-
_scissor: ($) =>
188-
seq(
189-
alias(SCISSORS, $.scissor),
190-
repeat1(
191-
choice(
192-
alias($._scissor_generated_comment, $.generated_comment),
193-
$.diff,
194-
NEWLINE
195-
)
196-
)
197-
),
187+
_scissor: ($) => seq(alias(SCISSORS, $.scissor), $.diff),
198188

199189
_scissor_generated_comment: ($) =>
200190
seq(
@@ -207,7 +197,14 @@ module.exports = grammar({
207197
)
208198
),
209199

210-
diff: () => seq(/[^#]/, optional(ANYTHING)),
200+
diff: ($) =>
201+
repeat1(
202+
choice(
203+
alias($._scissor_generated_comment, $.generated_comment),
204+
seq(/[^#]/, optional(ANYTHING)),
205+
NEWLINE
206+
)
207+
),
211208

212209
rebase_command: () =>
213210
seq(

src/grammar.json

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -858,29 +858,8 @@
858858
"value": "scissor"
859859
},
860860
{
861-
"type": "REPEAT1",
862-
"content": {
863-
"type": "CHOICE",
864-
"members": [
865-
{
866-
"type": "ALIAS",
867-
"content": {
868-
"type": "SYMBOL",
869-
"name": "_scissor_generated_comment"
870-
},
871-
"named": true,
872-
"value": "generated_comment"
873-
},
874-
{
875-
"type": "SYMBOL",
876-
"name": "diff"
877-
},
878-
{
879-
"type": "PATTERN",
880-
"value": "\\r?\\n"
881-
}
882-
]
883-
}
861+
"type": "SYMBOL",
862+
"name": "diff"
884863
}
885864
]
886865
},
@@ -927,25 +906,46 @@
927906
]
928907
},
929908
"diff": {
930-
"type": "SEQ",
931-
"members": [
932-
{
933-
"type": "PATTERN",
934-
"value": "[^#]"
935-
},
936-
{
937-
"type": "CHOICE",
938-
"members": [
939-
{
940-
"type": "PATTERN",
941-
"value": "[^\\n\\r]+"
909+
"type": "REPEAT1",
910+
"content": {
911+
"type": "CHOICE",
912+
"members": [
913+
{
914+
"type": "ALIAS",
915+
"content": {
916+
"type": "SYMBOL",
917+
"name": "_scissor_generated_comment"
942918
},
943-
{
944-
"type": "BLANK"
945-
}
946-
]
947-
}
948-
]
919+
"named": true,
920+
"value": "generated_comment"
921+
},
922+
{
923+
"type": "SEQ",
924+
"members": [
925+
{
926+
"type": "PATTERN",
927+
"value": "[^#]"
928+
},
929+
{
930+
"type": "CHOICE",
931+
"members": [
932+
{
933+
"type": "PATTERN",
934+
"value": "[^\\n\\r]+"
935+
},
936+
{
937+
"type": "BLANK"
938+
}
939+
]
940+
}
941+
]
942+
},
943+
{
944+
"type": "PATTERN",
945+
"value": "\\r?\\n"
946+
}
947+
]
948+
}
949949
},
950950
"rebase_command": {
951951
"type": "SEQ",

src/node-types.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,17 @@
4141
{
4242
"type": "diff",
4343
"named": true,
44-
"fields": {}
44+
"fields": {},
45+
"children": {
46+
"multiple": true,
47+
"required": false,
48+
"types": [
49+
{
50+
"type": "generated_comment",
51+
"named": true
52+
}
53+
]
54+
}
4555
},
4656
{
4757
"type": "generated_comment",

0 commit comments

Comments
 (0)