@@ -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 }