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: Add line range support to editor::CopyFileLocation command (#51328)

Shashank Suresh4d5e25f40880
diff --git a/crates/collab/tests/integration/editor_tests.rs b/crates/collab/tests/integration/editor_tests.rs
index 0d0569182d5..6b23780156e 100644
--- a/crates/collab/tests/integration/editor_tests.rs
+++ b/crates/collab/tests/integration/editor_tests.rs
@@ -4721,6 +4721,54 @@ async fn test_copy_file_location(cx_a: &mut TestAppContext, cx_b: &mut TestAppCo
cx_b.read_from_clipboard().and_then(|item| item.text()),
Some(format!("{}:2", path!("src/main.rs")))
);
+
+ editor_a.update_in(cx_a, |editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |s| {
+ s.select_ranges([MultiBufferOffset(16)..MultiBufferOffset(44)]);
+ });
+ editor.copy_file_location(&CopyFileLocation, window, cx);
+ });
+
+ assert_eq!(
+ cx_a.read_from_clipboard().and_then(|item| item.text()),
+ Some(format!("{}:2-3", path!("src/main.rs")))
+ );
+
+ editor_b.update_in(cx_b, |editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |s| {
+ s.select_ranges([MultiBufferOffset(16)..MultiBufferOffset(44)]);
+ });
+ editor.copy_file_location(&CopyFileLocation, window, cx);
+ });
+
+ assert_eq!(
+ cx_b.read_from_clipboard().and_then(|item| item.text()),
+ Some(format!("{}:2-3", path!("src/main.rs")))
+ );
+
+ editor_a.update_in(cx_a, |editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |s| {
+ s.select_ranges([MultiBufferOffset(16)..MultiBufferOffset(43)]);
+ });
+ editor.copy_file_location(&CopyFileLocation, window, cx);
+ });
+
+ assert_eq!(
+ cx_a.read_from_clipboard().and_then(|item| item.text()),
+ Some(format!("{}:2", path!("src/main.rs")))
+ );
+
+ editor_b.update_in(cx_b, |editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |s| {
+ s.select_ranges([MultiBufferOffset(16)..MultiBufferOffset(43)]);
+ });
+ editor.copy_file_location(&CopyFileLocation, window, cx);
+ });
+
+ assert_eq!(
+ cx_b.read_from_clipboard().and_then(|item| item.text()),
+ Some(format!("{}:2", path!("src/main.rs")))
+ );
}
#[track_caller]
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index dc2696eb2ca..18a02e9773b 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -22846,18 +22846,28 @@ impl Editor {
_: &mut Window,
cx: &mut Context<Self>,
) {
- let selection = self
- .selections
- .newest::<Point>(&self.display_snapshot(cx))
- .start
- .row
- + 1;
+ let selection = self.selections.newest::<Point>(&self.display_snapshot(cx));
+
+ let start_line = selection.start.row + 1;
+ let end_line = selection.end.row + 1;
+
+ let end_line = if selection.end.column == 0 && end_line > start_line {
+ end_line - 1
+ } else {
+ end_line
+ };
+
if let Some(file_location) = self.active_excerpt(cx).and_then(|(_, buffer, _)| {
let project = self.project()?.read(cx);
let file = buffer.read(cx).file()?;
let path = file.path().display(project.path_style(cx));
- Some(format!("{path}:{selection}"))
+ let location = if start_line == end_line {
+ format!("{path}:{start_line}")
+ } else {
+ format!("{path}:{start_line}-{end_line}")
+ };
+ Some(location)
}) {
cx.write_to_clipboard(ClipboardItem::new_string(file_location));
}