1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-01 07:46:05 +02:00

lsp: Test changeset_to_changes.

This commit is contained in:
Blaž Hrastnik 2021-02-16 15:39:41 +09:00
parent 9821c4dd3b
commit b7da7f83c3
2 changed files with 82 additions and 2 deletions

View File

@ -247,7 +247,7 @@ pub async fn text_document_did_open(
.await
}
fn changeset_to_changes(
pub fn changeset_to_changes(
old_text: &Rope,
changeset: &ChangeSet,
) -> Vec<lsp::TextDocumentContentChangeEvent> {

View File

@ -58,7 +58,7 @@ fn take_with<T, F>(mut_ref: &mut T, closure: F)
use url::Url;
impl Document {
fn new(state: State) -> Self {
pub fn new(state: State) -> Self {
let changes = ChangeSet::new(&state.doc);
let old_state = None;
@ -294,3 +294,83 @@ pub fn versioned_identifier(&self) -> lsp::VersionedTextDocumentIdentifier {
lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn changeset_to_changes() {
use helix_core::{Rope, State, Transaction};
// use helix_view::Document;
use helix_lsp::{lsp, Client};
let text = Rope::from("hello");
let mut state = State::new(text);
state.selection = Selection::single(5, 5);
let mut doc = Document::new(state);
// insert
let transaction = Transaction::insert(&doc.state, " world".into());
let old_doc = doc.state.clone();
let changes = Client::changeset_to_changes(&old_doc.doc, transaction.changes());
doc.apply(&transaction);
assert_eq!(
changes,
&[lsp::TextDocumentContentChangeEvent {
range: Some(lsp::Range::new(
lsp::Position::new(0, 5),
lsp::Position::new(0, 5)
)),
text: " world".into(),
range_length: None,
}]
);
// delete
let transaction = transaction.invert(&old_doc);
let old_doc = doc.state.clone();
let changes = Client::changeset_to_changes(&old_doc.doc, transaction.changes());
doc.apply(&transaction);
// line: 0-based.
// col: 0-based, gaps between chars.
// 0 1 2 3 4 5 6 7 8 9 0 1
// |h|e|l|l|o| |w|o|r|l|d|
// -------------
// (0, 5)-(0, 11)
assert_eq!(
changes,
&[lsp::TextDocumentContentChangeEvent {
range: Some(lsp::Range::new(
lsp::Position::new(0, 5),
lsp::Position::new(0, 11)
)),
text: "".into(),
range_length: None,
}]
);
// replace
doc.state.selection = Selection::single(0, 5);
let transaction = Transaction::change_by_selection(&doc.state, |range| {
(range.from(), range.to(), Some("aeiou".into()))
});
let changes = Client::changeset_to_changes(&doc.state.doc, transaction.changes());
assert_eq!(
changes,
&[lsp::TextDocumentContentChangeEvent {
range: Some(lsp::Range::new(
lsp::Position::new(0, 0),
lsp::Position::new(0, 5)
)),
text: "aeiou".into(),
range_length: None,
}]
);
}
}