Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Fix `DisplayMap::highlight_text` not always sorting its ranges (#58260)

Lukas Wirth06065c072915
diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs
index 143963a7f5e..5ba59083cf9 100644
--- a/crates/editor/src/display_map.rs
+++ b/crates/editor/src/display_map.rs
@@ -1150,15 +1150,22 @@ impl DisplayMap {
Some((previous_style, previous_ranges)) => {
*previous_style = style;
*previous_ranges = ranges;
+ previous_ranges.sort_by(|a, b| a.start.cmp(&b.start, &multi_buffer_snapshot));
}
None if merge => {
ranges.extend(slot.get().1.iter().cloned());
ranges.sort_by(|a, b| a.start.cmp(&b.start, &multi_buffer_snapshot));
slot.insert(Arc::new((style, ranges)));
}
- None => _ = slot.insert(Arc::new((style, ranges))),
+ None => {
+ ranges.sort_by(|a, b| a.start.cmp(&b.start, &multi_buffer_snapshot));
+ slot.insert(Arc::new((style, ranges)));
+ }
},
- Entry::Vacant(slot) => _ = slot.insert(Arc::new((style, ranges))),
+ Entry::Vacant(slot) => {
+ ranges.sort_by(|a, b| a.start.cmp(&b.start, &multi_buffer_snapshot));
+ slot.insert(Arc::new((style, ranges)));
+ }
}
}
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index e7f06bcacf5..10fb420017b 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -29305,6 +29305,72 @@ async fn test_rename_with_duplicate_edits(cx: &mut TestAppContext) {
"});
}
+#[gpui::test]
+async fn test_rename_with_out_of_order_document_highlights(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let capabilities = lsp::ServerCapabilities {
+ rename_provider: Some(lsp::OneOf::Right(lsp::RenameOptions {
+ prepare_provider: Some(true),
+ work_done_progress_options: Default::default(),
+ })),
+ ..Default::default()
+ };
+ let mut cx = EditorLspTestContext::new_rust(capabilities, cx).await;
+
+ cx.set_state(indoc! {"
+ struct Foo {}
+ fn main() {
+ let first = Foo {};
+ let second = Fˇoo {};
+ }
+ "});
+
+ cx.update_editor(|editor, _window, cx| {
+ let snapshot = editor.buffer().read(cx).snapshot(cx);
+ let read_highlight = (Point::new(2, 16)..Point::new(2, 19)).to_anchors(&snapshot);
+ let write_highlight = (Point::new(3, 17)..Point::new(3, 20)).to_anchors(&snapshot);
+ editor.highlight_background(
+ HighlightKey::DocumentHighlightRead,
+ &[read_highlight],
+ |_, theme| theme.colors().editor_document_highlight_read_background,
+ cx,
+ );
+ editor.highlight_background(
+ HighlightKey::DocumentHighlightWrite,
+ &[write_highlight],
+ |_, theme| theme.colors().editor_document_highlight_write_background,
+ cx,
+ );
+ });
+
+ let mut prepare_rename_handler = cx
+ .set_request_handler::<lsp::request::PrepareRenameRequest, _, _>(
+ move |_, _, _| async move {
+ Ok(Some(lsp::PrepareRenameResponse::Range(lsp::Range {
+ start: lsp::Position {
+ line: 3,
+ character: 17,
+ },
+ end: lsp::Position {
+ line: 3,
+ character: 20,
+ },
+ })))
+ },
+ );
+ let prepare_rename_task = cx
+ .update_editor(|e, window, cx| e.rename(&Rename, window, cx))
+ .expect("Prepare rename was not started");
+ prepare_rename_handler.next().await.unwrap();
+ prepare_rename_task.await.expect("Prepare rename failed");
+
+ cx.update_editor(|editor, window, cx| {
+ editor
+ .snapshot(window, cx)
+ .layout_row(DisplayRow(2), &editor.text_layout_details(window, cx));
+ });
+}
+
#[gpui::test]
async fn test_rename_without_prepare(cx: &mut TestAppContext) {
init_test(cx, |_| {});