Skip to content

Commit

Permalink
Fix the bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneToIgnore committed Feb 17, 2025
1 parent 48b8c73 commit adb0d32
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
8 changes: 6 additions & 2 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,7 +2193,7 @@ impl Editor {
let editor_id = cx.entity().entity_id().as_u64() as ItemId;
let snapshot = self.buffer().read(cx).snapshot(cx);
self.serialize_selections = cx.background_spawn(async move {
background_executor.timer(UPDATE_DEBOUNCE).await;
background_executor.timer(Duration::from_millis(100)).await;
let selections = selections
.iter()
.map(|selection| {
Expand Down Expand Up @@ -14906,7 +14906,8 @@ impl Editor {
self.load_diff_task.clone()
}

// TODO kb allow to turn it off in the settings
// TODO kb allow to turn it off in the settings.
// TODO kb does not work when keeping the editor open, and close && go back the same editor.
fn read_selections_from_db(
&mut self,
item_id: u64,
Expand All @@ -14917,6 +14918,9 @@ impl Editor {
let Some(selections) = DB.get_editor_selections(item_id, workspace_id).log_err() else {
return;
};
if selections.is_empty() {
return;
}

self.change_selections(None, window, cx, |s| {
s.select_ranges(selections.into_iter().map(|(start, end)| start..end));
Expand Down
22 changes: 8 additions & 14 deletions crates/editor/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ define_connection!(
end INTEGER NOT NULL,
PRIMARY KEY(item_id),
FOREIGN KEY(editor_id, workspace_id) REFERENCES editors(item_id, workspace_id)
ON DELETE CASCADE
) STRICT;
),
];
Expand Down Expand Up @@ -211,8 +212,8 @@ impl EditorDb {
workspace_id: WorkspaceId
) -> Result<Vec<(usize, usize)>> {
SELECT start, end
FROM selections
WHERE item_id = ?1 AND workspace_id = ?2
FROM editor_selections
WHERE editor_id = ?1 AND workspace_id = ?2
}
}

Expand All @@ -224,7 +225,7 @@ impl EditorDb {
) -> Result<()> {
let mut first_selection;
let mut last_selection = 0_usize;
for (count, placeholders) in std::iter::once("(?, ?, ?, ?)")
for (count, placeholders) in std::iter::once("(?1, ?2, ?, ?)")
.cycle()
.take(selections.len())
.chunks(MAX_QUERY_PLACEHOLDERS / 4)
Expand All @@ -244,16 +245,11 @@ impl EditorDb {
last_selection = last_selection + count;
let query = format!(
r#"
BEGIN TRANSACTION;
DELETE FROM editor_selections WHERE item_id = ?1 AND workspace_id = ?2;
INSERT INTO selections (item_id, workspace_id, start, end)
VALUES ({placeholders});
COMMIT;
DELETE FROM editor_selections WHERE editor_id = ?1 AND workspace_id = ?2;
DELETE FROM editors WHERE workspace_id = ? AND item_id NOT IN ({placeholders})"#
INSERT INTO editor_selections (editor_id, workspace_id, start, end)
VALUES {placeholders};
"#
);

let selections = selections[first_selection..last_selection].to_vec();
Expand All @@ -262,8 +258,6 @@ impl EditorDb {
statement.bind(&editor_id, 1)?;
let mut next_index = statement.bind(&workspace_id, 2)?;
for (start, end) in selections {
next_index = statement.bind(&editor_id, next_index)?;
next_index = statement.bind(&workspace_id, next_index)?;
next_index = statement.bind(&start, next_index)?;
next_index = statement.bind(&end, next_index)?;
}
Expand Down

0 comments on commit adb0d32

Please sign in to comment.