|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell |
| 3 | +//! LSP Backend - LanguageServer trait implementation |
| 4 | +
|
| 5 | +use dashmap::DashMap; |
| 6 | +use std::sync::Arc; |
| 7 | +use tower_lsp::jsonrpc::Result; |
| 8 | +use tower_lsp::lsp_types::*; |
| 9 | +use tower_lsp::{Client, LanguageServer}; |
| 10 | + |
| 11 | +use super::document::DocumentState; |
| 12 | +use super::handlers; |
| 13 | + |
| 14 | +/// LSP Backend for WokeLang |
| 15 | +pub struct Backend { |
| 16 | + /// LSP client for sending notifications |
| 17 | + pub client: Client, |
| 18 | + /// Map of document URIs to document state |
| 19 | + pub document_map: Arc<DashMap<Url, DocumentState>>, |
| 20 | +} |
| 21 | + |
| 22 | +impl Backend { |
| 23 | + /// Create a new LSP backend |
| 24 | + pub fn new(client: Client) -> Self { |
| 25 | + Self { |
| 26 | + client, |
| 27 | + document_map: Arc::new(DashMap::new()), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[tower_lsp::async_trait] |
| 33 | +impl LanguageServer for Backend { |
| 34 | + async fn initialize(&self, _params: InitializeParams) -> Result<InitializeResult> { |
| 35 | + Ok(InitializeResult { |
| 36 | + capabilities: ServerCapabilities { |
| 37 | + text_document_sync: Some(TextDocumentSyncCapability::Kind( |
| 38 | + TextDocumentSyncKind::FULL, |
| 39 | + )), |
| 40 | + completion_provider: Some(CompletionOptions { |
| 41 | + trigger_characters: Some(vec![".".to_string(), ":".to_string()]), |
| 42 | + resolve_provider: Some(false), |
| 43 | + ..Default::default() |
| 44 | + }), |
| 45 | + hover_provider: Some(HoverProviderCapability::Simple(true)), |
| 46 | + definition_provider: Some(OneOf::Left(true)), |
| 47 | + diagnostic_provider: Some(DiagnosticServerCapabilities::Options( |
| 48 | + DiagnosticOptions { |
| 49 | + identifier: Some("wokelang".to_string()), |
| 50 | + inter_file_dependencies: false, |
| 51 | + workspace_diagnostics: false, |
| 52 | + ..Default::default() |
| 53 | + }, |
| 54 | + )), |
| 55 | + ..Default::default() |
| 56 | + }, |
| 57 | + server_info: Some(ServerInfo { |
| 58 | + name: "woke-lsp".to_string(), |
| 59 | + version: Some("0.1.0".to_string()), |
| 60 | + }), |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + async fn initialized(&self, _params: InitializedParams) { |
| 65 | + self.client |
| 66 | + .log_message(MessageType::INFO, "WokeLang LSP server initialized") |
| 67 | + .await; |
| 68 | + } |
| 69 | + |
| 70 | + async fn shutdown(&self) -> Result<()> { |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | + |
| 74 | + async fn did_open(&self, params: DidOpenTextDocumentParams) { |
| 75 | + let uri = params.text_document.uri; |
| 76 | + let text = params.text_document.text; |
| 77 | + let version = params.text_document.version; |
| 78 | + |
| 79 | + // Store document |
| 80 | + let doc = DocumentState::new(uri.clone(), text, version); |
| 81 | + self.document_map.insert(uri.clone(), doc); |
| 82 | + |
| 83 | + // Publish diagnostics |
| 84 | + handlers::diagnostics::publish_diagnostics(self, &uri).await; |
| 85 | + } |
| 86 | + |
| 87 | + async fn did_change(&self, params: DidChangeTextDocumentParams) { |
| 88 | + let uri = params.text_document.uri; |
| 89 | + let version = params.text_document.version; |
| 90 | + |
| 91 | + // Get new text (full sync mode) |
| 92 | + if let Some(change) = params.content_changes.first() { |
| 93 | + let new_text = change.text.clone(); |
| 94 | + |
| 95 | + // Replace document state (invalidates all caches) |
| 96 | + let doc = DocumentState::new(uri.clone(), new_text, version); |
| 97 | + self.document_map.insert(uri.clone(), doc); |
| 98 | + |
| 99 | + // Publish diagnostics |
| 100 | + handlers::diagnostics::publish_diagnostics(self, &uri).await; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + async fn did_close(&self, params: DidCloseTextDocumentParams) { |
| 105 | + let uri = params.text_document.uri; |
| 106 | + // Remove from cache |
| 107 | + self.document_map.remove(&uri); |
| 108 | + } |
| 109 | + |
| 110 | + async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> { |
| 111 | + handlers::completion::completion(self, params).await |
| 112 | + } |
| 113 | + |
| 114 | + async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> { |
| 115 | + handlers::hover::hover(self, params).await |
| 116 | + } |
| 117 | + |
| 118 | + async fn goto_definition( |
| 119 | + &self, |
| 120 | + params: GotoDefinitionParams, |
| 121 | + ) -> Result<Option<GotoDefinitionResponse>> { |
| 122 | + handlers::definition::goto_definition(self, params).await |
| 123 | + } |
| 124 | +} |
0 commit comments