A simple CPU rendered GUI IDE experience.
apply wsedits everywhere
| -rw-r--r-- | src/edi.rs | 28 | ||||
| -rw-r--r-- | src/text.rs | 23 |
2 files changed, 40 insertions, 11 deletions
@@ -1,5 +1,6 @@ use std::borrow::Cow; use std::collections::HashMap; +use std::fs::OpenOptions; use std::mem::take; use std::ops::ControlFlow; use std::path::{Path, PathBuf}; @@ -1647,20 +1648,25 @@ impl Editor { ControlFlow::Continue(()) } pub fn apply_wsedit(&mut self, x: WorkspaceEdit, f: &Path) { - let mut f_ = |edits: &mut [SnippetTextEdit]| { - edits.sort_tedits(); - // let mut first = false; - for edit in edits { - self.text.apply_snippet_tedit(edit).unwrap(); - } - }; let mut f2 = - |TextDocumentEdit { edits, text_document, .. }| { + |TextDocumentEdit { mut edits, text_document, .. }| { + edits.sort_tedits(); if text_document.uri != f.tid().uri { - log::error!("didnt apply to {}", text_document.uri); - return; + let mut f = OpenOptions::new() + .write(true) + .read(true) + .open(text_document.uri.path()) + .unwrap(); + let mut r = Rope::from_reader(&mut f).unwrap(); + for edit in &edits { + TextArea::apply_snippet_tedit_raw(edit, &mut r); + } + r.write_to(f).unwrap(); + } else { + for edit in &edits { + self.text.apply_snippet_tedit(edit).unwrap(); + } } - f_(&mut { edits }); }; match x { WorkspaceEdit { diff --git a/src/text.rs b/src/text.rs index 00a2e65..0873802 100644 --- a/src/text.rs +++ b/src/text.rs @@ -478,6 +478,28 @@ impl TextArea { } Ok(()) } + pub fn apply_snippet_tedit_raw( + SnippetTextEdit { text_edit: x, insert_text_format, .. }: &SnippetTextEdit, + text: &'_ mut Rope, + ) -> Option<()> { + match insert_text_format { + Some(lsp_types::InsertTextFormat::SNIPPET) => { + let begin = text.l_position(x.range.start)?; + let end = text.l_position(x.range.end)?; + text.try_remove(begin..end).ok()?; + let (_, tex) = + crate::sni::Snippet::parse(&x.new_text, begin)?; + text.try_insert(begin, &tex).ok()?; + } + _ => { + let begin = text.l_position(x.range.start)?; + let end = text.l_position(x.range.end)?; + text.try_remove(begin..end).ok()?; + text.try_insert(begin, &x.new_text).ok()?; + } + } + Some(()) + } pub fn apply_snippet_tedit( &mut self, SnippetTextEdit { text_edit, insert_text_format, .. }: &SnippetTextEdit, @@ -491,6 +513,7 @@ impl TextArea { } Ok(()) } + pub fn apply_snippet(&mut self, x: &TextEdit) -> anyhow::Result<()> { let begin = self .l_position(x.range.start) |