reconcile_text/lib.rs
1//! # Reconcile: conflict-free 3-way text merging
2//!
3//! A library for merging conflicting text edits without manual intervention.
4//! Unlike traditional 3-way merge tools that produce conflict markers,
5//! reconcile-text automatically resolves conflicts by applying both sets of
6//! changes (while updating cursor positions) using an algorithm inspired by
7//! Operational Transformation.
8//!
9//! ✨ **[Try the interactive demo](https://schmelczer.dev/reconcile)** to see it in action.
10//!
11//! ## Simple example
12//!
13//! ```
14//! use reconcile_text::{reconcile, BuiltinTokenizer};
15//!
16//! // Start with original text
17//! let parent = "Merging text is hard!";
18//! // Two people edit simultaneously
19//! let left = "Merging text is easy!"; // Changed "hard" to "easy"
20//! let right = "With reconcile, merging documents is hard!"; // Added prefix and changed word
21//!
22//! // Reconcile combines both changes intelligently
23//! let result = reconcile(parent, &left.into(), &right.into(), &*BuiltinTokenizer::Word);
24//! assert_eq!(result.apply().text(), "With reconcile, merging documents is easy!");
25//! ```
26//!
27//! ## Tokenisation strategies
28//!
29//! Merging happens at the token level, and the choice of tokeniser
30//! significantly affects merge quality and behaviour.
31//!
32//! ### Built-in tokenisers
33//!
34//! - **`BuiltinTokenizer::Word`** (recommended): Splits on word boundaries,
35//! preserving word integrity
36//! - **`BuiltinTokenizer::Character`**: Character-level merging for
37//! fine-grained control
38//! - **`BuiltinTokenizer::Line`**: Line-based merging, similar to traditional
39//! diff tools
40//!
41//! ```
42//! use reconcile_text::{reconcile, BuiltinTokenizer};
43//!
44//! let parent = "The quick brown fox\njumps over the lazy dog";
45//! let left = "The very quick brown fox\njumps over the lazy dog"; // Added "very"
46//! let right = "The quick red fox\njumps over the lazy dog"; // Changed "brown" to "red"
47//!
48//! // Word-level tokenisation (recommended for most text)
49//! let result = reconcile(parent, &left.into(), &right.into(), &*BuiltinTokenizer::Word);
50//! assert_eq!(result.apply().text(), "The very quick red fox\njumps over the lazy dog");
51//!
52//! // Line-level tokenisation (similar to git merge)
53//! let result = reconcile(parent, &left.into(), &right.into(), &*BuiltinTokenizer::Line);
54//! // Line-level produces different results as it treats each line as atomic
55//! assert_eq!(result.apply().text(), "The quick red foxThe very quick brown fox\njumps over the lazy dog");
56//! ```
57//!
58//! ### Custom tokenisation
59//!
60//! For specialised use cases, such as structured languages, custom
61//! tokenisation logic can be implemented by providing a function with the
62//! signature `Fn(&str) -> Vec<Token<String>>`:
63//!
64//! ```
65//! use reconcile_text::{reconcile, Token, BuiltinTokenizer};
66//!
67//! // Example: sentence-based tokeniser function
68//! let sentence_tokeniser = |text: &str| {
69//! text.split_inclusive(". ")
70//! .map(|sentence| Token::new(
71//! sentence.to_string(),
72//! sentence.to_string(),
73//! false, // don't allow joining with preceding token
74//! false, // don't allow joining with following token
75//! ))
76//! .collect::<Vec<_>>()
77//! };
78//!
79//! let parent = "Hello world. This is a test.";
80//! let left = "Hello beautiful world. This is a test."; // Added "beautiful"
81//! let right = "Hello world. This is a great test."; // Changed "a" to "great"
82//!
83//! // For most cases, the built-in word tokeniser works well
84//! let result = reconcile(parent, &left.into(), &right.into(), &sentence_tokeniser);
85//! assert_eq!(result.apply().text(), "Hello beautiful world. This is a great test.");
86//! ```
87//!
88//! > **Note**: Setting token joinability to `false` causes insertions to
89//! > interleave (LRLRLR) rather than group together (LLLRRR), which often
90//! > produces more natural-looking merged text.
91//!
92//! ## Cursor tracking
93//!
94//! Automatically repositions cursors and selection ranges during merging,
95//! which is essential for collaborative editors:
96//!
97//! ```
98//! use reconcile_text::{reconcile, BuiltinTokenizer, TextWithCursors, CursorPosition};
99//!
100//! let parent = "Hello world";
101//! let left = TextWithCursors::new(
102//! "Hello beautiful world".to_string(),
103//! vec![CursorPosition::new(1, 6)] // After "Hello "
104//! );
105//! let right = TextWithCursors::new(
106//! "Hi world".to_string(),
107//! vec![CursorPosition::new(2, 0)] // At the beginning
108//! );
109//!
110//! let result = reconcile(parent, &left, &right, &*BuiltinTokenizer::Word);
111//! let merged = result.apply();
112//!
113//! assert_eq!(merged.text(), "Hi beautiful world");
114//! // Cursors are automatically repositioned in the merged text
115//! assert_eq!(merged.cursors().len(), 2);
116//! // Cursor 1 moves from position 6 to position 3 (after "Hi ")
117//! // Cursor 2 stays at position 0 (at the beginning)
118//! ```
119//! > The `cursors` list is sorted by character position (not IDs).
120//!
121//! ## Change provenance
122//!
123//! Track which changes came from where:
124//!
125//! ```rust
126//! use reconcile_text::{History, SpanWithHistory, BuiltinTokenizer, reconcile};
127//!
128//! let parent = "Merging text is hard!";
129//! let left = "Merging text is easy!"; // Changed "hard" to "easy"
130//! let right = "With reconcile, merging documents is hard!"; // Added prefix and changed word
131//!
132//! let result = reconcile(
133//! parent,
134//! &left.into(),
135//! &right.into(),
136//! &*BuiltinTokenizer::Word,
137//! );
138//!
139//! assert_eq!(
140//! result.apply_with_history(),
141//! vec![
142//! SpanWithHistory::new("Merging text".to_string(), History::RemovedFromRight),
143//! SpanWithHistory::new(
144//! "With reconcile, merging documents".to_string(),
145//! History::AddedFromRight
146//! ),
147//! SpanWithHistory::new(" ".to_string(), History::Unchanged),
148//! SpanWithHistory::new("is".to_string(), History::Unchanged),
149//! SpanWithHistory::new(" hard!".to_string(), History::RemovedFromLeft),
150//! SpanWithHistory::new(" easy!".to_string(), History::AddedFromLeft),
151//! ]
152//! );
153//! ```
154//!
155//! ## Compact change serialization
156//!
157//! The edits can be serialized into a compact representation without the full
158//! original text, making the size depend only on the changes made.
159//!
160//! ```rust
161//! # #[cfg(feature = "serde")]
162//! # {
163//! use reconcile_text::{EditedText, BuiltinTokenizer};
164//! use serde_yaml;
165//! use pretty_assertions::assert_eq;
166//!
167//!
168//! let original = "Merging text is hard!";
169//! let changes = "Merging text is easy with reconcile!";
170//!
171//! let result = EditedText::from_strings(
172//! original,
173//! &changes.into()
174//! );
175//!
176//! let serialized = serde_yaml::to_string(&result.to_diff().unwrap()).unwrap();
177//! assert_eq!(
178//! serialized,
179//! concat!(
180//! "- 15\n",
181//! "- -6\n",
182//! "- ' easy with reconcile!'\n"
183//! )
184//! );
185//!
186//! let deserialized = serde_yaml::from_str(&serialized).unwrap();
187//! let reconstructed = EditedText::from_diff(
188//! original,
189//! deserialized,
190//! &*BuiltinTokenizer::Word
191//! ).unwrap();
192//! assert_eq!(
193//! reconstructed.apply().text(),
194//! "Merging text is easy with reconcile!"
195//! );
196//! # }
197//! ```
198//!
199//! ## Error handling
200//!
201//! The library is designed to be robust and will always produce a result, even
202//! for edge cases.
203//!
204//! ## Performance
205//!
206//! Be aware that extremely large diffs may have performance implications.
207//!
208//! ## Algorithm overview
209//!
210//! For detailed algorithm explanation, see the
211//! [README](https://github.com/schmelczer/reconcile/blob/main/README.md#how-it-works).
212
213mod operation_transformation;
214mod raw_operation;
215mod tokenizer;
216mod types;
217mod utils;
218
219pub use operation_transformation::{DiffError, EditedText, reconcile};
220pub use tokenizer::{BuiltinTokenizer, Tokenizer, token::Token};
221pub use types::{
222 cursor_position::CursorPosition, history::History, number_or_text::NumberOrText, side::Side,
223 span_with_history::SpanWithHistory, text_with_cursors::TextWithCursors,
224};
225
226#[cfg(feature = "wasm")]
227pub mod wasm;