Skip to main content

oak_pascal/lsp/
mod.rs

1#![doc = include_str!("readme.md")]
2#[cfg(feature = "oak-highlight")]
3pub mod highlighter;
4
5use crate::{PascalElementType, PascalLanguage};
6use core::range::Range;
7use oak_core::tree::RedNode;
8#[cfg(feature = "lsp")]
9use oak_hover::HoverProvider;
10#[cfg(feature = "lsp")]
11use oak_lsp::{service::LanguageService, types::Hover};
12#[cfg(feature = "lsp")]
13use oak_vfs::Vfs;
14/// Hover provider implementation for Pascal.
15#[cfg(feature = "lsp")]
16pub struct PascalHoverProvider;
17#[cfg(feature = "lsp")]
18impl HoverProvider<PascalLanguage> for PascalHoverProvider {
19    fn hover(&self, node: &RedNode<PascalLanguage>, _range: Range<usize>) -> Option<oak_hover::Hover> {
20        let kind = node.green.kind;
21        let contents = match kind {
22            PascalElementType::Program => "### Pascal Program\nThe main entry point of the application.",
23            PascalElementType::Procedure => "### Pascal Procedure\nA subprogram that performs a specific task.",
24            PascalElementType::Function => "### Pascal Function\nA subprogram that returns a value.",
25            PascalElementType::VarSection => "### Variable Declaration Section\nDeclares variables for use in the program.",
26            _ => return None,
27        };
28        Some(oak_hover::Hover { contents: contents.to_string(), range: Some(node.span()) })
29    }
30}
31/// Language service implementation for Pascal.
32#[cfg(feature = "lsp")]
33pub struct PascalLanguageService<V: Vfs> {
34    /// The virtual file system.
35    pub vfs: V,
36    /// The workspace manager.
37    pub workspace: oak_lsp::workspace::WorkspaceManager,
38    /// The hover provider.
39    pub hover_provider: PascalHoverProvider,
40}
41impl<V: Vfs> PascalLanguageService<V> {
42    /// Creates a new Pascal language service.
43    pub fn new(vfs: V) -> Self {
44        Self { vfs, workspace: oak_lsp::workspace::WorkspaceManager::default(), hover_provider: PascalHoverProvider }
45    }
46}
47impl<V: Vfs + Send + Sync + 'static + oak_vfs::WritableVfs> LanguageService for PascalLanguageService<V> {
48    type Lang = PascalLanguage;
49    type Vfs = V;
50    fn vfs(&self) -> &Self::Vfs {
51        &self.vfs
52    }
53    fn workspace(&self) -> &oak_lsp::workspace::WorkspaceManager {
54        &self.workspace
55    }
56    fn get_root(&self, _uri: &str) -> impl std::future::Future<Output = Option<RedNode<'_, PascalLanguage>>> + Send + '_ {
57        async move { None }
58    }
59    fn hover(&self, uri: &str, range: Range<usize>) -> impl std::future::Future<Output = Option<Hover>> + Send + '_ {
60        let uri = uri.to_string();
61        async move { self.with_root(&uri, |root| self.hover_provider.hover(&root, range).map(|h| Hover { contents: h.contents, range: h.range })).await.flatten() }
62    }
63}