diff options
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r-- | crates/ra_lsp_server/src/conv.rs | 37 |
1 files changed, 1 insertions, 36 deletions
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 | }; |
5 | use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; | 5 | use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; |
6 | use ra_editor::{LineCol, LineIndex}; | 6 | use ra_editor::{LineCol, LineIndex, translate_offset_with_edit}; |
7 | use ra_text_edit::{AtomTextEdit, TextEdit}; | 7 | use ra_text_edit::{AtomTextEdit, TextEdit}; |
8 | use ra_syntax::{SyntaxKind, TextRange, TextUnit}; | 8 | use 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 | ||
269 | fn 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 | |||
299 | impl TryConvWith for SourceFileEdit { | 264 | impl TryConvWith for SourceFileEdit { |
300 | type Ctx = ServerWorld; | 265 | type Ctx = ServerWorld; |
301 | type Output = TextDocumentEdit; | 266 | type Output = TextDocumentEdit; |