diff options
Diffstat (limited to 'helix-lsp/src/lib.rs')
-rw-r--r-- | helix-lsp/src/lib.rs | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs index c56721a5..27a5e046 100644 --- a/helix-lsp/src/lib.rs +++ b/helix-lsp/src/lib.rs @@ -28,19 +28,44 @@ pub enum Error { pub mod util { use super::*; + use helix_core::{RopeSlice, State, Transaction}; - pub fn lsp_pos_to_pos(doc: &helix_core::RopeSlice, pos: lsp::Position) -> usize { + pub fn lsp_pos_to_pos(doc: &RopeSlice, pos: lsp::Position) -> usize { let line = doc.line_to_char(pos.line as usize); let line_start = doc.char_to_utf16_cu(line); doc.utf16_cu_to_char(pos.character as usize + line_start) } - pub fn pos_to_lsp_pos(doc: &helix_core::RopeSlice, pos: usize) -> lsp::Position { + pub fn pos_to_lsp_pos(doc: &RopeSlice, pos: usize) -> lsp::Position { let line = doc.char_to_line(pos); let line_start = doc.char_to_utf16_cu(line); let col = doc.char_to_utf16_cu(pos) - line_start; lsp::Position::new(line as u32, col as u32) } + + pub fn generate_transaction_from_edits( + state: &State, + edits: Vec<lsp::TextEdit>, + ) -> Transaction { + let doc = state.doc.slice(..); + Transaction::change( + state, + edits.into_iter().map(|edit| { + // simplify "" into None for cleaner changesets + let replacement = if !edit.new_text.is_empty() { + Some(edit.new_text.into()) + } else { + None + }; + + let start = lsp_pos_to_pos(&doc, edit.range.start); + let end = lsp_pos_to_pos(&doc, edit.range.end); + (start, end, replacement) + }), + ) + } + + // apply_insert_replace_edit } #[derive(Debug, PartialEq, Clone)] |