aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/server/src')
-rw-r--r--crates/server/src/main_loop/handlers.rs19
-rw-r--r--crates/server/src/main_loop/mod.rs36
-rw-r--r--crates/server/src/req.rs8
3 files changed, 40 insertions, 23 deletions
diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs
index d7b78b4fa..f25c64c97 100644
--- a/crates/server/src/main_loop/handlers.rs
+++ b/crates/server/src/main_loop/handlers.rs
@@ -6,7 +6,7 @@ use languageserver_types::{
6 SymbolInformation, Position, 6 SymbolInformation, Position,
7}; 7};
8use libanalysis::{World, Query}; 8use libanalysis::{World, Query};
9use libeditor; 9use libeditor::{self, CursorPosition};
10use libsyntax2::TextUnit; 10use libsyntax2::TextUnit;
11use serde_json::{to_value, from_value}; 11use serde_json::{to_value, from_value};
12 12
@@ -195,7 +195,7 @@ pub fn handle_execute_command(
195 world: World, 195 world: World,
196 path_map: PathMap, 196 path_map: PathMap,
197 mut params: req::ExecuteCommandParams, 197 mut params: req::ExecuteCommandParams,
198) -> Result<req::ApplyWorkspaceEditParams> { 198) -> Result<(req::ApplyWorkspaceEditParams, Option<Position>)> {
199 if params.command.as_str() != "apply_code_action" { 199 if params.command.as_str() != "apply_code_action" {
200 bail!("unknown cmd: {:?}", params.command); 200 bail!("unknown cmd: {:?}", params.command);
201 } 201 }
@@ -209,23 +209,24 @@ pub fn handle_execute_command(
209 let action_result = match arg.id { 209 let action_result = match arg.id {
210 ActionId::FlipComma => libeditor::flip_comma(&file, arg.offset).map(|f| f()), 210 ActionId::FlipComma => libeditor::flip_comma(&file, arg.offset).map(|f| f()),
211 ActionId::AddDerive => libeditor::add_derive(&file, arg.offset).map(|f| f()), 211 ActionId::AddDerive => libeditor::add_derive(&file, arg.offset).map(|f| f()),
212 }; 212 }.ok_or_else(|| format_err!("command not applicable"))?;
213 let edit = match action_result {
214 Some(action_result) => action_result.edit,
215 None => bail!("command not applicable"),
216 };
217 let line_index = world.file_line_index(file_id)?; 213 let line_index = world.file_line_index(file_id)?;
218 let mut changes = HashMap::new(); 214 let mut changes = HashMap::new();
219 changes.insert( 215 changes.insert(
220 arg.text_document.uri, 216 arg.text_document.uri,
221 edit.conv_with(&line_index), 217 action_result.edit.conv_with(&line_index),
222 ); 218 );
223 let edit = WorkspaceEdit { 219 let edit = WorkspaceEdit {
224 changes: Some(changes), 220 changes: Some(changes),
225 document_changes: None, 221 document_changes: None,
226 }; 222 };
223 let edit = req::ApplyWorkspaceEditParams { edit };
224 let cursor_pos = match action_result.cursor_position {
225 CursorPosition::Same => None,
226 CursorPosition::Offset(offset) => Some(offset.conv_with(&line_index)),
227 };
227 228
228 Ok(req::ApplyWorkspaceEditParams { edit }) 229 Ok((edit, cursor_pos))
229} 230}
230 231
231#[derive(Serialize, Deserialize)] 232#[derive(Serialize, Deserialize)]
diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs
index 4d5dfb437..1fbcc7d1f 100644
--- a/crates/server/src/main_loop/mod.rs
+++ b/crates/server/src/main_loop/mod.rs
@@ -171,21 +171,29 @@ fn on_request(
171 let path_map = path_map.clone(); 171 let path_map = path_map.clone();
172 let sender = sender.clone(); 172 let sender = sender.clone();
173 pool.execute(move || { 173 pool.execute(move || {
174 let task = match handle_execute_command(world, path_map, params) { 174 let (edit, cursor) = match handle_execute_command(world, path_map, params) {
175 Ok(req) => match to_value(req) { 175 Ok(res) => res,
176 Err(e) => Task::Die(e.into()), 176 Err(e) => return sender.send(Task::Die(e)),
177 Ok(params) => {
178 let request = RawRequest {
179 id: 0,
180 method: <req::ApplyWorkspaceEdit as req::ClientRequest>::METHOD.to_string(),
181 params,
182 };
183 Task::Request(request)
184 }
185 },
186 Err(e) => Task::Die(e),
187 }; 177 };
188 sender.send(task) 178 match to_value(edit) {
179 Err(e) => return sender.send(Task::Die(e.into())),
180 Ok(params) => {
181 let request = RawRequest {
182 id: 0,
183 method: <req::ApplyWorkspaceEdit as req::ClientRequest>::METHOD.to_string(),
184 params,
185 };
186 sender.send(Task::Request(request))
187 }
188 }
189 if let Some(cursor) = cursor {
190 let request = RawRequest {
191 id: 0,
192 method: <req::MoveCursor as req::ClientRequest>::METHOD.to_string(),
193 params: to_value(cursor).unwrap(),
194 };
195 sender.send(Task::Request(request))
196 }
189 }); 197 });
190 Ok(()) 198 Ok(())
191 })?; 199 })?;
diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs
index c3efc7489..73e82150c 100644
--- a/crates/server/src/req.rs
+++ b/crates/server/src/req.rs
@@ -101,3 +101,11 @@ pub struct Decoration {
101 pub range: Range, 101 pub range: Range,
102 pub tag: &'static str 102 pub tag: &'static str
103} 103}
104
105pub enum MoveCursor {}
106
107impl Request for MoveCursor {
108 type Params = Position;
109 type Result = ();
110 const METHOD: &'static str = "m/moveCursor";
111}