@@ -160,24 +160,13 @@ impl Document {
160 self.state.selection = selection;
160 self.state.selection = selection;
163− pub fn apply(&mut self, transaction: &Transaction) -> bool {
163+ pub fn _apply(&mut self, transaction: &Transaction) -> bool {
164 let old_doc = self.text().clone();
164 let old_doc = self.text().clone();
166− // store the state just before any changes are made. This allows us to undo to the
167− // state just before a transaction was applied.
168− if self.changes.is_empty() && !transaction.changes().is_empty() {
169− self.old_state = Some(self.state.clone());
172 let success = transaction.apply(&mut self.state);
166 let success = transaction.apply(&mut self.state);
174 if !transaction.changes().is_empty() {
168 if !transaction.changes().is_empty() {
175− // Compose this transaction with the previous one
169+ // TODO: self.version += 1;?
176− take_with(&mut self.changes, |changes| {
177− changes.compose(transaction.changes().clone())
180− // TODO: when composing, replace transaction.selection too
182 // update tree-sitter syntax tree
171 // update tree-sitter syntax tree
183 if let Some(syntax) = &mut self.syntax {
172 if let Some(syntax) = &mut self.syntax {
@@ -203,21 +192,28 @@ impl Document {
195+ pub fn apply(&mut self, transaction: &Transaction) -> bool {
196+ // store the state just before any changes are made. This allows us to undo to the
197+ // state just before a transaction was applied.
198+ if self.changes.is_empty() && !transaction.changes().is_empty() {
199+ self.old_state = Some(self.state.clone());
202+ let success = self._apply(&transaction);
204+ if !transaction.changes().is_empty() {
205+ // Compose this transaction with the previous one
206+ take_with(&mut self.changes, |changes| {
207+ changes.compose(transaction.changes().clone())
206 pub fn undo(&mut self) -> bool {
213 pub fn undo(&mut self) -> bool {
207 if let Some(transaction) = self.history.undo() {
214 if let Some(transaction) = self.history.undo() {
208− let old_doc = self.text().clone();
209 self.version += 1;
215 self.version += 1;
210− let success = transaction.apply(&mut self.state);
216+ let success = self._apply(&transaction);
212− // update tree-sitter syntax tree
213− if let Some(syntax) = &mut self.syntax {
216− .update(&old_doc, &self.state.doc, transaction.changes())
220− // TODO: undo / redo need to emit changes to lsp
222 // reset changeset to fix len
218 // reset changeset to fix len
223 self.changes = ChangeSet::new(self.text());
219 self.changes = ChangeSet::new(self.text());
@@ -229,17 +225,9 @@ impl Document {
230 pub fn redo(&mut self) -> bool {
226 pub fn redo(&mut self) -> bool {
231 if let Some(transaction) = self.history.redo() {
227 if let Some(transaction) = self.history.redo() {
232− let old_doc = self.text().clone();
233 self.version += 1;
228 self.version += 1;
234− let success = transaction.apply(&mut self.state);
236− // update tree-sitter syntax tree
230+ let success = self._apply(&transaction);
237− if let Some(syntax) = &mut self.syntax {
240− .update(&old_doc, &self.state.doc, transaction.changes())
244 // reset changeset to fix len
232 // reset changeset to fix len
245 self.changes = ChangeSet::new(self.text());
233 self.changes = ChangeSet::new(self.text());