From 6b2da4e5474ec064e44b7cf19523de1bab72ba27 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sun, 23 Dec 2018 15:49:14 +0100 Subject: [PATCH] use new translate_offset_with_edit for TryConvWith doc comments --- crates/ra_editor/src/lib.rs | 1 + crates/ra_editor/src/line_index_utils.rs | 2 +- crates/ra_lsp_server/src/conv.rs | 37 +----------------------- crates/ra_text_edit/src/lib.rs | 2 ++ 4 files changed, 5 insertions(+), 37 deletions(-) diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 7145c6cfbe..2e3635ea0e 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -14,6 +14,7 @@ pub use self::{ extend_selection::extend_selection, folding_ranges::{folding_ranges, Fold, FoldKind}, line_index::{LineCol, LineIndex}, + line_index_utils::translate_offset_with_edit, symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, typing::{join_lines, on_enter, on_eq_typed}, }; diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index a0bb9a6ddc..ba3ac8aebd 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,6 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol, line_index::Utf16Char, line_index}; +use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; use superslice::Ext; #[derive(Debug, Clone)] diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 051f1f995b..63827aeea0 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -3,7 +3,7 @@ use languageserver_types::{ TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, }; use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; -use ra_editor::{LineCol, LineIndex}; +use ra_editor::{LineCol, LineIndex, translate_offset_with_edit}; use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; @@ -261,41 +261,6 @@ impl TryConvWith for SourceChange { } } -// HACK: we should translate offset to line/column using linde_index *with edits applied*. -// A naive version of this function would be to apply `edits` to the original text, -// construct a new line index and use that, but it would be slow. -// -// Writing fast & correct version is issue #105, let's use a quick hack in the meantime -fn translate_offset_with_edit( - pre_edit_index: &LineIndex, - offset: TextUnit, - edits: &[AtomTextEdit], -) -> LineCol { - let fallback = pre_edit_index.line_col(offset); - let edit = match edits.first() { - None => return fallback, - Some(edit) => edit, - }; - let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert); - if !(edit.delete.start() <= offset && offset <= end_offset) { - return fallback; - } - let rel_offset = offset - edit.delete.start(); - let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset); - let edit_line_col = pre_edit_index.line_col(edit.delete.start()); - if in_edit_line_col.line == 0 { - LineCol { - line: edit_line_col.line, - col_utf16: edit_line_col.col_utf16 + in_edit_line_col.col_utf16, - } - } else { - LineCol { - line: edit_line_col.line + in_edit_line_col.line, - col_utf16: in_edit_line_col.col_utf16, - } - } -} - impl TryConvWith for SourceFileEdit { type Ctx = ServerWorld; type Output = TextDocumentEdit; diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index 89600413ad..8acf104484 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}; use text_unit::{TextRange, TextUnit}; +/// Must not overlap with other `AtomTextEdit`s #[derive(Debug, Clone)] pub struct AtomTextEdit { + /// Refers to offsets in the original text pub delete: TextRange, pub insert: String, }