Add diagnostics keybindings

imgbot
Wojciech Kępka 4 years ago committed by Blaž Hrastnik
parent 2066e866c7
commit 16b1cfa3be

@ -217,7 +217,7 @@ impl Application {
}) })
.collect(); .collect();
doc.diagnostics = diagnostics; doc.set_diagnostics(diagnostics);
// TODO: we want to process all the events in queue, then render. publishDiagnostic tends to send a whole bunch of events // TODO: we want to process all the events in queue, then render. publishDiagnostic tends to send a whole bunch of events
self.render(); self.render();
} }

@ -1536,6 +1536,86 @@ pub fn goto_reference(cx: &mut Context) {
); );
} }
fn goto_pos(editor: &mut Editor, pos: usize) {
push_jump(editor);
let (view, doc) = editor.current();
doc.set_selection(view.id, Selection::point(pos));
align_view(doc, view, Align::Center);
}
pub fn goto_first_diag(cx: &mut Context) {
let editor = &mut cx.editor;
let (view, doc) = editor.current();
let cursor_pos = doc.selection(view.id).cursor();
let diag = if let Some(diag) = doc.diagnostics().first() {
diag.range.start
} else {
return;
};
goto_pos(editor, diag);
}
pub fn goto_last_diag(cx: &mut Context) {
let editor = &mut cx.editor;
let (view, doc) = editor.current();
let cursor_pos = doc.selection(view.id).cursor();
let diag = if let Some(diag) = doc.diagnostics().last() {
diag.range.start
} else {
return;
};
goto_pos(editor, diag);
}
pub fn goto_next_diag(cx: &mut Context) {
let editor = &mut cx.editor;
let (view, doc) = editor.current();
let cursor_pos = doc.selection(view.id).cursor();
let diag = if let Some(diag) = doc
.diagnostics()
.iter()
.map(|diag| diag.range.start)
.find(|&pos| pos > cursor_pos)
{
diag
} else if let Some(diag) = doc.diagnostics().first() {
diag.range.start
} else {
return;
};
goto_pos(editor, diag);
}
pub fn goto_prev_diag(cx: &mut Context) {
let editor = &mut cx.editor;
let (view, doc) = editor.current();
let cursor_pos = doc.selection(view.id).cursor();
let diag = if let Some(diag) = doc
.diagnostics()
.iter()
.rev()
.map(|diag| diag.range.start)
.find(|&pos| pos < cursor_pos)
{
diag
} else if let Some(diag) = doc.diagnostics().last() {
diag.range.start
} else {
return;
};
goto_pos(editor, diag);
}
pub fn signature_help(cx: &mut Context) { pub fn signature_help(cx: &mut Context) {
let (view, doc) = cx.current(); let (view, doc) = cx.current();
@ -2433,3 +2513,35 @@ pub fn view_mode(cx: &mut Context) {
} }
}) })
} }
pub fn left_bracket_mode(cx: &mut Context) {
cx.on_next_key(move |cx, event| {
if let KeyEvent {
code: KeyCode::Char(ch),
..
} = event
{
match ch {
'd' => goto_prev_diag(cx),
'D' => goto_first_diag(cx),
_ => (),
}
}
})
}
pub fn right_bracket_mode(cx: &mut Context) {
cx.on_next_key(move |cx, event| {
if let KeyEvent {
code: KeyCode::Char(ch),
..
} = event
{
match ch {
'd' => goto_next_diag(cx),
'D' => goto_last_diag(cx),
_ => (),
}
}
})
}

@ -85,6 +85,10 @@ use std::collections::HashMap;
// //
// gd = goto definition // gd = goto definition
// gr = goto reference // gr = goto reference
// [d = previous diagnostic
// d] = next diagnostic
// [D = first diagnostic
// D] = last diagnostic
// } // }
// #[cfg(feature = "term")] // #[cfg(feature = "term")]
@ -209,7 +213,9 @@ pub fn default() -> Keymaps {
// repeat_select // repeat_select
// TODO: figure out what key to use // TODO: figure out what key to use
key!('[') => commands::expand_selection, // key!('[') => commands::expand_selection, ??
key!('[') => commands::left_bracket_mode,
key!(']') => commands::right_bracket_mode,
key!('/') => commands::search, key!('/') => commands::search,
// ? for search_reverse // ? for search_reverse

@ -195,7 +195,7 @@ impl EditorView {
} }
// ugh,interleave highlight spans with diagnostic spans // ugh,interleave highlight spans with diagnostic spans
let is_diagnostic = doc.diagnostics.iter().any(|diagnostic| { let is_diagnostic = doc.diagnostics().iter().any(|diagnostic| {
diagnostic.range.start <= char_index diagnostic.range.start <= char_index
&& diagnostic.range.end > char_index && diagnostic.range.end > char_index
}); });
@ -343,7 +343,7 @@ impl EditorView {
for (i, line) in (view.first_line..=last_line).enumerate() { for (i, line) in (view.first_line..=last_line).enumerate() {
use helix_core::diagnostic::Severity; use helix_core::diagnostic::Severity;
if let Some(diagnostic) = doc.diagnostics.iter().find(|d| d.line == line) { if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
surface.set_stringn( surface.set_stringn(
viewport.x - OFFSET, viewport.x - OFFSET,
viewport.y + i as u16, viewport.y + i as u16,
@ -387,7 +387,7 @@ impl EditorView {
let cursor = doc.selection(view.id).cursor(); let cursor = doc.selection(view.id).cursor();
let line = doc.text().char_to_line(cursor); let line = doc.text().char_to_line(cursor);
let diagnostics = doc.diagnostics.iter().filter(|diagnostic| { let diagnostics = doc.diagnostics().iter().filter(|diagnostic| {
diagnostic.range.start <= cursor && diagnostic.range.end >= cursor diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
}); });
@ -469,7 +469,7 @@ impl EditorView {
surface.set_stringn( surface.set_stringn(
viewport.x + viewport.width.saturating_sub(15), viewport.x + viewport.width.saturating_sub(15),
viewport.y, viewport.y,
format!("{}", doc.diagnostics.len()), format!("{}", doc.diagnostics().len()),
4, 4,
text_color, text_color,
); );

@ -48,7 +48,7 @@ pub struct Document {
last_saved_revision: usize, last_saved_revision: usize,
version: i32, // should be usize? version: i32, // should be usize?
pub diagnostics: Vec<Diagnostic>, diagnostics: Vec<Diagnostic>,
language_server: Option<Arc<helix_lsp::Client>>, language_server: Option<Arc<helix_lsp::Client>>,
} }
@ -519,6 +519,14 @@ impl Document {
pub fn versioned_identifier(&self) -> lsp::VersionedTextDocumentIdentifier { pub fn versioned_identifier(&self) -> lsp::VersionedTextDocumentIdentifier {
lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version) lsp::VersionedTextDocumentIdentifier::new(self.url().unwrap(), self.version)
} }
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
pub fn set_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) {
self.diagnostics = diagnostics;
}
} }
#[cfg(test)] #[cfg(test)]

Loading…
Cancel
Save