|
| 1 | +use std::{ops::ControlFlow, sync::Arc}; |
| 2 | + |
| 3 | +use async_lsp::{ClientSocket, ResponseError}; |
| 4 | +use lsp_types::{ |
| 5 | + InitializeParams, InitializeResult, InitializedParams, LogMessageParams, MessageType, |
| 6 | + ServerCapabilities, ServerInfo, TextDocumentSyncCapability, TextDocumentSyncKind, |
| 7 | + TextDocumentSyncOptions, notification as notif, |
| 8 | +}; |
| 9 | +use solar_config::version::SHORT_VERSION; |
| 10 | +use solar_interface::data_structures::sync::RwLock; |
| 11 | + |
| 12 | +use crate::{NotifyResult, vfs::Vfs}; |
| 13 | + |
| 14 | +pub(crate) struct GlobalState { |
| 15 | + client: ClientSocket, |
| 16 | + pub(crate) vfs: Arc<RwLock<Vfs>>, |
| 17 | +} |
| 18 | + |
| 19 | +impl GlobalState { |
| 20 | + pub(crate) fn new(client: ClientSocket) -> Self { |
| 21 | + Self { client, vfs: Arc::new(Default::default()) } |
| 22 | + } |
| 23 | + |
| 24 | + pub(crate) fn on_initialize( |
| 25 | + &mut self, |
| 26 | + _: InitializeParams, |
| 27 | + ) -> impl Future<Output = Result<InitializeResult, ResponseError>> + use<> { |
| 28 | + std::future::ready(Ok(InitializeResult { |
| 29 | + capabilities: ServerCapabilities { |
| 30 | + text_document_sync: Some(TextDocumentSyncCapability::Options( |
| 31 | + TextDocumentSyncOptions { |
| 32 | + open_close: Some(true), |
| 33 | + change: Some(TextDocumentSyncKind::INCREMENTAL), |
| 34 | + will_save: None, |
| 35 | + will_save_wait_until: None, |
| 36 | + ..Default::default() |
| 37 | + }, |
| 38 | + )), |
| 39 | + ..Default::default() |
| 40 | + }, |
| 41 | + server_info: Some(ServerInfo { |
| 42 | + name: "solar".into(), |
| 43 | + version: Some(SHORT_VERSION.into()), |
| 44 | + }), |
| 45 | + })) |
| 46 | + } |
| 47 | + |
| 48 | + pub(crate) fn on_initialized(&mut self, _: InitializedParams) -> NotifyResult { |
| 49 | + self.info_msg("solar initialized"); |
| 50 | + ControlFlow::Continue(()) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl GlobalState { |
| 55 | + #[expect(unused)] |
| 56 | + fn warn_msg(&self, message: impl Into<String>) { |
| 57 | + let _ = self.client.notify::<notif::LogMessage>(LogMessageParams { |
| 58 | + typ: MessageType::WARNING, |
| 59 | + message: message.into(), |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + #[expect(unused)] |
| 64 | + fn error_msg(&self, message: impl Into<String>) { |
| 65 | + let _ = self.client.notify::<notif::LogMessage>(LogMessageParams { |
| 66 | + typ: MessageType::ERROR, |
| 67 | + message: message.into(), |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + fn info_msg(&self, message: impl Into<String>) { |
| 72 | + let _ = self.client.notify::<notif::LogMessage>(LogMessageParams { |
| 73 | + typ: MessageType::INFO, |
| 74 | + message: message.into(), |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
0 commit comments