@@ -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]
@@ -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));
}