1use crate::{OCamlLanguage, kind::OCamlSyntaxKind};
2use core::range::Range;
3use oak_core::tree::RedNode;
4use oak_hover::HoverProvider;
5use oak_lsp::{service::LanguageService, types::Hover};
6use oak_vfs::Vfs;
7
8pub struct OCamlHoverProvider;
10
11impl HoverProvider<OCamlLanguage> for OCamlHoverProvider {
12 fn hover(&self, node: &RedNode<OCamlLanguage>, _range: Range<usize>) -> Option<oak_hover::Hover> {
13 let kind = node.green.kind;
14
15 let contents = match kind {
16 OCamlSyntaxKind::LetBinding => "### OCaml Let Binding\nDefines a value or function binding.",
17 OCamlSyntaxKind::ModuleDef => "### OCaml Module\nDefines an OCaml module.",
18 OCamlSyntaxKind::TypeDefinition => "### OCaml Type\nDefines a new type.",
19 OCamlSyntaxKind::MatchExpr => "### OCaml Match\nPattern matching expression.",
20 _ => return None,
21 };
22
23 Some(oak_hover::Hover { contents: contents.to_string(), range: Some(node.span()) })
24 }
25}
26
27pub struct OCamlLanguageService<V: Vfs> {
29 vfs: V,
30 workspace: oak_lsp::workspace::WorkspaceManager,
31 hover_provider: OCamlHoverProvider,
32}
33
34impl<V: Vfs> OCamlLanguageService<V> {
35 pub fn new(vfs: V) -> Self {
36 Self { vfs, workspace: oak_lsp::workspace::WorkspaceManager::default(), hover_provider: OCamlHoverProvider }
37 }
38}
39
40impl<V: Vfs + Send + Sync + 'static + oak_vfs::WritableVfs> LanguageService for OCamlLanguageService<V> {
41 type Lang = OCamlLanguage;
42 type Vfs = V;
43
44 fn vfs(&self) -> &Self::Vfs {
45 &self.vfs
46 }
47
48 fn workspace(&self) -> &oak_lsp::workspace::WorkspaceManager {
49 &self.workspace
50 }
51
52 fn get_root(&self, _uri: &str) -> impl std::future::Future<Output = Option<RedNode<'_, OCamlLanguage>>> + Send + '_ {
53 async move { None }
54 }
55
56 fn hover(&self, uri: &str, range: Range<usize>) -> impl std::future::Future<Output = Option<Hover>> + Send + '_ {
57 let uri = uri.to_string();
58 async move { self.with_root(&uri, |root| self.hover_provider.hover(&root, range).map(|h| Hover { contents: h.contents, range: h.range })).await.flatten() }
59 }
60}