diff options
author | Aleksey Kladov <[email protected]> | 2019-01-11 12:05:40 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-11 12:05:40 +0000 |
commit | d9e70e3160261fbb5c9bcd48a9feed22406c63cc (patch) | |
tree | 5ae9cff7196ac9bda48db6c312d361f24740caf3 /crates/ra_lsp_server/src/main_loop/handlers.rs | |
parent | 4aa632761f1db5f18338c9bd568156c0259b9252 (diff) |
fix on-type offset
Diffstat (limited to 'crates/ra_lsp_server/src/main_loop/handlers.rs')
-rw-r--r-- | crates/ra_lsp_server/src/main_loop/handlers.rs | 47 |
1 files changed, 21 insertions, 26 deletions
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::{ | |||
9 | SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, | 9 | SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, |
10 | }; | 10 | }; |
11 | use ra_ide_api::{ | 11 | use ra_ide_api::{ |
12 | FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange, | 12 | FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, |
13 | }; | 13 | }; |
14 | use ra_syntax::{TextUnit, AstNode}; | 14 | use ra_syntax::{TextUnit, AstNode}; |
15 | use rustc_hash::FxHashMap; | 15 | use rustc_hash::FxHashMap; |
@@ -92,35 +92,30 @@ pub fn handle_on_type_formatting( | |||
92 | world: ServerWorld, | 92 | world: ServerWorld, |
93 | params: req::DocumentOnTypeFormattingParams, | 93 | params: req::DocumentOnTypeFormattingParams, |
94 | ) -> Result<Option<Vec<TextEdit>>> { | 94 | ) -> Result<Option<Vec<TextEdit>>> { |
95 | let analysis: Option<Box<Fn(FilePosition) -> Option<SourceChange>>> = match params.ch.as_str() { | 95 | let file_id = params.text_document.try_conv_with(&world)?; |
96 | "=" => Some(Box::new(|pos| world.analysis().on_eq_typed(pos))), | 96 | let line_index = world.analysis().file_line_index(file_id); |
97 | "." => Some(Box::new(|pos| world.analysis().on_dot_typed(pos))), | 97 | let position = FilePosition { |
98 | _ => None, | 98 | file_id, |
99 | /// in `ra_ide_api`, the `on_type` invariant is that | ||
100 | /// `text.char_at(position) == typed_char`. | ||
101 | offset: params.position.conv_with(&line_index) - TextUnit::of_char('.'), | ||
99 | }; | 102 | }; |
100 | 103 | ||
101 | if let Some(ana) = analysis { | 104 | let edit = match params.ch.as_str() { |
102 | let file_id = params.text_document.try_conv_with(&world)?; | 105 | "=" => world.analysis().on_eq_typed(position), |
103 | let line_index = world.analysis().file_line_index(file_id); | 106 | "." => world.analysis().on_dot_typed(position), |
104 | let position = FilePosition { | 107 | _ => return Ok(None), |
105 | file_id, | 108 | }; |
106 | offset: params.position.conv_with(&line_index), | 109 | let mut edit = match edit { |
107 | }; | 110 | Some(it) => it, |
111 | None => return Ok(None), | ||
112 | }; | ||
108 | 113 | ||
109 | if let Some(mut action) = ana(position) { | 114 | // This should be a single-file edit |
110 | let change: Vec<TextEdit> = action | 115 | let edit = edit.source_file_edits.pop().unwrap(); |
111 | .source_file_edits | ||
112 | .pop() | ||
113 | .unwrap() | ||
114 | .edit | ||
115 | .as_atoms() | ||
116 | .iter() | ||
117 | .map_conv_with(&line_index) | ||
118 | .collect(); | ||
119 | return Ok(Some(change)); | ||
120 | } | ||
121 | } | ||
122 | 116 | ||
123 | return Ok(None); | 117 | let change: Vec<TextEdit> = edit.edit.conv_with(&line_index); |
118 | return Ok(Some(change)); | ||
124 | } | 119 | } |
125 | 120 | ||
126 | pub fn handle_document_symbol( | 121 | pub fn handle_document_symbol( |