1use crate::{ObjectiveCLanguage, kind::ObjectiveCSyntaxKind};
2use core::range::Range;
3use oak_core::tree::RedNode;
4use oak_hover::HoverProvider;
5use oak_lsp::{service::LanguageService, types::Hover};
6use oak_vfs::Vfs;
7use std::future::Future;
8
9pub struct ObjectiveCHoverProvider;
11
12impl HoverProvider<ObjectiveCLanguage> for ObjectiveCHoverProvider {
13 fn hover(&self, node: &RedNode<'_, ObjectiveCLanguage>, _range: Range<usize>) -> Option<oak_hover::Hover> {
14 let kind = node.green.kind;
15
16 let contents = match kind {
17 ObjectiveCSyntaxKind::InterfaceDeclaration => "### Objective-C @interface\nDefines the interface for a class.",
18 ObjectiveCSyntaxKind::ImplementationDeclaration => "### Objective-C @implementation\nProvides the implementation for a class.",
19 ObjectiveCSyntaxKind::ProtocolDeclaration => "### Objective-C @protocol\nDefines a set of methods that a class can implement.",
20 ObjectiveCSyntaxKind::PropertyDeclaration => "### Objective-C @property\nDeclares a property for a class.",
21 _ => return None,
22 };
23
24 Some(oak_hover::Hover { contents: contents.to_string(), range: Some(node.span()) })
25 }
26}
27
28pub struct ObjectiveCLanguageService<V: Vfs> {
30 vfs: V,
31 workspace: oak_lsp::workspace::WorkspaceManager,
32 hover_provider: ObjectiveCHoverProvider,
33}
34
35impl<V: Vfs> ObjectiveCLanguageService<V> {
36 pub fn new(vfs: V) -> Self {
37 Self { vfs, workspace: oak_lsp::workspace::WorkspaceManager::default(), hover_provider: ObjectiveCHoverProvider }
38 }
39}
40
41impl<V: Vfs + Send + Sync + 'static + oak_vfs::WritableVfs> LanguageService for ObjectiveCLanguageService<V> {
42 type Lang = ObjectiveCLanguage;
43 type Vfs = V;
44
45 fn vfs(&self) -> &Self::Vfs {
46 &self.vfs
47 }
48
49 fn workspace(&self) -> &oak_lsp::workspace::WorkspaceManager {
50 &self.workspace
51 }
52
53 fn get_root(&self, uri: &str) -> impl Future<Output = Option<RedNode<'_, ObjectiveCLanguage>>> + Send + '_ {
54 let source = self.get_source(uri);
55 async move {
56 let _source = source?;
57 None
59 }
60 }
61
62 fn hover(&self, uri: &str, range: Range<usize>) -> impl Future<Output = Option<Hover>> + Send + '_ {
63 let uri = uri.to_string();
64 async move {
65 let hover = self.with_root(&uri, |root| self.hover_provider.hover(&root, range)).await.flatten()?;
66 Some(Hover { contents: hover.contents, range: hover.range })
67 }
68 }
69}