|
| 1 | +use tower_lsp::{ |
| 2 | + jsonrpc::Result, |
| 3 | + lsp_types::{Diagnostic, MessageType}, |
| 4 | +}; |
| 5 | + |
| 6 | +use super::{InMemoryDocumentDatabase, LSPClient}; |
| 7 | + |
| 8 | +pub struct LspInteractor<C> { |
| 9 | + client: C, |
| 10 | + document_database: InMemoryDocumentDatabase, |
| 11 | +} |
| 12 | + |
| 13 | +impl<C> LspInteractor<C> { |
| 14 | + pub fn new(client: C, document_database: InMemoryDocumentDatabase) -> Self { |
| 15 | + Self { |
| 16 | + client, |
| 17 | + document_database, |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl<C> LspInteractor<C> |
| 23 | +where |
| 24 | + C: LSPClient, |
| 25 | +{ |
| 26 | + pub async fn update_document_with_text(&self, uri: &str, text: &str) { |
| 27 | + self.document_database.write_document_text(uri, text).await; |
| 28 | + self.document_database.remove_diagnostics(uri).await; |
| 29 | + let _ = self.publish_all_diagnostics().await; |
| 30 | + } |
| 31 | + |
| 32 | + pub async fn show_message(&self, message_type: MessageType, message: &str) { |
| 33 | + self.client.show_message(message_type, message).await; |
| 34 | + } |
| 35 | + |
| 36 | + pub async fn publish_all_diagnostics(&self) -> Result<()> { |
| 37 | + let all_diagnostics = self.document_database.all_diagnostics().await; |
| 38 | + for (url, diagnostics) in all_diagnostics { |
| 39 | + self.client |
| 40 | + .publish_diagnostics(&url, diagnostics, None) |
| 41 | + .await; |
| 42 | + } |
| 43 | + Ok(()) |
| 44 | + } |
| 45 | + |
| 46 | + pub async fn read_document_text(&self, uri: &str) -> Option<String> { |
| 47 | + self.document_database.read_document_text(uri).await |
| 48 | + } |
| 49 | + |
| 50 | + pub async fn remove_diagnostics(&self, uri: &str) { |
| 51 | + self.document_database.remove_diagnostics(uri).await |
| 52 | + } |
| 53 | + |
| 54 | + pub async fn append_document_diagnostics(&self, uri: &str, diagnostics: &[Diagnostic]) { |
| 55 | + self.document_database |
| 56 | + .append_document_diagnostics(uri, diagnostics) |
| 57 | + .await |
| 58 | + } |
| 59 | +} |
0 commit comments