diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 20:23:17 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 20:23:17 +0100 |
commit | 26262aaf05983c5b7f41cc438e287523268fe1eb (patch) | |
tree | 4473bd2559c838e0df5a10c9ea1307b49d51b8bc /libeditor | |
parent | 3fff5e94ebf772f8485aaa2bda2ea36be766fdb3 (diff) |
extend selection via LSP
Diffstat (limited to 'libeditor')
-rw-r--r-- | libeditor/src/lib.rs | 1 | ||||
-rw-r--r-- | libeditor/src/line_index.rs | 40 |
2 files changed, 24 insertions, 17 deletions
diff --git a/libeditor/src/lib.rs b/libeditor/src/lib.rs index 9da71743f..f77647338 100644 --- a/libeditor/src/lib.rs +++ b/libeditor/src/lib.rs | |||
@@ -10,6 +10,7 @@ use libsyntax2::{ | |||
10 | SyntaxKind::*, | 10 | SyntaxKind::*, |
11 | }; | 11 | }; |
12 | pub use libsyntax2::{TextRange, TextUnit, ast}; | 12 | pub use libsyntax2::{TextRange, TextUnit, ast}; |
13 | pub use self::line_index::{LineIndex, LineCol}; | ||
13 | 14 | ||
14 | #[derive(Debug)] | 15 | #[derive(Debug)] |
15 | pub struct HighlightedRange { | 16 | pub struct HighlightedRange { |
diff --git a/libeditor/src/line_index.rs b/libeditor/src/line_index.rs index feb482b32..801726aa5 100644 --- a/libeditor/src/line_index.rs +++ b/libeditor/src/line_index.rs | |||
@@ -1,6 +1,7 @@ | |||
1 | use superslice::Ext; | 1 | use superslice::Ext; |
2 | use ::{TextUnit}; | 2 | use ::TextUnit; |
3 | 3 | ||
4 | #[derive(Clone, Debug)] | ||
4 | pub struct LineIndex { | 5 | pub struct LineIndex { |
5 | newlines: Vec<TextUnit>, | 6 | newlines: Vec<TextUnit>, |
6 | } | 7 | } |
@@ -24,11 +25,16 @@ impl LineIndex { | |||
24 | LineIndex { newlines } | 25 | LineIndex { newlines } |
25 | } | 26 | } |
26 | 27 | ||
27 | pub fn translate(&self, offset: TextUnit) -> LineCol { | 28 | pub fn line_col(&self, offset: TextUnit) -> LineCol { |
28 | let line = self.newlines.upper_bound(&offset) - 1; | 29 | let line = self.newlines.upper_bound(&offset) - 1; |
29 | let line_start_offset = self.newlines[line]; | 30 | let line_start_offset = self.newlines[line]; |
30 | let col = offset - line_start_offset; | 31 | let col = offset - line_start_offset; |
31 | return LineCol { line: line as u32, col } | 32 | return LineCol { line: line as u32, col }; |
33 | } | ||
34 | |||
35 | pub fn offset(&self, line_col: LineCol) -> TextUnit { | ||
36 | //TODO: return Result | ||
37 | self.newlines[line_col.line as usize] + line_col.col | ||
32 | } | 38 | } |
33 | } | 39 | } |
34 | 40 | ||
@@ -36,21 +42,21 @@ impl LineIndex { | |||
36 | fn test_line_index() { | 42 | fn test_line_index() { |
37 | let text = "hello\nworld"; | 43 | let text = "hello\nworld"; |
38 | let index = LineIndex::new(text); | 44 | let index = LineIndex::new(text); |
39 | assert_eq!(index.translate(0.into()), LineCol { line: 0, col: 0.into()}); | 45 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col: 0.into() }); |
40 | assert_eq!(index.translate(1.into()), LineCol { line: 0, col: 1.into()}); | 46 | assert_eq!(index.line_col(1.into()), LineCol { line: 0, col: 1.into() }); |
41 | assert_eq!(index.translate(5.into()), LineCol { line: 0, col: 5.into()}); | 47 | assert_eq!(index.line_col(5.into()), LineCol { line: 0, col: 5.into() }); |
42 | assert_eq!(index.translate(6.into()), LineCol { line: 1, col: 0.into()}); | 48 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col: 0.into() }); |
43 | assert_eq!(index.translate(7.into()), LineCol { line: 1, col: 1.into()}); | 49 | assert_eq!(index.line_col(7.into()), LineCol { line: 1, col: 1.into() }); |
44 | assert_eq!(index.translate(8.into()), LineCol { line: 1, col: 2.into()}); | 50 | assert_eq!(index.line_col(8.into()), LineCol { line: 1, col: 2.into() }); |
45 | assert_eq!(index.translate(10.into()), LineCol { line: 1, col: 4.into()}); | 51 | assert_eq!(index.line_col(10.into()), LineCol { line: 1, col: 4.into() }); |
46 | assert_eq!(index.translate(11.into()), LineCol { line: 1, col: 5.into()}); | 52 | assert_eq!(index.line_col(11.into()), LineCol { line: 1, col: 5.into() }); |
47 | assert_eq!(index.translate(12.into()), LineCol { line: 1, col: 6.into()}); | 53 | assert_eq!(index.line_col(12.into()), LineCol { line: 1, col: 6.into() }); |
48 | 54 | ||
49 | let text = "\nhello\nworld"; | 55 | let text = "\nhello\nworld"; |
50 | let index = LineIndex::new(text); | 56 | let index = LineIndex::new(text); |
51 | assert_eq!(index.translate(0.into()), LineCol { line: 0, col: 0.into()}); | 57 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col: 0.into() }); |
52 | assert_eq!(index.translate(1.into()), LineCol { line: 1, col: 0.into()}); | 58 | assert_eq!(index.line_col(1.into()), LineCol { line: 1, col: 0.into() }); |
53 | assert_eq!(index.translate(2.into()), LineCol { line: 1, col: 1.into()}); | 59 | assert_eq!(index.line_col(2.into()), LineCol { line: 1, col: 1.into() }); |
54 | assert_eq!(index.translate(6.into()), LineCol { line: 1, col: 5.into()}); | 60 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col: 5.into() }); |
55 | assert_eq!(index.translate(7.into()), LineCol { line: 2, col: 0.into()}); | 61 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col: 0.into() }); |
56 | } | 62 | } |