Skip to content

Commit cd448bb

Browse files
baruchBaruchWeka
authored andcommitted
Add language support for D
Implement support in difftastic for the D programming language using the treesitter grammar provided in https://github.com/gdamore/tree-sitter-d
1 parent 6d80741 commit cd448bb

File tree

5 files changed

+283
-0
lines changed

5 files changed

+283
-0
lines changed

build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ fn main() {
8787
src_dir: "vendored_parsers/tree-sitter-commonlisp-src",
8888
extra_files: vec![],
8989
},
90+
TreeSitterParser {
91+
name: "tree-sitter-d",
92+
src_dir: "vendored_parsers/tree-sitter-d-src",
93+
extra_files: vec!["scanner.c"],
94+
},
9095
TreeSitterParser {
9196
name: "tree-sitter-dart",
9297
src_dir: "vendored_parsers/tree-sitter-dart-src",

src/parse/guess_language.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(crate) enum Language {
2828
CPlusPlus,
2929
CSharp,
3030
Css,
31+
D,
3132
Dart,
3233
DeviceTree,
3334
Elixir,
@@ -122,6 +123,7 @@ pub(crate) fn language_name(language: Language) -> &'static str {
122123
CPlusPlus => "C++",
123124
CSharp => "C#",
124125
Css => "CSS",
126+
D => "D",
125127
Dart => "Dart",
126128
DeviceTree => "Device Tree",
127129
Elixir => "Elixir",
@@ -252,6 +254,7 @@ pub(crate) fn language_globs(language: Language) -> Vec<glob::Pattern> {
252254
],
253255
CSharp => &["*.cs"],
254256
Css => &["*.css"],
257+
D => &["*.d", "*.di"],
255258
Dart => &["*.dart"],
256259
DeviceTree => &["*.dts", "*.dtsi", "*.dtso", "*.its"],
257260
Elm => &["*.elm"],

src/parse/tree_sitter_parser.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ extern "C" {
7474
fn tree_sitter_clojure() -> ts::Language;
7575
fn tree_sitter_cmake() -> ts::Language;
7676
fn tree_sitter_commonlisp() -> ts::Language;
77+
fn tree_sitter_d() -> ts::Language;
7778
fn tree_sitter_dart() -> ts::Language;
7879
fn tree_sitter_devicetree() -> ts::Language;
7980
fn tree_sitter_elisp() -> ts::Language;
@@ -278,6 +279,20 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
278279
sub_languages: vec![],
279280
}
280281
}
282+
D => {
283+
let language = unsafe { tree_sitter_d() };
284+
TreeSitterConfig {
285+
language: language.clone(),
286+
atom_nodes: ["string_literal", "char_literal"].into_iter().collect(),
287+
delimiter_tokens: vec![("(", ")"), ("{", "}"), ("[", "]")],
288+
highlight_query: ts::Query::new(
289+
&language,
290+
include_str!("../../vendored_parsers/highlights/d.scm"),
291+
)
292+
.unwrap(),
293+
sub_languages: vec![],
294+
}
295+
}
281296
Dart => {
282297
let language = unsafe { tree_sitter_dart() };
283298
TreeSitterConfig {

vendored_parsers/highlights/d.scm

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
; highlights.scm
2+
;
3+
; Highlighting queries for D code for use by Tree-Sitter.
4+
;
5+
; Copyright 2024 Garrett D'Amore
6+
;
7+
; Distributed under the MIT License.
8+
; (See accompanying file LICENSE.txt or https://opensource.org/licenses/MIT)
9+
; SPDX-License-Identifier: MIT
10+
11+
(string_literal) @string
12+
(int_literal) @number
13+
(float_literal) @number
14+
(char_literal) @number
15+
(identifier) @variable
16+
(at_attribute) @property
17+
(htmlentity) @string.special
18+
(escape_sequence) @string.escape
19+
20+
[
21+
(lazy)
22+
(align)
23+
(extern)
24+
(static)
25+
(abstract)
26+
(final)
27+
(override)
28+
(synchronized)
29+
(auto)
30+
(scope)
31+
(gshared)
32+
(ref)
33+
(deprecated)
34+
(nothrow)
35+
(pure)
36+
(type_ctor)
37+
] @keyword.storage
38+
39+
(parameter_attribute (return) @keyword.storage)
40+
(parameter_attribute (in) @keyword.storage)
41+
(parameter_attribute (out) @keyword.storage)
42+
43+
(function_declaration (identifier) @function)
44+
45+
(call_expression (identifier) @function)
46+
(call_expression (type (template_instance (identifier) @function)))
47+
(template_arguments (identifier) @variable.parameter)
48+
49+
(named_argument (identifier) @variable.parameter)
50+
51+
[
52+
(abstract)
53+
(alias)
54+
(align)
55+
(asm)
56+
(assert)
57+
(auto)
58+
(cast)
59+
(class)
60+
(const)
61+
(debug)
62+
(delegate)
63+
(delete)
64+
(deprecated)
65+
(enum)
66+
(export)
67+
(extern)
68+
(final)
69+
(function)
70+
(immutable)
71+
(import)
72+
(in)
73+
(inout)
74+
(interface)
75+
(invariant)
76+
(is)
77+
(lazy)
78+
; "macro" - obsolete
79+
(mixin)
80+
(module)
81+
(new)
82+
(nothrow)
83+
(out)
84+
(override)
85+
(package)
86+
(pragma)
87+
(private)
88+
(protected)
89+
(public)
90+
(pure)
91+
(ref)
92+
(scope)
93+
(shared)
94+
(static)
95+
(struct)
96+
(super)
97+
(synchronized)
98+
(template)
99+
(this)
100+
(throw)
101+
(typeid)
102+
(typeof)
103+
(union)
104+
(unittest)
105+
(version)
106+
(with)
107+
(gshared)
108+
(traits)
109+
(vector)
110+
(parameters_)
111+
] @keyword
112+
113+
[
114+
(break)
115+
(case)
116+
(catch)
117+
(continue)
118+
(do)
119+
(default)
120+
(finally)
121+
(else)
122+
(for)
123+
(foreach)
124+
(foreach_reverse)
125+
(goto)
126+
(if)
127+
(switch)
128+
(try)
129+
(return)
130+
(while)
131+
] @keyword.control
132+
133+
[
134+
(not_in)
135+
(not_is)
136+
"/="
137+
"/"
138+
".."
139+
"..."
140+
"&"
141+
"&="
142+
"&&"
143+
"|"
144+
"|="
145+
"||"
146+
"-"
147+
"-="
148+
"--"
149+
"+"
150+
"+="
151+
"++"
152+
"<"
153+
"<="
154+
"<<"
155+
"<<="
156+
">"
157+
">="
158+
">>="
159+
">>>="
160+
">>"
161+
">>>"
162+
"!"
163+
"!="
164+
"?"
165+
"$"
166+
"="
167+
"=="
168+
"*"
169+
"*="
170+
"%"
171+
"%="
172+
"^"
173+
"^="
174+
"^^"
175+
"^^="
176+
"~"
177+
"~="
178+
"@"
179+
"=>"
180+
] @operator
181+
182+
[
183+
";"
184+
"."
185+
":"
186+
","
187+
] @punctuation.delimiter
188+
189+
[
190+
"("
191+
")"
192+
"["
193+
"["
194+
"{"
195+
"}"
196+
] @punctuation.bracket
197+
198+
[
199+
(null)
200+
(true)
201+
(false)
202+
] @constant.language
203+
204+
(special_keyword) @constant.language
205+
206+
(directive) @keyword.directive
207+
(shebang) @keyword.directive
208+
209+
(comment) @comment
210+
211+
[
212+
(void)
213+
(bool)
214+
(byte)
215+
(ubyte)
216+
(char)
217+
(short)
218+
(ushort)
219+
(wchar)
220+
(dchar)
221+
(int)
222+
(uint)
223+
(long)
224+
(ulong)
225+
(real)
226+
(double)
227+
(float)
228+
(size_t)
229+
(ptrdiff_t)
230+
(string)
231+
(cstring)
232+
(wstring)
233+
(noreturn)
234+
] @type.builtin
235+
236+
[
237+
(cent)
238+
(ucent)
239+
(ireal)
240+
(idouble)
241+
(ifloat)
242+
(creal)
243+
(double)
244+
(cfloat)
245+
] @type.deprecated
246+
247+
(label (identifier) @label)
248+
(goto_statement (goto) @keyword.control (identifier) @label)
249+
250+
; this covers other cases where the identifier can only
251+
; be a type (such as in an is-expression on a constraint)
252+
(type (identifier) @type)
253+
254+
; these are listed last, because they override keyword queries
255+
(identity_expression (in) @operator)
256+
(identity_expression (is) @operator)
257+
258+
; everything after __EOF_ is plain text
259+
(end_file) @text

vendored_parsers/tree-sitter-d-src

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tree-sitter-d/src

0 commit comments

Comments
 (0)