use gen_lsp_types::*;
use super::document;
use crate::{Client, Protocol, Result};
fn whole_document(text: String) -> TextDocumentContentChangeEvent {
TextDocumentContentChangeWholeDocument { text }.into()
}
fn change(
uri: &Uri,
version: i32,
content_changes: Vec<TextDocumentContentChangeEvent>,
) -> DidChangeTextDocumentParams {
DidChangeTextDocumentParams {
text_document: VersionedTextDocumentIdentifier {
version,
text_document_identifier: document(uri),
},
content_changes,
}
}
impl Client {
pub fn did_open(
&mut self,
uri: Uri,
text: impl Into<String>,
language: LanguageKind,
) -> Result<()> {
let version = self.next_open_version(&uri);
self.protocol_did_open(DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri,
language_id: language,
version,
text: text.into(),
},
})
}
pub fn did_change_full(&mut self, uri: &Uri, text: impl Into<String>) -> Result<()> {
let (version, _) = self.next_document_version_and_text(uri)?;
self.protocol_did_change(change(uri, version, vec![whole_document(text.into())]))
}
pub fn did_change_incremental(
&mut self,
uri: &Uri,
range: Range,
text: impl Into<String>,
) -> Result<()> {
let (version, _) = self.next_document_version_and_text(uri)?;
self.protocol_did_change(change(
uri,
version,
vec![TextDocumentContentChangePartial::new(range, None, text.into()).into()],
))
}
pub fn did_change(&mut self, uri: &Uri, edits: &[TextEdit]) -> Result<()> {
if edits.is_empty() {
self.require_open_document(uri)?;
return Ok(());
}
let (version, text) = self.next_document_version_and_text(uri)?;
let content_changes = self.content_changes(text, edits)?;
self.protocol_did_change(change(uri, version, content_changes))
}
pub fn did_close(&mut self, uri: &Uri) -> Result<()> {
self.protocol_did_close(DidCloseTextDocumentParams {
text_document: document(uri),
})
}
pub fn document_text(&self, uri: &Uri) -> Option<&str> {
self.tracked_text(uri).ok()
}
pub fn will_save(&mut self, uri: &Uri, reason: TextDocumentSaveReason) -> Result<()> {
self.protocol_will_save(WillSaveTextDocumentParams {
text_document: document(uri),
reason,
})
}
pub fn will_save_wait_until(
&mut self,
uri: &Uri,
reason: TextDocumentSaveReason,
) -> Result<Option<Vec<TextEdit>>> {
self.protocol_will_save_wait_until(WillSaveTextDocumentParams {
text_document: document(uri),
reason,
})
}
pub fn did_save(&mut self, uri: &Uri) -> Result<()> {
let text = self
.supports_save_text()
.then(|| self.tracked_text(uri).map(str::to_owned))
.transpose()?;
self.protocol_did_save(DidSaveTextDocumentParams {
text_document: document(uri),
text,
})
}
pub fn notebook_document_did_open(
&mut self,
params: DidOpenNotebookDocumentParams,
) -> Result<()> {
self.protocol_notebook_document_did_open(params)
}
pub fn notebook_document_did_change(
&mut self,
params: DidChangeNotebookDocumentParams,
) -> Result<()> {
self.protocol_notebook_document_did_change(params)
}
pub fn notebook_document_did_save(
&mut self,
params: DidSaveNotebookDocumentParams,
) -> Result<()> {
self.protocol_notebook_document_did_save(params)
}
pub fn notebook_document_did_close(
&mut self,
params: DidCloseNotebookDocumentParams,
) -> Result<()> {
self.protocol_notebook_document_did_close(params)
}
}