Skip to main content

oak_pascal/
lsp.rs

1use crate::{PascalLanguage, kind::PascalSyntaxKind};
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
8/// Hover provider implementation for Pascal.
9pub struct PascalHoverProvider;
10
11impl HoverProvider<PascalLanguage> for PascalHoverProvider {
12    fn hover(&self, node: &RedNode<PascalLanguage>, _range: Range<usize>) -> Option<oak_hover::Hover> {
13        let kind = node.green.kind;
14
15        let contents = match kind {
16            PascalSyntaxKind::ProgramBlock => "### Pascal Program\nThe main entry point of a Pascal source file.",
17            PascalSyntaxKind::ProcedureDef => "### Pascal Procedure\nA subprogram that performs a specific task.",
18            PascalSyntaxKind::FunctionDef => "### Pascal Function\nA subprogram that returns a value.",
19            PascalSyntaxKind::VarSection => "### Variable Declaration Section\nDeclares variables for use in the program.",
20            _ => return None,
21        };
22
23        Some(oak_hover::Hover { contents: contents.to_string(), range: Some(node.span()) })
24    }
25}
26
27/// Language service implementation for Pascal.
28pub struct PascalLanguageService<V: Vfs> {
29    vfs: V,
30    workspace: oak_lsp::workspace::WorkspaceManager,
31    hover_provider: PascalHoverProvider,
32}
33
34impl<V: Vfs> PascalLanguageService<V> {
35    pub fn new(vfs: V) -> Self {
36        Self { vfs, workspace: oak_lsp::workspace::WorkspaceManager::default(), hover_provider: PascalHoverProvider }
37    }
38}
39
40impl<V: Vfs + Send + Sync + 'static + oak_vfs::WritableVfs> LanguageService for PascalLanguageService<V> {
41    type Lang = PascalLanguage;
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<'_, PascalLanguage>>> + 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}