Pass editor into render_view & gutter, reducing the number of params

This commit is contained in:
Blaž Hrastnik 2021-11-30 16:47:46 +09:00
parent 9ed930b233
commit 3633f85b38
3 changed files with 82 additions and 107 deletions

View file

@ -429,7 +429,7 @@ pub fn dap_toggle_breakpoint_impl(cx: &mut Context, path: std::path::PathBuf, li
Some(debugger) => debugger, Some(debugger) => debugger,
None => return, None => return,
}; };
let request = debugger.set_breakpoints(path.clone(), breakpoints); let request = debugger.set_breakpoints(path, breakpoints);
match block_on(request) { match block_on(request) {
Ok(Some(breakpoints)) => { Ok(Some(breakpoints)) => {
// TODO: handle breakpoint.message // TODO: handle breakpoint.message

View file

@ -63,20 +63,18 @@ impl EditorView {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn render_view( pub fn render_view(
&self, &self,
editor: &Editor,
doc: &Document, doc: &Document,
view: &View, view: &View,
viewport: Rect, viewport: Rect,
surface: &mut Surface, surface: &mut Surface,
theme: &Theme,
is_focused: bool, is_focused: bool,
loader: &syntax::Loader, loader: &syntax::Loader,
config: &helix_view::editor::Config,
debugger: &Option<helix_dap::Client>,
all_breakpoints: &HashMap<PathBuf, Vec<SourceBreakpoint>>,
dbg_breakpoints: &Option<Vec<Breakpoint>>,
) { ) {
let inner = view.inner_area(); let inner = view.inner_area();
let area = view.area; let area = view.area;
let theme = &editor.theme;
let all_breakpoints = HashMap::new();
let highlights = Self::doc_syntax_highlights(doc, view.offset, inner.height, theme, loader); let highlights = Self::doc_syntax_highlights(doc, view.offset, inner.height, theme, loader);
let highlights = syntax::merge(highlights, Self::doc_diagnostics_highlights(doc, theme)); let highlights = syntax::merge(highlights, Self::doc_diagnostics_highlights(doc, theme));
@ -90,18 +88,7 @@ impl EditorView {
}; };
Self::render_text_highlights(doc, view.offset, inner, surface, theme, highlights); Self::render_text_highlights(doc, view.offset, inner, surface, theme, highlights);
Self::render_gutter( Self::render_gutter(editor, doc, view, view.area, surface, theme, is_focused);
doc,
view,
view.area,
surface,
theme,
is_focused,
config,
debugger,
all_breakpoints,
dbg_breakpoints,
);
if is_focused { if is_focused {
Self::render_focused_view_elements(view, doc, inner, theme, surface); Self::render_focused_view_elements(view, doc, inner, theme, surface);
@ -120,7 +107,7 @@ impl EditorView {
} }
} }
self.render_diagnostics(doc, view, inner, surface, theme, all_breakpoints); self.render_diagnostics(doc, view, inner, surface, theme, &all_breakpoints);
let statusline_area = view let statusline_area = view
.area .area
@ -413,18 +400,14 @@ impl EditorView {
} }
} }
#[allow(clippy::too_many_arguments)]
pub fn render_gutter( pub fn render_gutter(
editor: &Editor,
doc: &Document, doc: &Document,
view: &View, view: &View,
viewport: Rect, viewport: Rect,
surface: &mut Surface, surface: &mut Surface,
theme: &Theme, theme: &Theme,
is_focused: bool, is_focused: bool,
config: &helix_view::editor::Config,
debugger: &Option<helix_dap::Client>,
all_breakpoints: &HashMap<PathBuf, Vec<SourceBreakpoint>>,
dbg_breakpoints: &Option<Vec<Breakpoint>>,
) { ) {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let last_line = view.last_line(doc); let last_line = view.last_line(doc);
@ -438,8 +421,10 @@ impl EditorView {
.map(|range| range.cursor_line(text)) .map(|range| range.cursor_line(text))
.collect(); .collect();
use helix_view::editor::Config;
use helix_view::gutter::GutterFn; use helix_view::gutter::GutterFn;
fn breakpoints<'doc>( fn breakpoints<'doc>(
editor: &'doc Editor,
doc: &'doc Document, doc: &'doc Document,
_view: &View, _view: &View,
theme: &Theme, theme: &Theme,
@ -447,14 +432,77 @@ impl EditorView {
_is_focused: bool, _is_focused: bool,
_width: usize, _width: usize,
) -> GutterFn<'doc> { ) -> GutterFn<'doc> {
let warning = theme.get("warning");
let error = theme.get("error");
let info = theme.get("info");
let all_breakpoints = &editor.breakpoints;
// TODO: debugger breakpoints should keep editor level breakpoints in sync
let breakpoints = doc
.path()
.and_then(|path| all_breakpoints.get(path))
.unwrap();
Box::new(move |line: usize, _selected: bool, out: &mut String| { Box::new(move |line: usize, _selected: bool, out: &mut String| {
// // TODO: debugger should translate received breakpoints to 0-indexing
let breakpoint = breakpoints.iter().find(|breakpoint| {
false
// if breakpoint.source == doc.path() {
// match (breakpoint.line, breakpoint.end_line) {
// #[allow(clippy::int_plus_one)]
// (Some(l), Some(el)) => l - 1 <= line && line <= el - 1,
// (Some(l), None) => l - 1 == line,
// _ => false,
// }
// } else {
// false
// }
});
let breakpoint = match breakpoint {
Some(b) => b,
None => return None,
};
// let verified = breakpoint.verified;
let verified = false;
let mut style =
if breakpoint.condition.is_some() && breakpoint.log_message.is_some() {
error.add_modifier(Modifier::UNDERLINED)
} else if breakpoint.condition.is_some() {
error
} else if breakpoint.log_message.is_some() {
info
} else {
warning
};
if !verified {
// Faded colors
style = if let Some(Color::Rgb(r, g, b)) = style.fg {
style.fg(Color::Rgb(
((r as f32) * 0.4).floor() as u8,
((g as f32) * 0.4).floor() as u8,
((b as f32) * 0.4).floor() as u8,
))
} else {
style.fg(Color::Gray)
}
};
// TODO: also handle breakpoints only present in the user struct
use std::fmt::Write;
let sym = if verified { "" } else { "" };
write!(out, "{}", sym).unwrap();
Some(style)
}) })
} }
// let mut breakpoints: Option<&Vec<SourceBreakpoint>> = None;
// let mut stack_frame: Option<&StackFrame> = None; // let mut stack_frame: Option<&StackFrame> = None;
// if let Some(path) = doc.path() { // if let Some(path) = doc.path() {
// breakpoints = all_breakpoints.get(path);
// if let Some(debugger) = debugger { // if let Some(debugger) = debugger {
// // if we have a frame, and the frame path matches document // // if we have a frame, and the frame path matches document
// if let (Some(frame), Some(thread_id)) = (debugger.active_frame, debugger.thread_id) // if let (Some(frame), Some(thread_id)) = (debugger.active_frame, debugger.thread_id)
@ -475,66 +523,6 @@ impl EditorView {
// }; // };
// } // }
// } // }
// TODO: debugger should translate received breakpoints to 0-indexing
// if let Some(user) = breakpoints.as_ref() {
// let debugger_breakpoint = if let Some(debugger) = dbg_breakpoints.as_ref() {
// debugger.iter().find(|breakpoint| {
// if breakpoint.source.is_some()
// && doc.path().is_some()
// && breakpoint.source.as_ref().unwrap().path == doc.path().cloned()
// {
// match (breakpoint.line, breakpoint.end_line) {
// #[allow(clippy::int_plus_one)]
// (Some(l), Some(el)) => l - 1 <= line && line <= el - 1,
// (Some(l), None) => l - 1 == line,
// _ => false,
// }
// } else {
// false
// }
// })
// } else {
// None
// };
// if let Some(breakpoint) = user.iter().find(|breakpoint| breakpoint.line - 1 == line)
// {
// let verified = debugger_breakpoint.map(|b| b.verified).unwrap_or(false);
// let mut style =
// if breakpoint.condition.is_some() && breakpoint.log_message.is_some() {
// error.add_modifier(Modifier::UNDERLINED)
// } else if breakpoint.condition.is_some() {
// error
// } else if breakpoint.log_message.is_some() {
// info
// } else {
// warning
// };
// if !verified {
// // Faded colors
// style = if let Some(Color::Rgb(r, g, b)) = style.fg {
// style.fg(Color::Rgb(
// ((r as f32) * 0.4).floor() as u8,
// ((g as f32) * 0.4).floor() as u8,
// ((b as f32) * 0.4).floor() as u8,
// ))
// } else {
// style.fg(Color::Gray)
// }
// };
// surface.set_stringn(viewport.x, viewport.y + i as u16, "▲", 1, style);
// } else if let Some(breakpoint) = debugger_breakpoint {
// let style = if breakpoint.verified {
// info
// } else {
// info.fg(Color::Gray)
// };
// surface.set_stringn(viewport.x, viewport.y + i as u16, "⊚", 1, style);
// }
// }
// if let Some(frame) = stack_frame { // if let Some(frame) = stack_frame {
// if frame.line - 1 == line { // if frame.line - 1 == line {
// surface.set_style( // surface.set_style(
@ -551,7 +539,7 @@ impl EditorView {
let mut text = String::with_capacity(8); let mut text = String::with_capacity(8);
for (constructor, width) in view.gutters() { for (constructor, width) in view.gutters() {
let gutter = constructor(doc, view, theme, config, is_focused, *width); let gutter = constructor(editor, doc, view, theme, is_focused, *width);
text.reserve(*width); // ensure there's enough space for the gutter text.reserve(*width); // ensure there's enough space for the gutter
for (i, line) in (view.offset.row..(last_line + 1)).enumerate() { for (i, line) in (view.offset.row..(last_line + 1)).enumerate() {
let selected = cursors.contains(&line); let selected = cursors.contains(&line);
@ -1250,20 +1238,7 @@ impl Component for EditorView {
for (view, is_focused) in cx.editor.tree.views() { for (view, is_focused) in cx.editor.tree.views() {
let doc = cx.editor.document(view.doc).unwrap(); let doc = cx.editor.document(view.doc).unwrap();
let loader = &cx.editor.syn_loader; let loader = &cx.editor.syn_loader;
let dbg_breakpoints = cx.editor.debugger.as_ref().map(|d| d.breakpoints.clone()); self.render_view(cx.editor, doc, view, area, surface, is_focused, loader);
self.render_view(
doc,
view,
area,
surface,
&cx.editor.theme,
is_focused,
loader,
&cx.editor.config,
&cx.editor.debugger,
&cx.editor.breakpoints,
&dbg_breakpoints,
);
} }
if cx.editor.config.auto_info { if cx.editor.config.auto_info {

View file

@ -1,16 +1,16 @@
use std::fmt::Write; use std::fmt::Write;
use crate::{editor::Config, graphics::Style, Document, Theme, View}; use crate::{graphics::Style, Document, Editor, Theme, View};
pub type GutterFn<'doc> = Box<dyn Fn(usize, bool, &mut String) -> Option<Style> + 'doc>; pub type GutterFn<'doc> = Box<dyn Fn(usize, bool, &mut String) -> Option<Style> + 'doc>;
pub type Gutter = pub type Gutter =
for<'doc> fn(&'doc Document, &View, &Theme, &Config, bool, usize) -> GutterFn<'doc>; for<'doc> fn(&'doc Editor, &'doc Document, &View, &Theme, bool, usize) -> GutterFn<'doc>;
pub fn diagnostic<'doc>( pub fn diagnostic<'doc>(
_editor: &'doc Editor,
doc: &'doc Document, doc: &'doc Document,
_view: &View, _view: &View,
theme: &Theme, theme: &Theme,
_config: &Config,
_is_focused: bool, _is_focused: bool,
_width: usize, _width: usize,
) -> GutterFn<'doc> { ) -> GutterFn<'doc> {
@ -36,10 +36,10 @@ pub fn diagnostic<'doc>(
} }
pub fn line_number<'doc>( pub fn line_number<'doc>(
editor: &'doc Editor,
doc: &'doc Document, doc: &'doc Document,
view: &View, view: &View,
theme: &Theme, theme: &Theme,
config: &Config,
is_focused: bool, is_focused: bool,
width: usize, width: usize,
) -> GutterFn<'doc> { ) -> GutterFn<'doc> {
@ -56,7 +56,7 @@ pub fn line_number<'doc>(
.text() .text()
.char_to_line(doc.selection(view.id).primary().cursor(text)); .char_to_line(doc.selection(view.id).primary().cursor(text));
let config = config.line_number; let config = editor.config.line_number;
Box::new(move |line: usize, selected: bool, out: &mut String| { Box::new(move |line: usize, selected: bool, out: &mut String| {
if line == last_line && !draw_last { if line == last_line && !draw_last {