aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/line_index.rs
blob: 9abbb0d0955185edf22a6f1559559dbfffd071e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::TextUnit;
use superslice::Ext;

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct LineIndex {
    newlines: Vec<TextUnit>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LineCol {
    pub line: u32,
    pub col: TextUnit,
}

impl LineIndex {
    pub fn new(text: &str) -> LineIndex {
        let mut newlines = vec![0.into()];
        let mut curr = 0.into();
        for c in text.chars() {
            curr += TextUnit::of_char(c);
            if c == '\n' {
                newlines.push(curr);
            }
        }
        LineIndex { newlines }
    }

    pub fn line_col(&self, offset: TextUnit) -> LineCol {
        let line = self.newlines.upper_bound(&offset) - 1;
        let line_start_offset = self.newlines[line];
        let col = offset - line_start_offset;
        LineCol {
            line: line as u32,
            col,
        }
    }

    pub fn offset(&self, line_col: LineCol) -> TextUnit {
        //TODO: return Result
        self.newlines[line_col.line as usize] + line_col.col
    }
}

#[test]
fn test_line_index() {
    let text = "hello\nworld";
    let index = LineIndex::new(text);
    assert_eq!(
        index.line_col(0.into()),
        LineCol {
            line: 0,
            col: 0.into()
        }
    );
    assert_eq!(
        index.line_col(1.into()),
        LineCol {
            line: 0,
            col: 1.into()
        }
    );
    assert_eq!(
        index.line_col(5.into()),
        LineCol {
            line: 0,
            col: 5.into()
        }
    );
    assert_eq!(
        index.line_col(6.into()),
        LineCol {
            line: 1,
            col: 0.into()
        }
    );
    assert_eq!(
        index.line_col(7.into()),
        LineCol {
            line: 1,
            col: 1.into()
        }
    );
    assert_eq!(
        index.line_col(8.into()),
        LineCol {
            line: 1,
            col: 2.into()
        }
    );
    assert_eq!(
        index.line_col(10.into()),
        LineCol {
            line: 1,
            col: 4.into()
        }
    );
    assert_eq!(
        index.line_col(11.into()),
        LineCol {
            line: 1,
            col: 5.into()
        }
    );
    assert_eq!(
        index.line_col(12.into()),
        LineCol {
            line: 1,
            col: 6.into()
        }
    );

    let text = "\nhello\nworld";
    let index = LineIndex::new(text);
    assert_eq!(
        index.line_col(0.into()),
        LineCol {
            line: 0,
            col: 0.into()
        }
    );
    assert_eq!(
        index.line_col(1.into()),
        LineCol {
            line: 1,
            col: 0.into()
        }
    );
    assert_eq!(
        index.line_col(2.into()),
        LineCol {
            line: 1,
            col: 1.into()
        }
    );
    assert_eq!(
        index.line_col(6.into()),
        LineCol {
            line: 1,
            col: 5.into()
        }
    );
    assert_eq!(
        index.line_col(7.into()),
        LineCol {
            line: 2,
            col: 0.into()
        }
    );
}