diff options
author | Blaž Hrastnik | 2021-03-23 08:47:40 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-03-23 09:14:35 +0000 |
commit | 8328fe926d34d12031cb50db47a9dd39ed681a84 (patch) | |
tree | acea128c5a3c2276f7ad553666a6572a5ba7d62d /helix-term/src/ui | |
parent | 3f9a94fd4364fe277f11668b0bfab4f2735c4daf (diff) |
Drop refcell use, make view simply ref doc.id.
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r-- | helix-term/src/ui/editor.rs | 27 | ||||
-rw-r--r-- | helix-term/src/ui/mod.rs | 71 |
2 files changed, 48 insertions, 50 deletions
diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 0302742d..ba9eda42 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -39,6 +39,7 @@ impl EditorView { } pub fn render_view( &self, + doc: &Document, view: &View, viewport: Rect, surface: &mut Surface, @@ -51,13 +52,12 @@ impl EditorView { viewport.width - OFFSET, viewport.height.saturating_sub(1), ); // - 1 for statusline - self.render_buffer(view, area, surface, theme, is_focused); + self.render_buffer(&doc, view, area, surface, theme, is_focused); // clear with background color // TODO: this seems to prevent setting style later // surface.set_style(viewport, theme.get("ui.background")); - let doc = view.doc.borrow(); self.render_diagnostics(&doc, area, surface, theme, is_focused); let area = Rect::new( @@ -71,16 +71,16 @@ impl EditorView { pub fn render_buffer( &self, + doc: &Document, view: &View, viewport: Rect, surface: &mut Surface, theme: &Theme, is_focused: bool, ) { - let doc = view.doc.borrow(); let text = doc.text().slice(..); - let last_line = view.last_line(); + let last_line = view.last_line(doc); let range = { // calculate viewport byte ranges @@ -211,16 +211,14 @@ impl EditorView { // let selection_style = Style::default().bg(Color::Rgb(94, 0, 128)); let selection_style = Style::default().bg(Color::Rgb(84, 0, 153)); - for selection in view - .doc - .borrow() + for selection in doc .selection() .iter() .filter(|range| range.overlaps(&screen)) { // TODO: render also if only one of the ranges is in viewport - let mut start = view.screen_coords_at_pos(text, selection.anchor); - let mut end = view.screen_coords_at_pos(text, selection.head); + let mut start = view.screen_coords_at_pos(doc, text, selection.anchor); + let mut end = view.screen_coords_at_pos(doc, text, selection.head); // cursor if let Some(end) = end { @@ -292,7 +290,6 @@ impl EditorView { let info: Style = theme.get("info"); let hint: Style = theme.get("hint"); - let last_line = view.last_line(); for (i, line) in (view.first_line..last_line).enumerate() { use helix_core::diagnostic::Severity; if let Some(diagnostic) = doc.diagnostics.iter().find(|d| d.line == line) { @@ -431,11 +428,12 @@ impl Component for EditorView { match event { Event::Resize(width, height) => { // HAXX: offset the render area height by 1 to account for prompt/commandline - cx.editor.tree.resize(Rect::new(0, 0, width, height - 1)); + cx.editor.resize(Rect::new(0, 0, width, height - 1)); EventResult::Consumed(None) } Event::Key(event) => { - let mode = cx.editor.view().doc.borrow().mode(); + let id = cx.editor.view().doc; + let mode = cx.editor.document(id).unwrap().mode(); let mut cxt = commands::Context { editor: &mut cx.editor, @@ -491,7 +489,7 @@ impl Component for EditorView { // appease borrowck let callback = cxt.callback.take(); drop(cxt); - cx.editor.view_mut().ensure_cursor_in_view(); + cx.editor.ensure_cursor_in_view(cx.editor.tree.focus); EventResult::Consumed(callback) } @@ -501,7 +499,8 @@ impl Component for EditorView { fn render(&self, mut area: Rect, surface: &mut Surface, cx: &mut Context) { for (view, is_focused) in cx.editor.tree.views() { - self.render_view(view, view.area, surface, &cx.editor.theme, is_focused); + let doc = cx.editor.document(view.doc).unwrap(); + self.render_view(doc, view, view.area, surface, &cx.editor.theme, is_focused); } } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index cb1b36fa..f91a9f35 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -40,7 +40,8 @@ pub fn regex_prompt( match event { PromptEvent::Abort => { // TODO: also revert text - let doc = &mut editor.view().doc.borrow_mut(); + let id = editor.view().doc; + let doc = &mut editor.documents[id]; doc.set_selection(snapshot.clone()); } PromptEvent::Validate => { @@ -54,18 +55,17 @@ pub fn regex_prompt( match Regex::new(input) { Ok(regex) => { - let view = &mut editor.view_mut(); - let mut doc = view.doc.borrow_mut(); + // let view = &mut editor.view_mut(); + let id = editor.view().doc; + let doc = &mut editor.documents[id]; // revert state to what it was before the last update // TODO: also revert text doc.set_selection(snapshot.clone()); - fun(&mut doc, regex); + fun(doc, regex); - drop(doc); - - view.ensure_cursor_in_view(); + editor.ensure_cursor_in_view(editor.view().id); } Err(_err) => (), // TODO: mark command line as error } @@ -101,40 +101,39 @@ pub fn file_picker(root: &str) -> Picker<PathBuf> { path.strip_prefix("./").unwrap().to_str().unwrap().into() }, move |editor: &mut Editor, path: &PathBuf| { - editor.open(path.into()); + let document_id = editor.open(path.into()).expect("editor.open failed"); }, ) } use helix_view::View; -pub fn buffer_picker(views: &[View], current: usize) -> Picker<(Option<PathBuf>, usize)> { - unimplemented!(); - // use helix_view::Editor; - // Picker::new( - // views - // .iter() - // .enumerate() - // .map(|(i, view)| (view.doc.relative_path().map(Path::to_path_buf), i)) - // .collect(), - // move |(path, index): &(Option<PathBuf>, usize)| { - // // format_fn - // match path { - // Some(path) => { - // if *index == current { - // format!("{} (*)", path.to_str().unwrap()).into() - // } else { - // path.to_str().unwrap().into() - // } - // } - // None => "[NEW]".into(), - // } - // }, - // |editor: &mut Editor, &(_, index): &(Option<PathBuf>, usize)| { - // if index < editor.views.len() { - // editor.focus = index; - // } - // }, - // ) +pub fn buffer_picker(buffers: &[Document], current: usize) -> Picker<(Option<PathBuf>, usize)> { + use helix_view::Editor; + Picker::new( + buffers + .iter() + .enumerate() + .map(|(i, doc)| (doc.relative_path().map(Path::to_path_buf), i)) + .collect(), + move |(path, index): &(Option<PathBuf>, usize)| { + // format_fn + match path { + Some(path) => { + if *index == current { + format!("{} (*)", path.to_str().unwrap()).into() + } else { + path.to_str().unwrap().into() + } + } + None => "[NEW]".into(), + } + }, + |editor: &mut Editor, &(_, index): &(Option<PathBuf>, usize)| { + // if index < editor.views.len() { + // editor.focus = index; + // } + }, + ) } pub mod completers { |