Skip to main content

oak_protobuf/
lsp.rs

1use crate::{kind::ProtobufSyntaxKind, language::ProtobufLanguage};
2use oak_core::{Range, tree::RedNode};
3use oak_lsp::{service::LanguageService, types::Hover};
4use oak_vfs::Vfs;
5use std::future::Future;
6
7/// Hover provider implementation for Protobuf.
8pub struct ProtobufHoverProvider;
9
10impl ProtobufHoverProvider {
11    fn hover(&self, node: &RedNode<ProtobufLanguage>, _range: Range<usize>) -> Option<Hover> {
12        let kind = node.green.kind;
13
14        let contents = match kind {
15            ProtobufSyntaxKind::MessageDef => "### Protobuf Message\nA message is a collection of fields. It's the primary way to define data structures in Protobuf.",
16            ProtobufSyntaxKind::EnumDef => "### Protobuf Enum\nAn enum is a set of named integer constants.",
17            ProtobufSyntaxKind::ServiceDef => "### Protobuf Service\nA service defines a set of RPC methods that can be called remotely.",
18            _ => return None,
19        };
20
21        Some(Hover { contents: contents.to_string(), range: Some(node.span()) })
22    }
23}
24
25/// Language service implementation for Protobuf.
26pub struct ProtobufLanguageService<V: Vfs> {
27    vfs: V,
28    workspace: oak_lsp::workspace::WorkspaceManager,
29    hover_provider: ProtobufHoverProvider,
30}
31
32impl<V: Vfs> ProtobufLanguageService<V> {
33    pub fn new(vfs: V) -> Self {
34        Self { vfs, workspace: oak_lsp::workspace::WorkspaceManager::default(), hover_provider: ProtobufHoverProvider }
35    }
36}
37
38impl<V: Vfs + Send + Sync + 'static + oak_vfs::WritableVfs> LanguageService for ProtobufLanguageService<V> {
39    type Lang = ProtobufLanguage;
40    type Vfs = V;
41
42    fn vfs(&self) -> &Self::Vfs {
43        &self.vfs
44    }
45
46    fn workspace(&self) -> &oak_lsp::workspace::WorkspaceManager {
47        &self.workspace
48    }
49
50    fn get_root(&self, _uri: &str) -> impl Future<Output = Option<RedNode<'_, ProtobufLanguage>>> + Send + '_ {
51        async move { None }
52    }
53
54    fn hover(&self, uri: &str, range: Range<usize>) -> impl Future<Output = Option<Hover>> + Send + '_ {
55        let uri = uri.to_string();
56        async move { self.with_root(&uri, |root| self.hover_provider.hover(&root, range)).await.flatten() }
57    }
58}