aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_editor/src/lib.rs1
-rw-r--r--crates/ra_editor/src/line_index_utils.rs2
-rw-r--r--crates/ra_lsp_server/src/conv.rs37
-rw-r--r--crates/ra_text_edit/src/lib.rs2
4 files changed, 5 insertions, 37 deletions
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs
index 7145c6cfb..2e3635ea0 100644
--- a/crates/ra_editor/src/lib.rs
+++ b/crates/ra_editor/src/lib.rs
@@ -14,6 +14,7 @@ pub use self::{
14 extend_selection::extend_selection, 14 extend_selection::extend_selection,
15 folding_ranges::{folding_ranges, Fold, FoldKind}, 15 folding_ranges::{folding_ranges, Fold, FoldKind},
16 line_index::{LineCol, LineIndex}, 16 line_index::{LineCol, LineIndex},
17 line_index_utils::translate_offset_with_edit,
17 symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, 18 symbols::{file_structure, file_symbols, FileSymbol, StructureNode},
18 typing::{join_lines, on_enter, on_eq_typed}, 19 typing::{join_lines, on_enter, on_eq_typed},
19}; 20};
diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs
index a0bb9a6dd..ba3ac8aeb 100644
--- a/crates/ra_editor/src/line_index_utils.rs
+++ b/crates/ra_editor/src/line_index_utils.rs
@@ -1,6 +1,6 @@
1use ra_text_edit::AtomTextEdit; 1use ra_text_edit::AtomTextEdit;
2use ra_syntax::{TextUnit, TextRange}; 2use ra_syntax::{TextUnit, TextRange};
3use crate::{LineIndex, LineCol, line_index::Utf16Char, line_index}; 3use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}};
4use superslice::Ext; 4use superslice::Ext;
5 5
6#[derive(Debug, Clone)] 6#[derive(Debug, Clone)]
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index 051f1f995..63827aeea 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -3,7 +3,7 @@ use languageserver_types::{
3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, 3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat,
4}; 4};
5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; 5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText};
6use ra_editor::{LineCol, LineIndex}; 6use ra_editor::{LineCol, LineIndex, translate_offset_with_edit};
7use ra_text_edit::{AtomTextEdit, TextEdit}; 7use ra_text_edit::{AtomTextEdit, TextEdit};
8use ra_syntax::{SyntaxKind, TextRange, TextUnit}; 8use ra_syntax::{SyntaxKind, TextRange, TextUnit};
9 9
@@ -261,41 +261,6 @@ impl TryConvWith for SourceChange {
261 } 261 }
262} 262}
263 263
264// HACK: we should translate offset to line/column using linde_index *with edits applied*.
265// A naive version of this function would be to apply `edits` to the original text,
266// construct a new line index and use that, but it would be slow.
267//
268// Writing fast & correct version is issue #105, let's use a quick hack in the meantime
269fn translate_offset_with_edit(
270 pre_edit_index: &LineIndex,
271 offset: TextUnit,
272 edits: &[AtomTextEdit],
273) -> LineCol {
274 let fallback = pre_edit_index.line_col(offset);
275 let edit = match edits.first() {
276 None => return fallback,
277 Some(edit) => edit,
278 };
279 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
280 if !(edit.delete.start() <= offset && offset <= end_offset) {
281 return fallback;
282 }
283 let rel_offset = offset - edit.delete.start();
284 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
285 let edit_line_col = pre_edit_index.line_col(edit.delete.start());
286 if in_edit_line_col.line == 0 {
287 LineCol {
288 line: edit_line_col.line,
289 col_utf16: edit_line_col.col_utf16 + in_edit_line_col.col_utf16,
290 }
291 } else {
292 LineCol {
293 line: edit_line_col.line + in_edit_line_col.line,
294 col_utf16: in_edit_line_col.col_utf16,
295 }
296 }
297}
298
299impl TryConvWith for SourceFileEdit { 264impl TryConvWith for SourceFileEdit {
300 type Ctx = ServerWorld; 265 type Ctx = ServerWorld;
301 type Output = TextDocumentEdit; 266 type Output = TextDocumentEdit;
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index 89600413a..8acf10448 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -6,8 +6,10 @@ pub use crate::text_edit::{TextEdit, TextEditBuilder};
6 6
7use text_unit::{TextRange, TextUnit}; 7use text_unit::{TextRange, TextUnit};
8 8
9/// Must not overlap with other `AtomTextEdit`s
9#[derive(Debug, Clone)] 10#[derive(Debug, Clone)]
10pub struct AtomTextEdit { 11pub struct AtomTextEdit {
12 /// Refers to offsets in the original text
11 pub delete: TextRange, 13 pub delete: TextRange,
12 pub insert: String, 14 pub insert: String,
13} 15}