Skip to main content

oak_nginx/lsp/
mod.rs

1#![doc = include_str!("readme.md")]
2#[cfg(feature = "oak-highlight")]
3pub mod highlighter;
4
5use crate::{NginxLanguage, parser::element_type::NginxElementType};
6use oak_core::{Range, tree::RedNode};
7#[cfg(feature = "lsp")]
8use {
9    oak_hover::HoverProvider,
10    oak_lsp::{service::LanguageService, types::Hover},
11    oak_vfs::Vfs,
12    std::future::Future,
13};
14/// Hover provider implementation for Nginx.
15#[cfg(feature = "lsp")]
16pub struct NginxHoverProvider;
17#[cfg(feature = "lsp")]
18impl HoverProvider<NginxLanguage> for NginxHoverProvider {
19    fn hover(&self, root: &RedNode<'_, NginxLanguage>, _range: Range<usize>) -> Option<oak_hover::Hover> {
20        let kind = root.green.kind;
21        let contents = match kind {
22            NginxElementType::Directive => "### Nginx Directive\nA configuration instruction for the Nginx server.",
23            NginxElementType::Block => "### Nginx Block\nA group of directives enclosed in braces.",
24            NginxElementType::Root => "### Nginx Configuration\nThe root of an Nginx configuration file.",
25            _ => return None,
26        };
27        Some(oak_hover::Hover { contents: contents.to_string(), range: Some(root.span()) })
28    }
29}
30/// Language service implementation for Nginx.
31#[cfg(feature = "lsp")]
32pub struct NginxLanguageService<V: Vfs> {
33    vfs: V,
34    workspace: oak_lsp::workspace::WorkspaceManager,
35    hover_provider: NginxHoverProvider,
36}
37impl<V: Vfs> NginxLanguageService<V> {
38    /// Creates a new Nginx language service with the given VFS.
39    pub fn new(vfs: V) -> Self {
40        Self { vfs, workspace: oak_lsp::workspace::WorkspaceManager::default(), hover_provider: NginxHoverProvider }
41    }
42}
43impl<V: Vfs + Send + Sync + 'static + oak_vfs::WritableVfs> LanguageService for NginxLanguageService<V> {
44    type Lang = NginxLanguage;
45    type Vfs = V;
46    fn vfs(&self) -> &Self::Vfs {
47        &self.vfs
48    }
49    fn workspace(&self) -> &oak_lsp::workspace::WorkspaceManager {
50        &self.workspace
51    }
52    fn get_root(&self, _uri: &str) -> impl Future<Output = Option<RedNode<'_, NginxLanguage>>> + Send + '_ {
53        async move { None }
54    }
55    fn hover(&self, uri: &str, range: Range<usize>) -> impl Future<Output = Option<Hover>> + Send + '_ {
56        let uri = uri.to_string();
57        async move {
58            let h = self.with_root(&uri, |root| self.hover_provider.hover(&root, range)).await.flatten()?;
59            Some(Hover { contents: h.contents, range: h.range })
60        }
61    }
62}