Author: Michael Davis [mcarsondavis@gmail.com]
Hash: c4896c818e9ae3bc95fc18874411dfeb824171c3
Timestamp: Wed, 02 Mar 2022 13:54:28 +0000 (2 years ago)

+100 -52 +/-14 browse
rebrand as tree-sitter-diff
1diff --git a/.gitignore b/.gitignore
2index e623bbb..1f11882 100644
3--- a/.gitignore
4+++ b/.gitignore
5 @@ -1,3 +1,4 @@
6 /node_modules/
7 /tmp/
8 /log.html
9+ /build/
10 diff --git a/Cargo.toml b/Cargo.toml
11index 4e07c5c..af0b147 100644
12--- a/Cargo.toml
13+++ b/Cargo.toml
14 @@ -1,10 +1,10 @@
15 [package]
16- name = "tree-sitter-gitdiff"
17- description = "gitdiff grammar for the tree-sitter parsing library"
18+ name = "tree-sitter-diff"
19+ description = "diff grammar for the tree-sitter parsing library"
20 version = "0.0.1"
21- keywords = ["incremental", "parsing", "gitdiff"]
22+ keywords = ["incremental", "parsing", "diff"]
23 categories = ["parsing", "text-editors"]
24- repository = "https://github.com/tree-sitter/tree-sitter-gitdiff"
25+ repository = "https://github.com/tree-sitter/tree-sitter-diff"
26 edition = "2018"
27 license = "MIT"
28
29 diff --git a/README.md b/README.md
30index c135929..2d758f6 100644
31--- a/README.md
32+++ b/README.md
33 @@ -1,30 +1,19 @@
34- # `tree-sitter-git-diff`
35+ # `tree-sitter-diff`
36
37- [![CI](https://github.com/the-mikedavis/tree-sitter-git-diff/actions/workflows/ci.yml/badge.svg)](https://github.com/the-mikedavis/tree-sitter-git-diff/actions/workflows/ci.yml)
38+ [![CI](ci-badge)](ci-workflow)
39
40- A [tree-sitter](https://tree-sitter.github.io/tree-sitter/) grammar for
41- `git diff`s.
42-
43- ### Status
44-
45- Working, but needs more testing.
46-
47- ### Examples
48+ _A [tree-sitter](tree-sitter) grammar for `diff`s_
49
50 Highlighting a `.diff` file:
51
52 <img src="assets/diff.png" width="500"/>
53
54- Injecting this grammar into [tree-sitter-git-commit](https://github.com/the-mikedavis/tree-sitter-git-commit) in a verbose commit (`git commit --verbose`):
55+ Injecting this grammar into [tree-sitter-git-commit](tree-sitter-git-commit)
56+ in a verbose commit (`git commit --verbose`):
57
58 <img src="assets/helix-commit-with-diff.png" width="500"/>
59
60- ### Design
61-
62- This grammar is designed to be line-based: each line is parsed as its own
63- top-level node in the `$.source`. It would probably be more useful to have
64- the grammar parse each diff section (starting with `diff --git`) as its own
65- subtree. As it is written now, though, the grammar can be injected into
66- tree-sitter-git-commit without using combined injections, which are less
67- supported among editors that use tree-sitter for highlighting. A future
68- revision of this parser may upend this design decision.
69+ [ci-badge]: https://github.com/the-mikedavis/tree-sitter-diff/actions/workflows/ci.yml/badge.svg
70+ [ci-workflow]: https://github.com/the-mikedavis/tree-sitter-diff/actions/workflows/ci.yml
71+ [tree-sitter]: https://tree-sitter.github.io/tree-sitter/
72+ [tree-sitter-git-commit]: https://github.com/the-mikedavis/tree-sitter-git-commit
73 diff --git a/binding.gyp b/binding.gyp
74index 4bd78e7..3a41509 100644
75--- a/binding.gyp
76+++ b/binding.gyp
77 @@ -1,7 +1,7 @@
78 {
79 "targets": [
80 {
81- "target_name": "tree_sitter_gitdiff_binding",
82+ "target_name": "tree_sitter_diff_binding",
83 "include_dirs": [
84 "<!(node -e \"require('nan')\")",
85 "src"
86 diff --git a/bindings/node/binding.cc b/bindings/node/binding.cc
87index fdf9373..76c9cff 100644
88--- a/bindings/node/binding.cc
89+++ b/bindings/node/binding.cc
90 @@ -4,7 +4,7 @@
91
92 using namespace v8;
93
94- extern "C" TSLanguage * tree_sitter_git_diff();
95+ extern "C" TSLanguage * tree_sitter_diff();
96
97 namespace {
98
99 @@ -17,12 +17,12 @@ void Init(Local<Object> exports, Local<Object> module) {
100
101 Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
102 Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
103- Nan::SetInternalFieldPointer(instance, 0, tree_sitter_git_diff());
104+ Nan::SetInternalFieldPointer(instance, 0, tree_sitter_diff());
105
106- Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("git_diff").ToLocalChecked());
107+ Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("diff").ToLocalChecked());
108 Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
109 }
110
111- NODE_MODULE(tree_sitter_git_diff_binding, Init)
112+ NODE_MODULE(tree_sitter_diff_binding, Init)
113
114 } // namespace
115 diff --git a/bindings/node/index.js b/bindings/node/index.js
116index 057ee90..9e3d716 100644
117--- a/bindings/node/index.js
118+++ b/bindings/node/index.js
119 @@ -1,11 +1,11 @@
120 try {
121- module.exports = require("../../build/Release/tree_sitter_git_diff_binding");
122+ module.exports = require("../../build/Release/tree_sitter_diff_binding");
123 } catch (error1) {
124 if (error1.code !== 'MODULE_NOT_FOUND') {
125 throw error1;
126 }
127 try {
128- module.exports = require("../../build/Debug/tree_sitter_git_diff_binding");
129+ module.exports = require("../../build/Debug/tree_sitter_diff_binding");
130 } catch (error2) {
131 if (error2.code !== 'MODULE_NOT_FOUND') {
132 throw error2;
133 diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs
134index 05d438e..adfc407 100644
135--- a/bindings/rust/lib.rs
136+++ b/bindings/rust/lib.rs
137 @@ -1,4 +1,4 @@
138- //! This crate provides git_diff language support for the [tree-sitter][] parsing library.
139+ //! This crate provides diff language support for the [tree-sitter][] parsing library.
140 //!
141 //! Typically, you will use the [language][language func] function to add this language to a
142 //! tree-sitter [Parser][], and then use the parser to parse some code:
143 @@ -6,7 +6,7 @@
144 //! ```
145 //! let code = "";
146 //! let mut parser = tree_sitter::Parser::new();
147- //! parser.set_language(tree_sitter_git_diff::language()).expect("Error loading git_diff grammar");
148+ //! parser.set_language(tree_sitter_diff::language()).expect("Error loading diff grammar");
149 //! let tree = parser.parse(code, None).unwrap();
150 //! ```
151 //!
152 @@ -18,14 +18,14 @@
153 use tree_sitter::Language;
154
155 extern "C" {
156- fn tree_sitter_git_diff() -> Language;
157+ fn tree_sitter_diff() -> Language;
158 }
159
160 /// Get the tree-sitter [Language][] for this grammar.
161 ///
162 /// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
163 pub fn language() -> Language {
164- unsafe { tree_sitter_git_diff() }
165+ unsafe { tree_sitter_diff() }
166 }
167
168 /// The content of the [`node-types.json`][] file for this grammar.
169 @@ -47,6 +47,6 @@ mod tests {
170 let mut parser = tree_sitter::Parser::new();
171 parser
172 .set_language(super::language())
173- .expect("Error loading git_diff language");
174+ .expect("Error loading diff language");
175 }
176 }
177 diff --git a/docs/playground.js b/docs/playground.js
178index 42b14a7..1d31e40 100644
179--- a/docs/playground.js
180+++ b/docs/playground.js
181 @@ -82,7 +82,7 @@ let tree;
182 async function handleLanguageChange() {
183 const newLanguageName = languageSelect.value;
184 if (!languagesByName[newLanguageName]) {
185- const url = 'https://raw.githubusercontent.com/the-mikedavis/tree-sitter-git-diff/main/tree-sitter-git_diff.wasm'
186+ const url = 'https://raw.githubusercontent.com/the-mikedavis/tree-sitter-diff/main/tree-sitter-diff.wasm'
187 languageSelect.disabled = true;
188 try {
189 languagesByName[newLanguageName] = await TreeSitter.Language.load(url);
190 diff --git a/grammar.js b/grammar.js
191index 21d8bc8..7169e19 100644
192--- a/grammar.js
193+++ b/grammar.js
194 @@ -3,7 +3,7 @@ const WHITE_SPACE = /[\t\f\v ]+/;
195 const ANYTHING = /[^\r\n]+/;
196
197 module.exports = grammar({
198- name: "git_diff",
199+ name: "diff",
200
201 extras: ($) => [WHITE_SPACE],
202
203 @@ -26,6 +26,7 @@ module.exports = grammar({
204 $.context
205 ),
206
207+ // FIXME: remove git assumption
208 command: ($) => iseq("diff", "--git", $.filename),
209
210 file_change: ($) =>
211 diff --git a/package-lock.json b/package-lock.json
212index 5acd075..f560f53 100644
213--- a/package-lock.json
214+++ b/package-lock.json
215 @@ -1,5 +1,5 @@
216 {
217- "name": "tree-sitter-gitdiff",
218+ "name": "tree-sitter-diff",
219 "version": "0.0.1",
220 "lockfileVersion": 1,
221 "requires": true,
222 @@ -16,9 +16,9 @@
223 "dev": true
224 },
225 "tree-sitter-cli": {
226- "version": "0.20.1",
227- "resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.20.1.tgz",
228- "integrity": "sha512-I0Gp4ThRp39TDnBAaZKiogvoE85MSeL6/ILZMXbzeEo8hUsudpVhEHRE4CU+Bk5QUaiMiDkD+ZIL3gT2zZ++wg==",
229+ "version": "0.20.4",
230+ "resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.20.4.tgz",
231+ "integrity": "sha512-G42x0Ev7mxA8WLUfZY+two5LIhPf6R/m7qDZtKxOzE77zXi6didNI/vf17kHaKaRXJrWnyCxHFaVQFO2LL81yg==",
232 "dev": true
233 }
234 }
235 diff --git a/package.json b/package.json
236index 1eb5ed7..22bf32b 100644
237--- a/package.json
238+++ b/package.json
239 @@ -1,7 +1,7 @@
240 {
241- "name": "tree-sitter-gitdiff",
242+ "name": "tree-sitter-diff",
243 "version": "0.0.1",
244- "description": "A tree-sitter grammar for git diffs",
245+ "description": "A tree-sitter grammar for diffs",
246 "main": "bindings/node",
247 "scripts": {
248 "test": "tree-sitter test",
249 @@ -10,32 +10,32 @@
250 },
251 "repository": {
252 "type": "git",
253- "url": "git+https://github.com/the-mikedavis/tree-sitter-gitdiff.git"
254+ "url": "git+https://github.com/the-mikedavis/tree-sitter-diff.git"
255 },
256 "keywords": [
257 "tree-sitter",
258 "parser",
259 "lexer",
260- "git",
261 "diff"
262 ],
263 "author": "the-mikedavis",
264 "license": "MIT",
265 "bugs": {
266- "url": "https://github.com/the-mikedavis/tree-sitter-gitdiff/issues"
267+ "url": "https://github.com/the-mikedavis/tree-sitter-diff/issues"
268 },
269- "homepage": "https://github.com/the-mikedavis/tree-sitter-gitdiff#readme",
270+ "homepage": "https://github.com/the-mikedavis/tree-sitter-diff#readme",
271 "dependencies": {
272 "nan": "^2.15.0"
273 },
274 "devDependencies": {
275 "prettier": "^2.5.1",
276- "tree-sitter-cli": "^0.20.1"
277+ "tree-sitter-cli": "^0.20.4"
278 },
279 "tree-sitter": [
280 {
281 "file-types": [
282- "diff"
283+ "diff",
284+ "patch"
285 ]
286 }
287 ]
288 diff --git a/src/grammar.json b/src/grammar.json
289index cc01a75..d9bb8e5 100644
290--- a/src/grammar.json
291+++ b/src/grammar.json
292 @@ -1,5 +1,5 @@
293 {
294- "name": "git_diff",
295+ "name": "diff",
296 "rules": {
297 "source": {
298 "type": "SEQ",
299 diff --git a/src/parser.c b/src/parser.c
300index f450a4c..6ccd4cc 100644
301--- a/src/parser.c
302+++ b/src/parser.c
303 @@ -375,6 +375,62 @@ static const uint16_t ts_non_terminal_alias_map[] = {
304 0,
305 };
306
307+ static const TSStateId ts_primary_state_ids[STATE_COUNT] = {
308+ [0] = 0,
309+ [1] = 1,
310+ [2] = 2,
311+ [3] = 3,
312+ [4] = 4,
313+ [5] = 5,
314+ [6] = 6,
315+ [7] = 7,
316+ [8] = 8,
317+ [9] = 9,
318+ [10] = 10,
319+ [11] = 11,
320+ [12] = 6,
321+ [13] = 13,
322+ [14] = 14,
323+ [15] = 15,
324+ [16] = 5,
325+ [17] = 17,
326+ [18] = 18,
327+ [19] = 6,
328+ [20] = 5,
329+ [21] = 21,
330+ [22] = 22,
331+ [23] = 23,
332+ [24] = 24,
333+ [25] = 25,
334+ [26] = 26,
335+ [27] = 27,
336+ [28] = 28,
337+ [29] = 29,
338+ [30] = 30,
339+ [31] = 31,
340+ [32] = 32,
341+ [33] = 33,
342+ [34] = 34,
343+ [35] = 35,
344+ [36] = 36,
345+ [37] = 37,
346+ [38] = 38,
347+ [39] = 39,
348+ [40] = 40,
349+ [41] = 41,
350+ [42] = 42,
351+ [43] = 43,
352+ [44] = 44,
353+ [45] = 45,
354+ [46] = 46,
355+ [47] = 47,
356+ [48] = 48,
357+ [49] = 49,
358+ [50] = 50,
359+ [51] = 51,
360+ [52] = 52,
361+ };
362+
363 static bool ts_lex(TSLexer *lexer, TSStateId state) {
364 START_LEXER();
365 eof = lexer->eof(lexer);
366 @@ -2193,7 +2249,7 @@ extern "C" {
367 #define extern __declspec(dllexport)
368 #endif
369
370- extern const TSLanguage *tree_sitter_git_diff(void) {
371+ extern const TSLanguage *tree_sitter_diff(void) {
372 static const TSLanguage language = {
373 .version = LANGUAGE_VERSION,
374 .symbol_count = SYMBOL_COUNT,
375 diff --git a/src/tree_sitter/parser.h b/src/tree_sitter/parser.h
376index cbbc7b4..2b14ac1 100644
377--- a/src/tree_sitter/parser.h
378+++ b/src/tree_sitter/parser.h
379 @@ -123,6 +123,7 @@ struct TSLanguage {
380 unsigned (*serialize)(void *, char *);
381 void (*deserialize)(void *, const char *, unsigned);
382 } external_scanner;
383+ const TSStateId *primary_state_ids;
384 };
385
386 /*