Skip to content

Add Java support #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ tree-sitter-css = "<0.25.0"
tree-sitter-go = "<0.25.0"
tree-sitter-haskell = "<0.25.0"
tree-sitter-html = "<0.25.0"
tree-sitter-java = "0.23.5"
tree-sitter-javascript = "<0.25.0"
tree-sitter-php = "<0.24.0"
tree-sitter-python = "<0.25.0"
Expand Down
1 change: 1 addition & 0 deletions crates/codebook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tree-sitter-css.workspace = true
tree-sitter-go.workspace = true
tree-sitter-haskell.workspace = true
tree-sitter-html.workspace = true
tree-sitter-java.workspace = true
tree-sitter-javascript.workspace = true
tree-sitter-php.workspace = true
tree-sitter-python.workspace = true
Expand Down
9 changes: 9 additions & 0 deletions crates/codebook/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum LanguageType {
Go,
Haskell,
HTML,
Java,
Javascript,
Php,
Python,
Expand Down Expand Up @@ -73,6 +74,13 @@ pub static LANGUAGE_SETTINGS: &[LanguageSetting] = &[
query: include_str!("queries/python.scm"),
extensions: &["py"],
},
LanguageSetting {
type_: LanguageType::Java,
ids: &["java"],
dictionary_ids: &["java"],
query: include_str!("queries/java.scm"),
extensions: &["java"],
},
LanguageSetting {
type_: LanguageType::Javascript,
ids: &["javascript", "javascriptreact"],
Expand Down Expand Up @@ -172,6 +180,7 @@ impl LanguageSetting {
LanguageType::Go => Some(tree_sitter_go::LANGUAGE.into()),
LanguageType::Haskell => Some(tree_sitter_haskell::LANGUAGE.into()),
LanguageType::HTML => Some(tree_sitter_html::LANGUAGE.into()),
LanguageType::Java => Some(tree_sitter_java::LANGUAGE.into()),
LanguageType::Javascript => Some(tree_sitter_javascript::LANGUAGE.into()),
LanguageType::Php => Some(tree_sitter_php::LANGUAGE_PHP.into()),
LanguageType::Python => Some(tree_sitter_python::LANGUAGE.into()),
Expand Down
24 changes: 24 additions & 0 deletions crates/codebook/src/queries/java.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
(line_comment)
(block_comment)
] @comment
[
(character_literal)
(string_literal)
] @string
(variable_declarator
name: (identifier) @identifier)
(interface_declaration
name: (identifier) @identifier)
(class_declaration
name: (identifier) @identifier)
(method_declaration
name: (identifier) @identifier)
(enum_declaration
name: (identifier) @identifier)
(enum_constant
name: (identifier) @identifier)
(formal_parameter
name: (identifier) @identifier)
(catch_formal_parameter
name: (identifier) @identifier)
170 changes: 170 additions & 0 deletions crates/codebook/tests/test_java.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
use codebook::{
parser::{TextRange, WordLocation},
queries::LanguageType,
};

mod utils;

#[test]
fn test_java_location() {
utils::init_logging();
let sample_text = r#"
// Singl-line comment
/* Blck comment */

interface ExamplInterface {
void doSomethng();
}

enum Statuss { ACTIV }

public class SoemJavaDemo implements ExamplInterface {

String messag = "Hello";

public void doSomethng(String smth) {
System.out.println("Doing " + smth + "...");
}

public static void main(String[] args) {
try {
int x = 1 / 0;
} catch (ArithmeticException errorr) {
System.out.println("Caught: " + errorr);
some.recoveryMthod();
}
}
}"#;

let expected = vec![
WordLocation::new(
"Singl".to_string(),
vec![TextRange {
start_char: 7,
end_char: 12,
line: 1,
}],
),
WordLocation::new(
"Blck".to_string(),
vec![TextRange {
start_char: 7,
end_char: 11,
line: 2,
}],
),
WordLocation::new(
"Exampl".to_string(),
vec![TextRange {
start_char: 14,
end_char: 20,
line: 4,
}],
),
WordLocation::new(
"Somethng".to_string(),
vec![
TextRange {
start_char: 15,
end_char: 23,
line: 5,
},
TextRange {
start_char: 22,
end_char: 30,
line: 14,
},
],
),
WordLocation::new(
"Statuss".to_string(),
vec![TextRange {
start_char: 9,
end_char: 16,
line: 8,
}],
),
WordLocation::new(
"ACTIV".to_string(),
vec![TextRange {
start_char: 19,
end_char: 24,
line: 8,
}],
),
WordLocation::new(
"Soem".to_string(),
vec![TextRange {
start_char: 17,
end_char: 21,
line: 10,
}],
),
WordLocation::new(
"messag".to_string(),
vec![TextRange {
start_char: 15,
end_char: 21,
line: 12,
}],
),
WordLocation::new(
"smth".to_string(),
vec![TextRange {
start_char: 38,
end_char: 42,
line: 14,
}],
),
WordLocation::new(
"errorr".to_string(),
vec![TextRange {
start_char: 41,
end_char: 47,
line: 21,
}],
),
];

let not_expected = [
"interface",
"void",
"enum",
"public",
"class",
"implements",
"String",
"System",
"out",
"println",
"static",
"main",
"try",
"catch",
"ArithmeticException",
"Hello",
"Doing",
"Caught",
"Mthod",
];

let processor = utils::get_processor();
let misspelled = processor
.spell_check(sample_text, Some(LanguageType::Java), None)
.to_vec();

println!("Misspelled words: {misspelled:?}\n");

for e in &expected {
println!("Expecting: {e:?}");
let miss = misspelled
.iter()
.find(|r| r.word == e.word)
.expect("Word not found");
assert_eq!(miss.locations, e.locations);
}

for result in misspelled {
assert!(!not_expected.contains(&result.word.as_str()));
}
}
48 changes: 48 additions & 0 deletions examples/example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
public class example {
// single line comment with a spelling mistake: helllo

/**
* Block comment with a spelling mistake: byee
*/

// variable declarators with spelling mistakes
short myFavoritNum = 404;
String myFavoiteString = "Hello, World!";

// a string with a spelling mistake
String myStr = "foooooooooooooooood";

// enum declaration with a spelling mistake
enum Levvel {
// enum constant with a spelling mistake
BEGINER,
INTERMEDIETE,
XPERT,
}

// interface declaration with a spelling mistake
public interface Innerexample {
// method declaration with a spelling mistake
void doSmething();
}

// class declaration with a spelling mistake
class PointlessClasss {
}

public static void main(String[] args) {
// catch formal parameter spelling mistake
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException uhoooh) {
System.out.println(uhoooh);
}
}

// method declaration and formal parameter with spelling mistakes
public String anthrMethod(String smth) {
// string literal with a spelling mistake
return "anthr method called with: " + smth;
}
}