Skip to content

Commit d980739

Browse files
the-mikedavisFrederik Vestre
authored and
Frederik Vestre
committed
Select diagnostic range in goto_*_diag commands (helix-editor#4713)
This roughly matches the behavior of the diagnostic picker: when jumping to a diagnostic with `[d`/`]d`/`[D`/`]D`, the range of the diagnostic is selected instead of the start point.
1 parent 4526b11 commit d980739

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

helix-term/src/commands.rs

+22-28
Original file line numberDiff line numberDiff line change
@@ -2789,35 +2789,28 @@ fn exit_select_mode(cx: &mut Context) {
27892789
}
27902790
}
27912791

2792-
fn goto_pos(editor: &mut Editor, pos: usize) {
2793-
let (view, doc) = current!(editor);
2794-
2795-
push_jump(view, doc);
2796-
doc.set_selection(view.id, Selection::point(pos));
2797-
align_view(doc, view, Align::Center);
2798-
}
2799-
28002792
fn goto_first_diag(cx: &mut Context) {
2801-
let doc = doc!(cx.editor);
2802-
let pos = match doc.diagnostics().first() {
2803-
Some(diag) => diag.range.start,
2793+
let (view, doc) = current!(cx.editor);
2794+
let selection = match doc.diagnostics().first() {
2795+
Some(diag) => Selection::single(diag.range.start, diag.range.end),
28042796
None => return,
28052797
};
2806-
goto_pos(cx.editor, pos);
2798+
doc.set_selection(view.id, selection);
2799+
align_view(doc, view, Align::Center);
28072800
}
28082801

28092802
fn goto_last_diag(cx: &mut Context) {
2810-
let doc = doc!(cx.editor);
2811-
let pos = match doc.diagnostics().last() {
2812-
Some(diag) => diag.range.start,
2803+
let (view, doc) = current!(cx.editor);
2804+
let selection = match doc.diagnostics().last() {
2805+
Some(diag) => Selection::single(diag.range.start, diag.range.end),
28132806
None => return,
28142807
};
2815-
goto_pos(cx.editor, pos);
2808+
doc.set_selection(view.id, selection);
2809+
align_view(doc, view, Align::Center);
28162810
}
28172811

28182812
fn goto_next_diag(cx: &mut Context) {
2819-
let editor = &mut cx.editor;
2820-
let (view, doc) = current!(editor);
2813+
let (view, doc) = current!(cx.editor);
28212814

28222815
let cursor_pos = doc
28232816
.selection(view.id)
@@ -2830,17 +2823,16 @@ fn goto_next_diag(cx: &mut Context) {
28302823
.find(|diag| diag.range.start > cursor_pos)
28312824
.or_else(|| doc.diagnostics().first());
28322825

2833-
let pos = match diag {
2834-
Some(diag) => diag.range.start,
2826+
let selection = match diag {
2827+
Some(diag) => Selection::single(diag.range.start, diag.range.end),
28352828
None => return,
28362829
};
2837-
2838-
goto_pos(editor, pos);
2830+
doc.set_selection(view.id, selection);
2831+
align_view(doc, view, Align::Center);
28392832
}
28402833

28412834
fn goto_prev_diag(cx: &mut Context) {
2842-
let editor = &mut cx.editor;
2843-
let (view, doc) = current!(editor);
2835+
let (view, doc) = current!(cx.editor);
28442836

28452837
let cursor_pos = doc
28462838
.selection(view.id)
@@ -2854,12 +2846,14 @@ fn goto_prev_diag(cx: &mut Context) {
28542846
.find(|diag| diag.range.start < cursor_pos)
28552847
.or_else(|| doc.diagnostics().last());
28562848

2857-
let pos = match diag {
2858-
Some(diag) => diag.range.start,
2849+
let selection = match diag {
2850+
// NOTE: the selection is reversed because we're jumping to the
2851+
// previous diagnostic.
2852+
Some(diag) => Selection::single(diag.range.end, diag.range.start),
28592853
None => return,
28602854
};
2861-
2862-
goto_pos(editor, pos);
2855+
doc.set_selection(view.id, selection);
2856+
align_view(doc, view, Align::Center);
28632857
}
28642858

28652859
fn goto_first_change(cx: &mut Context) {

0 commit comments

Comments
 (0)