Skip to content

Commit 0afcede

Browse files
beebHerringtonDarkholme
authored andcommitted
feat(language): add solidity support
1 parent 9b0fcb1 commit 0afcede

File tree

5 files changed

+105
-30
lines changed

5 files changed

+105
-30
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/language/Cargo.toml

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ serde.workspace = true
1818
tree-sitter.workspace = true
1919

2020
tree-sitter-bash = { version = "0.25.0", optional = true }
21-
tree-sitter-c = { version = "0.24.0", optional = true }
2221
tree-sitter-cpp = { version = "0.23.0", optional = true }
2322
tree-sitter-c-sharp = { version = "0.23.0", optional = true }
2423
tree-sitter-css = { version = "0.23.2", optional = true }
24+
tree-sitter-c = { version = "0.24.0", optional = true }
2525
tree-sitter-elixir = { version = "0.3.0", optional = true }
2626
tree-sitter-go = { version = "0.23.0", optional = true }
2727
tree-sitter-haskell = { version = "0.23.0", optional = true }
2828
tree-sitter-html = { version = "0.23.0", optional = true }
29-
tree-sitter-java = { version = "0.23.0", optional = true }
3029
tree-sitter-javascript = { version = "0.23.0", optional = true }
30+
tree-sitter-java = { version = "0.23.0", optional = true }
3131
tree-sitter-json = { version = "0.23.0", optional = true }
3232
tree-sitter-kotlin = { version = "0.4.0", optional = true, package = "tree-sitter-kotlin-sg" }
3333
tree-sitter-lua = { version = "0.2.0", optional = true }
@@ -36,39 +36,41 @@ tree-sitter-python = { version = "0.23.0", optional = true }
3636
tree-sitter-ruby = { version = "0.23.0", optional = true }
3737
tree-sitter-rust = { version = "0.24.0", optional = true }
3838
tree-sitter-scala = { version = "0.24.0", optional = true }
39+
tree-sitter-solidity = { version = "1.2.11", optional = true }
3940
tree-sitter-swift = { version = "0.7.0", optional = true }
4041
tree-sitter-typescript = { version = "0.23.2", optional = true }
4142
tree-sitter-yaml = { version = "0.7.0", optional = true }
4243

4344
[features]
4445
builtin-parser = [
45-
"tree-sitter-bash",
46-
"tree-sitter-c",
47-
"tree-sitter-cpp",
48-
"tree-sitter-c-sharp",
49-
"tree-sitter-css",
50-
"tree-sitter-elixir",
51-
"tree-sitter-go",
52-
"tree-sitter-haskell",
53-
"tree-sitter-html",
54-
"tree-sitter-java",
55-
"tree-sitter-javascript",
56-
"tree-sitter-json",
57-
"tree-sitter-kotlin",
58-
"tree-sitter-lua",
59-
"tree-sitter-php",
60-
"tree-sitter-python",
61-
"tree-sitter-ruby",
62-
"tree-sitter-rust",
63-
"tree-sitter-scala",
64-
"tree-sitter-swift",
65-
"tree-sitter-typescript",
66-
"tree-sitter-yaml",
46+
"tree-sitter-bash",
47+
"tree-sitter-c",
48+
"tree-sitter-cpp",
49+
"tree-sitter-c-sharp",
50+
"tree-sitter-css",
51+
"tree-sitter-elixir",
52+
"tree-sitter-go",
53+
"tree-sitter-haskell",
54+
"tree-sitter-html",
55+
"tree-sitter-java",
56+
"tree-sitter-javascript",
57+
"tree-sitter-json",
58+
"tree-sitter-kotlin",
59+
"tree-sitter-lua",
60+
"tree-sitter-php",
61+
"tree-sitter-python",
62+
"tree-sitter-ruby",
63+
"tree-sitter-rust",
64+
"tree-sitter-scala",
65+
"tree-sitter-solidity",
66+
"tree-sitter-swift",
67+
"tree-sitter-typescript",
68+
"tree-sitter-yaml",
6769
]
6870
napi-lang = [
69-
"tree-sitter-css",
70-
"tree-sitter-html",
71-
"tree-sitter-javascript",
72-
"tree-sitter-typescript",
71+
"tree-sitter-css",
72+
"tree-sitter-html",
73+
"tree-sitter-javascript",
74+
"tree-sitter-typescript",
7375
]
74-
default = ["builtin-parser"]
76+
default = ["builtin-parser"]

crates/language/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod python;
2424
mod ruby;
2525
mod rust;
2626
mod scala;
27+
mod solidity;
2728
mod swift;
2829
mod yaml;
2930

@@ -227,6 +228,7 @@ impl_lang!(JavaScript, language_javascript);
227228
impl_lang!(Json, language_json);
228229
impl_lang!(Lua, language_lua);
229230
impl_lang!(Scala, language_scala);
231+
impl_lang!(Solidity, language_solidity);
230232
impl_lang!(Tsx, language_tsx);
231233
impl_lang!(TypeScript, language_typescript);
232234
impl_lang!(Yaml, language_yaml);
@@ -255,6 +257,7 @@ pub enum SupportLang {
255257
Ruby,
256258
Rust,
257259
Scala,
260+
Solidity,
258261
Swift,
259262
Tsx,
260263
TypeScript,
@@ -266,7 +269,7 @@ impl SupportLang {
266269
use SupportLang::*;
267270
&[
268271
Bash, C, Cpp, CSharp, Css, Elixir, Go, Haskell, Html, Java, JavaScript, Json, Kotlin, Lua,
269-
Php, Python, Ruby, Rust, Scala, Swift, Tsx, TypeScript, Yaml,
272+
Php, Python, Ruby, Rust, Scala, Solidity, Swift, Tsx, TypeScript, Yaml,
270273
]
271274
}
272275

@@ -366,6 +369,7 @@ impl_aliases! {
366369
Ruby => &["rb", "ruby"],
367370
Rust => &["rs", "rust"],
368371
Scala => &["scala"],
372+
Solidity => &["sol", "solidity"],
369373
Swift => &["swift"],
370374
TypeScript => &["ts", "typescript"],
371375
Tsx => &["tsx"],
@@ -410,6 +414,7 @@ macro_rules! execute_lang_method {
410414
S::Ruby => Ruby.$method($($pname,)*),
411415
S::Rust => Rust.$method($($pname,)*),
412416
S::Scala => Scala.$method($($pname,)*),
417+
S::Solidity => Solidity.$method($($pname,)*),
413418
S::Swift => Swift.$method($($pname,)*),
414419
S::Tsx => Tsx.$method($($pname,)*),
415420
S::TypeScript => TypeScript.$method($($pname,)*),
@@ -479,6 +484,7 @@ fn extensions(lang: SupportLang) -> &'static [&'static str] {
479484
Ruby => &["rb", "rbw", "gemspec"],
480485
Rust => &["rs"],
481486
Scala => &["scala", "sc", "sbt"],
487+
Solidity => &["sol"],
482488
Swift => &["swift"],
483489
TypeScript => &["ts", "cts", "mts"],
484490
Tsx => &["tsx"],

crates/language/src/parsers.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ pub fn language_rust() -> TSLanguage {
100100
pub fn language_scala() -> TSLanguage {
101101
into_lang!(tree_sitter_scala)
102102
}
103+
pub fn language_solidity() -> TSLanguage {
104+
into_lang!(tree_sitter_solidity)
105+
}
103106
pub fn language_swift() -> TSLanguage {
104107
into_lang!(tree_sitter_swift)
105108
}

crates/language/src/solidity.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#![cfg(test)]
2+
use super::*;
3+
4+
fn test_match(query: &str, source: &str) {
5+
use crate::test::test_match_lang;
6+
test_match_lang(query, source, Solidity);
7+
}
8+
9+
fn test_non_match(query: &str, source: &str) {
10+
use crate::test::test_non_match_lang;
11+
test_non_match_lang(query, source, Solidity);
12+
}
13+
14+
#[test]
15+
fn test_solidity_str() {
16+
test_match("pragma solidity 0.8.28;", "pragma solidity 0.8.28;");
17+
test_match(
18+
r#"import { Test } from "forge-std/Test.sol";"#,
19+
r#"import { Test } from "forge-std/Test.sol";"#,
20+
);
21+
test_non_match("pragma solidity 0.8.28;", "pragma solidity 0.8.26;");
22+
test_non_match(
23+
r#"import { Test } from "forge-std/Test.sol";"#,
24+
r#"import { console } from "forge-std/Test.sol";"#,
25+
);
26+
}
27+
28+
#[test]
29+
fn test_solidity_pattern() {
30+
test_match(
31+
r#"import { $A } from "forge-std/Test.sol";"#,
32+
r#"import { Test } from "forge-std/Test.sol";"#,
33+
);
34+
test_match(
35+
r#"import { $$$ } from "forge-std/Test.sol";"#,
36+
r#"import { Test, console } from "forge-std/Test.sol";"#,
37+
);
38+
}
39+
40+
fn test_replace(src: &str, pattern: &str, replacer: &str) -> String {
41+
use crate::test::test_replace_lang;
42+
test_replace_lang(src, pattern, replacer, Solidity)
43+
}
44+
45+
#[test]
46+
fn test_solidity_replace() {
47+
let ret = test_replace(
48+
r#"import { Test } from "forge-std/Test.sol";"#,
49+
"Test",
50+
"console",
51+
);
52+
assert_eq!(ret, r#"import { console } from "forge-std/Test.sol";"#);
53+
}

0 commit comments

Comments
 (0)