Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new editor.enable-diagnostics option #12203

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@
| `:move`, `:mv` | Move the current buffer and its corresponding file to a different path |
| `:yank-diagnostic` | Yank diagnostic(s) under primary cursor to register, or clipboard by default |
| `:read`, `:r` | Load a file into buffer |
| `:toggle-diagnostics`, `:td` | Toggle Diagnostics |
25 changes: 25 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,24 @@ fn set_option(
Ok(())
}

fn toggle_diagnostics(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

ensure!(args.is_empty(), ":toggle-diagnostics takes no arguments");

let (view, _) = current!(cx.editor);
view.diagnostics_handler.toggle_active();
cx.editor.toggle_diagnostics();

Ok(())
}

/// Toggle boolean config option at runtime. Access nested values by dot
/// syntax, for example to toggle smart case search, use `:toggle search.smart-
/// case`.
Expand Down Expand Up @@ -3149,6 +3167,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: read,
signature: CommandSignature::positional(&[completers::filename]),
},
TypableCommand {
name: "toggle-diagnostics",
aliases: &["td"],
doc: "Toggle Diagnostics",
fun: toggle_diagnostics,
signature: CommandSignature::all(completers::register)
}
];

pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
Expand Down
32 changes: 20 additions & 12 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,26 @@ impl EditorView {
primary_cursor,
});
}
let width = view.inner_width(doc);
let config = doc.config.load();
let enable_cursor_line = view
.diagnostics_handler
.show_cursorline_diagnostics(doc, view.id);
let inline_diagnostic_config = config.inline_diagnostics.prepare(width, enable_cursor_line);
decorations.add_decoration(InlineDiagnostics::new(
doc,
theme,
primary_cursor,
inline_diagnostic_config,
config.end_of_line_diagnostics,
));

if editor.show_diagnostics {
log::error!("{:#?}", editor.show_diagnostics);
let width = view.inner_width(doc);
let enable_cursor_line = view
.diagnostics_handler
.show_cursorline_diagnostics(doc, view.id);
let inline_diagnostic_config =
config.inline_diagnostics.prepare(width, enable_cursor_line);

decorations.add_decoration(InlineDiagnostics::new(
doc,
theme,
primary_cursor,
inline_diagnostic_config,
config.end_of_line_diagnostics,
));
}

render_document(
surface,
inner,
Expand Down Expand Up @@ -229,6 +236,7 @@ impl EditorView {

if config.inline_diagnostics.disabled()
&& config.end_of_line_diagnostics == DiagnosticFilter::Disable
&& editor.show_diagnostics
{
Self::render_diagnostics(doc, view, inner, surface, theme);
}
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/ui/text_decorations/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl Renderer<'_, '_> {
end_col - start_col
}

// need to toggle this
NikitaRevenco marked this conversation as resolved.
Show resolved Hide resolved
fn draw_diagnostic(&mut self, diag: &Diagnostic, col: u16, next_severity: Option<Severity>) {
let severity = diag.severity();
let (sym, sym_severity) = if let Some(next_severity) = next_severity {
Expand Down
7 changes: 7 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,8 @@ pub struct Editor {
pub debugger_events: SelectAll<UnboundedReceiverStream<dap::Payload>>,
pub breakpoints: HashMap<PathBuf, Vec<Breakpoint>>,

pub show_diagnostics: bool,

pub syn_loader: Arc<ArcSwap<syntax::Loader>>,
pub theme_loader: Arc<theme::Loader>,
/// last_theme is used for theme previews. We store the current theme here,
Expand Down Expand Up @@ -1195,6 +1197,7 @@ impl Editor {
breakpoints: HashMap::new(),
syn_loader,
theme_loader,
show_diagnostics: true,
last_theme: None,
last_selection: None,
registers: Registers::new(Box::new(arc_swap::access::Map::new(
Expand Down Expand Up @@ -1328,6 +1331,10 @@ impl Editor {
self.set_theme_impl(theme, ThemeAction::Set);
}

pub fn toggle_diagnostics(&mut self) {
self.show_diagnostics = !self.show_diagnostics;
}

fn set_theme_impl(&mut self, theme: Theme, preview: ThemeAction) {
// `ui.selection` is the only scope required to be able to render a theme.
if theme.find_scope_index_exact("ui.selection").is_none() {
Expand Down
5 changes: 5 additions & 0 deletions helix-view/src/handlers/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ impl DiagnosticsHandler {
self.active_generation
.store(self.generation.get(), atomic::Ordering::Relaxed);
}

pub fn toggle_active(&mut self) {
self.active = !self.active;
}

pub fn show_cursorline_diagnostics(&self, doc: &Document, view: ViewId) -> bool {
if !self.active {
return false;
Expand Down
Loading