From 3d475648b109695c7fd3bd02783c92a1eb32e74b Mon Sep 17 00:00:00 2001 From: Orual Date: Tue, 6 Jan 2026 15:27:19 -0500 Subject: [PATCH] fixing some claude-isms --- .../src/components/editor/document.rs | 37 ++++---- crates/weaver-editor-crdt/src/buffer.rs | 32 +++---- docs/graph-data.json | 88 +++++++++++++++++++ 3 files changed, 121 insertions(+), 36 deletions(-) diff --git a/crates/weaver-app/src/components/editor/document.rs b/crates/weaver-app/src/components/editor/document.rs index 7c07cd9..65b95e2 100644 --- a/crates/weaver-app/src/components/editor/document.rs +++ b/crates/weaver-app/src/components/editor/document.rs @@ -200,25 +200,20 @@ impl PartialEq for LoadedDocState { impl EditorDocument { /// Check if a character position is within the block-syntax zone of its line. fn is_in_block_syntax_zone(&self, pos: usize) -> bool { - if pos == 0 { + if pos <= BLOCK_SYNTAX_ZONE { return true; } - let content_str = self.content.to_string(); - let mut last_newline_pos: Option = None; - - for (i, c) in content_str.chars().take(pos).enumerate() { - if c == '\n' { - last_newline_pos = Some(i); + // Search backwards from pos-1, only need to check BLOCK_SYNTAX_ZONE + 1 chars. + let search_start = pos.saturating_sub(BLOCK_SYNTAX_ZONE + 1); + for i in (search_start..pos).rev() { + if self.content.char_at(i).ok() == Some('\n') { + // Found newline at i, distance from line start is pos - i - 1. + return (pos - i - 1) <= BLOCK_SYNTAX_ZONE; } } - - let chars_from_line_start = match last_newline_pos { - Some(nl_pos) => pos - nl_pos - 1, - None => pos, - }; - - chars_from_line_start <= BLOCK_SYNTAX_ZONE + // No newline found in search range, and pos > BLOCK_SYNTAX_ZONE, so not in zone. + false } /// Create a new editor document with the given content. @@ -695,8 +690,11 @@ impl EditorDocument { /// Remove text range from content and record edit info for incremental rendering. pub fn remove_tracked(&mut self, start: usize, len: usize) -> LoroResult<()> { - let content_str = self.content.to_string(); - let contains_newline = content_str.chars().skip(start).take(len).any(|c| c == '\n'); + let contains_newline = self + .content + .slice(start, start + len) + .map(|s| s.contains('\n')) + .unwrap_or(false); let in_block_syntax_zone = self.is_in_block_syntax_zone(start); let result = self.content.delete(start, len); @@ -714,8 +712,11 @@ impl EditorDocument { /// Replace text in content (delete then insert) and record combined edit info. pub fn replace_tracked(&mut self, start: usize, len: usize, text: &str) -> LoroResult<()> { - let content_str = self.content.to_string(); - let delete_has_newline = content_str.chars().skip(start).take(len).any(|c| c == '\n'); + let delete_has_newline = self + .content + .slice(start, start + len) + .map(|s| s.contains('\n')) + .unwrap_or(false); let in_block_syntax_zone = self.is_in_block_syntax_zone(start); let len_before = self.content.len_unicode(); diff --git a/crates/weaver-editor-crdt/src/buffer.rs b/crates/weaver-editor-crdt/src/buffer.rs index 7e7b47d..33c5777 100644 --- a/crates/weaver-editor-crdt/src/buffer.rs +++ b/crates/weaver-editor-crdt/src/buffer.rs @@ -4,7 +4,7 @@ use std::cell::RefCell; use std::ops::Range; use std::rc::Rc; -use loro::{LoroDoc, LoroText, UndoManager as LoroUndoManager, VersionVector}; +use loro::{cursor::PosType, LoroDoc, LoroText, UndoManager as LoroUndoManager, VersionVector}; use smol_str::{SmolStr, ToSmolStr}; use weaver_editor_core::{TextBuffer, UndoManager}; @@ -110,7 +110,7 @@ impl Default for LoroTextBuffer { impl TextBuffer for LoroTextBuffer { fn len_bytes(&self) -> usize { - self.content.to_string().len() + self.content.len_utf8() } fn len_chars(&self) -> usize { @@ -126,20 +126,17 @@ impl TextBuffer for LoroTextBuffer { } fn slice(&self, char_range: Range) -> Option { - let s = self.content.to_string(); - let chars: Vec = s.chars().collect(); - - if char_range.end > chars.len() { + if char_range.end > self.content.len_unicode() { return None; } - - let slice: String = chars[char_range].iter().collect(); - Some(slice.to_smolstr()) + self.content + .slice(char_range.start, char_range.end) + .ok() + .map(|s| s.to_smolstr()) } fn char_at(&self, char_offset: usize) -> Option { - let s = self.content.to_string(); - s.chars().nth(char_offset) + self.content.char_at(char_offset).ok() } fn to_string(&self) -> String { @@ -147,16 +144,15 @@ impl TextBuffer for LoroTextBuffer { } fn char_to_byte(&self, char_offset: usize) -> usize { - let s = self.content.to_string(); - s.char_indices() - .nth(char_offset) - .map(|(i, _)| i) - .unwrap_or(s.len()) + self.content + .convert_pos(char_offset, PosType::Unicode, PosType::Bytes) + .unwrap_or(self.content.len_utf8()) } fn byte_to_char(&self, byte_offset: usize) -> usize { - let s = self.content.to_string(); - s[..byte_offset.min(s.len())].chars().count() + self.content + .convert_pos(byte_offset, PosType::Bytes, PosType::Unicode) + .unwrap_or(self.content.len_unicode()) } } diff --git a/docs/graph-data.json b/docs/graph-data.json index 7933d53..8467891 100644 --- a/docs/graph-data.json +++ b/docs/graph-data.json @@ -1704,6 +1704,50 @@ "created_at": "2026-01-06T14:36:23.710415853-05:00", "updated_at": "2026-01-06T14:36:23.710415853-05:00", "metadata_json": "{\"confidence\":95}" + }, + { + "id": 157, + "change_id": "e1666f70-0b8a-4a9e-9716-bd550899e65c", + "node_type": "action", + "title": "Moved fetch_blob to WeaverExt trait in weaver-common", + "description": null, + "status": "pending", + "created_at": "2026-01-06T15:16:33.868867777-05:00", + "updated_at": "2026-01-06T15:16:33.868867777-05:00", + "metadata_json": "{\"confidence\":95}" + }, + { + "id": 158, + "change_id": "89ef84b5-0909-4367-8644-4392c1cf4c3a", + "node_type": "action", + "title": "Added use-index cfg gates to crdt sync functions (find_all_edit_roots, list_drafts)", + "description": null, + "status": "pending", + "created_at": "2026-01-06T15:16:33.886464900-05:00", + "updated_at": "2026-01-06T15:16:33.886464900-05:00", + "metadata_json": "{\"confidence\":95}" + }, + { + "id": 159, + "change_id": "f3f310fb-78e3-4178-98a3-20d55e20c85a", + "node_type": "action", + "title": "Refactored app sync.rs - removed ~1100 lines of duplicated code, re-exports crdt types", + "description": null, + "status": "pending", + "created_at": "2026-01-06T15:16:33.904856197-05:00", + "updated_at": "2026-01-06T15:16:33.904856197-05:00", + "metadata_json": "{\"confidence\":95}" + }, + { + "id": 160, + "change_id": "da49b35a-6ca3-4d59-a1f8-84f359693565", + "node_type": "outcome", + "title": "Sync extraction complete - app sync.rs now thin wrapper over weaver-editor-crdt, both use-index and non-use-index paths compile", + "description": null, + "status": "pending", + "created_at": "2026-01-06T15:16:33.936978250-05:00", + "updated_at": "2026-01-06T15:16:33.936978250-05:00", + "metadata_json": "{\"confidence\":95}" } ], "edges": [ @@ -3477,6 +3521,50 @@ "weight": 1.0, "rationale": "Completed fix", "created_at": "2026-01-06T14:36:23.726201054-05:00" + }, + { + "id": 163, + "from_node_id": 153, + "to_node_id": 157, + "from_change_id": "f357a30f-0000-4b09-bb35-fe6c23b1dccf", + "to_change_id": "e1666f70-0b8a-4a9e-9716-bd550899e65c", + "edge_type": "leads_to", + "weight": 1.0, + "rationale": "fetch_blob needed for crdt sync to work standalone", + "created_at": "2026-01-06T15:16:39.378273071-05:00" + }, + { + "id": 164, + "from_node_id": 157, + "to_node_id": 158, + "from_change_id": "e1666f70-0b8a-4a9e-9716-bd550899e65c", + "to_change_id": "89ef84b5-0909-4367-8644-4392c1cf4c3a", + "edge_type": "leads_to", + "weight": 1.0, + "rationale": "cfg gates next step after fetch_blob available", + "created_at": "2026-01-06T15:16:39.394339283-05:00" + }, + { + "id": 165, + "from_node_id": 158, + "to_node_id": 159, + "from_change_id": "89ef84b5-0909-4367-8644-4392c1cf4c3a", + "to_change_id": "f3f310fb-78e3-4178-98a3-20d55e20c85a", + "edge_type": "leads_to", + "weight": 1.0, + "rationale": "app sync.rs refactor follows crdt having both paths", + "created_at": "2026-01-06T15:16:39.410629272-05:00" + }, + { + "id": 166, + "from_node_id": 159, + "to_node_id": 160, + "from_change_id": "f3f310fb-78e3-4178-98a3-20d55e20c85a", + "to_change_id": "da49b35a-6ca3-4d59-a1f8-84f359693565", + "edge_type": "leads_to", + "weight": 1.0, + "rationale": "refactor completed successfully", + "created_at": "2026-01-06T15:16:39.426610084-05:00" } ] } \ No newline at end of file -- knotmirror 0.0.1