Collect some common patterns into methods on `Range`.

pull/376/head
Nathan Vegdahl 3 years ago
parent f96b8b769b
commit 0883b4fae0

@ -31,15 +31,7 @@ pub fn move_horizontally(
count: usize, count: usize,
behaviour: Movement, behaviour: Movement,
) -> Range { ) -> Range {
use Movement::Extend; let pos = range.cursor(slice);
// Shift back one grapheme if needed, to account for
// the cursor being visually 1-width.
let pos = if range.head > range.anchor {
prev_grapheme_boundary(slice, range.head)
} else {
range.head
};
// Compute the new position. // Compute the new position.
let new_pos = if dir == Direction::Backward { let new_pos = if dir == Direction::Backward {
@ -49,11 +41,7 @@ pub fn move_horizontally(
}; };
// Compute the final new range. // Compute the final new range.
if behaviour == Extend { range.put_cursor(slice, new_pos, behaviour == Movement::Extend)
range.move_head(slice, new_pos, true)
} else {
Range::point(new_pos)
}
} }
pub fn move_vertically( pub fn move_vertically(
@ -63,13 +51,7 @@ pub fn move_vertically(
count: usize, count: usize,
behaviour: Movement, behaviour: Movement,
) -> Range { ) -> Range {
// Shift back one grapheme if needed, to account for let pos = range.cursor(slice);
// the cursor being visually 1-width.
let pos = if range.head > range.anchor {
prev_grapheme_boundary(slice, range.head)
} else {
range.head
};
// Compute the current position's 2d coordinates. // Compute the current position's 2d coordinates.
let Position { row, col } = coords_at_pos(slice, pos); let Position { row, col } = coords_at_pos(slice, pos);
@ -89,24 +71,14 @@ pub fn move_vertically(
) )
}; };
// Compute the new range according to the type of movement. // Special-case to avoid moving to the end of the last non-empty line.
match behaviour { if behaviour == Movement::Extend && slice.line(new_row).len_chars() == 0 {
Movement::Move => Range { return range;
anchor: new_pos,
head: new_pos,
horiz: Some(horiz),
},
Movement::Extend => {
if slice.line(new_row).len_chars() > 0 {
let mut new_range = range.move_head(slice, new_pos, true);
new_range.horiz = Some(horiz);
new_range
} else {
range
}
}
} }
let mut new_range = range.put_cursor(slice, new_pos, behaviour == Movement::Extend);
new_range.horiz = Some(horiz);
new_range
} }
pub fn move_next_word_start(slice: RopeSlice, range: Range, count: usize) -> Range { pub fn move_next_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
@ -153,7 +125,7 @@ fn word_move(slice: RopeSlice, range: Range, count: usize, target: WordMotionTar
// Prepare the range appropriately based on the target movement // Prepare the range appropriately based on the target movement
// direction. This is addressing two things at once: // direction. This is addressing two things at once:
// //
// 1. 1-width range sementics. // 1. Block-cursor semantics.
// 2. The anchor position being irrelevant to the output result. // 2. The anchor position being irrelevant to the output result.
#[allow(clippy::collapsible_else_if)] // Makes the structure clearer in this case. #[allow(clippy::collapsible_else_if)] // Makes the structure clearer in this case.
let start_range = if is_prev { let start_range = if is_prev {

@ -73,16 +73,11 @@ impl Range {
std::cmp::max(self.anchor, self.head) std::cmp::max(self.anchor, self.head)
} }
/// The line number that the head is on (using 1-width semantics). /// The line number that the block-cursor is on.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn head_line(&self, text: RopeSlice) -> usize { pub fn cursor_line(&self, text: RopeSlice) -> usize {
let head = if self.anchor < self.head { text.char_to_line(self.cursor(text))
prev_grapheme_boundary(text, self.head)
} else {
self.head
};
text.char_to_line(head)
} }
/// The (inclusive) range of lines that the range overlaps. /// The (inclusive) range of lines that the range overlaps.
@ -248,29 +243,44 @@ impl Range {
} }
} }
/// Moves the head of the `Range` to `char_idx`, adjusting the anchor /// Gets the left-side position of the block cursor.
/// as needed to preserve 1-width range semantics. #[must_use]
#[inline]
pub fn cursor(self, text: RopeSlice) -> usize {
if self.head > self.anchor {
prev_grapheme_boundary(text, self.head)
} else {
self.head
}
}
/// Puts the left side of the block cursor at `char_idx`, optionally extending.
/// ///
/// `block_cursor` specifies whether it should treat `char_idx` as a block /// This follows "1-width" semantics, and therefore does a combination of anchor
/// cursor position or as a range-end position. /// and head moves to behave as if both the front and back of the range are 1-width
/// blocks
/// ///
/// This method assumes that the range and `char_idx` are already properly /// This method assumes that the range and `char_idx` are already properly
/// grapheme-aligned. /// grapheme-aligned.
#[must_use] #[must_use]
#[inline] #[inline]
pub fn move_head(self, text: RopeSlice, char_idx: usize, block_cursor: bool) -> Range { pub fn put_cursor(self, text: RopeSlice, char_idx: usize, extend: bool) -> Range {
let anchor = if self.head >= self.anchor && char_idx < self.anchor { if extend {
next_grapheme_boundary(text, self.anchor) let anchor = if self.head >= self.anchor && char_idx < self.anchor {
} else if self.head < self.anchor && char_idx >= self.anchor { next_grapheme_boundary(text, self.anchor)
prev_grapheme_boundary(text, self.anchor) } else if self.head < self.anchor && char_idx >= self.anchor {
} else { prev_grapheme_boundary(text, self.anchor)
self.anchor } else {
}; self.anchor
};
if block_cursor && anchor <= char_idx { if anchor <= char_idx {
Range::new(anchor, next_grapheme_boundary(text, char_idx)) Range::new(anchor, next_grapheme_boundary(text, char_idx))
} else {
Range::new(anchor, char_idx)
}
} else { } else {
Range::new(anchor, char_idx) Range::point(char_idx)
} }
} }
@ -309,18 +319,6 @@ impl Selection {
self.ranges[self.primary_index] self.ranges[self.primary_index]
} }
#[must_use]
pub fn cursor(&self, text: RopeSlice) -> usize {
let range = self.primary();
// For 1-width cursor semantics.
if range.anchor < range.head {
prev_grapheme_boundary(text, range.head).max(range.anchor)
} else {
range.head
}
}
/// Ensure selection containing only the primary selection. /// Ensure selection containing only the primary selection.
pub fn into_single(self) -> Self { pub fn into_single(self) -> Self {
if self.ranges.len() == 1 { if self.ranges.len() == 1 {
@ -452,17 +450,9 @@ impl Selection {
} }
/// Transforms the selection into all of the left-side head positions, /// Transforms the selection into all of the left-side head positions,
/// using 1-width semantics. /// using block-cursor semantics.
pub fn cursors(self, text: RopeSlice) -> Self { pub fn cursors(self, text: RopeSlice) -> Self {
self.transform(|range| { self.transform(|range| Range::point(range.cursor(text)))
// For 1-width cursor semantics.
let pos = if range.anchor < range.head {
prev_grapheme_boundary(text, range.head).max(range.anchor)
} else {
range.head
};
Range::new(pos, pos)
})
} }
pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a { pub fn fragments<'a>(&'a self, text: RopeSlice<'a>) -> impl Iterator<Item = Cow<str>> + 'a {

@ -59,17 +59,12 @@ pub fn textobject_word(
textobject: TextObject, textobject: TextObject,
_count: usize, _count: usize,
) -> Range { ) -> Range {
// For 1-width cursor semantics. let pos = range.cursor(slice);
let head = if range.head > range.anchor {
prev_grapheme_boundary(slice, range.head)
} else {
range.head
};
let word_start = find_word_boundary(slice, head, Direction::Backward); let word_start = find_word_boundary(slice, pos, Direction::Backward);
let word_end = match slice.get_char(head).map(categorize_char) { let word_end = match slice.get_char(pos).map(categorize_char) {
None | Some(CharCategory::Whitespace | CharCategory::Eol) => head, None | Some(CharCategory::Whitespace | CharCategory::Eol) => pos,
_ => find_word_boundary(slice, head + 1, Direction::Forward), _ => find_word_boundary(slice, pos + 1, Direction::Forward),
}; };
// Special case. // Special case.

@ -121,7 +121,10 @@ enum Align {
} }
fn align_view(doc: &Document, view: &mut View, align: Align) { fn align_view(doc: &Document, view: &mut View, align: Align) {
let pos = doc.selection(view.id).cursor(doc.text().slice(..)); let pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let line = doc.text().char_to_line(pos); let line = doc.text().char_to_line(pos);
let relative = match align { let relative = match align {
@ -372,17 +375,13 @@ fn goto_line_end(cx: &mut Context) {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.head_line(text); let line = range.cursor_line(text);
let line_start = text.line_to_char(line); let line_start = text.line_to_char(line);
let pos = graphemes::prev_grapheme_boundary(text, line_end_char_index(&text, line)) let pos = graphemes::prev_grapheme_boundary(text, line_end_char_index(&text, line))
.max(line_start); .max(line_start);
if doc.mode == Mode::Select { range.put_cursor(text, pos, doc.mode == Mode::Select)
range.move_head(text, pos, true)
} else {
Range::point(pos)
}
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
@ -392,14 +391,10 @@ fn goto_line_end_newline(cx: &mut Context) {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.head_line(text); let line = range.cursor_line(text);
let pos = line_end_char_index(&text, line); let pos = line_end_char_index(&text, line);
if doc.mode == Mode::Select { range.put_cursor(text, pos, doc.mode == Mode::Select)
range.move_head(text, pos, true)
} else {
Range::point(pos)
}
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
@ -409,15 +404,11 @@ fn goto_line_start(cx: &mut Context) {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.head_line(text); let line = range.cursor_line(text);
// adjust to start of the line // adjust to start of the line
let pos = text.line_to_char(line); let pos = text.line_to_char(line);
if doc.mode == Mode::Select { range.put_cursor(text, pos, doc.mode == Mode::Select)
range.move_head(text, pos, true)
} else {
Range::point(pos)
}
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
@ -427,15 +418,11 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.head_line(text); let line = range.cursor_line(text);
if let Some(pos) = find_first_non_whitespace_char(text.line(line)) { if let Some(pos) = find_first_non_whitespace_char(text.line(line)) {
let pos = pos + text.line_to_char(line); let pos = pos + text.line_to_char(line);
if doc.mode == Mode::Select { range.put_cursor(text, pos, doc.mode == Mode::Select)
range.move_head(text, pos, true)
} else {
Range::point(pos)
}
} else { } else {
range range
} }
@ -579,8 +566,8 @@ fn extend_next_word_start(cx: &mut Context) {
.min_width_1(text) .min_width_1(text)
.transform(|range| { .transform(|range| {
let word = movement::move_next_word_start(text, range, count); let word = movement::move_next_word_start(text, range, count);
let pos = graphemes::prev_grapheme_boundary(text, word.head); let pos = word.cursor(text);
range.move_head(text, pos, true) range.put_cursor(text, pos, true)
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
@ -596,8 +583,8 @@ fn extend_prev_word_start(cx: &mut Context) {
.min_width_1(text) .min_width_1(text)
.transform(|range| { .transform(|range| {
let word = movement::move_prev_word_start(text, range, count); let word = movement::move_prev_word_start(text, range, count);
let pos = word.head; let pos = word.cursor(text);
range.move_head(text, pos, true) range.put_cursor(text, pos, true)
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
@ -613,8 +600,8 @@ fn extend_next_word_end(cx: &mut Context) {
.min_width_1(text) .min_width_1(text)
.transform(|range| { .transform(|range| {
let word = movement::move_next_word_end(text, range, count); let word = movement::move_next_word_end(text, range, count);
let pos = graphemes::prev_grapheme_boundary(text, word.head); let pos = word.cursor(text);
range.move_head(text, pos, true) range.put_cursor(text, pos, true)
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
@ -663,18 +650,13 @@ where
let selection = doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
let range = if range.anchor < range.head { let range = if range.anchor < range.head {
// For 1-width cursor semantics. // For block-cursor semantics.
Range::new(range.anchor, range.head - 1) Range::new(range.anchor, range.head - 1)
} else { } else {
range range
}; };
search_fn(text, ch, range.head, count, inclusive).map_or(range, |pos| { search_fn(text, ch, range.head, count, inclusive)
if extend { .map_or(range, |pos| range.put_cursor(text, pos, extend))
range.move_head(text, pos, true)
} else {
Range::point(pos)
}
})
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
}) })
@ -895,7 +877,9 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let cursor = coords_at_pos( let cursor = coords_at_pos(
doc.text().slice(..), doc.text().slice(..),
doc.selection(view.id).cursor(doc.text().slice(..)), doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
); );
let doc_last_line = doc.text().len_lines() - 1; let doc_last_line = doc.text().len_lines() - 1;
@ -1220,12 +1204,7 @@ fn collapse_selection(cx: &mut Context) {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
let pos = if range.head > range.anchor { let pos = range.cursor(text);
// For 1-width cursor semantics.
graphemes::prev_grapheme_boundary(text, range.head)
} else {
range.head
};
Range::new(pos, pos) Range::new(pos, pos)
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
@ -2203,7 +2182,7 @@ fn append_to_line(cx: &mut Context) {
let selection = doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let line = range.head_line(text); let line = range.cursor_line(text);
let pos = line_end_char_index(&text, line); let pos = line_end_char_index(&text, line);
Range::new(pos, pos) Range::new(pos, pos)
}); });
@ -2264,7 +2243,7 @@ fn open(cx: &mut Context, open: Open) {
let mut offs = 0; let mut offs = 0;
let mut transaction = Transaction::change_by_selection(contents, selection, |range| { let mut transaction = Transaction::change_by_selection(contents, selection, |range| {
let line = range.head_line(text); let line = range.cursor_line(text);
let line = match open { let line = match open {
// adjust position to the end of the line (next line - 1) // adjust position to the end of the line (next line - 1)
@ -2472,7 +2451,9 @@ fn goto_definition(cx: &mut Context) {
let pos = pos_to_lsp_pos( let pos = pos_to_lsp_pos(
doc.text(), doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)), doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding, offset_encoding,
); );
@ -2513,7 +2494,9 @@ fn goto_type_definition(cx: &mut Context) {
let pos = pos_to_lsp_pos( let pos = pos_to_lsp_pos(
doc.text(), doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)), doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding, offset_encoding,
); );
@ -2554,7 +2537,9 @@ fn goto_implementation(cx: &mut Context) {
let pos = pos_to_lsp_pos( let pos = pos_to_lsp_pos(
doc.text(), doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)), doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding, offset_encoding,
); );
@ -2595,7 +2580,9 @@ fn goto_reference(cx: &mut Context) {
let pos = pos_to_lsp_pos( let pos = pos_to_lsp_pos(
doc.text(), doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)), doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
offset_encoding, offset_encoding,
); );
@ -2656,7 +2643,10 @@ fn goto_next_diag(cx: &mut Context) {
let editor = &mut cx.editor; let editor = &mut cx.editor;
let (view, doc) = current!(editor); let (view, doc) = current!(editor);
let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor_pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let diag = if let Some(diag) = doc let diag = if let Some(diag) = doc
.diagnostics() .diagnostics()
.iter() .iter()
@ -2677,7 +2667,10 @@ fn goto_prev_diag(cx: &mut Context) {
let editor = &mut cx.editor; let editor = &mut cx.editor;
let (view, doc) = current!(editor); let (view, doc) = current!(editor);
let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor_pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let diag = if let Some(diag) = doc let diag = if let Some(diag) = doc
.diagnostics() .diagnostics()
.iter() .iter()
@ -2705,7 +2698,9 @@ fn signature_help(cx: &mut Context) {
let pos = pos_to_lsp_pos( let pos = pos_to_lsp_pos(
doc.text(), doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)), doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
language_server.offset_encoding(), language_server.offset_encoding(),
); );
@ -3457,7 +3452,10 @@ fn completion(cx: &mut Context) {
}; };
let offset_encoding = language_server.offset_encoding(); let offset_encoding = language_server.offset_encoding();
let cursor = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let pos = pos_to_lsp_pos(doc.text(), cursor, offset_encoding); let pos = pos_to_lsp_pos(doc.text(), cursor, offset_encoding);
@ -3514,7 +3512,9 @@ fn hover(cx: &mut Context) {
let pos = pos_to_lsp_pos( let pos = pos_to_lsp_pos(
doc.text(), doc.text(),
doc.selection(view.id).cursor(doc.text().slice(..)), doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
language_server.offset_encoding(), language_server.offset_encoding(),
); );
@ -3580,7 +3580,10 @@ fn match_brackets(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
if let Some(syntax) = doc.syntax() { if let Some(syntax) = doc.syntax() {
let pos = doc.selection(view.id).cursor(doc.text().slice(..)); let pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) { if let Some(pos) = match_brackets::find(syntax, doc.text(), pos) {
let selection = Selection::point(pos); let selection = Selection::point(pos);
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
@ -3684,7 +3687,10 @@ fn align_view_bottom(cx: &mut Context) {
fn align_view_middle(cx: &mut Context) { fn align_view_middle(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let pos = doc.selection(view.id).cursor(doc.text().slice(..)); let pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let pos = coords_at_pos(doc.text().slice(..), pos); let pos = coords_at_pos(doc.text().slice(..), pos);
const OFFSET: usize = 7; // gutters const OFFSET: usize = 7; // gutters

@ -86,7 +86,10 @@ impl Completion {
let item = item.unwrap(); let item = item.unwrap();
// if more text was entered, remove it // if more text was entered, remove it
let cursor = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
if trigger_offset < cursor { if trigger_offset < cursor {
let remove = Transaction::change( let remove = Transaction::change(
doc.text(), doc.text(),
@ -109,7 +112,10 @@ impl Completion {
) )
} else { } else {
let text = item.insert_text.as_ref().unwrap_or(&item.label); let text = item.insert_text.as_ref().unwrap_or(&item.label);
let cursor = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
Transaction::change( Transaction::change(
doc.text(), doc.text(),
vec![(cursor, cursor, Some(text.as_str().into()))].into_iter(), vec![(cursor, cursor, Some(text.as_str().into()))].into_iter(),
@ -155,7 +161,10 @@ impl Completion {
// TODO: hooks should get processed immediately so maybe do it after select!(), before // TODO: hooks should get processed immediately so maybe do it after select!(), before
// looping? // looping?
let cursor = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
if self.trigger_offset <= cursor { if self.trigger_offset <= cursor {
let fragment = doc.text().slice(self.trigger_offset..cursor); let fragment = doc.text().slice(self.trigger_offset..cursor);
let text = Cow::from(fragment); let text = Cow::from(fragment);
@ -212,7 +221,10 @@ impl Component for Completion {
.language() .language()
.and_then(|scope| scope.strip_prefix("source.")) .and_then(|scope| scope.strip_prefix("source."))
.unwrap_or(""); .unwrap_or("");
let cursor_pos = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor_pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row
- view.first_line) as u16; - view.first_line) as u16;

@ -406,7 +406,10 @@ impl EditorView {
// TODO: set cursor position for IME // TODO: set cursor position for IME
if let Some(syntax) = doc.syntax() { if let Some(syntax) = doc.syntax() {
use helix_core::match_brackets; use helix_core::match_brackets;
let pos = doc.selection(view.id).cursor(doc.text().slice(..)); let pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let pos = match_brackets::find(syntax, doc.text(), pos) let pos = match_brackets::find(syntax, doc.text(), pos)
.and_then(|pos| view.screen_coords_at_pos(doc, text, pos)); .and_then(|pos| view.screen_coords_at_pos(doc, text, pos));
@ -451,7 +454,10 @@ impl EditorView {
widgets::{Paragraph, Widget}, widgets::{Paragraph, Widget},
}; };
let cursor = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let diagnostics = doc.diagnostics().iter().filter(|diagnostic| { let diagnostics = doc.diagnostics().iter().filter(|diagnostic| {
diagnostic.range.start <= cursor && diagnostic.range.end >= cursor diagnostic.range.start <= cursor && diagnostic.range.end >= cursor
@ -565,7 +571,9 @@ impl EditorView {
let position_info = { let position_info = {
let pos = coords_at_pos( let pos = coords_at_pos(
doc.text().slice(..), doc.text().slice(..),
doc.selection(view.id).cursor(doc.text().slice(..)), doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
); );
format!("{}:{}", pos.row + 1, pos.col + 1) // convert to 1-indexing format!("{}:{}", pos.row + 1, pos.col + 1) // convert to 1-indexing
}; };

@ -145,7 +145,10 @@ impl Editor {
.entry(view.id) .entry(view.id)
.or_insert_with(|| Selection::point(0)); .or_insert_with(|| Selection::point(0));
// TODO: reuse align_view // TODO: reuse align_view
let pos = doc.selection(view.id).cursor(doc.text().slice(..)); let pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let line = doc.text().char_to_line(pos); let line = doc.text().char_to_line(pos);
view.first_line = line.saturating_sub(view.area.height as usize / 2); view.first_line = line.saturating_sub(view.area.height as usize / 2);
@ -295,7 +298,10 @@ impl Editor {
const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
let view = view!(self); let view = view!(self);
let doc = &self.documents[view.doc]; let doc = &self.documents[view.doc];
let cursor = doc.selection(view.id).cursor(doc.text().slice(..)); let cursor = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) { if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) {
pos.col += view.area.x as usize + OFFSET as usize; pos.col += view.area.x as usize + OFFSET as usize;
pos.row += view.area.y as usize; pos.row += view.area.y as usize;

@ -84,7 +84,10 @@ impl View {
} }
pub fn ensure_cursor_in_view(&mut self, doc: &Document) { pub fn ensure_cursor_in_view(&mut self, doc: &Document) {
let cursor = doc.selection(self.id).cursor(doc.text().slice(..)); let cursor = doc
.selection(self.id)
.primary()
.cursor(doc.text().slice(..));
let pos = coords_at_pos(doc.text().slice(..), cursor); let pos = coords_at_pos(doc.text().slice(..), cursor);
let line = pos.row; let line = pos.row;
let col = pos.col; let col = pos.col;

Loading…
Cancel
Save