Skip to main content

code_moniker_core/lang/java/
mod.rs

1use tree_sitter::{Language, Parser, Tree};
2
3use crate::core::code_graph::CodeGraph;
4use crate::core::moniker::Moniker;
5use crate::core::shape::Shape;
6
7use crate::lang::KindSpec;
8
9mod kinds;
10pub mod pom_manifest;
11mod sdk_pipeline;
12
13#[derive(Clone, Debug, Default)]
14pub struct Presets {
15	pub external_packages: Vec<String>,
16}
17
18pub fn parse(source: &str) -> Tree {
19	let mut parser = Parser::new();
20	let language: Language = tree_sitter_java::LANGUAGE.into();
21	parser.set_language(&language).unwrap_or_else(|err| {
22		panic!("failed to load tree-sitter Java grammar: {err}");
23	});
24	parser.parse(source, None).unwrap_or_else(|| {
25		panic!("tree-sitter parse returned None on a non-cancelled call");
26	})
27}
28
29pub fn extract(
30	uri: &str,
31	source: &str,
32	anchor: &Moniker,
33	deep: bool,
34	presets: &Presets,
35) -> CodeGraph {
36	extract_sdk(uri, source, anchor, deep, presets)
37}
38
39pub fn extract_sdk(
40	uri: &str,
41	source: &str,
42	anchor: &Moniker,
43	deep: bool,
44	presets: &Presets,
45) -> CodeGraph {
46	sdk_pipeline::extract(uri, source, anchor, deep, presets)
47}
48
49pub struct Lang;
50
51const DEF_KINDS: &[&str] = &[
52	"class",
53	"interface",
54	"enum",
55	"record",
56	"annotation_type",
57	"method",
58	"constructor",
59	"field",
60	"enum_constant",
61];
62
63const DEF_KIND_SPECS: &[KindSpec] = &[
64	KindSpec::new("class", Shape::Type, 20, "class"),
65	KindSpec::new("interface", Shape::Type, 21, "interface"),
66	KindSpec::new("enum", Shape::Type, 22, "enum"),
67	KindSpec::new("record", Shape::Type, 23, "record"),
68	KindSpec::new("annotation_type", Shape::Type, 24, "annotation_type"),
69	KindSpec::new("enum_constant", Shape::Value, 30, "enum_constant"),
70	KindSpec::new("field", Shape::Value, 31, "field"),
71	KindSpec::new("constructor", Shape::Callable, 40, "constructor"),
72	KindSpec::new("method", Shape::Callable, 41, "method"),
73];
74
75impl crate::lang::LangExtractor for Lang {
76	type Presets = Presets;
77	const LANG_TAG: &'static str = "java";
78	const ALLOWED_KINDS: &'static [&'static str] = DEF_KINDS;
79	const KIND_SPECS: &'static [KindSpec] = DEF_KIND_SPECS;
80	const ALLOWED_VISIBILITIES: &'static [&'static str] =
81		&["public", "protected", "package", "private"];
82
83	fn extract(
84		uri: &str,
85		source: &str,
86		anchor: &Moniker,
87		deep: bool,
88		presets: &Self::Presets,
89	) -> CodeGraph {
90		extract(uri, source, anchor, deep, presets)
91	}
92}