зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 00:15:50 +03:00
make the first element of range is smaller than the second one (#81)
Этот коммит содержится в:
@@ -1430,7 +1430,7 @@ impl<'a> TextArea<'a> {
|
|||||||
self.select_style
|
self.select_style
|
||||||
}
|
}
|
||||||
|
|
||||||
fn selection_range_(&self) -> Option<(Pos, Pos)> {
|
fn selection_positions(&self) -> Option<(Pos, Pos)> {
|
||||||
let (sr, sc) = self.selection_start?;
|
let (sr, sc) = self.selection_start?;
|
||||||
let (er, ec) = self.cursor;
|
let (er, ec) = self.cursor;
|
||||||
let (so, eo) = (self.line_offset(sr, sc), self.line_offset(er, ec));
|
let (so, eo) = (self.line_offset(sr, sc), self.line_offset(er, ec));
|
||||||
@@ -1443,8 +1443,8 @@ impl<'a> TextArea<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take_selection_range(&mut self) -> Option<(Pos, Pos)> {
|
fn take_selection_positions(&mut self) -> Option<(Pos, Pos)> {
|
||||||
let range = self.selection_range_();
|
let range = self.selection_positions();
|
||||||
self.cancel_selection();
|
self.cancel_selection();
|
||||||
range
|
range
|
||||||
}
|
}
|
||||||
@@ -1468,7 +1468,7 @@ impl<'a> TextArea<'a> {
|
|||||||
/// assert_eq!(textarea.lines(), ["Hello World"]); // Text does not change
|
/// assert_eq!(textarea.lines(), ["Hello World"]); // Text does not change
|
||||||
/// ```
|
/// ```
|
||||||
pub fn copy(&mut self) {
|
pub fn copy(&mut self) {
|
||||||
if let Some((start, end)) = self.take_selection_range() {
|
if let Some((start, end)) = self.take_selection_positions() {
|
||||||
if start.row == end.row {
|
if start.row == end.row {
|
||||||
self.yank = self.lines[start.row][start.offset..end.offset]
|
self.yank = self.lines[start.row][start.offset..end.offset]
|
||||||
.to_string()
|
.to_string()
|
||||||
@@ -1506,7 +1506,7 @@ impl<'a> TextArea<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn delete_selection(&mut self, should_yank: bool) -> bool {
|
fn delete_selection(&mut self, should_yank: bool) -> bool {
|
||||||
if let Some((s, e)) = self.take_selection_range() {
|
if let Some((s, e)) = self.take_selection_positions() {
|
||||||
self.delete_range(s, e, should_yank);
|
self.delete_range(s, e, should_yank);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1608,7 +1608,7 @@ impl<'a> TextArea<'a> {
|
|||||||
hl.search(matches, self.search.style);
|
hl.search(matches, self.search.style);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((start, end)) = self.selection_range_() {
|
if let Some((start, end)) = self.selection_positions() {
|
||||||
hl.selection(row, start.row, start.offset, end.row, end.offset);
|
hl.selection(row, start.row, start.offset, end.row, end.offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2040,14 +2040,21 @@ impl<'a> TextArea<'a> {
|
|||||||
self.cursor
|
self.cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current selection range. A tuple (selection_start, cursor) of 0-base character-wise (row, col)
|
/// Get the current selection range as a pair of the start position and the end position. The range is bounded
|
||||||
/// positions. The selection start position will always come first, even when it is ahead of
|
/// inclusively below and exclusively above. The positions are 0-base character-wise (row, col) values.
|
||||||
/// the cursor.
|
/// The first element of the pair is always smaller than the second one even when it is ahead of the cursor.
|
||||||
|
/// When no text is selected, this method returns `None`.
|
||||||
/// ```
|
/// ```
|
||||||
/// use tui_textarea::TextArea;
|
/// use tui_textarea::TextArea;
|
||||||
/// use tui_textarea::CursorMove;
|
/// use tui_textarea::CursorMove;
|
||||||
///
|
///
|
||||||
/// let mut textarea = TextArea::from(["abc"]);
|
/// let mut textarea = TextArea::from([
|
||||||
|
/// "aaa",
|
||||||
|
/// "bbb",
|
||||||
|
/// "ccc",
|
||||||
|
/// ]);
|
||||||
|
///
|
||||||
|
/// // It returns `None` when the text selection is not ongoing
|
||||||
/// assert_eq!(textarea.selection_range(), None);
|
/// assert_eq!(textarea.selection_range(), None);
|
||||||
///
|
///
|
||||||
/// textarea.start_selection();
|
/// textarea.start_selection();
|
||||||
@@ -2056,16 +2063,25 @@ impl<'a> TextArea<'a> {
|
|||||||
/// textarea.move_cursor(CursorMove::Forward);
|
/// textarea.move_cursor(CursorMove::Forward);
|
||||||
/// assert_eq!(textarea.selection_range(), Some(((0, 0), (0, 1))));
|
/// assert_eq!(textarea.selection_range(), Some(((0, 0), (0, 1))));
|
||||||
///
|
///
|
||||||
/// textarea.cancel_selection();
|
/// textarea.move_cursor(CursorMove::Down);
|
||||||
/// assert_eq!(textarea.selection_range(), None);
|
/// assert_eq!(textarea.selection_range(), Some(((0, 0), (1, 1))));
|
||||||
///
|
///
|
||||||
|
/// // Start selection at (1, 1)
|
||||||
|
/// textarea.cancel_selection();
|
||||||
/// textarea.start_selection();
|
/// textarea.start_selection();
|
||||||
|
///
|
||||||
|
/// // The first element of the pair is always smaller than the second one
|
||||||
/// textarea.move_cursor(CursorMove::Back);
|
/// textarea.move_cursor(CursorMove::Back);
|
||||||
/// assert_eq!(textarea.selection_range(), Some(((0, 1), (0, 0))));
|
/// assert_eq!(textarea.selection_range(), Some(((1, 0), (1, 1))));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn selection_range(&self) -> Option<((usize, usize), (usize, usize))> {
|
pub fn selection_range(&self) -> Option<((usize, usize), (usize, usize))> {
|
||||||
self.selection_start
|
self.selection_start.map(|pos| {
|
||||||
.map(|selection_start| (selection_start, self.cursor))
|
if pos > self.cursor {
|
||||||
|
(self.cursor, pos)
|
||||||
|
} else {
|
||||||
|
(pos, self.cursor)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set text alignment. When [`Alignment::Center`] or [`Alignment::Right`] is set, line number is automatically
|
/// Set text alignment. When [`Alignment::Center`] or [`Alignment::Right`] is set, line number is automatically
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user