From d9e70e3160261fbb5c9bcd48a9feed22406c63cc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 15:05:40 +0300 Subject: fix on-type offset --- crates/ra_lsp_server/src/main_loop/handlers.rs | 47 ++++++++++++-------------- 1 file changed, 21 insertions(+), 26 deletions(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 0dda9548a..5f4b27149 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -9,7 +9,7 @@ use languageserver_types::{ SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, }; use ra_ide_api::{ - FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange, + FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, }; use ra_syntax::{TextUnit, AstNode}; use rustc_hash::FxHashMap; @@ -92,35 +92,30 @@ pub fn handle_on_type_formatting( world: ServerWorld, params: req::DocumentOnTypeFormattingParams, ) -> Result>> { - let analysis: Option Option>> = match params.ch.as_str() { - "=" => Some(Box::new(|pos| world.analysis().on_eq_typed(pos))), - "." => Some(Box::new(|pos| world.analysis().on_dot_typed(pos))), - _ => None, + let file_id = params.text_document.try_conv_with(&world)?; + let line_index = world.analysis().file_line_index(file_id); + let position = FilePosition { + file_id, + /// in `ra_ide_api`, the `on_type` invariant is that + /// `text.char_at(position) == typed_char`. + offset: params.position.conv_with(&line_index) - TextUnit::of_char('.'), }; - if let Some(ana) = analysis { - let file_id = params.text_document.try_conv_with(&world)?; - let line_index = world.analysis().file_line_index(file_id); - let position = FilePosition { - file_id, - offset: params.position.conv_with(&line_index), - }; + let edit = match params.ch.as_str() { + "=" => world.analysis().on_eq_typed(position), + "." => world.analysis().on_dot_typed(position), + _ => return Ok(None), + }; + let mut edit = match edit { + Some(it) => it, + None => return Ok(None), + }; - if let Some(mut action) = ana(position) { - let change: Vec = action - .source_file_edits - .pop() - .unwrap() - .edit - .as_atoms() - .iter() - .map_conv_with(&line_index) - .collect(); - return Ok(Some(change)); - } - } + // This should be a single-file edit + let edit = edit.source_file_edits.pop().unwrap(); - return Ok(None); + let change: Vec = edit.edit.conv_with(&line_index); + return Ok(Some(change)); } pub fn handle_document_symbol( -- cgit v1.2.3