Skip to main content

oak_nginx/
lsp.rs

1use crate::{NginxLanguage, kind::NginxSyntaxKind};
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
8use std::future::Future;
9
10/// Hover provider implementation for Nginx.
11pub struct NginxHoverProvider;
12
13impl HoverProvider<NginxLanguage> for NginxHoverProvider {
14    fn hover(&self, root: &RedNode<'_, NginxLanguage>, _range: Range<usize>) -> Option<oak_hover::Hover> {
15        let kind = root.green.kind;
16
17        let contents = match kind {
18            NginxSyntaxKind::Directive => "### Nginx Directive\nA configuration instruction for the Nginx server.",
19            NginxSyntaxKind::Block => "### Nginx Block\nA group of directives enclosed in braces.",
20            NginxSyntaxKind::Root => "### Nginx Configuration\nThe root of an Nginx configuration file.",
21            _ => return None,
22        };
23
24        Some(oak_hover::Hover { contents: contents.to_string(), range: Some(root.span()) })
25    }
26}
27
28/// Language service implementation for Nginx.
29pub struct NginxLanguageService<V: Vfs> {
30    vfs: V,
31    workspace: oak_lsp::workspace::WorkspaceManager,
32    hover_provider: NginxHoverProvider,
33}
34
35impl<V: Vfs> NginxLanguageService<V> {
36    pub fn new(vfs: V) -> Self {
37        Self { vfs, workspace: oak_lsp::workspace::WorkspaceManager::default(), hover_provider: NginxHoverProvider }
38    }
39}
40
41impl<V: Vfs + Send + Sync + 'static + oak_vfs::WritableVfs> LanguageService for NginxLanguageService<V> {
42    type Lang = NginxLanguage;
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<'_, NginxLanguage>>> + Send + '_ {
54        async move { None }
55    }
56
57    fn hover(&self, uri: &str, range: Range<usize>) -> impl Future<Output = Option<Hover>> + Send + '_ {
58        let uri = uri.to_string();
59        async move {
60            let h = self.with_root(&uri, |root| self.hover_provider.hover(&root, range)).await.flatten()?;
61            Some(Hover { contents: h.contents, range: h.range })
62        }
63    }
64}