Skip to content

Commit c8ad3e6

Browse files
committed
Add Java support
1 parent 37a50d9 commit c8ad3e6

File tree

7 files changed

+264
-0
lines changed

7 files changed

+264
-0
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.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ tree-sitter-css = "<0.25.0"
4646
tree-sitter-go = "<0.25.0"
4747
tree-sitter-haskell = "<0.25.0"
4848
tree-sitter-html = "<0.25.0"
49+
tree-sitter-java = "0.23.5"
4950
tree-sitter-javascript = "<0.25.0"
5051
tree-sitter-php = "<0.24.0"
5152
tree-sitter-python = "<0.25.0"

crates/codebook/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ tree-sitter-css.workspace = true
2828
tree-sitter-go.workspace = true
2929
tree-sitter-haskell.workspace = true
3030
tree-sitter-html.workspace = true
31+
tree-sitter-java.workspace = true
3132
tree-sitter-javascript.workspace = true
3233
tree-sitter-php.workspace = true
3334
tree-sitter-python.workspace = true

crates/codebook/src/queries.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub enum LanguageType {
1010
Go,
1111
Haskell,
1212
HTML,
13+
Java,
1314
Javascript,
1415
Php,
1516
Python,
@@ -73,6 +74,13 @@ pub static LANGUAGE_SETTINGS: &[LanguageSetting] = &[
7374
query: include_str!("queries/python.scm"),
7475
extensions: &["py"],
7576
},
77+
LanguageSetting {
78+
type_: LanguageType::Java,
79+
ids: &["java"],
80+
dictionary_ids: &["java"],
81+
query: include_str!("queries/java.scm"),
82+
extensions: &["java"],
83+
},
7684
LanguageSetting {
7785
type_: LanguageType::Javascript,
7886
ids: &["javascript", "javascriptreact"],
@@ -172,6 +180,7 @@ impl LanguageSetting {
172180
LanguageType::Go => Some(tree_sitter_go::LANGUAGE.into()),
173181
LanguageType::Haskell => Some(tree_sitter_haskell::LANGUAGE.into()),
174182
LanguageType::HTML => Some(tree_sitter_html::LANGUAGE.into()),
183+
LanguageType::Java => Some(tree_sitter_java::LANGUAGE.into()),
175184
LanguageType::Javascript => Some(tree_sitter_javascript::LANGUAGE.into()),
176185
LanguageType::Php => Some(tree_sitter_php::LANGUAGE_PHP.into()),
177186
LanguageType::Python => Some(tree_sitter_python::LANGUAGE.into()),

crates/codebook/src/queries/java.scm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
(line_comment)
3+
(block_comment)
4+
] @comment
5+
[
6+
(character_literal)
7+
(string_literal)
8+
] @string
9+
(variable_declarator
10+
name: (identifier) @identifier)
11+
(interface_declaration
12+
name: (identifier) @identifier)
13+
(class_declaration
14+
name: (identifier) @identifier)
15+
(method_declaration
16+
name: (identifier) @identifier)
17+
(enum_declaration
18+
name: (identifier) @identifier)
19+
(enum_constant
20+
name: (identifier) @identifier)
21+
(formal_parameter
22+
name: (identifier) @identifier)
23+
(catch_formal_parameter
24+
name: (identifier) @identifier)

crates/codebook/tests/test_java.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
use codebook::{
2+
parser::{TextRange, WordLocation},
3+
queries::LanguageType,
4+
};
5+
6+
mod utils;
7+
8+
#[test]
9+
fn test_java_location() {
10+
utils::init_logging();
11+
let sample_text = r#"
12+
// Singl-line comment
13+
/* Blck comment */
14+
15+
interface ExamplInterface {
16+
void doSomethng();
17+
}
18+
19+
enum Statuss { ACTIV }
20+
21+
public class SoemJavaDemo implements ExamplInterface {
22+
23+
String messag = "Hello";
24+
25+
public void doSomethng(String smth) {
26+
System.out.println("Doing " + smth + "...");
27+
}
28+
29+
public static void main(String[] args) {
30+
try {
31+
int x = 1 / 0;
32+
} catch (ArithmeticException errorr) {
33+
System.out.println("Caught: " + errorr);
34+
some.recoveryMthod();
35+
}
36+
}
37+
}"#;
38+
39+
let expected = vec![
40+
WordLocation::new(
41+
"Singl".to_string(),
42+
vec![TextRange {
43+
start_char: 7,
44+
end_char: 12,
45+
line: 1,
46+
}],
47+
),
48+
WordLocation::new(
49+
"Blck".to_string(),
50+
vec![TextRange {
51+
start_char: 7,
52+
end_char: 11,
53+
line: 2,
54+
}],
55+
),
56+
WordLocation::new(
57+
"Exampl".to_string(),
58+
vec![TextRange {
59+
start_char: 14,
60+
end_char: 20,
61+
line: 4,
62+
}],
63+
),
64+
WordLocation::new(
65+
"Somethng".to_string(),
66+
vec![
67+
TextRange {
68+
start_char: 15,
69+
end_char: 23,
70+
line: 5,
71+
},
72+
TextRange {
73+
start_char: 22,
74+
end_char: 30,
75+
line: 14,
76+
},
77+
],
78+
),
79+
WordLocation::new(
80+
"Statuss".to_string(),
81+
vec![TextRange {
82+
start_char: 9,
83+
end_char: 16,
84+
line: 8,
85+
}],
86+
),
87+
WordLocation::new(
88+
"ACTIV".to_string(),
89+
vec![TextRange {
90+
start_char: 19,
91+
end_char: 24,
92+
line: 8,
93+
}],
94+
),
95+
WordLocation::new(
96+
"Soem".to_string(),
97+
vec![TextRange {
98+
start_char: 17,
99+
end_char: 21,
100+
line: 10,
101+
}],
102+
),
103+
WordLocation::new(
104+
"messag".to_string(),
105+
vec![TextRange {
106+
start_char: 15,
107+
end_char: 21,
108+
line: 12,
109+
}],
110+
),
111+
WordLocation::new(
112+
"smth".to_string(),
113+
vec![TextRange {
114+
start_char: 38,
115+
end_char: 42,
116+
line: 14,
117+
}],
118+
),
119+
WordLocation::new(
120+
"errorr".to_string(),
121+
vec![TextRange {
122+
start_char: 41,
123+
end_char: 47,
124+
line: 21,
125+
}],
126+
),
127+
];
128+
129+
let not_expected = [
130+
"interface",
131+
"void",
132+
"enum",
133+
"public",
134+
"class",
135+
"implements",
136+
"String",
137+
"System",
138+
"out",
139+
"println",
140+
"static",
141+
"main",
142+
"try",
143+
"catch",
144+
"ArithmeticException",
145+
"Hello",
146+
"Doing",
147+
"Caught",
148+
"Mthod",
149+
];
150+
151+
let processor = utils::get_processor();
152+
let misspelled = processor
153+
.spell_check(sample_text, Some(LanguageType::Java), None)
154+
.to_vec();
155+
156+
println!("Misspelled words: {misspelled:?}\n");
157+
158+
for e in &expected {
159+
println!("Expecting: {e:?}");
160+
let miss = misspelled
161+
.iter()
162+
.find(|r| r.word == e.word)
163+
.expect("Word not found");
164+
assert_eq!(miss.locations, e.locations);
165+
}
166+
167+
for result in misspelled {
168+
assert!(!not_expected.contains(&result.word.as_str()));
169+
}
170+
}

examples/example.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class example {
2+
// single line comment with a spelling mistake: helllo
3+
4+
/**
5+
* Block comment with a spelling mistake: byee
6+
*/
7+
8+
// variable declarators with spelling mistakes
9+
short myFavoritNum = 404;
10+
String myFavoiteString = "Hello, World!";
11+
12+
// a string with a spelling mistake
13+
String myStr = "foooooooooooooooood";
14+
15+
// enum declaration with a spelling mistake
16+
enum Levvel {
17+
// enum constant with a spelling mistake
18+
BEGINER,
19+
INTERMEDIETE,
20+
XPERT,
21+
}
22+
23+
// interface declaration with a spelling mistake
24+
public interface Innerexample {
25+
// method declaration with a spelling mistake
26+
void doSmething();
27+
}
28+
29+
// class declaration with a spelling mistake
30+
class PointlessClasss {
31+
}
32+
33+
public static void main(String[] args) {
34+
// catch formal parameter spelling mistake
35+
try {
36+
int result = 10 / 0;
37+
System.out.println(result);
38+
} catch (ArithmeticException uhoooh) {
39+
System.out.println(uhoooh);
40+
}
41+
}
42+
43+
// method declaration and formal parameter with spelling mistakes
44+
public String anthrMethod(String smth) {
45+
// string literal with a spelling mistake
46+
return "anthr method called with: " + smth;
47+
}
48+
}

0 commit comments

Comments
 (0)