2021-03-29 08:22:43 +02:00
|
|
|
use anyhow::{Context, Error};
|
2020-10-30 09:00:30 +01:00
|
|
|
use std::future::Future;
|
2020-12-18 09:18:11 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-12-25 09:20:09 +01:00
|
|
|
use std::sync::Arc;
|
2020-10-22 07:35:07 +02:00
|
|
|
|
|
|
|
use helix_core::{
|
2021-03-14 09:13:55 +01:00
|
|
|
syntax::{LanguageConfiguration, LOADER},
|
|
|
|
ChangeSet, Diagnostic, History, Rope, Selection, State, Syntax, Transaction,
|
2020-10-22 07:35:07 +02:00
|
|
|
};
|
|
|
|
|
2021-03-23 09:47:40 +01:00
|
|
|
use crate::DocumentId;
|
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum Mode {
|
|
|
|
Normal,
|
2021-03-01 06:31:34 +01:00
|
|
|
Select,
|
2020-10-22 07:35:07 +02:00
|
|
|
Insert,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Document {
|
2021-03-18 07:07:02 +01:00
|
|
|
// rope + selection
|
2021-03-23 09:47:40 +01:00
|
|
|
pub(crate) id: DocumentId,
|
2021-03-18 07:07:02 +01:00
|
|
|
state: State,
|
2021-02-21 11:47:21 +01:00
|
|
|
path: Option<PathBuf>,
|
2020-10-22 07:35:07 +02:00
|
|
|
|
|
|
|
/// Current editing mode.
|
|
|
|
pub mode: Mode,
|
|
|
|
pub restore_cursor: bool,
|
|
|
|
|
2021-03-18 06:48:42 +01:00
|
|
|
syntax: Option<Syntax>,
|
2021-03-14 09:13:55 +01:00
|
|
|
// /// Corresponding language scope name. Usually `source.<lang>`.
|
|
|
|
pub(crate) language: Option<Arc<LanguageConfiguration>>,
|
2020-10-22 07:35:07 +02:00
|
|
|
|
2021-03-30 11:19:48 +02:00
|
|
|
modified: bool,
|
2020-10-22 07:35:07 +02:00
|
|
|
/// Pending changes since last history commit.
|
2021-02-21 11:47:21 +01:00
|
|
|
changes: ChangeSet,
|
|
|
|
/// State at last commit. Used for calculating reverts.
|
|
|
|
old_state: Option<State>,
|
|
|
|
/// Undo tree.
|
|
|
|
history: History,
|
|
|
|
version: i32, // should be usize?
|
2020-10-22 07:35:07 +02:00
|
|
|
|
|
|
|
pub diagnostics: Vec<Diagnostic>,
|
2021-03-18 06:40:22 +01:00
|
|
|
language_server: Option<Arc<helix_lsp::Client>>,
|
2020-10-22 07:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Like std::mem::replace() except it allows the replacement value to be mapped from the
|
|
|
|
/// original value.
|
|
|
|
fn take_with<T, F>(mut_ref: &mut T, closure: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(T) -> T,
|
|
|
|
{
|
|
|
|
use std::{panic, ptr};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let old_t = ptr::read(mut_ref);
|
|
|
|
let new_t = panic::catch_unwind(panic::AssertUnwindSafe(|| closure(old_t)))
|
|
|
|
.unwrap_or_else(|_| ::std::process::abort());
|
|
|
|
ptr::write(mut_ref, new_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 09:20:09 +01:00
|
|
|
use helix_lsp::lsp;
|
2020-10-22 07:35:07 +02:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
impl Document {
|
2021-03-18 06:44:32 +01:00
|
|
|
pub fn new(text: Rope) -> Self {
|
|
|
|
let changes = ChangeSet::new(&text);
|
2020-12-21 05:42:47 +01:00
|
|
|
let old_state = None;
|
2020-10-22 07:35:07 +02:00
|
|
|
|
|
|
|
Self {
|
2021-03-23 09:47:40 +01:00
|
|
|
id: DocumentId::default(),
|
2020-10-22 07:35:07 +02:00
|
|
|
path: None,
|
2021-03-18 06:44:32 +01:00
|
|
|
state: State::new(text),
|
2020-10-22 07:35:07 +02:00
|
|
|
mode: Mode::Normal,
|
|
|
|
restore_cursor: false,
|
|
|
|
syntax: None,
|
2020-11-05 07:15:19 +01:00
|
|
|
language: None,
|
2021-03-30 11:19:48 +02:00
|
|
|
modified: false,
|
2020-10-22 07:35:07 +02:00
|
|
|
changes,
|
|
|
|
old_state,
|
|
|
|
diagnostics: Vec::new(),
|
|
|
|
version: 0,
|
|
|
|
history: History::default(),
|
2020-12-25 09:20:09 +01:00
|
|
|
language_server: None,
|
2020-10-22 07:35:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: passing scopes here is awkward
|
2020-11-05 07:15:19 +01:00
|
|
|
// TODO: async fn?
|
2020-10-22 07:35:07 +02:00
|
|
|
pub fn load(path: PathBuf, scopes: &[String]) -> Result<Self, Error> {
|
|
|
|
use std::{env, fs::File, io::BufReader};
|
|
|
|
let _current_dir = env::current_dir()?;
|
|
|
|
|
2021-03-29 08:22:43 +02:00
|
|
|
let file = File::open(path.clone()).context(format!("unable to open {:?}", path))?;
|
|
|
|
let doc = Rope::from_reader(BufReader::new(file))?;
|
2020-10-22 07:35:07 +02:00
|
|
|
|
|
|
|
// TODO: create if not found
|
|
|
|
|
2021-03-18 06:44:32 +01:00
|
|
|
let mut doc = Self::new(doc);
|
2020-10-22 07:35:07 +02:00
|
|
|
|
2021-03-25 07:26:25 +01:00
|
|
|
let language_config = LOADER
|
|
|
|
.get()
|
|
|
|
.unwrap()
|
|
|
|
.language_config_for_file_name(path.as_path());
|
2021-02-21 11:47:21 +01:00
|
|
|
doc.set_language(language_config, scopes);
|
2020-10-22 07:35:07 +02:00
|
|
|
|
|
|
|
// canonicalize path to absolute value
|
|
|
|
doc.path = Some(std::fs::canonicalize(path)?);
|
|
|
|
|
|
|
|
Ok(doc)
|
|
|
|
}
|
|
|
|
|
2020-11-05 07:15:19 +01:00
|
|
|
// TODO: do we need some way of ensuring two save operations on the same doc can't run at once?
|
|
|
|
// or is that handled by the OS/async layer
|
2021-03-30 11:19:48 +02:00
|
|
|
pub fn save(&mut self) -> impl Future<Output = Result<(), anyhow::Error>> {
|
2020-10-30 09:00:30 +01:00
|
|
|
// we clone and move text + path into the future so that we asynchronously save the current
|
|
|
|
// state without blocking any further edits.
|
|
|
|
|
|
|
|
let text = self.text().clone();
|
|
|
|
let path = self.path.clone().expect("Can't save with no path set!"); // TODO: handle no path
|
2021-03-12 08:20:56 +01:00
|
|
|
let identifier = self.identifier();
|
2020-10-30 09:00:30 +01:00
|
|
|
|
|
|
|
// TODO: mark changes up to now as saved
|
|
|
|
|
2021-03-12 08:20:56 +01:00
|
|
|
let language_server = self.language_server.clone();
|
|
|
|
|
2021-03-30 11:19:48 +02:00
|
|
|
// reset the modified flag
|
|
|
|
self.modified = false;
|
|
|
|
|
2020-10-30 09:00:30 +01:00
|
|
|
async move {
|
|
|
|
use smol::{fs::File, prelude::*};
|
|
|
|
let mut file = File::create(path).await?;
|
|
|
|
|
|
|
|
// write all the rope chunks to file
|
|
|
|
for chunk in text.chunks() {
|
|
|
|
file.write_all(chunk.as_bytes()).await?;
|
|
|
|
}
|
|
|
|
// TODO: flush?
|
|
|
|
|
2021-03-12 08:20:56 +01:00
|
|
|
if let Some(language_server) = language_server {
|
|
|
|
language_server
|
|
|
|
.text_document_did_save(identifier, &text)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2020-10-30 09:00:30 +01:00
|
|
|
Ok(())
|
2021-03-12 08:20:56 +01:00
|
|
|
}
|
2020-10-30 09:00:30 +01:00
|
|
|
}
|
|
|
|
|
2021-02-21 11:47:21 +01:00
|
|
|
pub fn set_language(
|
|
|
|
&mut self,
|
|
|
|
language_config: Option<Arc<helix_core::syntax::LanguageConfiguration>>,
|
|
|
|
scopes: &[String],
|
|
|
|
) {
|
|
|
|
if let Some(language_config) = language_config {
|
2021-03-14 09:13:55 +01:00
|
|
|
if let Some(highlight_config) = language_config.highlight_config(scopes) {
|
|
|
|
let syntax = Syntax::new(&self.state.doc, highlight_config);
|
|
|
|
self.syntax = Some(syntax);
|
|
|
|
// TODO: config.configure(scopes) is now delayed, is that ok?
|
|
|
|
}
|
2020-10-22 07:35:07 +02:00
|
|
|
|
2021-03-14 09:13:55 +01:00
|
|
|
self.language = Some(language_config);
|
2021-02-21 11:47:21 +01:00
|
|
|
} else {
|
|
|
|
self.syntax = None;
|
|
|
|
self.language = None;
|
2020-10-22 07:35:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-21 11:47:21 +01:00
|
|
|
pub fn set_language2(&mut self, scope: &str, scopes: &[String]) {
|
2021-03-25 07:26:25 +01:00
|
|
|
let language_config = LOADER.get().unwrap().language_config_for_scope(scope);
|
|
|
|
|
2021-02-21 11:47:21 +01:00
|
|
|
self.set_language(language_config, scopes);
|
|
|
|
}
|
|
|
|
|
2020-12-25 09:20:09 +01:00
|
|
|
pub fn set_language_server(&mut self, language_server: Option<Arc<helix_lsp::Client>>) {
|
|
|
|
self.language_server = language_server;
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:06:33 +02:00
|
|
|
pub fn set_selection(&mut self, selection: Selection) {
|
|
|
|
// TODO: use a transaction?
|
|
|
|
self.state.selection = selection;
|
|
|
|
}
|
|
|
|
|
2021-03-18 06:40:22 +01:00
|
|
|
fn _apply(&mut self, transaction: &Transaction) -> bool {
|
2020-10-22 07:35:07 +02:00
|
|
|
let old_doc = self.text().clone();
|
|
|
|
|
|
|
|
let success = transaction.apply(&mut self.state);
|
|
|
|
|
|
|
|
if !transaction.changes().is_empty() {
|
2021-03-29 08:22:43 +02:00
|
|
|
self.version += 1;
|
2020-10-23 05:06:33 +02:00
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
// update tree-sitter syntax tree
|
|
|
|
if let Some(syntax) = &mut self.syntax {
|
|
|
|
// TODO: no unwrap
|
|
|
|
syntax
|
|
|
|
.update(&old_doc, &self.state.doc, transaction.changes())
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:22:43 +02:00
|
|
|
// if let Some(diagnostics) = &mut self.diagnostics {
|
|
|
|
// for diagnostic in diagnostics {
|
|
|
|
// // TODO: map state.diagnostics over changes::map_pos too
|
|
|
|
// }
|
|
|
|
// }
|
2020-12-25 09:20:09 +01:00
|
|
|
|
|
|
|
// emit lsp notification
|
|
|
|
if let Some(language_server) = &self.language_server {
|
2020-12-25 09:42:50 +01:00
|
|
|
let notify = language_server.text_document_did_change(
|
|
|
|
self.versioned_identifier(),
|
|
|
|
&old_doc,
|
2021-02-17 09:28:55 +01:00
|
|
|
self.text(),
|
2020-12-25 09:42:50 +01:00
|
|
|
transaction.changes(),
|
|
|
|
);
|
2020-12-25 09:20:09 +01:00
|
|
|
|
|
|
|
smol::block_on(notify).expect("failed to emit textDocument/didChange");
|
|
|
|
}
|
2020-10-22 07:35:07 +02:00
|
|
|
}
|
|
|
|
success
|
|
|
|
}
|
|
|
|
|
2021-02-15 10:45:36 +01:00
|
|
|
pub fn apply(&mut self, transaction: &Transaction) -> bool {
|
|
|
|
// store the state just before any changes are made. This allows us to undo to the
|
|
|
|
// state just before a transaction was applied.
|
|
|
|
if self.changes.is_empty() && !transaction.changes().is_empty() {
|
|
|
|
self.old_state = Some(self.state.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
let success = self._apply(&transaction);
|
|
|
|
|
2021-03-30 11:19:48 +02:00
|
|
|
self.modified = true;
|
|
|
|
// TODO: be smarter about modified by keeping track of saved version instead. That way if
|
|
|
|
// current version == version then it's not modified.
|
|
|
|
|
2021-02-15 10:45:36 +01:00
|
|
|
if !transaction.changes().is_empty() {
|
|
|
|
// Compose this transaction with the previous one
|
|
|
|
take_with(&mut self.changes, |changes| {
|
|
|
|
changes.compose(transaction.changes().clone())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
success
|
|
|
|
}
|
|
|
|
|
2020-12-21 05:58:54 +01:00
|
|
|
pub fn undo(&mut self) -> bool {
|
|
|
|
if let Some(transaction) = self.history.undo() {
|
2021-02-15 10:45:36 +01:00
|
|
|
let success = self._apply(&transaction);
|
2021-01-08 08:31:19 +01:00
|
|
|
|
2020-12-21 05:58:54 +01:00
|
|
|
// reset changeset to fix len
|
|
|
|
self.changes = ChangeSet::new(self.text());
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn redo(&mut self) -> bool {
|
|
|
|
if let Some(transaction) = self.history.redo() {
|
2021-02-15 10:45:36 +01:00
|
|
|
let success = self._apply(&transaction);
|
2020-12-21 05:58:54 +01:00
|
|
|
|
|
|
|
// reset changeset to fix len
|
|
|
|
self.changes = ChangeSet::new(self.text());
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-21 11:47:21 +01:00
|
|
|
pub fn append_changes_to_history(&mut self) {
|
|
|
|
if self.changes.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_changeset = ChangeSet::new(self.text());
|
|
|
|
let changes = std::mem::replace(&mut self.changes, new_changeset);
|
|
|
|
// Instead of doing this messy merge we could always commit, and based on transaction
|
|
|
|
// annotations either add a new layer or compose into the previous one.
|
|
|
|
let transaction = Transaction::from(changes).with_selection(self.selection().clone());
|
|
|
|
|
|
|
|
// HAXX: we need to reconstruct the state as it was before the changes..
|
|
|
|
let old_state = self.old_state.take().expect("no old_state available");
|
|
|
|
|
|
|
|
self.history.commit_revision(&transaction, &old_state);
|
|
|
|
}
|
|
|
|
|
2021-03-29 09:32:42 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn id(&self) -> DocumentId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:19:48 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn modified(&self) -> bool {
|
|
|
|
self.modified
|
|
|
|
}
|
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn mode(&self) -> Mode {
|
|
|
|
self.mode
|
|
|
|
}
|
|
|
|
|
2021-02-21 11:47:21 +01:00
|
|
|
#[inline]
|
|
|
|
/// Corresponding language scope name. Usually `source.<lang>`.
|
|
|
|
pub fn language(&self) -> Option<&str> {
|
2021-03-14 09:13:55 +01:00
|
|
|
self.language
|
|
|
|
.as_ref()
|
|
|
|
.map(|language| language.scope.as_str())
|
2021-02-21 11:47:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-03-18 06:48:42 +01:00
|
|
|
/// Current document version, incremented at each change.
|
2021-02-21 11:47:21 +01:00
|
|
|
pub fn version(&self) -> i32 {
|
|
|
|
self.version
|
|
|
|
}
|
|
|
|
|
2021-03-18 06:40:22 +01:00
|
|
|
pub fn language_server(&self) -> Option<&helix_lsp::Client> {
|
|
|
|
self.language_server.as_deref()
|
|
|
|
}
|
|
|
|
|
2021-03-18 06:48:42 +01:00
|
|
|
/// Tree-sitter AST tree
|
|
|
|
pub fn syntax(&self) -> Option<&Syntax> {
|
|
|
|
self.syntax.as_ref()
|
|
|
|
}
|
|
|
|
|
2021-03-22 05:47:39 +01:00
|
|
|
/// Tab size in columns.
|
|
|
|
pub fn tab_width(&self) -> usize {
|
|
|
|
self.language
|
|
|
|
.as_ref()
|
2021-03-25 07:26:25 +01:00
|
|
|
.and_then(|config| config.indent.as_ref())
|
2021-03-22 05:47:39 +01:00
|
|
|
.map(|config| config.tab_width)
|
|
|
|
.unwrap_or(4) // fallback to 4 columns
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a string containing a single level of indentation.
|
|
|
|
pub fn indent_unit(&self) -> &str {
|
|
|
|
self.language
|
|
|
|
.as_ref()
|
2021-03-25 07:26:25 +01:00
|
|
|
.and_then(|config| config.indent.as_ref())
|
|
|
|
.map(|config| config.unit.as_str())
|
2021-03-22 05:47:39 +01:00
|
|
|
.unwrap_or(" ") // fallback to 2 spaces
|
|
|
|
|
|
|
|
// " ".repeat(TAB_WIDTH)
|
|
|
|
}
|
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
#[inline]
|
2021-03-18 06:48:42 +01:00
|
|
|
/// File path on disk.
|
2020-10-22 07:35:07 +02:00
|
|
|
pub fn path(&self) -> Option<&PathBuf> {
|
|
|
|
self.path.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn url(&self) -> Option<Url> {
|
|
|
|
self.path().map(|path| Url::from_file_path(path).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn text(&self) -> &Rope {
|
|
|
|
&self.state.doc
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:06:33 +02:00
|
|
|
pub fn selection(&self) -> &Selection {
|
|
|
|
&self.state.selection
|
|
|
|
}
|
|
|
|
|
2020-12-18 09:18:11 +01:00
|
|
|
pub fn relative_path(&self) -> Option<&Path> {
|
2021-02-22 04:06:28 +01:00
|
|
|
let cwdir = std::env::current_dir().expect("couldn't determine current directory");
|
|
|
|
|
|
|
|
self.path
|
|
|
|
.as_ref()
|
|
|
|
.map(|path| path.strip_prefix(cwdir).unwrap_or(path))
|
2020-12-18 09:18:11 +01:00
|
|
|
}
|
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
// pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {
|
|
|
|
// self.state.doc.slice
|
|
|
|
// }
|
2020-10-23 06:51:08 +02:00
|
|
|
|
2021-03-16 07:30:29 +01:00
|
|
|
// transact(Fn) ?
|
2020-12-25 09:20:09 +01:00
|
|
|
|
|
|
|
// -- LSP methods
|
|
|
|
|
|
|
|
pub fn identifier(&self) -> lsp::TextDocumentIdentifier {
|
|
|
|
lsp::TextDocumentIdentifier::new(self.url().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn versioned_identifier(&self) -> lsp::VersionedTextDocumentIdentifier {
|
|
|
|
lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version)
|
|
|
|
}
|
2020-10-22 07:35:07 +02:00
|
|
|
}
|
2021-02-16 07:39:41 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn changeset_to_changes() {
|
|
|
|
use helix_lsp::{lsp, Client};
|
|
|
|
let text = Rope::from("hello");
|
2021-03-18 06:44:32 +01:00
|
|
|
let mut doc = Document::new(text);
|
|
|
|
doc.set_selection(Selection::single(5, 5));
|
2021-02-16 07:39:41 +01:00
|
|
|
|
|
|
|
// insert
|
|
|
|
|
2021-03-18 05:45:57 +01:00
|
|
|
let transaction = Transaction::insert(doc.text(), doc.selection(), " world".into());
|
2021-02-16 07:39:41 +01:00
|
|
|
let old_doc = doc.state.clone();
|
|
|
|
doc.apply(&transaction);
|
2021-02-17 09:28:55 +01:00
|
|
|
let changes = Client::changeset_to_changes(&old_doc.doc, doc.text(), transaction.changes());
|
2021-02-16 07:39:41 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
doc.apply(&transaction);
|
2021-02-17 09:28:55 +01:00
|
|
|
let changes = Client::changeset_to_changes(&old_doc.doc, doc.text(), transaction.changes());
|
2021-02-16 07:39:41 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2021-02-17 09:28:55 +01:00
|
|
|
// also tests that changes are layered, positions depend on previous changes.
|
|
|
|
|
2021-02-16 07:39:41 +01:00
|
|
|
doc.state.selection = Selection::single(0, 5);
|
2021-02-17 09:28:55 +01:00
|
|
|
let transaction = Transaction::change(
|
2021-03-18 05:28:27 +01:00
|
|
|
&doc.state.doc,
|
2021-02-17 09:28:55 +01:00
|
|
|
vec![(0, 2, Some("aei".into())), (3, 5, Some("ou".into()))].into_iter(),
|
|
|
|
);
|
|
|
|
// aeilou
|
|
|
|
doc.apply(&transaction);
|
|
|
|
let changes =
|
|
|
|
Client::changeset_to_changes(&doc.state.doc, doc.text(), transaction.changes());
|
2021-02-16 07:39:41 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
changes,
|
2021-02-17 09:28:55 +01:00
|
|
|
&[
|
|
|
|
// 0 1 2 3 4 5
|
|
|
|
// |h|e|l|l|o|
|
|
|
|
// ----
|
|
|
|
//
|
|
|
|
// aeillo
|
|
|
|
lsp::TextDocumentContentChangeEvent {
|
|
|
|
range: Some(lsp::Range::new(
|
|
|
|
lsp::Position::new(0, 0),
|
|
|
|
lsp::Position::new(0, 2)
|
|
|
|
)),
|
|
|
|
text: "aei".into(),
|
|
|
|
range_length: None,
|
|
|
|
},
|
|
|
|
// 0 1 2 3 4 5 6
|
|
|
|
// |a|e|i|l|l|o|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// aeilou
|
|
|
|
lsp::TextDocumentContentChangeEvent {
|
|
|
|
range: Some(lsp::Range::new(
|
|
|
|
lsp::Position::new(0, 4),
|
|
|
|
lsp::Position::new(0, 6)
|
|
|
|
)),
|
|
|
|
text: "ou".into(),
|
|
|
|
range_length: None,
|
|
|
|
}
|
|
|
|
]
|
2021-02-16 07:39:41 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|