From df5079efea18a7e6e7a2130266ea16127c85bccc Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 14 Jun 2025 12:04:19 +0100 Subject: [PATCH] Make OrderedOperation orderable --- .../ordered_operation.rs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/backend/reconcile/src/operation_transformation/ordered_operation.rs b/backend/reconcile/src/operation_transformation/ordered_operation.rs index 116b6372..6a668e2c 100644 --- a/backend/reconcile/src/operation_transformation/ordered_operation.rs +++ b/backend/reconcile/src/operation_transformation/ordered_operation.rs @@ -12,3 +12,37 @@ where pub order: usize, pub operation: Operation, } + +impl OrderedOperation +where + T: PartialEq + Clone + std::fmt::Debug, +{ + pub fn get_sort_key(&self) -> (usize, usize, String) { + ( + self.order, + self.operation.start_index(), + // Make sure that the ordering is deterministic regardless of which text + // is left or right. + match &self.operation { + Operation::Equal { index, .. } => index.to_string(), + Operation::Insert { text, .. } => text + .iter() + .map(crate::tokenizer::token::Token::original) + .collect::(), + Operation::Delete { + deleted_character_count, + .. + } => deleted_character_count.to_string(), + }, + ) + } +} + +impl PartialOrd for OrderedOperation +where + T: PartialEq + Clone + std::fmt::Debug, +{ + fn partial_cmp(&self, other: &Self) -> Option { + self.get_sort_key().partial_cmp(&other.get_sort_key()) + } +}