aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db/src/line_index.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-04-24 23:17:50 +0100
committerAleksey Kladov <[email protected]>2020-04-25 10:59:18 +0100
commitdc2151085e9b117bc87307bf47edf3d17a170b49 (patch)
tree24a22cdd8e5fb3b11d7b95fc264e56a91bd206a9 /crates/ra_ide_db/src/line_index.rs
parent8843588fca7a6022b86800d5d2539594c0de93cf (diff)
Cleanups
Diffstat (limited to 'crates/ra_ide_db/src/line_index.rs')
-rw-r--r--crates/ra_ide_db/src/line_index.rs24
1 files changed, 10 insertions, 14 deletions
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs
index 7794dc9fd..81eebc711 100644
--- a/crates/ra_ide_db/src/line_index.rs
+++ b/crates/ra_ide_db/src/line_index.rs
@@ -1,9 +1,8 @@
1//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` 1//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)`
2//! representation. 2//! representation.
3use std::iter;
4// TODO: un TextSize
5use ra_syntax::{TextRange, TextSize}; 3use ra_syntax::{TextRange, TextSize};
6use rustc_hash::FxHashMap; 4use rustc_hash::FxHashMap;
5use std::iter;
7use superslice::Ext; 6use superslice::Ext;
8 7
9#[derive(Clone, Debug, PartialEq, Eq)] 8#[derive(Clone, Debug, PartialEq, Eq)]
@@ -42,7 +41,8 @@ impl LineIndex {
42 let mut curr_col = 0.into(); 41 let mut curr_col = 0.into();
43 let mut line = 0; 42 let mut line = 0;
44 for c in text.chars() { 43 for c in text.chars() {
45 curr_row += TextSize::of(c); 44 let c_len = TextSize::of(c);
45 curr_row += c_len;
46 if c == '\n' { 46 if c == '\n' {
47 newlines.push(curr_row); 47 newlines.push(curr_row);
48 48
@@ -58,12 +58,11 @@ impl LineIndex {
58 continue; 58 continue;
59 } 59 }
60 60
61 let char_len = TextSize::of(c); 61 if !c.is_ascii() {
62 if char_len > TextSize::from_usize(1) { 62 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + c_len });
63 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len });
64 } 63 }
65 64
66 curr_col += char_len; 65 curr_col += c_len;
67 } 66 }
68 67
69 // Save any utf-16 characters seen in the last line 68 // Save any utf-16 characters seen in the last line
@@ -102,22 +101,19 @@ impl LineIndex {
102 } 101 }
103 102
104 fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize { 103 fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize {
104 let mut res: usize = col.into();
105 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 105 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
106 let mut correction = 0;
107 for c in utf16_chars { 106 for c in utf16_chars {
108 if col >= c.end { 107 if c.end <= col {
109 correction += usize::from(c.len()) - 1; 108 res -= usize::from(c.len()) - 1;
110 } else { 109 } else {
111 // From here on, all utf16 characters come *after* the character we are mapping, 110 // From here on, all utf16 characters come *after* the character we are mapping,
112 // so we don't need to take them into account 111 // so we don't need to take them into account
113 break; 112 break;
114 } 113 }
115 } 114 }
116
117 usize::from(col) - correction
118 } else {
119 usize::from(col)
120 } 115 }
116 res
121 } 117 }
122 118
123 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize { 119 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize {