aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--code/.vscode/launch.json4
-rw-r--r--code/src/extension.ts10
-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
5 files changed, 52 insertions, 25 deletions
diff --git a/code/.vscode/launch.json b/code/.vscode/launch.json
index 64f2c3693..5e615ad4c 100644
--- a/code/.vscode/launch.json
+++ b/code/.vscode/launch.json
@@ -9,10 +9,10 @@
9 "type": "extensionHost", 9 "type": "extensionHost",
10 "request": "launch", 10 "request": "launch",
11 "runtimeExecutable": "${execPath}", 11 "runtimeExecutable": "${execPath}",
12 "args": ["--extensionDevelopmentPath=${workspaceRoot}"], 12 "args": ["--extensionDevelopmentPath='./'"],
13 "stopOnEntry": false, 13 "stopOnEntry": false,
14 "sourceMaps": true, 14 "sourceMaps": true,
15 "outFiles": [ "${workspaceRoot}/out/src/**/*.js" ], 15 "outFiles": [ "./out/src/**/*.js" ],
16 "preLaunchTask": "npm" 16 "preLaunchTask": "npm"
17 }, 17 },
18 ] 18 ]
diff --git a/code/src/extension.ts b/code/src/extension.ts
index afcbccf63..95305db2d 100644
--- a/code/src/extension.ts
+++ b/code/src/extension.ts
@@ -111,6 +111,16 @@ function startServer() {
111 ) 111 )
112 } 112 }
113 ) 113 )
114 client.onRequest(
115 new lc.RequestType<lc.Position, void, any, any>("m/moveCursor"),
116 (params: lc.Position, token: lc.CancellationToken) => {
117 let editor = vscode.window.activeTextEditor;
118 if (editor == null) return
119 if (!editor.selection.isEmpty) return
120 let position = client.protocol2CodeConverter.asPosition(params)
121 editor.selection = new vscode.Selection(position, position);
122 }
123 )
114 }) 124 })
115 client.start(); 125 client.start();
116} 126}
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}