@@ -8,7 +8,7 @@ use crate::{
9use helix_core::{
9use helix_core::{
10 coords_at_pos,
10 coords_at_pos,
11− graphemes::ensure_grapheme_boundary_next,
11+ graphemes::{ensure_grapheme_boundary_next, next_grapheme_boundary, prev_grapheme_boundary},
12 syntax::{self, HighlightEvent},
12 syntax::{self, HighlightEvent},
13 LineEnding, Position, Range,
13 LineEnding, Position, Range,
@@ -165,21 +165,18 @@ impl EditorView {
166 .unwrap_or(base_cursor_scope);
166 .unwrap_or(base_cursor_scope);
168− let primary_selection_scope = theme
169− .find_scope_index("ui.selection.primary")
170− .unwrap_or(selection_scope);
172 let highlights: Box<dyn Iterator<Item = HighlightEvent>> = if is_focused {
168 let highlights: Box<dyn Iterator<Item = HighlightEvent>> = if is_focused {
173− // inject selections as highlight scopes
174− let mut spans: Vec<(usize, std::ops::Range<usize>)> = Vec::new();
176 // TODO: primary + insert mode patching:
169 // TODO: primary + insert mode patching:
177 // (ui.cursor.primary).patch(mode).unwrap_or(cursor)
170 // (ui.cursor.primary).patch(mode).unwrap_or(cursor)
179 let primary_cursor_scope = theme
171 let primary_cursor_scope = theme
180 .find_scope_index("ui.cursor.primary")
172 .find_scope_index("ui.cursor.primary")
181 .unwrap_or(cursor_scope);
173 .unwrap_or(cursor_scope);
174+ let primary_selection_scope = theme
175+ .find_scope_index("ui.selection.primary")
176+ .unwrap_or(selection_scope);
178+ // inject selections as highlight scopes
179+ let mut spans: Vec<(usize, std::ops::Range<usize>)> = Vec::new();
183 for (i, range) in selections.iter().enumerate() {
180 for (i, range) in selections.iter().enumerate() {
184 let (cursor_scope, selection_scope) = if i == primary_idx {
181 let (cursor_scope, selection_scope) = if i == primary_idx {
185 (primary_cursor_scope, primary_selection_scope)
182 (primary_cursor_scope, primary_selection_scope)
@@ -187,19 +184,23 @@ impl EditorView {
187 (cursor_scope, selection_scope)
184 (cursor_scope, selection_scope)
187+ // Special-case: cursor at end of the rope.
190− if range.head == range.anchor {
188+ if range.head == range.anchor && range.head == text.len_chars() {
191 spans.push((cursor_scope, range.head..range.head + 1));
189 spans.push((cursor_scope, range.head..range.head + 1));
192 continue;
190 continue;
195− let reverse = range.head < range.anchor;
193+ let range = range.min_width_1(text);
196−
194+ if range.head > range.anchor {
197− if reverse {
195+ // Standard case.
198− spans.push((cursor_scope, range.head..range.head + 1));
196+ let cursor_start = prev_grapheme_boundary(text, range.head);
199− spans.push((selection_scope, range.head + 1..range.anchor + 1));
197+ spans.push((selection_scope, range.anchor..cursor_start));
198+ spans.push((cursor_scope, cursor_start..range.head));
201+ let cursor_end = next_grapheme_boundary(text, range.head);
202+ spans.push((cursor_scope, range.head..cursor_end));
201− spans.push((selection_scope, range.anchor..range.head));
203+ spans.push((selection_scope, cursor_end..range.anchor));
202− spans.push((cursor_scope, range.head..range.head + 1));
@@ -232,7 +233,10 @@ impl EditorView {
232 spans.pop();
233 spans.pop();
234 HighlightEvent::Source { start, end } => {
235 HighlightEvent::Source { start, end } => {
236+ // `unwrap_or_else` part is for off-the-end indices of
237+ // the rope, to allow cursor highlighting at the end
235− let text = text.slice(start..end);
239+ let text = text.get_slice(start..end).unwrap_or_else(|| " ".into());
237 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
241 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
@@ -301,7 +305,11 @@ impl EditorView {
301 let info: Style = theme.get("info");
305 let info: Style = theme.get("info");
302 let hint: Style = theme.get("hint");
306 let hint: Style = theme.get("hint");
308+ // Whether to draw the line number for the last line of the
309+ // document or not. We only draw it if it's not an empty line.
310+ let draw_last = text.line_to_byte(last_line) < text.len_bytes();
304− for (i, line) in (view.first_line..last_line).enumerate() {
312+ for (i, line) in (view.first_line..=last_line).enumerate() {
305 use helix_core::diagnostic::Severity;
313 use helix_core::diagnostic::Severity;
306 if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
314 if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
307 surface.set_stringn(
315 surface.set_stringn(
@@ -318,11 +326,17 @@ impl EditorView {
321− // line numbers having selections are rendered differently
329+ // Line numbers having selections are rendered
330+ // differently, further below.
331+ let line_number_text = if line == last_line && !draw_last {
334+ format!("{:>5}", line + 1)
322 surface.set_stringn(
336 surface.set_stringn(
323 viewport.x + 1 - OFFSET,
337 viewport.x + 1 - OFFSET,
324 viewport.y + i as u16,
338 viewport.y + i as u16,
325− format!("{:>5}", line + 1),
339+ line_number_text,
@@ -336,7 +350,7 @@ impl EditorView {
336 if is_focused {
350 if is_focused {
337 let screen = {
351 let screen = {
338 let start = text.line_to_char(view.first_line);
352 let start = text.line_to_char(view.first_line);
339− let end = text.line_to_char(last_line + 1);
353+ let end = text.line_to_char(last_line + 1) + 1; // +1 for cursor at end of text.
340 Range::new(start, end)
354 Range::new(start, end)
@@ -345,10 +359,17 @@ impl EditorView {
345 for selection in selection.iter().filter(|range| range.overlaps(&screen)) {
359 for selection in selection.iter().filter(|range| range.overlaps(&screen)) {
346 let head = view.screen_coords_at_pos(doc, text, selection.head);
360 let head = view.screen_coords_at_pos(doc, text, selection.head);
347 if let Some(head) = head {
361 if let Some(head) = head {
362+ // Draw line number for selected lines.
363+ let line_number = view.first_line + head.row;
364+ let line_number_text = if line_number == last_line && !draw_last {
367+ format!("{:>5}", line_number + 1)
348 surface.set_stringn(
369 surface.set_stringn(
349 viewport.x + 1 - OFFSET,
370 viewport.x + 1 - OFFSET,
350 viewport.y + head.row as u16,
371 viewport.y + head.row as u16,
351− format!("{:>5}", view.first_line + head.row + 1),
372+ line_number_text,
353 linenr_select,
374 linenr_select,