Visual Studio Code, rebuilt in Rust
Rust
//! coreCommands.ts mouse commands on the cursor state's `selectionStart`: a double or triple click selects a word
//! or a line, and its drag goes on by whole words or lines (`_wordSelect`, `_lineSelect`, their `Drag` and
//! `lastCursor` forms, `_moveToSelect`).
use super::cursor::{self, Position, Selection};
use super::word_operations::{self, WordClassifier};
use super::OpenEditor;
use crate::brackets::BracketRange;
use crate::languages::Languages;
impl OpenEditor {
/// `_moveToSelect`: the primary cursor alone, from its selection start to `position`.
pub fn move_to_select(&mut self, position: Position) {
let start = self.selection_start(0);
self.set_primary_from(start, position);
}
/// `_lastCursorMoveToSelect`: the cursor added last, from its selection start to `position`.
pub fn last_cursor_move_to_select(&mut self, position: Position) {
let index = self.last_added();
let start = self.selection_start(index);
self.set_last_from(index, start, position);
}
/// `_wordSelect` (a press) and `_wordSelectDrag` on the primary cursor, which stays alone.
pub fn word_select(&mut self, classifier: &WordClassifier, position: Position, in_selection_mode: bool) {
let start = in_selection_mode.then(|| self.selection_start(0));
let (start, head) = word_operations::word(classifier, &self.document().lines, start, position);
self.set_primary_from(start, head);
}
/// `lastCursorWordSelect`: the cursor added last selects the word at `position`, or extends by words while it
/// has a selection.
pub fn last_cursor_word_select(&mut self, classifier: &WordClassifier, position: Position) {
let index = self.last_added();
let start = self.selection_start(index);
let has_selection = !self.selections[index].is_empty() || start.0 != start.1;
let (start, head) = word_operations::word(classifier, &self.document().lines, has_selection.then_some(start), position);
self.set_last_from(index, start, head);
}
/// `_lineSelect` (a press) and `_lineSelectDrag` on the primary cursor, which stays alone.
pub fn line_select(&mut self, position: Position, in_selection_mode: bool) {
let start = in_selection_mode.then(|| self.selection_start(0));
let (start, head) = self.line(start, position);
self.set_primary_from(start, head);
}
/// `lastCursorLineSelect` and `lastCursorLineSelectDrag`: the same on the cursor added last.
pub fn last_cursor_line_select(&mut self, position: Position, in_selection_mode: bool) {
let index = self.last_added();
let start = in_selection_mode.then(|| self.selection_start(index));
let (start, head) = self.line(start, position);
self.set_last_from(index, start, head);
}
/// `CursorMoveCommands.line`: a press selects the line with its break; a drag from the line it started on
/// selects to the start of the view line under `position` above it, or past the view line below it.
fn line(&self, selection_start: Option<(Position, Position)>, position: Position) -> ((Position, Position), Position) {
let lines = &self.document().lines;
let Some(start) = selection_start else {
let end = if position.line + 1 < lines.len() { Position::new(position.line + 1, 0) } else { cursor::line_end(lines, position) };
return ((Position::new(position.line, 0), end), end);
};
let view_line = self.position_view_line(position, false);
let head = match position.line.cmp(&start.0.line) {
std::cmp::Ordering::Less => {
let segment = self.segment(view_line);
Position::new(segment.line, segment.start)
}
std::cmp::Ordering::Greater if view_line + 1 < self.view_line_count() => {
let segment = self.segment(view_line + 1);
Position::new(segment.line, segment.start)
}
std::cmp::Ordering::Greater => {
let segment = self.segment(view_line);
Position::new(segment.line, segment.end)
}
std::cmp::Ordering::Equal => start.1,
};
(start, head)
}
/// viewController.ts `_trySelectBracketContent` and `_trySelectStringContent` (`editor.doubleClickSelectsBlock`):
/// what lies between a bracket pair or the quotes of a string, for a double click right inside either end.
pub fn double_click_block(&self, languages: &Languages, position: Position) -> Option<Selection> {
let brackets = self.bracket_pairs(languages);
let document = self.document();
let lines = &document.lines;
let text = &lines[position.line];
let byte = |column: usize| text.char_indices().nth(column).map_or(text.len(), |(offset, _)| offset);
let at_byte = |line: usize, offset: usize| Position::new(line, lines[line][..offset].chars().count());
let between = |[open, close]: [BracketRange; 2]| Selection { anchor: at_byte(open.0, open.1.end), head: at_byte(close.0, close.1.start) };
let offset = byte(position.column);
if position.column > 0
&& let Some(pair) = brackets.match_bracket(position.line, byte(position.column - 1))
&& (pair[0].0, pair[0].1.end) == (position.line, offset)
{
return Some(between(pair));
}
if let Some(pair) = brackets.match_bracket(position.line, offset)
&& (pair[1].0, pair[1].1.start) == (position.line, offset)
{
return Some(between(pair));
}
if !document.tokens.is_cheap_to_tokenize(lines, position.line) {
return None;
}
// The run of string tokens around the token at the position, by byte ends.
let tokens = document.tokens.line_tokens(lines, position.line);
let is_string = |index: usize| tokens[index].1 == textmate::StandardTokenType::String;
let mut first = tokens.iter().position(|token| token.0 > offset).unwrap_or(tokens.len() - 1);
let mut last = first;
while first > 0 && is_string(first - 1) {
first -= 1;
}
while last + 1 < tokens.len() && is_string(last + 1) {
last += 1;
}
let start = if first == 0 { 0 } else { tokens[first - 1].0 };
let end = tokens[last].0;
if offset != start + 1 && offset + 1 != end {
return None;
}
let quote = text[start..].chars().next()?;
if !matches!(quote, '"' | '\'' | '`') || !text[..end].ends_with(quote) || end - start < 2 || contains_rtl(&text[start + 1..end - 1]) {
return None;
}
Some(Selection { anchor: at_byte(position.line, start + 1), head: at_byte(position.line, end - 1) })
}
/// `SingleCursorState.selectionStart` of cursor `index`: the word or line a mouse command started from while
/// that cursor still holds the selection made from it, else the empty range at its anchor.
fn selection_start(&self, index: usize) -> (Position, Position) {
let selection = self.selections[index];
match self.selection_start {
Some((start, made)) if made == selection => start,
_ => (selection.anchor, selection.anchor),
}
}
fn set_primary_from(&mut self, start: (Position, Position), head: Position) {
let selection = compute_selection(start, head);
self.set_selections(vec![selection]);
self.selection_start = Some((start, selection));
}
fn set_last_from(&mut self, index: usize, start: (Position, Position), head: Position) {
let selection = compute_selection(start, head);
self.set_last_cursor(index, selection);
self.selection_start = Some((start, selection));
}
}
#[cfg(test)]
mod tests;
/// strings.ts `containsRTL`: a char of bidi class R or AL.
fn contains_rtl(text: &str) -> bool {
const RTL: &[(u32, u32)] = &[
(0x05BE, 0x05BE), (0x05C0, 0x05C0), (0x05C3, 0x05C3), (0x05C6, 0x05C6), (0x05D0, 0x05F4), (0x0608, 0x0608), (0x060B, 0x060B),
(0x060D, 0x060D), (0x061B, 0x064A), (0x066D, 0x066F), (0x0671, 0x06D5), (0x06E5, 0x06E6), (0x06EE, 0x06EF), (0x06FA, 0x0710),
(0x0712, 0x072F), (0x074D, 0x07A5), (0x07B1, 0x07EA), (0x07F4, 0x07F5), (0x07FA, 0x07FA), (0x07FE, 0x0815), (0x081A, 0x081A),
(0x0824, 0x0824), (0x0828, 0x0828), (0x0830, 0x0858), (0x085E, 0x088E), (0x08A0, 0x08C9), (0x200F, 0x200F), (0xFB1D, 0xFB1D),
(0xFB1F, 0xFB28), (0xFB2A, 0xFD3D), (0xFD50, 0xFDC7), (0xFDF0, 0xFDFC), (0xFE70, 0xFEFC), (0x10800, 0x1091B), (0x10920, 0x10A00),
(0x10A10, 0x10A35), (0x10A40, 0x10AE4), (0x10AEB, 0x10B35), (0x10B40, 0x10D23), (0x10E80, 0x10EA9), (0x10EAD, 0x10F45),
(0x10F51, 0x10F81), (0x10F86, 0x10FF6), (0x1E800, 0x1E8CF), (0x1E900, 0x1E943), (0x1E94B, 0x1EEBB),
];
text.chars().any(|c| RTL.iter().any(|&(first, last)| (first..=last).contains(&(c as u32))))
}
/// `SingleCursorState._computeSelection`: a head before a non-empty start range selects from the range's end.
fn compute_selection(start: (Position, Position), head: Position) -> Selection {
match start.0 != start.1 && head <= start.0 {
true => Selection { anchor: start.1, head },
false => Selection { anchor: start.0, head },
}
}