1use std::{any::TypeId, sync::Arc};
4
5use {
6 reovim_core::{
7 event_bus::EventBus,
8 plugin::{Plugin, PluginContext, PluginId, PluginStateRegistry},
9 },
10 reovim_plugin_treesitter::{LanguageSupport, RegisterLanguage, TreesitterPlugin},
11};
12
13pub struct RustLanguage;
15
16impl LanguageSupport for RustLanguage {
17 fn language_id(&self) -> &'static str {
18 "rust"
19 }
20
21 fn file_extensions(&self) -> &'static [&'static str] {
22 &["rs"]
23 }
24
25 fn tree_sitter_language(&self) -> reovim_plugin_treesitter::Language {
26 tree_sitter_rust::LANGUAGE.into()
27 }
28
29 fn highlights_query(&self) -> &'static str {
30 include_str!("queries/highlights.scm")
31 }
32
33 fn folds_query(&self) -> Option<&'static str> {
34 Some(include_str!("queries/folds.scm"))
35 }
36
37 fn textobjects_query(&self) -> Option<&'static str> {
38 Some(include_str!("queries/textobjects.scm"))
39 }
40
41 fn injections_query(&self) -> Option<&'static str> {
42 Some(include_str!("queries/injections.scm"))
43 }
44
45 fn context_query(&self) -> Option<&'static str> {
46 Some(include_str!("queries/context.scm"))
47 }
48}
49
50pub struct RustPlugin;
52
53impl Plugin for RustPlugin {
54 fn id(&self) -> PluginId {
55 PluginId::new("reovim:lang-rust")
56 }
57
58 fn name(&self) -> &'static str {
59 "Rust Language"
60 }
61
62 fn description(&self) -> &'static str {
63 "Rust language support with syntax highlighting and semantic analysis"
64 }
65
66 fn dependencies(&self) -> Vec<TypeId> {
67 vec![TypeId::of::<TreesitterPlugin>()]
68 }
69
70 fn build(&self, _ctx: &mut PluginContext) {
71 }
73
74 fn subscribe(&self, bus: &EventBus, _state: Arc<PluginStateRegistry>) {
75 bus.emit(RegisterLanguage {
77 language: Arc::new(RustLanguage),
78 });
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use {super::*, reovim_plugin_treesitter::LanguageSupport};
85
86 #[test]
87 fn test_highlights_query_compiles() {
88 let lang = RustLanguage;
89 let ts_lang = lang.tree_sitter_language();
90 let query_src = lang.highlights_query();
91
92 let query = tree_sitter::Query::new(&ts_lang, query_src);
93 assert!(query.is_ok(), "Rust highlights query should compile: {:?}", query.err());
94
95 let q = query.unwrap();
96 println!("Capture count: {}", q.capture_names().len());
97 println!("Pattern count: {}", q.pattern_count());
98
99 for name in q.capture_names() {
101 println!("Capture: {}", name);
102 }
103 }
104}