2022-11-08 16:19:59 +04:00
|
|
|
use crate::{editor::GutterType, graphics::Rect, Document, DocumentId, ViewId};
|
2022-10-10 23:54:57 +04:00
|
|
|
use helix_core::{
|
|
|
|
pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
|
|
|
|
};
|
2020-09-21 13:24:16 +04:00
|
|
|
|
2022-04-16 05:41:25 +04:00
|
|
|
use std::fmt;
|
|
|
|
|
2021-03-24 13:01:26 +04:00
|
|
|
type Jump = (DocumentId, Selection);
|
|
|
|
|
2021-08-12 11:00:42 +04:00
|
|
|
#[derive(Debug, Clone)]
|
2021-03-24 13:01:26 +04:00
|
|
|
pub struct JumpList {
|
|
|
|
jumps: Vec<Jump>,
|
|
|
|
current: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JumpList {
|
|
|
|
pub fn new(initial: Jump) -> Self {
|
|
|
|
Self {
|
|
|
|
jumps: vec![initial],
|
|
|
|
current: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, jump: Jump) {
|
2021-03-29 11:32:42 +04:00
|
|
|
self.jumps.truncate(self.current);
|
|
|
|
// don't push duplicates
|
|
|
|
if self.jumps.last() != Some(&jump) {
|
|
|
|
self.jumps.push(jump);
|
|
|
|
self.current = self.jumps.len();
|
|
|
|
}
|
2021-03-24 13:01:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn forward(&mut self, count: usize) -> Option<&Jump> {
|
|
|
|
if self.current + count < self.jumps.len() {
|
|
|
|
self.current += count;
|
2021-06-08 20:40:38 +04:00
|
|
|
self.jumps.get(self.current)
|
|
|
|
} else {
|
|
|
|
None
|
2021-03-24 13:01:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 20:40:38 +04:00
|
|
|
// Taking view and doc to prevent unnecessary cloning when jump is not required.
|
|
|
|
pub fn backward(&mut self, view_id: ViewId, doc: &mut Document, count: usize) -> Option<&Jump> {
|
|
|
|
if let Some(current) = self.current.checked_sub(count) {
|
|
|
|
if self.current == self.jumps.len() {
|
|
|
|
let jump = (doc.id(), doc.selection(view_id).clone());
|
|
|
|
self.push(jump);
|
|
|
|
}
|
|
|
|
self.current = current;
|
|
|
|
self.jumps.get(self.current)
|
|
|
|
} else {
|
|
|
|
None
|
2021-03-24 13:01:26 +04:00
|
|
|
}
|
|
|
|
}
|
helix-term/commands: implement buffer-close (bc, bclose) (#1035)
* helix-view/view: impl method to remove document from jumps
* helix-view/editor: impl close_document
* helix-view/editor: remove close_buffer argument from `close`
According to archseer, this was never implemented or used properly. Now
that we have a proper "buffer close" function, we can get rid of this.
* helix-term/commands: implement buffer-close (bc, bclose)
This behaves the same as Kakoune's `delete-buffer` / `db` command:
* With 3 files opened by the user with `:o ab`, `:o cd`, and `:o ef`:
* `buffer-close` once closes `ef` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ab`
* `buffer-close` again closes `ab` and switches to a scratch buffer
* With 3 files opened from the command line with `hx -- ab cd ef`:
* `buffer-close` once closes `ab` and switches to `cd`
* `buffer-close` again closes `cd` and switches to `ef`
* `buffer-close` again closes `ef` and switches to a scratch buffer
* With 1 file opened (`ab`):
* `buffer-close` once closes `ab` and switches to a scratch buffer
* `buffer-close` again closes the scratch buffer and switches to a new
scratch buffer
* helix-term/commands: implement buffer-close! (bclose!, bc!)
Namely, if you have a document open in multiple splits, all the splits
will be closed at the same time, leaving only splits without that
document focused (or a scratch buffer if they were all focused on that
buffer).
* helix-view/tree: reset focus if Tree is empty
2021-11-15 19:30:45 +04:00
|
|
|
|
|
|
|
pub fn remove(&mut self, doc_id: &DocumentId) {
|
|
|
|
self.jumps.retain(|(other_id, _)| other_id != doc_id);
|
|
|
|
}
|
2022-07-22 05:21:46 +04:00
|
|
|
|
|
|
|
pub fn get(&self) -> &[Jump] {
|
|
|
|
&self.jumps
|
|
|
|
}
|
2022-10-10 23:54:57 +04:00
|
|
|
|
|
|
|
/// Applies a [`Transaction`] of changes to the jumplist.
|
|
|
|
/// This is necessary to ensure that changes to documents do not leave jump-list
|
|
|
|
/// selections pointing to parts of the text which no longer exist.
|
|
|
|
fn apply(&mut self, transaction: &Transaction, doc: &Document) {
|
|
|
|
let text = doc.text().slice(..);
|
|
|
|
|
|
|
|
for (doc_id, selection) in &mut self.jumps {
|
|
|
|
if doc.id() == *doc_id {
|
|
|
|
*selection = selection
|
|
|
|
.clone()
|
|
|
|
.map(transaction.changes())
|
|
|
|
.ensure_invariants(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 13:01:26 +04:00
|
|
|
}
|
|
|
|
|
2022-07-22 05:23:00 +04:00
|
|
|
#[derive(Clone)]
|
2020-09-21 13:24:16 +04:00
|
|
|
pub struct View {
|
2021-03-24 09:03:20 +04:00
|
|
|
pub id: ViewId,
|
2021-08-19 07:52:07 +04:00
|
|
|
pub offset: Position,
|
2021-02-03 14:36:54 +04:00
|
|
|
pub area: Rect,
|
2022-04-16 05:41:25 +04:00
|
|
|
pub doc: DocumentId,
|
2021-03-24 13:01:26 +04:00
|
|
|
pub jumps: JumpList,
|
2022-04-27 20:14:46 +04:00
|
|
|
// documents accessed from this view from the oldest one to last viewed one
|
|
|
|
pub docs_access_history: Vec<DocumentId>,
|
2021-12-02 08:46:57 +04:00
|
|
|
/// the last modified files before the current one
|
|
|
|
/// ordered from most frequent to least frequent
|
|
|
|
// uses two docs because we want to be able to swap between the
|
|
|
|
// two last modified docs which we need to manually keep track of
|
|
|
|
pub last_modified_docs: [Option<DocumentId>; 2],
|
2022-04-27 23:21:20 +04:00
|
|
|
/// used to store previous selections of tree-sitter objects
|
2022-01-06 06:12:02 +04:00
|
|
|
pub object_selections: Vec<Selection>,
|
2022-11-08 16:19:59 +04:00
|
|
|
/// GutterTypes used to fetch Gutter (constructor) and width for rendering
|
|
|
|
gutters: Vec<GutterType>,
|
2022-04-16 05:41:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for View {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("View")
|
|
|
|
.field("id", &self.id)
|
|
|
|
.field("area", &self.area)
|
|
|
|
.field("doc", &self.doc)
|
|
|
|
.finish()
|
|
|
|
}
|
2020-09-21 13:24:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View {
|
2022-04-16 05:41:25 +04:00
|
|
|
pub fn new(doc: DocumentId, gutter_types: Vec<crate::editor::GutterType>) -> Self {
|
2021-05-07 09:40:11 +04:00
|
|
|
Self {
|
2021-03-24 09:03:20 +04:00
|
|
|
id: ViewId::default(),
|
2020-10-22 09:35:07 +04:00
|
|
|
doc,
|
2021-08-19 07:52:07 +04:00
|
|
|
offset: Position::new(0, 0),
|
2021-02-03 14:36:54 +04:00
|
|
|
area: Rect::default(), // will get calculated upon inserting into tree
|
2021-03-24 13:01:26 +04:00
|
|
|
jumps: JumpList::new((doc, Selection::point(0))), // TODO: use actual sel
|
2022-04-27 20:14:46 +04:00
|
|
|
docs_access_history: Vec::new(),
|
2021-12-02 08:46:57 +04:00
|
|
|
last_modified_docs: [None, None],
|
2022-01-06 06:12:02 +04:00
|
|
|
object_selections: Vec::new(),
|
2022-11-08 16:19:59 +04:00
|
|
|
gutters: gutter_types,
|
2021-05-07 09:40:11 +04:00
|
|
|
}
|
2020-09-21 13:24:16 +04:00
|
|
|
}
|
|
|
|
|
2022-04-27 20:14:46 +04:00
|
|
|
pub fn add_to_history(&mut self, id: DocumentId) {
|
|
|
|
if let Some(pos) = self.docs_access_history.iter().position(|&doc| doc == id) {
|
|
|
|
self.docs_access_history.remove(pos);
|
|
|
|
}
|
|
|
|
self.docs_access_history.push(id);
|
|
|
|
}
|
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
pub fn inner_area(&self, doc: &Document) -> Rect {
|
|
|
|
self.area.clip_left(self.gutter_offset(doc)).clip_bottom(1) // -1 for statusline
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inner_height(&self) -> usize {
|
|
|
|
self.area.clip_bottom(1).height.into() // -1 for statusline
|
2022-07-18 05:13:47 +04:00
|
|
|
}
|
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
pub fn gutters(&self) -> &[GutterType] {
|
2022-07-18 05:13:47 +04:00
|
|
|
&self.gutters
|
2021-08-19 08:19:15 +04:00
|
|
|
}
|
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
pub fn gutter_offset(&self, doc: &Document) -> u16 {
|
|
|
|
let mut offset = self
|
|
|
|
.gutters
|
|
|
|
.iter()
|
|
|
|
.map(|gutter| gutter.width(self, doc) as u16)
|
|
|
|
.sum();
|
|
|
|
|
|
|
|
if offset > 0 {
|
|
|
|
offset += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
offset
|
|
|
|
}
|
|
|
|
|
2021-11-09 06:11:45 +04:00
|
|
|
//
|
|
|
|
pub fn offset_coords_to_in_view(
|
|
|
|
&self,
|
|
|
|
doc: &Document,
|
|
|
|
scrolloff: usize,
|
|
|
|
) -> Option<(usize, usize)> {
|
2021-07-26 19:40:30 +04:00
|
|
|
let cursor = doc
|
|
|
|
.selection(self.id)
|
|
|
|
.primary()
|
|
|
|
.cursor(doc.text().slice(..));
|
2021-11-03 07:02:29 +04:00
|
|
|
|
|
|
|
let Position { col, row: line } =
|
|
|
|
visual_coords_at_pos(doc.text().slice(..), cursor, doc.tab_width());
|
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
let inner_area = self.inner_area(doc);
|
2021-08-19 08:19:15 +04:00
|
|
|
let last_line = (self.offset.row + inner_area.height as usize).saturating_sub(1);
|
2021-04-14 09:27:47 +04:00
|
|
|
|
2021-08-11 08:53:38 +04:00
|
|
|
// - 1 so we have at least one gap in the middle.
|
|
|
|
// a height of 6 with padding of 3 on each side will keep shifting the view back and forth
|
|
|
|
// as we type
|
2021-08-19 08:19:15 +04:00
|
|
|
let scrolloff = scrolloff.min(inner_area.height.saturating_sub(1) as usize / 2);
|
2020-09-21 13:24:16 +04:00
|
|
|
|
2021-08-19 08:19:15 +04:00
|
|
|
let last_col = self.offset.col + inner_area.width.saturating_sub(1) as usize;
|
2020-09-21 13:24:16 +04:00
|
|
|
|
2021-11-09 06:11:45 +04:00
|
|
|
let row = if line > last_line.saturating_sub(scrolloff) {
|
2020-09-21 13:24:16 +04:00
|
|
|
// scroll down
|
2021-11-09 06:11:45 +04:00
|
|
|
self.offset.row + line - (last_line.saturating_sub(scrolloff))
|
2021-08-19 07:52:07 +04:00
|
|
|
} else if line < self.offset.row + scrolloff {
|
2020-09-21 13:24:16 +04:00
|
|
|
// scroll up
|
2021-11-09 06:11:45 +04:00
|
|
|
line.saturating_sub(scrolloff)
|
|
|
|
} else {
|
|
|
|
self.offset.row
|
|
|
|
};
|
2021-04-09 13:48:56 +04:00
|
|
|
|
2021-11-09 06:11:45 +04:00
|
|
|
let col = if col > last_col.saturating_sub(scrolloff) {
|
2021-04-09 13:48:56 +04:00
|
|
|
// scroll right
|
2021-11-09 06:11:45 +04:00
|
|
|
self.offset.col + col - (last_col.saturating_sub(scrolloff))
|
2021-08-19 07:52:07 +04:00
|
|
|
} else if col < self.offset.col + scrolloff {
|
2021-04-09 13:48:56 +04:00
|
|
|
// scroll left
|
2021-11-09 06:11:45 +04:00
|
|
|
col.saturating_sub(scrolloff)
|
|
|
|
} else {
|
|
|
|
self.offset.col
|
|
|
|
};
|
|
|
|
if row == self.offset.row && col == self.offset.col {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((row, col))
|
2021-04-09 13:48:56 +04:00
|
|
|
}
|
2020-09-21 13:24:16 +04:00
|
|
|
}
|
2020-10-01 23:16:24 +04:00
|
|
|
|
2021-11-09 06:11:45 +04:00
|
|
|
pub fn ensure_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) {
|
|
|
|
if let Some((row, col)) = self.offset_coords_to_in_view(doc, scrolloff) {
|
|
|
|
self.offset.row = row;
|
|
|
|
self.offset.col = col;
|
2021-04-09 13:48:56 +04:00
|
|
|
}
|
2020-09-21 13:24:16 +04:00
|
|
|
}
|
2020-10-01 23:16:24 +04:00
|
|
|
|
2021-11-09 06:11:45 +04:00
|
|
|
pub fn is_cursor_in_view(&mut self, doc: &Document, scrolloff: usize) -> bool {
|
|
|
|
self.offset_coords_to_in_view(doc, scrolloff).is_none()
|
|
|
|
}
|
|
|
|
|
2020-10-01 23:16:24 +04:00
|
|
|
/// Calculates the last visible line on screen
|
|
|
|
#[inline]
|
2021-03-23 12:47:40 +04:00
|
|
|
pub fn last_line(&self, doc: &Document) -> usize {
|
2020-10-01 23:16:24 +04:00
|
|
|
std::cmp::min(
|
2021-07-19 19:39:48 +04:00
|
|
|
// Saturating subs to make it inclusive zero indexing.
|
2022-11-08 16:19:59 +04:00
|
|
|
(self.offset.row + self.inner_height()).saturating_sub(1),
|
2021-07-19 19:39:48 +04:00
|
|
|
doc.text().len_lines().saturating_sub(1),
|
2020-10-01 23:16:24 +04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Translates a document position to an absolute position in the terminal.
|
|
|
|
/// Returns a (line, col) position if the position is visible on screen.
|
|
|
|
// TODO: Could return width as well for the character width at cursor.
|
2021-03-23 12:47:40 +04:00
|
|
|
pub fn screen_coords_at_pos(
|
|
|
|
&self,
|
|
|
|
doc: &Document,
|
|
|
|
text: RopeSlice,
|
|
|
|
pos: usize,
|
|
|
|
) -> Option<Position> {
|
2020-10-01 23:16:24 +04:00
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
|
2021-08-19 07:52:07 +04:00
|
|
|
if line < self.offset.row || line > self.last_line(doc) {
|
2020-10-01 23:16:24 +04:00
|
|
|
// Line is not visible on screen
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:47:40 +04:00
|
|
|
let tab_width = doc.tab_width();
|
2022-03-03 12:41:39 +04:00
|
|
|
// TODO: visual_coords_at_pos also does char_to_line which we ignore, can we reuse the call?
|
|
|
|
let Position { col, .. } = visual_coords_at_pos(text, pos, tab_width);
|
2020-10-01 23:16:24 +04:00
|
|
|
|
2021-06-03 03:30:40 +04:00
|
|
|
// It is possible for underflow to occur if the buffer length is larger than the terminal width.
|
2021-08-19 07:52:07 +04:00
|
|
|
let row = line.saturating_sub(self.offset.row);
|
|
|
|
let col = col.saturating_sub(self.offset.col);
|
2020-10-01 23:16:24 +04:00
|
|
|
|
|
|
|
Some(Position::new(row, col))
|
|
|
|
}
|
2020-10-22 09:35:07 +04:00
|
|
|
|
2021-07-30 11:52:00 +04:00
|
|
|
pub fn text_pos_at_screen_coords(
|
|
|
|
&self,
|
2022-11-08 16:19:59 +04:00
|
|
|
doc: &Document,
|
2021-07-30 11:50:38 +04:00
|
|
|
row: u16,
|
|
|
|
column: u16,
|
2021-07-30 11:52:00 +04:00
|
|
|
tab_width: usize,
|
|
|
|
) -> Option<usize> {
|
2022-11-08 16:19:59 +04:00
|
|
|
let text = doc.text().slice(..);
|
|
|
|
let inner = self.inner_area(doc);
|
2021-08-19 08:19:15 +04:00
|
|
|
// 1 for status
|
|
|
|
if row < inner.top() || row >= inner.bottom() {
|
2021-07-30 11:52:00 +04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-08-19 08:19:15 +04:00
|
|
|
if column < inner.left() || column > inner.right() {
|
2021-07-30 11:50:38 +04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-06-21 20:36:36 +04:00
|
|
|
let text_row = (row - inner.y) as usize + self.offset.row;
|
|
|
|
if text_row > text.len_lines() - 1 {
|
2021-07-30 11:52:00 +04:00
|
|
|
return Some(text.len_chars());
|
|
|
|
}
|
|
|
|
|
2022-06-21 20:36:36 +04:00
|
|
|
let text_col = (column - inner.x) as usize + self.offset.col;
|
2021-07-30 11:52:00 +04:00
|
|
|
|
2022-06-21 20:36:36 +04:00
|
|
|
Some(pos_at_visual_coords(
|
2022-11-08 16:19:59 +04:00
|
|
|
text,
|
2022-06-21 20:36:36 +04:00
|
|
|
Position {
|
|
|
|
row: text_row,
|
|
|
|
col: text_col,
|
|
|
|
},
|
|
|
|
tab_width,
|
|
|
|
))
|
2021-07-30 11:52:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Translates a screen position to position in the text document.
|
|
|
|
/// Returns a usize typed position in bounds of the text if found in this view, None if out of view.
|
2021-07-30 11:50:38 +04:00
|
|
|
pub fn pos_at_screen_coords(&self, doc: &Document, row: u16, column: u16) -> Option<usize> {
|
2022-11-08 16:19:59 +04:00
|
|
|
self.text_pos_at_screen_coords(doc, row, column, doc.tab_width())
|
2021-07-30 11:52:00 +04:00
|
|
|
}
|
2021-09-04 22:14:24 +04:00
|
|
|
|
|
|
|
/// Translates screen coordinates into coordinates on the gutter of the view.
|
|
|
|
/// Returns a tuple of usize typed line and column numbers starting with 0.
|
|
|
|
/// Returns None if coordinates are not on the gutter.
|
2021-11-22 06:09:09 +04:00
|
|
|
pub fn gutter_coords_at_screen_coords(&self, row: u16, column: u16) -> Option<Position> {
|
2021-09-04 22:14:24 +04:00
|
|
|
// 1 for status
|
|
|
|
if row < self.area.top() || row >= self.area.bottom() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if column < self.area.left() || column > self.area.right() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-11-22 06:09:09 +04:00
|
|
|
Some(Position::new(
|
2021-09-04 22:14:24 +04:00
|
|
|
(row - self.area.top()) as usize,
|
|
|
|
(column - self.area.left()) as usize,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-06-05 06:27:41 +04:00
|
|
|
pub fn remove_document(&mut self, doc_id: &DocumentId) {
|
|
|
|
self.jumps.remove(doc_id);
|
|
|
|
self.docs_access_history.retain(|doc| doc != doc_id);
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:34:22 +04:00
|
|
|
// pub fn traverse<F>(&self, text: RopeSlice, start: usize, end: usize, fun: F)
|
2020-10-23 08:51:08 +04:00
|
|
|
// where
|
|
|
|
// F: Fn(usize, usize),
|
|
|
|
// {
|
|
|
|
// let start = self.screen_coords_at_pos(text, start);
|
|
|
|
// let end = self.screen_coords_at_pos(text, end);
|
|
|
|
|
|
|
|
// match (start, end) {
|
|
|
|
// // fully on screen
|
|
|
|
// (Some(start), Some(end)) => {
|
|
|
|
// // we want to calculate ends of lines for each char..
|
|
|
|
// }
|
|
|
|
// // from start to end of screen
|
|
|
|
// (Some(start), None) => {}
|
|
|
|
// // from start of screen to end
|
|
|
|
// (None, Some(end)) => {}
|
|
|
|
// // not on screen
|
|
|
|
// (None, None) => return,
|
|
|
|
// }
|
|
|
|
// }
|
2022-10-10 23:54:57 +04:00
|
|
|
|
|
|
|
/// Applies a [`Transaction`] to the view.
|
|
|
|
/// Instead of calling this function directly, use [crate::apply_transaction]
|
|
|
|
/// which applies a transaction to the [`Document`] and view together.
|
|
|
|
pub fn apply(&mut self, transaction: &Transaction, doc: &Document) -> bool {
|
|
|
|
self.jumps.apply(transaction, doc);
|
|
|
|
true
|
|
|
|
}
|
2020-09-21 13:24:16 +04:00
|
|
|
}
|
2021-07-30 11:52:00 +04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use helix_core::Rope;
|
2022-11-08 16:19:59 +04:00
|
|
|
const OFFSET: u16 = 4; // 1 diagnostic + 2 linenr (< 100 lines) + 1 gutter
|
2022-04-16 05:41:25 +04:00
|
|
|
const OFFSET_WITHOUT_LINE_NUMBERS: u16 = 2; // 1 diagnostic + 1 gutter
|
|
|
|
// const OFFSET: u16 = GUTTERS.iter().map(|(_, width)| *width as u16).sum();
|
2022-11-08 16:19:59 +04:00
|
|
|
use crate::document::Document;
|
2022-04-16 05:41:25 +04:00
|
|
|
use crate::editor::GutterType;
|
2021-07-30 11:52:00 +04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_text_pos_at_screen_coords() {
|
2022-04-16 05:41:25 +04:00
|
|
|
let mut view = View::new(
|
|
|
|
DocumentId::default(),
|
2022-07-26 05:08:09 +04:00
|
|
|
vec![GutterType::Diagnostics, GutterType::LineNumbers],
|
2022-04-16 05:41:25 +04:00
|
|
|
);
|
2021-07-30 11:52:00 +04:00
|
|
|
view.area = Rect::new(40, 40, 40, 40);
|
2021-07-30 19:37:13 +04:00
|
|
|
let rope = Rope::from_str("abc\n\tdef");
|
2022-11-08 16:19:59 +04:00
|
|
|
let doc = Document::from(rope, None);
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 40, 2, 4), None);
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 40, 41, 4), None);
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 0, 2, 4), None);
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 0, 49, 4), None);
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 0, 41, 4), None);
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 40, 81, 4), None);
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 78, 41, 4), None);
|
2021-07-30 11:52:00 +04:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 3, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(3)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 40, 80, 4), Some(3));
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2021-07-30 11:52:00 +04:00
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 41, 40 + OFFSET + 1, 4),
|
2022-03-04 06:23:05 +04:00
|
|
|
Some(4)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 41, 40 + OFFSET + 4, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(5)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 41, 40 + OFFSET + 7, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(8)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
2022-11-08 16:19:59 +04:00
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 41, 80, 4), Some(8));
|
2021-07-30 19:37:13 +04:00
|
|
|
}
|
|
|
|
|
2022-04-16 05:41:25 +04:00
|
|
|
#[test]
|
|
|
|
fn test_text_pos_at_screen_coords_without_line_numbers_gutter() {
|
2022-07-26 05:08:09 +04:00
|
|
|
let mut view = View::new(DocumentId::default(), vec![GutterType::Diagnostics]);
|
2022-04-16 05:41:25 +04:00
|
|
|
view.area = Rect::new(40, 40, 40, 40);
|
|
|
|
let rope = Rope::from_str("abc\n\tdef");
|
2022-11-08 16:19:59 +04:00
|
|
|
let doc = Document::from(rope, None);
|
2022-04-16 05:41:25 +04:00
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 41, 40 + OFFSET_WITHOUT_LINE_NUMBERS + 1, 4),
|
2022-04-16 05:41:25 +04:00
|
|
|
Some(4)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_text_pos_at_screen_coords_without_any_gutters() {
|
|
|
|
let mut view = View::new(DocumentId::default(), vec![]);
|
|
|
|
view.area = Rect::new(40, 40, 40, 40);
|
|
|
|
let rope = Rope::from_str("abc\n\tdef");
|
2022-11-08 16:19:59 +04:00
|
|
|
let doc = Document::from(rope, None);
|
|
|
|
assert_eq!(view.text_pos_at_screen_coords(&doc, 41, 40 + 1, 4), Some(4));
|
2022-04-16 05:41:25 +04:00
|
|
|
}
|
|
|
|
|
2021-07-30 19:37:13 +04:00
|
|
|
#[test]
|
|
|
|
fn test_text_pos_at_screen_coords_cjk() {
|
2022-04-16 05:41:25 +04:00
|
|
|
let mut view = View::new(
|
|
|
|
DocumentId::default(),
|
2022-07-26 05:08:09 +04:00
|
|
|
vec![GutterType::Diagnostics, GutterType::LineNumbers],
|
2022-04-16 05:41:25 +04:00
|
|
|
);
|
2021-07-30 19:37:13 +04:00
|
|
|
view.area = Rect::new(40, 40, 40, 40);
|
|
|
|
let rope = Rope::from_str("Hi! こんにちは皆さん");
|
2022-11-08 16:19:59 +04:00
|
|
|
let doc = Document::from(rope, None);
|
2021-07-30 19:37:13 +04:00
|
|
|
|
2021-07-30 11:52:00 +04:00
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(0)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
2022-03-04 06:23:05 +04:00
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 4, 4),
|
2022-03-04 06:23:05 +04:00
|
|
|
Some(4)
|
|
|
|
);
|
2021-07-30 11:52:00 +04:00
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 5, 4),
|
2022-03-04 06:23:05 +04:00
|
|
|
Some(4)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 6, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(5)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 7, 4),
|
2022-03-04 06:23:05 +04:00
|
|
|
Some(5)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 8, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(6)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
2021-07-30 19:37:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_text_pos_at_screen_coords_graphemes() {
|
2022-04-16 05:41:25 +04:00
|
|
|
let mut view = View::new(
|
|
|
|
DocumentId::default(),
|
2022-07-26 05:08:09 +04:00
|
|
|
vec![GutterType::Diagnostics, GutterType::LineNumbers],
|
2022-04-16 05:41:25 +04:00
|
|
|
);
|
2021-07-30 19:37:13 +04:00
|
|
|
view.area = Rect::new(40, 40, 40, 40);
|
|
|
|
let rope = Rope::from_str("Hèl̀l̀ò world!");
|
2022-11-08 16:19:59 +04:00
|
|
|
let doc = Document::from(rope, None);
|
2021-07-30 11:52:00 +04:00
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(0)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 1, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(1)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 2, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(3)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 3, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(5)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-11-08 16:19:59 +04:00
|
|
|
view.text_pos_at_screen_coords(&doc, 40, 40 + OFFSET + 4, 4),
|
2021-07-30 19:37:13 +04:00
|
|
|
Some(7)
|
2021-07-30 11:52:00 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|