Skip to main content

oak_objective_c/lsp/
mod.rs

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