aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r--crates/ra_lsp_server/src/conv.rs41
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs14
-rw-r--r--crates/ra_lsp_server/src/main_loop/mod.rs1
-rw-r--r--crates/ra_lsp_server/src/req.rs8
4 files changed, 63 insertions, 1 deletions
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index 759e5e914..08a656569 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -190,9 +190,13 @@ impl TryConvWith for SourceChange {
190 None => None, 190 None => None,
191 Some(pos) => { 191 Some(pos) => {
192 let line_index = world.analysis().file_line_index(pos.file_id); 192 let line_index = world.analysis().file_line_index(pos.file_id);
193 let edits = self.source_file_edits.iter().find(|it| it.file_id == pos.file_id)
194 .map(|it| it.edits.as_slice()).unwrap_or(&[]);
195 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits);
196 let position = Position::new(line_col.line as u64, u32::from(line_col.col) as u64);
193 Some(TextDocumentPositionParams { 197 Some(TextDocumentPositionParams {
194 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?), 198 text_document: TextDocumentIdentifier::new(pos.file_id.try_conv_with(world)?),
195 position: pos.offset.conv_with(&line_index), 199 position,
196 }) 200 })
197 } 201 }
198 }; 202 };
@@ -207,6 +211,41 @@ impl TryConvWith for SourceChange {
207 } 211 }
208} 212}
209 213
214// HACK: we should translate offset to line/column using linde_index *with edits applied*.
215// A naive version of this function would be to apply `edits` to the original text,
216// construct a new line index and use that, but it would be slow.
217//
218// Writing fast & correct version is issue #105, let's use a quick hack in the meantime
219fn translate_offset_with_edit(
220 pre_edit_index: &LineIndex,
221 offset: TextUnit,
222 edits: &[AtomEdit],
223) -> LineCol {
224 let fallback = pre_edit_index.line_col(offset);
225 let edit = match edits.first() {
226 None => return fallback,
227 Some(edit) => edit
228 };
229 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
230 if !(edit.delete.start() <= offset && offset <= end_offset) {
231 return fallback
232 }
233 let rel_offset = offset - edit.delete.start();
234 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
235 let edit_line_col = pre_edit_index.line_col(edit.delete.start());
236 if in_edit_line_col.line == 0 {
237 LineCol {
238 line: edit_line_col.line,
239 col: edit_line_col.col + in_edit_line_col.col,
240 }
241 } else {
242 LineCol {
243 line: edit_line_col.line + in_edit_line_col.line,
244 col: in_edit_line_col.col,
245 }
246 }
247}
248
210impl TryConvWith for SourceFileEdit { 249impl TryConvWith for SourceFileEdit {
211 type Ctx = ServerWorld; 250 type Ctx = ServerWorld;
212 type Output = TextDocumentEdit; 251 type Output = TextDocumentEdit;
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 79a54183e..725036cc7 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -77,6 +77,20 @@ pub fn handle_join_lines(
77 .try_conv_with(&world) 77 .try_conv_with(&world)
78} 78}
79 79
80pub fn handle_on_enter(
81 world: ServerWorld,
82 params: req::TextDocumentPositionParams,
83 _token: JobToken,
84) -> Result<Option<req::SourceChange>> {
85 let file_id = params.text_document.try_conv_with(&world)?;
86 let line_index = world.analysis().file_line_index(file_id);
87 let offset = params.position.conv_with(&line_index);
88 match world.analysis().on_enter(file_id, offset) {
89 None => Ok(None),
90 Some(edit) => Ok(Some(edit.try_conv_with(&world)?))
91 }
92}
93
80pub fn handle_on_type_formatting( 94pub fn handle_on_type_formatting(
81 world: ServerWorld, 95 world: ServerWorld,
82 params: req::DocumentOnTypeFormattingParams, 96 params: req::DocumentOnTypeFormattingParams,
diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs
index 47a9b202e..53c6f1dff 100644
--- a/crates/ra_lsp_server/src/main_loop/mod.rs
+++ b/crates/ra_lsp_server/src/main_loop/mod.rs
@@ -244,6 +244,7 @@ fn on_request(
244 .on::<req::ExtendSelection>(handlers::handle_extend_selection)? 244 .on::<req::ExtendSelection>(handlers::handle_extend_selection)?
245 .on::<req::FindMatchingBrace>(handlers::handle_find_matching_brace)? 245 .on::<req::FindMatchingBrace>(handlers::handle_find_matching_brace)?
246 .on::<req::JoinLines>(handlers::handle_join_lines)? 246 .on::<req::JoinLines>(handlers::handle_join_lines)?
247 .on::<req::OnEnter>(handlers::handle_on_enter)?
247 .on::<req::OnTypeFormatting>(handlers::handle_on_type_formatting)? 248 .on::<req::OnTypeFormatting>(handlers::handle_on_type_formatting)?
248 .on::<req::DocumentSymbolRequest>(handlers::handle_document_symbol)? 249 .on::<req::DocumentSymbolRequest>(handlers::handle_document_symbol)?
249 .on::<req::WorkspaceSymbol>(handlers::handle_workspace_symbol)? 250 .on::<req::WorkspaceSymbol>(handlers::handle_workspace_symbol)?
diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs
index 4af61dbbd..458c79ea9 100644
--- a/crates/ra_lsp_server/src/req.rs
+++ b/crates/ra_lsp_server/src/req.rs
@@ -119,6 +119,14 @@ pub struct JoinLinesParams {
119 pub range: Range, 119 pub range: Range,
120} 120}
121 121
122pub enum OnEnter {}
123
124impl Request for OnEnter {
125 type Params = TextDocumentPositionParams;
126 type Result = Option<SourceChange>;
127 const METHOD: &'static str = "m/onEnter";
128}
129
122pub enum Runnables {} 130pub enum Runnables {}
123 131
124impl Request for Runnables { 132impl Request for Runnables {