tuix_core/text/selection.rs
1
2// Adapted from xi-editor
3
4// Copyright 2017 The xi-editor Authors.
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18/// The affinity of a cursor on a line break
19pub enum Affinity {
20 Downstream,
21 Upstream,
22}
23
24pub struct SelectionRegion {
25 pub start: usize,
26 pub end: usize,
27 pub affinity: Affinity,
28}
29
30pub struct Selection {
31 regions: Vec<SelectionRegion>,
32}
33
34#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
35pub struct CursorIndex {
36 line: usize,
37 character: usize,
38}
39
40impl CursorIndex {
41 pub fn next(self, lines: Vec<std::ops::Range<usize>>) -> Option<Self> {
42
43 //let index = self;
44
45 lines.iter().nth(self.line).and_then(|rng| {
46 if self.character >= rng.clone().count() {
47 Some(CursorIndex {
48 line: self.line + 1,
49 character: 0,
50 })
51 } else {
52 Some(CursorIndex {
53 line: self.line,
54 character: self.character + 1,
55 })
56 }
57 })
58 }
59}
60
61pub enum Cursor {
62 Index(CursorIndex),
63 Selection {
64 start: CursorIndex,
65 end: CursorIndex,
66 }
67}