diff options
Diffstat (limited to 'cli/lsp/text.rs')
-rw-r--r-- | cli/lsp/text.rs | 56 |
1 files changed, 51 insertions, 5 deletions
diff --git a/cli/lsp/text.rs b/cli/lsp/text.rs index 1d350c12f..f444d639e 100644 --- a/cli/lsp/text.rs +++ b/cli/lsp/text.rs @@ -181,8 +181,8 @@ impl LineIndex { /// Returns a u16 position based on a u8 offset. pub fn position_utf16(&self, offset: TextSize) -> lsp_types::Position { - let line = partition_point(&self.utf8_offsets, |&it| it <= offset) - 1; - let line_start_offset = self.utf8_offsets[line]; + let line = partition_point(&self.utf16_offsets, |&it| it <= offset) - 1; + let line_start_offset = self.utf16_offsets[line]; let col = offset - line_start_offset; lsp_types::Position { @@ -208,13 +208,21 @@ impl LineIndex { /// Compare two strings and return a vector of text edit records which are /// supported by the Language Server Protocol. -pub fn get_edits(a: &str, b: &str) -> Vec<TextEdit> { +pub fn get_edits( + a: &str, + b: &str, + maybe_line_index: Option<LineIndex>, +) -> Vec<TextEdit> { if a == b { return vec![]; } let chunks = diff(a, b); let mut text_edits = Vec::<TextEdit>::new(); - let line_index = LineIndex::new(a); + let line_index = if let Some(line_index) = maybe_line_index { + line_index + } else { + LineIndex::new(a) + }; let mut iter = chunks.iter().peekable(); let mut a_pos = TextSize::from(0); loop { @@ -565,7 +573,7 @@ const C: char = \"γ‘ γ‘\"; fn test_get_edits() { let a = "abcdefg"; let b = "a\nb\nchije\nfg\n"; - let actual = get_edits(a, b); + let actual = get_edits(a, b, None); assert_eq!( actual, vec![ @@ -600,6 +608,44 @@ const C: char = \"γ‘ γ‘\"; } #[test] + fn test_get_edits_mbc() { + let a = "const bar = \"ππΊπΈπ\";\nconsole.log('hello deno')\n"; + let b = "const bar = \"ππΊπΈπ\";\nconsole.log(\"hello deno\");\n"; + let actual = get_edits(a, b, None); + assert_eq!( + actual, + vec![ + TextEdit { + range: lsp_types::Range { + start: lsp_types::Position { + line: 1, + character: 12 + }, + end: lsp_types::Position { + line: 1, + character: 13 + } + }, + new_text: "\"".to_string() + }, + TextEdit { + range: lsp_types::Range { + start: lsp_types::Position { + line: 1, + character: 23 + }, + end: lsp_types::Position { + line: 1, + character: 25 + } + }, + new_text: "\");".to_string() + }, + ] + ) + } + + #[test] fn test_get_range_change() { let a = "abcdefg"; let b = "abcdefg"; |