aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_editor/src/line_index.rs34
-rw-r--r--crates/ra_lsp_server/src/conv.rs17
2 files changed, 28 insertions, 23 deletions
diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs
index 0b3a28cd4..26b309b78 100644
--- a/crates/ra_editor/src/line_index.rs
+++ b/crates/ra_editor/src/line_index.rs
@@ -11,7 +11,7 @@ pub struct LineIndex {
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12pub struct LineCol { 12pub struct LineCol {
13 pub line: u32, 13 pub line: u32,
14 pub col: u32, 14 pub col_utf16: u32,
15} 15}
16 16
17#[derive(Clone, Debug, Hash, PartialEq, Eq)] 17#[derive(Clone, Debug, Hash, PartialEq, Eq)]
@@ -75,13 +75,13 @@ impl LineIndex {
75 75
76 LineCol { 76 LineCol {
77 line: line as u32, 77 line: line as u32,
78 col: self.utf8_to_utf16_col(line as u32, col) as u32, 78 col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32,
79 } 79 }
80 } 80 }
81 81
82 pub fn offset(&self, line_col: LineCol) -> TextUnit { 82 pub fn offset(&self, line_col: LineCol) -> TextUnit {
83 //TODO: return Result 83 //TODO: return Result
84 let col = self.utf16_to_utf8_col(line_col.line, line_col.col); 84 let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16);
85 self.newlines[line_col.line as usize] + col 85 self.newlines[line_col.line as usize] + col
86 } 86 }
87 87
@@ -126,23 +126,23 @@ impl LineIndex {
126fn test_line_index() { 126fn test_line_index() {
127 let text = "hello\nworld"; 127 let text = "hello\nworld";
128 let index = LineIndex::new(text); 128 let index = LineIndex::new(text);
129 assert_eq!(index.line_col(0.into()), LineCol { line: 0, col: 0 }); 129 assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 });
130 assert_eq!(index.line_col(1.into()), LineCol { line: 0, col: 1 }); 130 assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 });
131 assert_eq!(index.line_col(5.into()), LineCol { line: 0, col: 5 }); 131 assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 });
132 assert_eq!(index.line_col(6.into()), LineCol { line: 1, col: 0 }); 132 assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 });
133 assert_eq!(index.line_col(7.into()), LineCol { line: 1, col: 1 }); 133 assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 });
134 assert_eq!(index.line_col(8.into()), LineCol { line: 1, col: 2 }); 134 assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 });
135 assert_eq!(index.line_col(10.into()), LineCol { line: 1, col: 4 }); 135 assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 });
136 assert_eq!(index.line_col(11.into()), LineCol { line: 1, col: 5 }); 136 assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 });
137 assert_eq!(index.line_col(12.into()), LineCol { line: 1, col: 6 }); 137 assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 });
138 138
139 let text = "\nhello\nworld"; 139 let text = "\nhello\nworld";
140 let index = LineIndex::new(text); 140 let index = LineIndex::new(text);
141 assert_eq!(index.line_col(0.into()), LineCol { line: 0, col: 0 }); 141 assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 });
142 assert_eq!(index.line_col(1.into()), LineCol { line: 1, col: 0 }); 142 assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 });
143 assert_eq!(index.line_col(2.into()), LineCol { line: 1, col: 1 }); 143 assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 });
144 assert_eq!(index.line_col(6.into()), LineCol { line: 1, col: 5 }); 144 assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 });
145 assert_eq!(index.line_col(7.into()), LineCol { line: 2, col: 0 }); 145 assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 });
146} 146}
147 147
148#[cfg(test)] 148#[cfg(test)]
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index a102b9105..5d5a0c55e 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -51,7 +51,7 @@ impl ConvWith for Position {
51 fn conv_with(self, line_index: &LineIndex) -> TextUnit { 51 fn conv_with(self, line_index: &LineIndex) -> TextUnit {
52 let line_col = LineCol { 52 let line_col = LineCol {
53 line: self.line as u32, 53 line: self.line as u32,
54 col: (self.character as u32).into(), 54 col_utf16: self.character as u32,
55 }; 55 };
56 line_index.offset(line_col) 56 line_index.offset(line_col)
57 } 57 }
@@ -63,7 +63,10 @@ impl ConvWith for TextUnit {
63 63
64 fn conv_with(self, line_index: &LineIndex) -> Position { 64 fn conv_with(self, line_index: &LineIndex) -> Position {
65 let line_col = line_index.line_col(self); 65 let line_col = line_index.line_col(self);
66 Position::new(u64::from(line_col.line), u64::from(u32::from(line_col.col))) 66 Position::new(
67 u64::from(line_col.line),
68 u64::from(u32::from(line_col.col_utf16)),
69 )
67 } 70 }
68} 71}
69 72
@@ -201,8 +204,10 @@ impl TryConvWith for SourceChange {
201 .map(|it| it.edits.as_slice()) 204 .map(|it| it.edits.as_slice())
202 .unwrap_or(&[]); 205 .unwrap_or(&[]);
203 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits); 206 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
204 let position = 207 let position = Position::new(
205 Position::new(u64::from(line_col.line), u64::from(u32::from(line_col.col))); 208 u64::from(line_col.line),
209 u64::from(u32::from(line_col.col_utf16)),
210 );
206 Some(TextDocumentPositionParams { 211 Some(TextDocumentPositionParams {
207 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?), 212 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
208 position, 213 position,
@@ -245,12 +250,12 @@ fn translate_offset_with_edit(
245 if in_edit_line_col.line == 0 { 250 if in_edit_line_col.line == 0 {
246 LineCol { 251 LineCol {
247 line: edit_line_col.line, 252 line: edit_line_col.line,
248 col: edit_line_col.col + in_edit_line_col.col, 253 col_utf16: edit_line_col.col_utf16 + in_edit_line_col.col_utf16,
249 } 254 }
250 } else { 255 } else {
251 LineCol { 256 LineCol {
252 line: edit_line_col.line + in_edit_line_col.line, 257 line: edit_line_col.line + in_edit_line_col.line,
253 col: in_edit_line_col.col, 258 col_utf16: in_edit_line_col.col_utf16,
254 } 259 }
255 } 260 }
256} 261}