on master help sign in
Add diagnostics keybindings
commit16b1cfa3be2665e78bea6c876d3ced96dbfeb44f authorWojciech Kępka <wojtek.kepka@protonmail.com> dateSun 6 Jun 2021, 11:59 (5 years ago) parent2066e86
5 files changed+133−7
helix-term/src/application.rs+1−1 helix-term/src/commands.rs+112 helix-term/src/keymap.rs+7−1 helix-term/src/ui/editor.rs+4−4 helix-view/src/document.rs+9−1
helix-term/src/application.rs+1−1
@@ -217,7 +217,7 @@ impl Application {
217217 })
218218 .collect();
219219
220− doc.diagnostics = diagnostics;
220+ doc.set_diagnostics(diagnostics);
221221 // TODO: we want to process all the events in queue, then render. publishDiagnostic tends to send a whole bunch of events
222222 self.render();
223223 }
helix-term/src/commands.rs+112
@@ -1536,6 +1536,86 @@ pub fn goto_reference(cx: &mut Context) {
15361536 );
15371537}
15381538
1539+fn goto_pos(editor: &mut Editor, pos: usize) {
1540+ push_jump(editor);
1541+
1542+ let (view, doc) = editor.current();
1543+
1544+ doc.set_selection(view.id, Selection::point(pos));
1545+ align_view(doc, view, Align::Center);
1546+}
1547+
1548+pub fn goto_first_diag(cx: &mut Context) {
1549+ let editor = &mut cx.editor;
1550+ let (view, doc) = editor.current();
1551+
1552+ let cursor_pos = doc.selection(view.id).cursor();
1553+ let diag = if let Some(diag) = doc.diagnostics().first() {
1554+ diag.range.start
1555+ } else {
1556+ return;
1557+ };
1558+
1559+ goto_pos(editor, diag);
1560+}
1561+
1562+pub fn goto_last_diag(cx: &mut Context) {
1563+ let editor = &mut cx.editor;
1564+ let (view, doc) = editor.current();
1565+
1566+ let cursor_pos = doc.selection(view.id).cursor();
1567+ let diag = if let Some(diag) = doc.diagnostics().last() {
1568+ diag.range.start
1569+ } else {
1570+ return;
1571+ };
1572+
1573+ goto_pos(editor, diag);
1574+}
1575+
1576+pub fn goto_next_diag(cx: &mut Context) {
1577+ let editor = &mut cx.editor;
1578+ let (view, doc) = editor.current();
1579+
1580+ let cursor_pos = doc.selection(view.id).cursor();
1581+ let diag = if let Some(diag) = doc
1582+ .diagnostics()
1583+ .iter()
1584+ .map(|diag| diag.range.start)
1585+ .find(|&pos| pos > cursor_pos)
1586+ {
1587+ diag
1588+ } else if let Some(diag) = doc.diagnostics().first() {
1589+ diag.range.start
1590+ } else {
1591+ return;
1592+ };
1593+
1594+ goto_pos(editor, diag);
1595+}
1596+
1597+pub fn goto_prev_diag(cx: &mut Context) {
1598+ let editor = &mut cx.editor;
1599+ let (view, doc) = editor.current();
1600+
1601+ let cursor_pos = doc.selection(view.id).cursor();
1602+ let diag = if let Some(diag) = doc
1603+ .diagnostics()
1604+ .iter()
1605+ .rev()
1606+ .map(|diag| diag.range.start)
1607+ .find(|&pos| pos < cursor_pos)
1608+ {
1609+ diag
1610+ } else if let Some(diag) = doc.diagnostics().last() {
1611+ diag.range.start
1612+ } else {
1613+ return;
1614+ };
1615+
1616+ goto_pos(editor, diag);
1617+}
1618+
15391619pub fn signature_help(cx: &mut Context) {
15401620 let (view, doc) = cx.current();
15411621
@@ -2433,3 +2513,35 @@ pub fn view_mode(cx: &mut Context) {
24332513 }
24342514 })
24352515}
2516+
2517+pub fn left_bracket_mode(cx: &mut Context) {
2518+ cx.on_next_key(move |cx, event| {
2519+ if let KeyEvent {
2520+ code: KeyCode::Char(ch),
2521+ ..
2522+ } = event
2523+ {
2524+ match ch {
2525+ 'd' => goto_prev_diag(cx),
2526+ 'D' => goto_first_diag(cx),
2527+ _ => (),
2528+ }
2529+ }
2530+ })
2531+}
2532+
2533+pub fn right_bracket_mode(cx: &mut Context) {
2534+ cx.on_next_key(move |cx, event| {
2535+ if let KeyEvent {
2536+ code: KeyCode::Char(ch),
2537+ ..
2538+ } = event
2539+ {
2540+ match ch {
2541+ 'd' => goto_next_diag(cx),
2542+ 'D' => goto_last_diag(cx),
2543+ _ => (),
2544+ }
2545+ }
2546+ })
2547+}
helix-term/src/keymap.rs+7−1
@@ -85,6 +85,10 @@ use std::collections::HashMap;
8585//
8686// gd = goto definition
8787// gr = goto reference
88+// [d = previous diagnostic
89+// d] = next diagnostic
90+// [D = first diagnostic
91+// D] = last diagnostic
8892// }
8993
9094// #[cfg(feature = "term")]
@@ -209,7 +213,9 @@ pub fn default() -> Keymaps {
209213 // repeat_select
210214
211215 // TODO: figure out what key to use
212− key!('[') => commands::expand_selection,
216+ // key!('[') => commands::expand_selection, ??
217+ key!('[') => commands::left_bracket_mode,
218+ key!(']') => commands::right_bracket_mode,
213219
214220 key!('/') => commands::search,
215221 // ? for search_reverse
helix-term/src/ui/editor.rs+4−4
@@ -195,7 +195,7 @@ impl EditorView {
195195 }
196196
197197 // ugh,interleave highlight spans with diagnostic spans
198− let is_diagnostic = doc.diagnostics.iter().any(|diagnostic| {
198+ let is_diagnostic = doc.diagnostics().iter().any(|diagnostic| {
199199 diagnostic.range.start <= char_index
200200 && diagnostic.range.end > char_index
201201 });
@@ -343,7 +343,7 @@ impl EditorView {
343343
344344 for (i, line) in (view.first_line..=last_line).enumerate() {
345345 use helix_core::diagnostic::Severity;
346− if let Some(diagnostic) = doc.diagnostics.iter().find(|d| d.line == line) {
346+ if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
347347 surface.set_stringn(
348348 viewport.x - OFFSET,
349349 viewport.y + i as u16,
@@ -387,7 +387,7 @@ impl EditorView {
387387 let cursor = doc.selection(view.id).cursor();
388388 let line = doc.text().char_to_line(cursor);
389389
390− let diagnostics = doc.diagnostics.iter().filter(|diagnostic| {
390+ let diagnostics = doc.diagnostics().iter().filter(|diagnostic| {
391391 diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
392392 });
393393
@@ -469,7 +469,7 @@ impl EditorView {
469469 surface.set_stringn(
470470 viewport.x + viewport.width.saturating_sub(15),
471471 viewport.y,
472− format!("{}", doc.diagnostics.len()),
472+ format!("{}", doc.diagnostics().len()),
473473 4,
474474 text_color,
475475 );
helix-view/src/document.rs+9−1
@@ -48,7 +48,7 @@ pub struct Document {
4848 last_saved_revision: usize,
4949 version: i32, // should be usize?
5050
51− pub diagnostics: Vec<Diagnostic>,
51+ diagnostics: Vec<Diagnostic>,
5252 language_server: Option<Arc<helix_lsp::Client>>,
5353}
5454
@@ -519,6 +519,14 @@ impl Document {
519519 pub fn versioned_identifier(&self) -> lsp::VersionedTextDocumentIdentifier {
520520 lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version)
521521 }
522+
523+ pub fn diagnostics(&self) -> &[Diagnostic] {
524+ &self.diagnostics
525+ }
526+
527+ pub fn set_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) {
528+ self.diagnostics = diagnostics;
529+ }
522530}
523531
524532#[cfg(test)]