1use crate::{ActionScriptLanguage, parser::ActionScriptElementType};
2use core::range::Range;
3use futures::Future;
4use oak_core::tree::RedNode;
5use oak_hover::{Hover, HoverProvider};
6use oak_lsp::service::LanguageService;
7use oak_vfs::Vfs;
8
9pub struct ActionScriptHoverProvider;
11
12impl HoverProvider<ActionScriptLanguage> for ActionScriptHoverProvider {
13 fn hover(&self, node: &RedNode<ActionScriptLanguage>, _range: Range<usize>) -> Option<Hover> {
14 let kind = node.green.kind;
15
16 let contents = match kind {
17 ActionScriptElementType::Class => "### ActionScript Class\nDefines a blueprint for objects.",
18 ActionScriptElementType::Interface => "### ActionScript Interface\nDefines a contract for classes.",
19 ActionScriptElementType::Function => "### ActionScript Function\nDefines a block of code that performs a task.",
20 _ => return None,
21 };
22
23 Some(Hover { contents: contents.to_string(), range: Some(node.span()) })
24 }
25}
26
27pub struct ActionScriptLanguageService<V: Vfs> {
29 vfs: V,
30 workspace: oak_lsp::workspace::WorkspaceManager,
31 hover_provider: ActionScriptHoverProvider,
32}
33
34impl<V: Vfs> ActionScriptLanguageService<V> {
35 pub fn new(vfs: V) -> Self {
37 Self { vfs, workspace: oak_lsp::workspace::WorkspaceManager::default(), hover_provider: ActionScriptHoverProvider }
38 }
39}
40
41impl<V: Vfs + Send + Sync + 'static + oak_vfs::WritableVfs> LanguageService for ActionScriptLanguageService<V> {
42 type Lang = ActionScriptLanguage;
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<'_, ActionScriptLanguage>>> + Send + '_ {
54 let source = self.vfs().get_source(uri);
55 async move {
56 let _source = source?;
57 None
59 }
60 }
61
62 fn hover(&self, uri: &str, range: Range<usize>) -> impl Future<Output = Option<oak_lsp::Hover>> + Send + '_ {
63 let uri = uri.to_string();
64 async move { self.with_root(&uri, |root| self.hover_provider.hover(&root, range).map(|h| oak_lsp::Hover { contents: h.contents, range: h.range })).await.flatten() }
65 }
66}