Skip to main content

compute_text_change

Function compute_text_change 

Source
pub fn compute_text_change(old: &str, new: &str) -> Option<LspTextChange>
Expand description

LSP integration types and traits for editor clients. Computes a minimal text change between two snapshots.

The change is narrowed by trimming the common prefix and suffix, so a one-character edit in a large document produces a one-character change rather than a whole-document replacement. This is what lets the editor send incremental didChange notifications instead of resending the buffer.

§Arguments

  • old - The previous document contents
  • new - The current document contents

§Returns

Some(change) describing the edit, or None when the snapshots are identical

§Example

use iced_code_editor::compute_text_change;

// Identical snapshots produce no change at all.
assert!(compute_text_change("fn main() {}", "fn main() {}").is_none());

// Only the differing region is reported, not the whole line.
let change = compute_text_change("let x = 1;", "let x = 42;")
    .expect("the snapshots differ");
assert_eq!(change.text, "42");
assert_eq!(change.range.start.character, 8);
assert_eq!(change.range.end.character, 9);