on master help sign in
Pass editor into render_view & gutter, reducing the number of params
commit3633f85b382d6f3e6324d08d880a8466aee67fa1 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 30 Nov 2021, 16:47 (5 years ago) parent9ed930b
3 files changed+82−107
helix-term/src/commands/dap.rs+1−1 helix-term/src/ui/editor.rs+76−101 helix-view/src/gutter.rs+5−5
helix-term/src/commands/dap.rs+1−1
@@ -429,7 +429,7 @@ pub fn dap_toggle_breakpoint_impl(cx: &mut Context, path: std::path::PathBuf, li
429429 Some(debugger) => debugger,
430430 None => return,
431431 };
432− let request = debugger.set_breakpoints(path.clone(), breakpoints);
432+ let request = debugger.set_breakpoints(path, breakpoints);
433433 match block_on(request) {
434434 Ok(Some(breakpoints)) => {
435435 // TODO: handle breakpoint.message
helix-term/src/ui/editor.rs+76−101
@@ -63,20 +63,18 @@ impl EditorView {
6363 #[allow(clippy::too_many_arguments)]
6464 pub fn render_view(
6565 &self,
66+ editor: &Editor,
6667 doc: &Document,
6768 view: &View,
6869 viewport: Rect,
6970 surface: &mut Surface,
70− theme: &Theme,
7171 is_focused: bool,
7272 loader: &syntax::Loader,
73− config: &helix_view::editor::Config,
74− debugger: &Option<helix_dap::Client>,
75− all_breakpoints: &HashMap<PathBuf, Vec<SourceBreakpoint>>,
76− dbg_breakpoints: &Option<Vec<Breakpoint>>,
7773 ) {
7874 let inner = view.inner_area();
7975 let area = view.area;
76+ let theme = &editor.theme;
77+ let all_breakpoints = HashMap::new();
8078
8179 let highlights = Self::doc_syntax_highlights(doc, view.offset, inner.height, theme, loader);
8280 let highlights = syntax::merge(highlights, Self::doc_diagnostics_highlights(doc, theme));
@@ -90,18 +88,7 @@ impl EditorView {
9088 };
9189
9290 Self::render_text_highlights(doc, view.offset, inner, surface, theme, highlights);
93− Self::render_gutter(
94− doc,
95− view,
96− view.area,
97− surface,
98− theme,
99− is_focused,
100− config,
101− debugger,
102− all_breakpoints,
103− dbg_breakpoints,
104− );
91+ Self::render_gutter(editor, doc, view, view.area, surface, theme, is_focused);
10592
10693 if is_focused {
10794 Self::render_focused_view_elements(view, doc, inner, theme, surface);
@@ -120,7 +107,7 @@ impl EditorView {
120107 }
121108 }
122109
123− self.render_diagnostics(doc, view, inner, surface, theme, all_breakpoints);
110+ self.render_diagnostics(doc, view, inner, surface, theme, &all_breakpoints);
124111
125112 let statusline_area = view
126113 .area
@@ -413,18 +400,14 @@ impl EditorView {
413400 }
414401 }
415402
416− #[allow(clippy::too_many_arguments)]
417403 pub fn render_gutter(
404+ editor: &Editor,
418405 doc: &Document,
419406 view: &View,
420407 viewport: Rect,
421408 surface: &mut Surface,
422409 theme: &Theme,
423410 is_focused: bool,
424− config: &helix_view::editor::Config,
425− debugger: &Option<helix_dap::Client>,
426− all_breakpoints: &HashMap<PathBuf, Vec<SourceBreakpoint>>,
427− dbg_breakpoints: &Option<Vec<Breakpoint>>,
428411 ) {
429412 let text = doc.text().slice(..);
430413 let last_line = view.last_line(doc);
@@ -438,8 +421,10 @@ impl EditorView {
438421 .map(|range| range.cursor_line(text))
439422 .collect();
440423
424+ use helix_view::editor::Config;
441425 use helix_view::gutter::GutterFn;
442426 fn breakpoints<'doc>(
427+ editor: &'doc Editor,
443428 doc: &'doc Document,
444429 _view: &View,
445430 theme: &Theme,
@@ -447,14 +432,77 @@ impl EditorView {
447432 _is_focused: bool,
448433 _width: usize,
449434 ) -> GutterFn<'doc> {
435+ let warning = theme.get("warning");
436+ let error = theme.get("error");
437+ let info = theme.get("info");
438+
439+ let all_breakpoints = &editor.breakpoints;
440+
441+ // TODO: debugger breakpoints should keep editor level breakpoints in sync
442+
443+ let breakpoints = doc
444+ .path()
445+ .and_then(|path| all_breakpoints.get(path))
446+ .unwrap();
447+
450448 Box::new(move |line: usize, _selected: bool, out: &mut String| {
451− //
449+ // TODO: debugger should translate received breakpoints to 0-indexing
450+ let breakpoint = breakpoints.iter().find(|breakpoint| {
451+ false
452+ // if breakpoint.source == doc.path() {
453+ // match (breakpoint.line, breakpoint.end_line) {
454+ // #[allow(clippy::int_plus_one)]
455+ // (Some(l), Some(el)) => l - 1 <= line && line <= el - 1,
456+ // (Some(l), None) => l - 1 == line,
457+ // _ => false,
458+ // }
459+ // } else {
460+ // false
461+ // }
462+ });
463+
464+ let breakpoint = match breakpoint {
465+ Some(b) => b,
466+ None => return None,
467+ };
468+
469+ // let verified = breakpoint.verified;
470+ let verified = false;
471+
472+ let mut style =
473+ if breakpoint.condition.is_some() && breakpoint.log_message.is_some() {
474+ error.add_modifier(Modifier::UNDERLINED)
475+ } else if breakpoint.condition.is_some() {
476+ error
477+ } else if breakpoint.log_message.is_some() {
478+ info
479+ } else {
480+ warning
481+ };
482+
483+ if !verified {
484+ // Faded colors
485+ style = if let Some(Color::Rgb(r, g, b)) = style.fg {
486+ style.fg(Color::Rgb(
487+ ((r as f32) * 0.4).floor() as u8,
488+ ((g as f32) * 0.4).floor() as u8,
489+ ((b as f32) * 0.4).floor() as u8,
490+ ))
491+ } else {
492+ style.fg(Color::Gray)
493+ }
494+ };
495+
496+ // TODO: also handle breakpoints only present in the user struct
497+ use std::fmt::Write;
498+ let sym = if verified { "▲" } else { "⊚" };
499+ write!(out, "{}", sym).unwrap();
500+ Some(style)
452501 })
453502 }
454− // let mut breakpoints: Option<&Vec<SourceBreakpoint>> = None;
503+
455504 // let mut stack_frame: Option<&StackFrame> = None;
456505 // if let Some(path) = doc.path() {
457− // breakpoints = all_breakpoints.get(path);
458506 // if let Some(debugger) = debugger {
459507 // // if we have a frame, and the frame path matches document
460508 // if let (Some(frame), Some(thread_id)) = (debugger.active_frame, debugger.thread_id)
@@ -475,66 +523,6 @@ impl EditorView {
475523 // };
476524 // }
477525 // }
478−
479− // TODO: debugger should translate received breakpoints to 0-indexing
480−
481− // if let Some(user) = breakpoints.as_ref() {
482− // let debugger_breakpoint = if let Some(debugger) = dbg_breakpoints.as_ref() {
483− // debugger.iter().find(|breakpoint| {
484− // if breakpoint.source.is_some()
485− // && doc.path().is_some()
486− // && breakpoint.source.as_ref().unwrap().path == doc.path().cloned()
487− // {
488− // match (breakpoint.line, breakpoint.end_line) {
489− // #[allow(clippy::int_plus_one)]
490− // (Some(l), Some(el)) => l - 1 <= line && line <= el - 1,
491− // (Some(l), None) => l - 1 == line,
492− // _ => false,
493− // }
494− // } else {
495− // false
496− // }
497− // })
498− // } else {
499− // None
500− // };
501−
502− // if let Some(breakpoint) = user.iter().find(|breakpoint| breakpoint.line - 1 == line)
503− // {
504− // let verified = debugger_breakpoint.map(|b| b.verified).unwrap_or(false);
505− // let mut style =
506− // if breakpoint.condition.is_some() && breakpoint.log_message.is_some() {
507− // error.add_modifier(Modifier::UNDERLINED)
508− // } else if breakpoint.condition.is_some() {
509− // error
510− // } else if breakpoint.log_message.is_some() {
511− // info
512− // } else {
513− // warning
514− // };
515− // if !verified {
516− // // Faded colors
517− // style = if let Some(Color::Rgb(r, g, b)) = style.fg {
518− // style.fg(Color::Rgb(
519− // ((r as f32) * 0.4).floor() as u8,
520− // ((g as f32) * 0.4).floor() as u8,
521− // ((b as f32) * 0.4).floor() as u8,
522− // ))
523− // } else {
524− // style.fg(Color::Gray)
525− // }
526− // };
527− // surface.set_stringn(viewport.x, viewport.y + i as u16, "▲", 1, style);
528− // } else if let Some(breakpoint) = debugger_breakpoint {
529− // let style = if breakpoint.verified {
530− // info
531− // } else {
532− // info.fg(Color::Gray)
533− // };
534− // surface.set_stringn(viewport.x, viewport.y + i as u16, "⊚", 1, style);
535− // }
536− // }
537−
538526 // if let Some(frame) = stack_frame {
539527 // if frame.line - 1 == line {
540528 // surface.set_style(
@@ -551,7 +539,7 @@ impl EditorView {
551539 let mut text = String::with_capacity(8);
552540
553541 for (constructor, width) in view.gutters() {
554− let gutter = constructor(doc, view, theme, config, is_focused, *width);
542+ let gutter = constructor(editor, doc, view, theme, is_focused, *width);
555543 text.reserve(*width); // ensure there's enough space for the gutter
556544 for (i, line) in (view.offset.row..(last_line + 1)).enumerate() {
557545 let selected = cursors.contains(&line);
@@ -1250,20 +1238,7 @@ impl Component for EditorView {
12501238 for (view, is_focused) in cx.editor.tree.views() {
12511239 let doc = cx.editor.document(view.doc).unwrap();
12521240 let loader = &cx.editor.syn_loader;
1253− let dbg_breakpoints = cx.editor.debugger.as_ref().map(|d| d.breakpoints.clone());
1254− self.render_view(
1255− doc,
1256− view,
1257− area,
1258− surface,
1259− &cx.editor.theme,
1260− is_focused,
1261− loader,
1262− &cx.editor.config,
1263− &cx.editor.debugger,
1264− &cx.editor.breakpoints,
1265− &dbg_breakpoints,
1266− );
1241+ self.render_view(cx.editor, doc, view, area, surface, is_focused, loader);
12671242 }
12681243
12691244 if cx.editor.config.auto_info {
helix-view/src/gutter.rs+5−5
@@ -1,16 +1,16 @@
11use std::fmt::Write;
22
3−use crate::{editor::Config, graphics::Style, Document, Theme, View};
3+use crate::{graphics::Style, Document, Editor, Theme, View};
44
55pub type GutterFn<'doc> = Box<dyn Fn(usize, bool, &mut String) -> Option<Style> + 'doc>;
66pub type Gutter =
7− for<'doc> fn(&'doc Document, &View, &Theme, &Config, bool, usize) -> GutterFn<'doc>;
7+ for<'doc> fn(&'doc Editor, &'doc Document, &View, &Theme, bool, usize) -> GutterFn<'doc>;
88
99pub fn diagnostic<'doc>(
10+ _editor: &'doc Editor,
1011 doc: &'doc Document,
1112 _view: &View,
1213 theme: &Theme,
13− _config: &Config,
1414 _is_focused: bool,
1515 _width: usize,
1616) -> GutterFn<'doc> {
@@ -36,10 +36,10 @@ pub fn diagnostic<'doc>(
3636}
3737
3838pub fn line_number<'doc>(
39+ editor: &'doc Editor,
3940 doc: &'doc Document,
4041 view: &View,
4142 theme: &Theme,
42− config: &Config,
4343 is_focused: bool,
4444 width: usize,
4545) -> GutterFn<'doc> {
@@ -56,7 +56,7 @@ pub fn line_number<'doc>(
5656 .text()
5757 .char_to_line(doc.selection(view.id).primary().cursor(text));
5858
59− let config = config.line_number;
59+ let config = editor.config.line_number;
6060
6161 Box::new(move |line: usize, selected: bool, out: &mut String| {
6262 if line == last_line && !draw_last {