on master help sign in
Show file preview in split pane in fuzzy finder (#534)

* Add preview pane for fuzzy finder * Fix picker preview lag by caching * Add picker preview for document symbols * Cache picker preview per document instead of view * Use line instead of range for preview doc * Add picker preview for buffer picker * Fix render bug and refactor picker * Refactor picker preview rendering * Split picker and preview and compose The current selected item is cloned on every event, which is undesirable * Refactor out clones in previewed picker * Retrieve doc from editor if possible in filepicker * Disable syntax highlight for picker preview Files already loaded in memory have syntax highlighting enabled * Ignore directory symlinks in file picker * Cleanup unnecessary pubs and derives * Remove unnecessary highlight from file picker * Reorganize buffer rendering * Use normal picker for code actions * Remove unnecessary generics and trait impls * Remove prepare_for_render and make render mutable * Skip picker preview if screen small, less padding

commitd84f8b5fdef71da87ee108db07ba1167fc6a769b authorGokul Soumya <gokulps15@gmail.com> dateThu 12 Aug 2021, 12:30 (5 years ago) parent7d51805
15 files changed+483−240
helix-term/src/commands.rs+26−7 helix-term/src/compositor.rs+3−3 helix-term/src/ui/completion.rs+2−2 helix-term/src/ui/editor.rs+235−173 helix-term/src/ui/info.rs+1−1 helix-term/src/ui/markdown.rs+1−1 helix-term/src/ui/menu.rs+1−1 helix-term/src/ui/mod.rs+20−22 helix-term/src/ui/picker.rs+171−20 helix-term/src/ui/popup.rs+1−1 helix-term/src/ui/prompt.rs+2−2 helix-term/src/ui/text.rs+1−1 helix-view/src/document.rs+7−3 helix-view/src/editor.rs+11−2 helix-view/src/view.rs+1−1
helix-term/src/commands.rs+26−7
@@ -27,7 +27,7 @@ use movement::Movement;
2727
2828use crate::{
2929 compositor::{self, Component, Compositor},
30− ui::{self, Picker, Popup, Prompt, PromptEvent},
30+ ui::{self, FilePicker, Picker, Popup, Prompt, PromptEvent},
3131};
3232
3333use crate::job::{self, Job, Jobs};
@@ -2212,7 +2212,7 @@ fn file_picker(cx: &mut Context) {
22122212fn buffer_picker(cx: &mut Context) {
22132213 let current = view!(cx.editor).doc;
22142214
2215− let picker = Picker::new(
2215+ let picker = FilePicker::new(
22162216 cx.editor
22172217 .documents
22182218 .iter()
@@ -2234,6 +2234,15 @@ fn buffer_picker(cx: &mut Context) {
22342234 |editor: &mut Editor, (id, _path): &(DocumentId, Option<PathBuf>), _action| {
22352235 editor.switch(*id, Action::Replace);
22362236 },
2237+ |editor, (id, path)| {
2238+ let doc = &editor.documents.get(*id)?;
2239+ let &view_id = doc.selections().keys().next()?;
2240+ let line = doc
2241+ .selection(view_id)
2242+ .primary()
2243+ .cursor_line(doc.text().slice(..));
2244+ Some((path.clone()?, Some(line)))
2245+ },
22372246 );
22382247 cx.push_layer(Box::new(picker));
22392248}
@@ -2287,7 +2296,7 @@ fn symbol_picker(cx: &mut Context) {
22872296 }
22882297 };
22892298
2290− let picker = Picker::new(
2299+ let picker = FilePicker::new(
22912300 symbols,
22922301 |symbol| (&symbol.name).into(),
22932302 move |editor: &mut Editor, symbol, _action| {
@@ -2297,10 +2306,15 @@ fn symbol_picker(cx: &mut Context) {
22972306 if let Some(range) =
22982307 lsp_range_to_range(doc.text(), symbol.location.range, offset_encoding)
22992308 {
2300− doc.set_selection(view.id, Selection::single(range.to(), range.from()));
2309+ doc.set_selection(view.id, Selection::single(range.anchor, range.head));
23012310 align_view(doc, view, Align::Center);
23022311 }
23032312 },
2313+ move |_editor, symbol| {
2314+ let path = symbol.location.uri.to_file_path().unwrap();
2315+ let line = Some(symbol.location.range.start.line as usize);
2316+ Some((path, line))
2317+ },
23042318 );
23052319 compositor.push(Box::new(picker))
23062320 }
@@ -2332,6 +2346,7 @@ pub fn code_action(cx: &mut Context) {
23322346 response: Option<lsp::CodeActionResponse>| {
23332347 if let Some(actions) = response {
23342348 let picker = Picker::new(
2349+ true,
23352350 actions,
23362351 |action| match action {
23372352 lsp::CodeActionOrCommand::CodeAction(action) => {
@@ -2703,7 +2718,7 @@ fn goto_impl(
27032718 editor.set_error("No definition found.".to_string());
27042719 }
27052720 _locations => {
2706− let picker = ui::Picker::new(
2721+ let picker = FilePicker::new(
27072722 locations,
27082723 move |location| {
27092724 let file: Cow<'_, str> = (location.uri.scheme() == "file")
@@ -2728,6 +2743,11 @@ fn goto_impl(
27282743 move |editor: &mut Editor, location, action| {
27292744 jump_to(editor, location, offset_encoding, action)
27302745 },
2746+ |_editor, location| {
2747+ let path = location.uri.to_file_path().unwrap();
2748+ let line = Some(location.range.start.line as usize);
2749+ Some((path, line))
2750+ },
27312751 );
27322752 compositor.push(Box::new(picker));
27332753 }
@@ -3729,8 +3749,7 @@ fn keep_primary_selection(cx: &mut Context) {
37293749 let (view, doc) = current!(cx.editor);
37303750
37313751 let range = doc.selection(view.id).primary();
3732− let selection = Selection::single(range.anchor, range.head);
3733− doc.set_selection(view.id, selection);
3752+ doc.set_selection(view.id, Selection::single(range.anchor, range.head));
37343753}
37353754
37363755fn completion(cx: &mut Context) {
helix-term/src/compositor.rs+3−3
@@ -46,7 +46,7 @@ pub trait Component: Any + AnyComponent {
4646 }
4747
4848 /// Render the component onto the provided surface.
49− fn render(&self, area: Rect, frame: &mut Surface, ctx: &mut Context);
49+ fn render(&mut self, area: Rect, frame: &mut Surface, ctx: &mut Context);
5050
5151 /// Get cursor position and cursor kind.
5252 fn cursor(&self, _area: Rect, _ctx: &Editor) -> (Option<Position>, CursorKind) {
@@ -152,8 +152,8 @@ impl Compositor {
152152
153153 let area = *surface.area();
154154
155− for layer in &self.layers {
156− layer.render(area, surface, cx)
155+ for layer in &mut self.layers {
156+ layer.render(area, surface, cx);
157157 }
158158
159159 let (pos, kind) = self.cursor(area, cx.editor);
helix-term/src/ui/completion.rs+2−2
@@ -241,7 +241,7 @@ impl Component for Completion {
241241 self.popup.required_size(viewport)
242242 }
243243
244− fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
244+ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
245245 self.popup.render(area, surface, cx);
246246
247247 // if we have a selection, render a markdown popup on top/below with info
@@ -263,7 +263,7 @@ impl Component for Completion {
263263 let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row
264264 - view.first_line) as u16;
265265
266− let doc = match &option.documentation {
266+ let mut doc = match &option.documentation {
267267 Some(lsp::Documentation::String(contents))
268268 | Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
269269 kind: lsp::MarkupKind::PlainText,
helix-term/src/ui/editor.rs+235−173
@@ -34,7 +34,7 @@ pub struct EditorView {
3434 last_insert: (commands::Command, Vec<KeyEvent>),
3535 completion: Option<Completion>,
3636 spinners: ProgressSpinners,
37− pub autoinfo: Option<Info>,
37+ autoinfo: Option<Info>,
3838}
3939
4040pub const GUTTER_OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
@@ -78,8 +78,26 @@ impl EditorView {
7878 view.area.width - GUTTER_OFFSET,
7979 view.area.height.saturating_sub(1),
8080 ); // - 1 for statusline
81+ let offset = Position::new(view.first_line, view.first_col);
82+ let height = view.area.height.saturating_sub(1); // - 1 for statusline
8183
82− self.render_buffer(doc, view, area, surface, theme, is_focused, loader);
84+ let highlights = Self::doc_syntax_highlights(doc, offset, height, theme, loader);
85+ let highlights = syntax::merge(highlights, Self::doc_diagnostics_highlights(doc, theme));
86+ let highlights: Box<dyn Iterator<Item = HighlightEvent>> = if is_focused {
87+ Box::new(syntax::merge(
88+ highlights,
89+ Self::doc_selection_highlights(doc, view, theme),
90+ ))
91+ } else {
92+ Box::new(highlights)
93+ };
94+
95+ Self::render_text_highlights(doc, offset, area, surface, theme, highlights);
96+ Self::render_gutter(doc, view, area, surface, theme);
97+
98+ if is_focused {
99+ Self::render_focused_view_elements(view, doc, area, theme, surface);
100+ }
83101
84102 // if we're not at the edge of the screen, draw a right border
85103 if viewport.right() != view.area.right() {
@@ -94,7 +112,7 @@ impl EditorView {
94112 }
95113 }
96114
97− self.render_diagnostics(doc, view, area, surface, theme, is_focused);
115+ self.render_diagnostics(doc, view, area, surface, theme);
98116
99117 let area = Rect::new(
100118 view.area.x,
@@ -105,31 +123,34 @@ impl EditorView {
105123 self.render_statusline(doc, view, area, surface, theme, is_focused);
106124 }
107125
126+ /// Get syntax highlights for a document in a view represented by the first line
127+ /// and column (`offset`) and the last line. This is done instead of using a view
128+ /// directly to enable rendering syntax highlighted docs anywhere (eg. picker preview)
108129 #[allow(clippy::too_many_arguments)]
109− pub fn render_buffer(
110− &self,
111− doc: &Document,
112− view: &View,
113− viewport: Rect,
114− surface: &mut Surface,
130+ pub fn doc_syntax_highlights<'doc>(
131+ doc: &'doc Document,
132+ offset: Position,
133+ height: u16,
115134 theme: &Theme,
116− is_focused: bool,
117135 loader: &syntax::Loader,
118− ) {
136+ ) -> Box<dyn Iterator<Item = HighlightEvent> + 'doc> {
119137 let text = doc.text().slice(..);
120−
121− let last_line = view.last_line(doc);
138+ let last_line = std::cmp::min(
139+ // Saturating subs to make it inclusive zero indexing.
140+ (offset.row + height as usize).saturating_sub(1),
141+ doc.text().len_lines().saturating_sub(1),
142+ );
122143
123144 let range = {
124145 // calculate viewport byte ranges
125− let start = text.line_to_byte(view.first_line);
146+ let start = text.line_to_byte(offset.row);
126147 let end = text.line_to_byte(last_line + 1);
127148
128149 start..end
129150 };
130151
131152 // TODO: range doesn't actually restrict source, just highlight range
132− let highlights: Vec<_> = match doc.syntax() {
153+ let highlights = match doc.syntax() {
133154 Some(syntax) => {
134155 let scopes = theme.scopes();
135156 syntax
@@ -151,20 +172,16 @@ impl EditorView {
151172 Some(config_ref)
152173 })
153174 })
175+ .map(|event| event.unwrap())
154176 .collect() // TODO: we collect here to avoid holding the lock, fix later
155177 }
156− None => vec![Ok(HighlightEvent::Source {
178+ None => vec![HighlightEvent::Source {
157179 start: range.start,
158180 end: range.end,
159− })],
160− };
161− let mut spans = Vec::new();
162− let mut visual_x = 0u16;
163− let mut line = 0u16;
164− let tab_width = doc.tab_width();
165− let tab = " ".repeat(tab_width);
166−
167− let highlights = highlights.into_iter().map(|event| match event.unwrap() {
181+ }],
182+ }
183+ .into_iter()
184+ .map(move |event| match event {
168185 // convert byte offsets to char offset
169186 HighlightEvent::Source { start, end } => {
170187 let start = ensure_grapheme_boundary_next(text, text.byte_to_char(start));
@@ -174,13 +191,44 @@ impl EditorView {
174191 event => event,
175192 });
176193
177− let selections = doc.selection(view.id);
178− let primary_idx = selections.primary_index();
194+ Box::new(highlights)
195+ }
196+
197+ /// Get highlight spans for document diagnostics
198+ pub fn doc_diagnostics_highlights(
199+ doc: &Document,
200+ theme: &Theme,
201+ ) -> Vec<(usize, std::ops::Range<usize>)> {
202+ let diagnostic_scope = theme
203+ .find_scope_index("diagnostic")
204+ .or_else(|| theme.find_scope_index("ui.cursor"))
205+ .or_else(|| theme.find_scope_index("ui.selection"))
206+ .expect("no selection scope found!");
207+
208+ doc.diagnostics()
209+ .iter()
210+ .map(|diagnostic| {
211+ (
212+ diagnostic_scope,
213+ diagnostic.range.start..diagnostic.range.end,
214+ )
215+ })
216+ .collect()
217+ }
218+
219+ /// Get highlight spans for selections in a document view.
220+ pub fn doc_selection_highlights(
221+ doc: &Document,
222+ view: &View,
223+ theme: &Theme,
224+ ) -> Vec<(usize, std::ops::Range<usize>)> {
225+ let text = doc.text().slice(..);
226+ let selection = doc.selection(view.id);
227+ let primary_idx = selection.primary_index();
179228
180229 let selection_scope = theme
181230 .find_scope_index("ui.selection")
182231 .expect("no selection scope found!");
183−
184232 let base_cursor_scope = theme
185233 .find_scope_index("ui.cursor")
186234 .unwrap_or(selection_scope);
@@ -192,64 +240,59 @@ impl EditorView {
192240 }
193241 .unwrap_or(base_cursor_scope);
194242
195− let highlights: Box<dyn Iterator<Item = HighlightEvent>> = if is_focused {
196− // TODO: primary + insert mode patching:
197− // (ui.cursor.primary).patch(mode).unwrap_or(cursor)
198− let primary_cursor_scope = theme
199− .find_scope_index("ui.cursor.primary")
200− .unwrap_or(cursor_scope);
201− let primary_selection_scope = theme
202− .find_scope_index("ui.selection.primary")
203− .unwrap_or(selection_scope);
204−
205− // inject selections as highlight scopes
206− let mut spans: Vec<(usize, std::ops::Range<usize>)> = Vec::new();
207− for (i, range) in selections.iter().enumerate() {
208− let (cursor_scope, selection_scope) = if i == primary_idx {
209− (primary_cursor_scope, primary_selection_scope)
210− } else {
211− (cursor_scope, selection_scope)
212− };
243+ let primary_cursor_scope = theme
244+ .find_scope_index("ui.cursor.primary")
245+ .unwrap_or(cursor_scope);
246+ let primary_selection_scope = theme
247+ .find_scope_index("ui.selection.primary")
248+ .unwrap_or(selection_scope);
213249
214− // Special-case: cursor at end of the rope.
215− if range.head == range.anchor && range.head == text.len_chars() {
216− spans.push((cursor_scope, range.head..range.head + 1));
217− continue;
218− }
250+ let mut spans: Vec<(usize, std::ops::Range<usize>)> = Vec::new();
251+ for (i, range) in selection.iter().enumerate() {
252+ let (cursor_scope, selection_scope) = if i == primary_idx {
253+ (primary_cursor_scope, primary_selection_scope)
254+ } else {
255+ (cursor_scope, selection_scope)
256+ };
219257
220− let range = range.min_width_1(text);
221− if range.head > range.anchor {
222− // Standard case.
223− let cursor_start = prev_grapheme_boundary(text, range.head);
224− spans.push((selection_scope, range.anchor..cursor_start));
225− spans.push((cursor_scope, cursor_start..range.head));
226− } else {
227− // Reverse case.
228− let cursor_end = next_grapheme_boundary(text, range.head);
229− spans.push((cursor_scope, range.head..cursor_end));
230− spans.push((selection_scope, cursor_end..range.anchor));
231− }
258+ // Special-case: cursor at end of the rope.
259+ if range.head == range.anchor && range.head == text.len_chars() {
260+ spans.push((cursor_scope, range.head..range.head + 1));
261+ continue;
232262 }
233263
234− Box::new(syntax::merge(highlights, spans))
235− } else {
236− Box::new(highlights)
237− };
264+ let range = range.min_width_1(text);
265+ if range.head > range.anchor {
266+ // Standard case.
267+ let cursor_start = prev_grapheme_boundary(text, range.head);
268+ spans.push((selection_scope, range.anchor..cursor_start));
269+ spans.push((cursor_scope, cursor_start..range.head));
270+ } else {
271+ // Reverse case.
272+ let cursor_end = next_grapheme_boundary(text, range.head);
273+ spans.push((cursor_scope, range.head..cursor_end));
274+ spans.push((selection_scope, cursor_end..range.anchor));
275+ }
276+ }
277+
278+ spans
279+ }
280+
281+ pub fn render_text_highlights<H: Iterator<Item = HighlightEvent>>(
282+ doc: &Document,
283+ offset: Position,
284+ viewport: Rect,
285+ surface: &mut Surface,
286+ theme: &Theme,
287+ highlights: H,
288+ ) {
289+ let text = doc.text().slice(..);
238290
239− // diagnostic injection
240− let diagnostic_scope = theme.find_scope_index("diagnostic").unwrap_or(cursor_scope);
241− let highlights = Box::new(syntax::merge(
242− highlights,
243− doc.diagnostics()
244− .iter()
245− .map(|diagnostic| {
246− (
247− diagnostic_scope,
248− diagnostic.range.start..diagnostic.range.end,
249− )
250− })
251− .collect(),
252− ));
291+ let mut spans = Vec::new();
292+ let mut visual_x = 0u16;
293+ let mut line = 0u16;
294+ let tab_width = doc.tab_width();
295+ let tab = " ".repeat(tab_width);
253296
254297 'outer: for event in highlights {
255298 match event {
@@ -273,14 +316,14 @@ impl EditorView {
273316 });
274317
275318 for grapheme in RopeGraphemes::new(text) {
276− let out_of_bounds = visual_x < view.first_col as u16
277− || visual_x >= viewport.width + view.first_col as u16;
319+ let out_of_bounds = visual_x < offset.col as u16
320+ || visual_x >= viewport.width + offset.col as u16;
278321
279322 if LineEnding::from_rope_slice(&grapheme).is_some() {
280323 if !out_of_bounds {
281324 // we still want to render an empty cell with the style
282325 surface.set_string(
283− viewport.x + visual_x - view.first_col as u16,
326+ viewport.x + visual_x - offset.col as u16,
284327 viewport.y + line,
285328 " ",
286329 style,
@@ -310,7 +353,7 @@ impl EditorView {
310353 if !out_of_bounds {
311354 // if we're offscreen just keep going until we hit a new line
312355 surface.set_string(
313− viewport.x + visual_x - view.first_col as u16,
356+ viewport.x + visual_x - offset.col as u16,
314357 viewport.y + line,
315358 grapheme,
316359 style,
@@ -323,14 +366,108 @@ impl EditorView {
323366 }
324367 }
325368 }
369+ }
370+
371+ /// Render brace match, selected line numbers, etc (meant for the focused view only)
372+ pub fn render_focused_view_elements(
373+ view: &View,
374+ doc: &Document,
375+ viewport: Rect,
376+ theme: &Theme,
377+ surface: &mut Surface,
378+ ) {
379+ let text = doc.text().slice(..);
380+ let selection = doc.selection(view.id);
381+ let last_line = view.last_line(doc);
382+ let screen = {
383+ let start = text.line_to_char(view.first_line);
384+ let end = text.line_to_char(last_line + 1) + 1; // +1 for cursor at end of text.
385+ Range::new(start, end)
386+ };
387+
388+ // render selected linenr(s)
389+ let linenr_select: Style = theme
390+ .try_get("ui.linenr.selected")
391+ .unwrap_or_else(|| theme.get("ui.linenr"));
392+
393+ // Whether to draw the line number for the last line of the
394+ // document or not. We only draw it if it's not an empty line.
395+ let draw_last = text.line_to_byte(last_line) < text.len_bytes();
396+
397+ for selection in selection.iter().filter(|range| range.overlaps(&screen)) {
398+ let head = view.screen_coords_at_pos(
399+ doc,
400+ text,
401+ if selection.head > selection.anchor {
402+ selection.head - 1
403+ } else {
404+ selection.head
405+ },
406+ );
407+ if let Some(head) = head {
408+ // Highlight line number for selected lines.
409+ let line_number = view.first_line + head.row;
410+ let line_number_text = if line_number == last_line && !draw_last {
411+ " ~".into()
412+ } else {
413+ format!("{:>5}", line_number + 1)
414+ };
415+ surface.set_stringn(
416+ viewport.x - GUTTER_OFFSET + 1,
417+ viewport.y + head.row as u16,
418+ line_number_text,
419+ 5,
420+ linenr_select,
421+ );
422+
423+ // Highlight matching braces
424+ // TODO: set cursor position for IME
425+ if let Some(syntax) = doc.syntax() {
426+ use helix_core::match_brackets;
427+ let pos = doc
428+ .selection(view.id)
429+ .primary()
430+ .cursor(doc.text().slice(..));
431+ let pos = match_brackets::find(syntax, doc.text(), pos)
432+ .and_then(|pos| view.screen_coords_at_pos(doc, text, pos));
433+
434+ if let Some(pos) = pos {
435+ // ensure col is on screen
436+ if (pos.col as u16) < viewport.width + view.first_col as u16
437+ && pos.col >= view.first_col
438+ {
439+ let style = theme.try_get("ui.cursor.match").unwrap_or_else(|| {
440+ Style::default()
441+ .add_modifier(Modifier::REVERSED)
442+ .add_modifier(Modifier::DIM)
443+ });
444+
445+ surface
446+ .get_mut(viewport.x + pos.col as u16, viewport.y + pos.row as u16)
447+ .set_style(style);
448+ }
449+ }
450+ }
451+ }
452+ }
453+ }
326454
327− // render gutters
455+ #[allow(clippy::too_many_arguments)]
456+ pub fn render_gutter(
457+ doc: &Document,
458+ view: &View,
459+ viewport: Rect,
460+ surface: &mut Surface,
461+ theme: &Theme,
462+ ) {
463+ let text = doc.text().slice(..);
464+ let last_line = view.last_line(doc);
328465
329− let linenr: Style = theme.get("ui.linenr");
330− let warning: Style = theme.get("warning");
331− let error: Style = theme.get("error");
332− let info: Style = theme.get("info");
333− let hint: Style = theme.get("hint");
466+ let linenr = theme.get("ui.linenr");
467+ let warning = theme.get("warning");
468+ let error = theme.get("error");
469+ let info = theme.get("info");
470+ let hint = theme.get("hint");
334471
335472 // Whether to draw the line number for the last line of the
336473 // document or not. We only draw it if it's not an empty line.
@@ -368,80 +505,6 @@ impl EditorView {
368505 linenr,
369506 );
370507 }
371−
372− // render selections and selected linenr(s)
373− let linenr_select: Style = theme
374− .try_get("ui.linenr.selected")
375− .unwrap_or_else(|| theme.get("ui.linenr"));
376−
377− if is_focused {
378− let screen = {
379− let start = text.line_to_char(view.first_line);
380− let end = text.line_to_char(last_line + 1) + 1; // +1 for cursor at end of text.
381− Range::new(start, end)
382− };
383−
384− let selection = doc.selection(view.id);
385−
386− for selection in selection.iter().filter(|range| range.overlaps(&screen)) {
387− let head = view.screen_coords_at_pos(
388− doc,
389− text,
390− if selection.head > selection.anchor {
391− selection.head - 1
392− } else {
393− selection.head
394− },
395− );
396− if let Some(head) = head {
397− // Draw line number for selected lines.
398− let line_number = view.first_line + head.row;
399− let line_number_text = if line_number == last_line && !draw_last {
400− " ~".into()
401− } else {
402− format!("{:>5}", line_number + 1)
403− };
404− surface.set_stringn(
405− viewport.x + 1 - GUTTER_OFFSET,
406− viewport.y + head.row as u16,
407− line_number_text,
408− 5,
409− linenr_select,
410− );
411−
412− // TODO: set cursor position for IME
413− if let Some(syntax) = doc.syntax() {
414− use helix_core::match_brackets;
415− let pos = doc
416− .selection(view.id)
417− .primary()
418− .cursor(doc.text().slice(..));
419− let pos = match_brackets::find(syntax, doc.text(), pos)
420− .and_then(|pos| view.screen_coords_at_pos(doc, text, pos));
421−
422− if let Some(pos) = pos {
423− // ensure col is on screen
424− if (pos.col as u16) < viewport.width + view.first_col as u16
425− && pos.col >= view.first_col
426− {
427− let style = theme.try_get("ui.cursor.match").unwrap_or_else(|| {
428− Style::default()
429− .add_modifier(Modifier::REVERSED)
430− .add_modifier(Modifier::DIM)
431− });
432−
433− surface
434− .get_mut(
435− viewport.x + pos.col as u16,
436− viewport.y + pos.row as u16,
437− )
438− .set_style(style);
439− }
440− }
441− }
442− }
443− }
444− }
445508 }
446509
447510 pub fn render_diagnostics(
@@ -451,7 +514,6 @@ impl EditorView {
451514 viewport: Rect,
452515 surface: &mut Surface,
453516 theme: &Theme,
454− _is_focused: bool,
455517 ) {
456518 use helix_core::diagnostic::Severity;
457519 use tui::{
@@ -469,10 +531,10 @@ impl EditorView {
469531 diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
470532 });
471533
472− let warning: Style = theme.get("warning");
473− let error: Style = theme.get("error");
474− let info: Style = theme.get("info");
475− let hint: Style = theme.get("hint");
534+ let warning = theme.get("warning");
535+ let error = theme.get("error");
536+ let info = theme.get("info");
537+ let hint = theme.get("hint");
476538
477539 // Vec::with_capacity(diagnostics.len()); // rough estimate
478540 let mut lines = Vec::new();
@@ -961,7 +1023,7 @@ impl Component for EditorView {
9611023 }
9621024 }
9631025
964− fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
1026+ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
9651027 // clear with background color
9661028 surface.set_style(area, cx.editor.theme.get("ui.background"));
9671029
@@ -983,7 +1045,7 @@ impl Component for EditorView {
9831045 );
9841046 }
9851047
986− if let Some(ref info) = self.autoinfo {
1048+ if let Some(ref mut info) = self.autoinfo {
9871049 info.render(area, surface, cx);
9881050 }
9891051
@@ -1030,7 +1092,7 @@ impl Component for EditorView {
10301092 );
10311093 }
10321094
1033− if let Some(completion) = &self.completion {
1095+ if let Some(completion) = self.completion.as_mut() {
10341096 completion.render(area, surface, cx);
10351097 }
10361098 }
helix-term/src/ui/info.rs+1−1
@@ -5,7 +5,7 @@ use tui::buffer::Buffer as Surface;
55use tui::widgets::{Block, Borders, Widget};
66
77impl Component for Info {
8− fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
8+ fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
99 let style = cx.editor.theme.get("ui.popup");
1010
1111 // Calculate the area of the terminal to modify. Because we want to
helix-term/src/ui/markdown.rs+1−1
@@ -198,7 +198,7 @@ fn parse<'a>(
198198 Text::from(lines)
199199}
200200impl Component for Markdown {
201− fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
201+ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
202202 use tui::widgets::{Paragraph, Widget, Wrap};
203203
204204 let text = parse(&self.contents, Some(&cx.editor.theme), &self.config_loader);
helix-term/src/ui/menu.rs+1−1
@@ -258,7 +258,7 @@ impl<T: Item + 'static> Component for Menu<T> {
258258
259259 // TODO: required size should re-trigger when we filter items so we can draw a smaller menu
260260
261− fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
261+ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
262262 let style = cx.editor.theme.get("ui.text");
263263 let selected = cx.editor.theme.get("ui.menu.selected");
264264
helix-term/src/ui/mod.rs+20−22
@@ -13,7 +13,7 @@ pub use completion::Completion;
1313pub use editor::EditorView;
1414pub use markdown::Markdown;
1515pub use menu::Menu;
16−pub use picker::Picker;
16+pub use picker::{FilePicker, Picker};
1717pub use popup::Popup;
1818pub use prompt::{Prompt, PromptEvent};
1919pub use spinner::{ProgressSpinners, Spinner};
@@ -73,29 +73,26 @@ pub fn regex_prompt(
7373 )
7474}
7575
76−pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
76+pub fn file_picker(root: PathBuf) -> FilePicker<PathBuf> {
7777 use ignore::Walk;
7878 use std::time;
79− let files = Walk::new(root.clone()).filter_map(|entry| match entry {
80− Ok(entry) => {
81− // filter dirs, but we might need special handling for symlinks!
82− if !entry.file_type().map_or(false, |entry| entry.is_dir()) {
83− let time = if let Ok(metadata) = entry.metadata() {
84− metadata
85− .accessed()
86− .or_else(|_| metadata.modified())
87− .or_else(|_| metadata.created())
88− .unwrap_or(time::UNIX_EPOCH)
89− } else {
90− time::UNIX_EPOCH
91− };
92−
93− Some((entry.into_path(), time))
94− } else {
95− None
96− }
79+ let files = Walk::new(root.clone()).filter_map(|entry| {
80+ let entry = entry.ok()?;
81+ // Path::is_dir() traverses symlinks, so we use it over DirEntry::is_dir
82+ if entry.path().is_dir() {
83+ // Will give a false positive if metadata cannot be read (eg. permission error)
84+ return None;
9785 }
98− Err(_err) => None,
86+
87+ let time = entry.metadata().map_or(time::UNIX_EPOCH, |metadata| {
88+ metadata
89+ .accessed()
90+ .or_else(|_| metadata.modified())
91+ .or_else(|_| metadata.created())
92+ .unwrap_or(time::UNIX_EPOCH)
93+ });
94+
95+ Some((entry.into_path(), time))
9996 });
10097
10198 let mut files: Vec<_> = if root.join(".git").is_dir() {
@@ -109,7 +106,7 @@ pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
109106
110107 let files = files.into_iter().map(|(path, _)| path).collect();
111108
112− Picker::new(
109+ FilePicker::new(
113110 files,
114111 move |path: &PathBuf| {
115112 // format_fn
@@ -124,6 +121,7 @@ pub fn file_picker(root: PathBuf) -> Picker<PathBuf> {
124121 .open(path.into(), action)
125122 .expect("editor.open failed");
126123 },
124+ |_editor, path| Some((path.clone(), None)),
127125 )
128126}
129127
helix-term/src/ui/picker.rs+171−20
@@ -1,4 +1,7 @@
1−use crate::compositor::{Component, Compositor, Context, EventResult};
1+use crate::{
2+ compositor::{Component, Compositor, Context, EventResult},
3+ ui::EditorView,
4+};
25use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
36use tui::{
47 buffer::Buffer as Surface,
@@ -7,17 +10,153 @@ use tui::{
710
811use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
912use fuzzy_matcher::FuzzyMatcher;
13+use tui::widgets::Widget;
1014
11−use std::borrow::Cow;
15+use std::{borrow::Cow, collections::HashMap, path::PathBuf};
1216
1317use crate::ui::{Prompt, PromptEvent};
1418use helix_core::Position;
1519use helix_view::{
20+ document::canonicalize_path,
1621 editor::Action,
1722 graphics::{Color, CursorKind, Rect, Style},
18− Editor,
23+ Document, Editor,
1924};
2025
26+pub const MIN_SCREEN_WIDTH_FOR_PREVIEW: u16 = 80;
27+
28+/// File path and line number (used to align and highlight a line)
29+type FileLocation = (PathBuf, Option<usize>);
30+
31+pub struct FilePicker<T> {
32+ picker: Picker<T>,
33+ /// Caches paths to documents
34+ preview_cache: HashMap<PathBuf, Document>,
35+ /// Given an item in the picker, return the file path and line number to display.
36+ file_fn: Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>,
37+}
38+
39+impl<T> FilePicker<T> {
40+ pub fn new(
41+ options: Vec<T>,
42+ format_fn: impl Fn(&T) -> Cow<str> + 'static,
43+ callback_fn: impl Fn(&mut Editor, &T, Action) + 'static,
44+ preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
45+ ) -> Self {
46+ Self {
47+ picker: Picker::new(false, options, format_fn, callback_fn),
48+ preview_cache: HashMap::new(),
49+ file_fn: Box::new(preview_fn),
50+ }
51+ }
52+
53+ fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
54+ self.picker
55+ .selection()
56+ .and_then(|current| (self.file_fn)(editor, current))
57+ .and_then(|(path, line)| canonicalize_path(&path).ok().zip(Some(line)))
58+ }
59+
60+ fn calculate_preview(&mut self, editor: &Editor) {
61+ if let Some((path, _line)) = self.current_file(editor) {
62+ if !self.preview_cache.contains_key(&path) && editor.document_by_path(&path).is_none() {
63+ // TODO: enable syntax highlighting; blocked by async rendering
64+ let doc = Document::open(&path, None, Some(&editor.theme), None).unwrap();
65+ self.preview_cache.insert(path, doc);
66+ }
67+ }
68+ }
69+}
70+
71+impl<T: 'static> Component for FilePicker<T> {
72+ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
73+ // +---------+ +---------+
74+ // |prompt | |preview |
75+ // +---------+ | |
76+ // |picker | | |
77+ // | | | |
78+ // +---------+ +---------+
79+ self.calculate_preview(cx.editor);
80+ let render_preview = area.width > MIN_SCREEN_WIDTH_FOR_PREVIEW;
81+ let area = inner_rect(area);
82+ // -- Render the frame:
83+ // clear area
84+ let background = cx.editor.theme.get("ui.background");
85+ surface.clear_with(area, background);
86+
87+ let picker_width = if render_preview {
88+ area.width / 2
89+ } else {
90+ area.width
91+ };
92+
93+ let picker_area = Rect::new(area.x, area.y, picker_width, area.height);
94+ self.picker.render(picker_area, surface, cx);
95+
96+ if !render_preview {
97+ return;
98+ }
99+
100+ let preview_area = Rect::new(area.x + picker_width, area.y, area.width / 2, area.height);
101+
102+ // don't like this but the lifetime sucks
103+ let block = Block::default().borders(Borders::ALL);
104+
105+ // calculate the inner area inside the box
106+ let mut inner = block.inner(preview_area);
107+ // 1 column gap on either side
108+ inner.x += 1;
109+ inner.width = inner.width.saturating_sub(2);
110+
111+ block.render(preview_area, surface);
112+
113+ if let Some((doc, line)) = self.current_file(cx.editor).and_then(|(path, line)| {
114+ cx.editor
115+ .document_by_path(&path)
116+ .or_else(|| self.preview_cache.get(&path))
117+ .zip(Some(line))
118+ }) {
119+ // align to middle
120+ let first_line = line.unwrap_or(0).saturating_sub(inner.height as usize / 2);
121+ let offset = Position::new(first_line, 0);
122+
123+ let highlights = EditorView::doc_syntax_highlights(
124+ doc,
125+ offset,
126+ area.height,
127+ &cx.editor.theme,
128+ &cx.editor.syn_loader,
129+ );
130+ EditorView::render_text_highlights(
131+ doc,
132+ offset,
133+ inner,
134+ surface,
135+ &cx.editor.theme,
136+ highlights,
137+ );
138+
139+ // highlight the line
140+ if let Some(line) = line {
141+ for x in inner.left()..inner.right() {
142+ surface
143+ .get_mut(x, inner.y + line.saturating_sub(first_line) as u16)
144+ .set_style(cx.editor.theme.get("ui.selection.primary"));
145+ }
146+ }
147+ }
148+ }
149+
150+ fn handle_event(&mut self, event: Event, ctx: &mut Context) -> EventResult {
151+ // TODO: keybinds for scrolling preview
152+ self.picker.handle_event(event, ctx)
153+ }
154+
155+ fn cursor(&self, area: Rect, ctx: &Editor) -> (Option<Position>, CursorKind) {
156+ self.picker.cursor(area, ctx)
157+ }
158+}
159+
21160pub struct Picker<T> {
22161 options: Vec<T>,
23162 // filter: String,
@@ -30,6 +169,8 @@ pub struct Picker<T> {
30169 cursor: usize,
31170 // pattern: String,
32171 prompt: Prompt,
172+ /// Whether to render in the middle of the area
173+ render_centered: bool,
33174
34175 format_fn: Box<dyn Fn(&T) -> Cow<str>>,
35176 callback_fn: Box<dyn Fn(&mut Editor, &T, Action)>,
@@ -37,6 +178,7 @@ pub struct Picker<T> {
37178
38179impl<T> Picker<T> {
39180 pub fn new(
181+ render_centered: bool,
40182 options: Vec<T>,
41183 format_fn: impl Fn(&T) -> Cow<str> + 'static,
42184 callback_fn: impl Fn(&mut Editor, &T, Action) + 'static,
@@ -57,6 +199,7 @@ impl<T> Picker<T> {
57199 filters: Vec::new(),
58200 cursor: 0,
59201 prompt,
202+ render_centered,
60203 format_fn: Box::new(format_fn),
61204 callback_fn: Box::new(callback_fn),
62205 };
@@ -139,8 +282,8 @@ impl<T> Picker<T> {
139282// - score all the names in relation to input
140283
141284fn inner_rect(area: Rect) -> Rect {
142− let padding_vertical = area.height * 20 / 100;
143− let padding_horizontal = area.width * 20 / 100;
285+ let padding_vertical = area.height * 10 / 100;
286+ let padding_horizontal = area.width * 10 / 100;
144287
145288 Rect::new(
146289 area.x + padding_horizontal,
@@ -174,7 +317,9 @@ impl<T: 'static> Component for Picker<T> {
174317 | KeyEvent {
175318 code: KeyCode::Char('p'),
176319 modifiers: KeyModifiers::CONTROL,
177− } => self.move_up(),
320+ } => {
321+ self.move_up();
322+ }
178323 KeyEvent {
179324 code: KeyCode::Down,
180325 ..
@@ -185,7 +330,9 @@ impl<T: 'static> Component for Picker<T> {
185330 | KeyEvent {
186331 code: KeyCode::Char('n'),
187332 modifiers: KeyModifiers::CONTROL,
188− } => self.move_down(),
333+ } => {
334+ self.move_down();
335+ }
189336 KeyEvent {
190337 code: KeyCode::Esc, ..
191338 }
@@ -239,16 +386,18 @@ impl<T: 'static> Component for Picker<T> {
239386 EventResult::Consumed(None)
240387 }
241388
242− fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
243− let area = inner_rect(area);
389+ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
390+ let area = if self.render_centered {
391+ inner_rect(area)
392+ } else {
393+ area
394+ };
244395
245396 // -- Render the frame:
246−
247397 // clear area
248398 let background = cx.editor.theme.get("ui.background");
249399 surface.clear_with(area, background);
250400
251− use tui::widgets::Widget;
252401 // don't like this but the lifetime sucks
253402 let block = Block::default().borders(Borders::ALL);
254403
@@ -263,21 +412,23 @@ impl<T: 'static> Component for Picker<T> {
263412 self.prompt.render(area, surface, cx);
264413
265414 // -- Separator
266− let style = Style::default().fg(Color::Rgb(90, 89, 119));
267− let symbols = BorderType::line_symbols(BorderType::Plain);
415+ let sep_style = Style::default().fg(Color::Rgb(90, 89, 119));
416+ let borders = BorderType::line_symbols(BorderType::Plain);
268417 for x in inner.left()..inner.right() {
269418 surface
270419 .get_mut(x, inner.y + 1)
271− .set_symbol(symbols.horizontal)
272− .set_style(style);
420+ .set_symbol(borders.horizontal)
421+ .set_style(sep_style);
273422 }
274423
275424 // -- Render the contents:
425+ // subtract the area of the prompt (-2) and current item marker " > " (-3)
426+ let inner = Rect::new(inner.x + 3, inner.y + 2, inner.width - 3, inner.height - 2);
276427
277428 let style = cx.editor.theme.get("ui.text");
278429 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
279430
280− let rows = inner.height - 2; // -1 for search bar
431+ let rows = inner.height;
281432 let offset = self.cursor / (rows as usize) * (rows as usize);
282433
283434 let files = self.matches.iter().skip(offset).map(|(index, _score)| {
@@ -286,14 +437,14 @@ impl<T: 'static> Component for Picker<T> {
286437
287438 for (i, (_index, option)) in files.take(rows as usize).enumerate() {
288439 if i == (self.cursor - offset) {
289− surface.set_string(inner.x + 1, inner.y + 2 + i as u16, ">", selected);
440+ surface.set_string(inner.x - 2, inner.y + i as u16, ">", selected);
290441 }
291442
292443 surface.set_string_truncated(
293− inner.x + 3,
294− inner.y + 2 + i as u16,
444+ inner.x,
445+ inner.y + i as u16,
295446 (self.format_fn)(option),
296− (inner.width as usize).saturating_sub(3), // account for the " > "
447+ inner.width as usize,
297448 if i == (self.cursor - offset) {
298449 selected
299450 } else {
helix-term/src/ui/popup.rs+1−1
@@ -105,7 +105,7 @@ impl<T: Component> Component for Popup<T> {
105105 Some(self.size)
106106 }
107107
108− fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
108+ fn render(&mut self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
109109 cx.scroll = Some(self.scroll);
110110
111111 let position = self
helix-term/src/ui/prompt.rs+2−2
@@ -352,7 +352,7 @@ impl Prompt {
352352 }
353353
354354 if let Some(doc) = (self.doc_fn)(&self.line) {
355− let text = ui::Text::new(doc.to_string());
355+ let mut text = ui::Text::new(doc.to_string());
356356
357357 let viewport = area;
358358 let area = viewport.intersection(Rect::new(
@@ -546,7 +546,7 @@ impl Component for Prompt {
546546 EventResult::Consumed(None)
547547 }
548548
549− fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
549+ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
550550 self.render_prompt(area, surface, cx)
551551 }
552552
helix-term/src/ui/text.rs+1−1
@@ -13,7 +13,7 @@ impl Text {
1313 }
1414}
1515impl Component for Text {
16− fn render(&self, area: Rect, surface: &mut Surface, _cx: &mut Context) {
16+ fn render(&mut self, area: Rect, surface: &mut Surface, _cx: &mut Context) {
1717 use tui::widgets::{Paragraph, Widget, Wrap};
1818 let contents = tui::text::Text::from(self.contents.clone());
1919
helix-view/src/document.rs+7−3
@@ -432,14 +432,14 @@ impl Document {
432432 /// Create a new document from `path`. Encoding is auto-detected, but it can be manually
433433 /// overwritten with the `encoding` parameter.
434434 pub fn open(
435− path: PathBuf,
435+ path: &Path,
436436 encoding: Option<&'static encoding_rs::Encoding>,
437437 theme: Option<&Theme>,
438438 config_loader: Option<&syntax::Loader>,
439439 ) -> Result<Self, Error> {
440440 let (rope, encoding) = if path.exists() {
441441 let mut file =
442− std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
442+ std::fs::File::open(path).context(format!("unable to open {:?}", path))?;
443443 from_reader(&mut file, encoding)?
444444 } else {
445445 let encoding = encoding.unwrap_or(encoding_rs::UTF_8);
@@ -449,7 +449,7 @@ impl Document {
449449 let mut doc = Self::from(rope, Some(encoding));
450450
451451 // set the path and try detecting the language
452− doc.set_path(&path)?;
452+ doc.set_path(path)?;
453453 if let Some(loader) = config_loader {
454454 doc.detect_language(theme, loader);
455455 }
@@ -904,6 +904,10 @@ impl Document {
904904 &self.selections[&view_id]
905905 }
906906
907+ pub fn selections(&self) -> &HashMap<ViewId, Selection> {
908+ &self.selections
909+ }
910+
907911 pub fn relative_path(&self) -> Option<PathBuf> {
908912 let cwdir = std::env::current_dir().expect("couldn't determine current directory");
909913
helix-view/src/editor.rs+11−2
@@ -7,7 +7,11 @@ use crate::{
77};
88
99use futures_util::future;
10−use std::{path::PathBuf, sync::Arc, time::Duration};
10+use std::{
11+ path::{Path, PathBuf},
12+ sync::Arc,
13+ time::Duration,
14+};
1115
1216use slotmap::SlotMap;
1317
@@ -222,7 +226,7 @@ impl Editor {
222226 let id = if let Some(id) = id {
223227 id
224228 } else {
225− let mut doc = Document::open(path, None, Some(&self.theme), Some(&self.syn_loader))?;
229+ let mut doc = Document::open(&path, None, Some(&self.theme), Some(&self.syn_loader))?;
226230
227231 // try to find a language server based on the language name
228232 let language_server = doc
@@ -316,6 +320,11 @@ impl Editor {
316320 self.documents.iter_mut().map(|(_id, doc)| doc)
317321 }
318322
323+ pub fn document_by_path<P: AsRef<Path>>(&self, path: P) -> Option<&Document> {
324+ self.documents()
325+ .find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
326+ }
327+
319328 // pub fn current_document(&self) -> Document {
320329 // let id = self.view().doc;
321330 // let doc = &mut editor.documents[id];
helix-view/src/view.rs+1−1
@@ -10,7 +10,7 @@ use helix_core::{
1010
1111type Jump = (DocumentId, Selection);
1212
13−#[derive(Debug)]
13+#[derive(Debug, Clone)]
1414pub struct JumpList {
1515 jumps: Vec<Jump>,
1616 current: usize,