vscode moves cursor

This commit is contained in:
Aleksey Kladov 2018-08-16 13:46:31 +03:00
parent eda52cbc34
commit e0a43a159d
5 changed files with 52 additions and 25 deletions

View file

@ -9,10 +9,10 @@
"type": "extensionHost", "type": "extensionHost",
"request": "launch", "request": "launch",
"runtimeExecutable": "${execPath}", "runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}"], "args": ["--extensionDevelopmentPath='./'"],
"stopOnEntry": false, "stopOnEntry": false,
"sourceMaps": true, "sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ], "outFiles": [ "./out/src/**/*.js" ],
"preLaunchTask": "npm" "preLaunchTask": "npm"
}, },
] ]

View file

@ -111,6 +111,16 @@ function startServer() {
) )
} }
) )
client.onRequest(
new lc.RequestType<lc.Position, void, any, any>("m/moveCursor"),
(params: lc.Position, token: lc.CancellationToken) => {
let editor = vscode.window.activeTextEditor;
if (editor == null) return
if (!editor.selection.isEmpty) return
let position = client.protocol2CodeConverter.asPosition(params)
editor.selection = new vscode.Selection(position, position);
}
)
}) })
client.start(); client.start();
} }

View file

@ -6,7 +6,7 @@ use languageserver_types::{
SymbolInformation, Position, SymbolInformation, Position,
}; };
use libanalysis::{World, Query}; use libanalysis::{World, Query};
use libeditor; use libeditor::{self, CursorPosition};
use libsyntax2::TextUnit; use libsyntax2::TextUnit;
use serde_json::{to_value, from_value}; use serde_json::{to_value, from_value};
@ -195,7 +195,7 @@ pub fn handle_execute_command(
world: World, world: World,
path_map: PathMap, path_map: PathMap,
mut params: req::ExecuteCommandParams, mut params: req::ExecuteCommandParams,
) -> Result<req::ApplyWorkspaceEditParams> { ) -> Result<(req::ApplyWorkspaceEditParams, Option<Position>)> {
if params.command.as_str() != "apply_code_action" { if params.command.as_str() != "apply_code_action" {
bail!("unknown cmd: {:?}", params.command); bail!("unknown cmd: {:?}", params.command);
} }
@ -209,23 +209,24 @@ pub fn handle_execute_command(
let action_result = match arg.id { let action_result = match arg.id {
ActionId::FlipComma => libeditor::flip_comma(&file, arg.offset).map(|f| f()), ActionId::FlipComma => libeditor::flip_comma(&file, arg.offset).map(|f| f()),
ActionId::AddDerive => libeditor::add_derive(&file, arg.offset).map(|f| f()), ActionId::AddDerive => libeditor::add_derive(&file, arg.offset).map(|f| f()),
}; }.ok_or_else(|| format_err!("command not applicable"))?;
let edit = match action_result {
Some(action_result) => action_result.edit,
None => bail!("command not applicable"),
};
let line_index = world.file_line_index(file_id)?; let line_index = world.file_line_index(file_id)?;
let mut changes = HashMap::new(); let mut changes = HashMap::new();
changes.insert( changes.insert(
arg.text_document.uri, arg.text_document.uri,
edit.conv_with(&line_index), action_result.edit.conv_with(&line_index),
); );
let edit = WorkspaceEdit { let edit = WorkspaceEdit {
changes: Some(changes), changes: Some(changes),
document_changes: None, document_changes: None,
}; };
let edit = req::ApplyWorkspaceEditParams { edit };
let cursor_pos = match action_result.cursor_position {
CursorPosition::Same => None,
CursorPosition::Offset(offset) => Some(offset.conv_with(&line_index)),
};
Ok(req::ApplyWorkspaceEditParams { edit }) Ok((edit, cursor_pos))
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]

View file

@ -171,21 +171,29 @@ fn on_request(
let path_map = path_map.clone(); let path_map = path_map.clone();
let sender = sender.clone(); let sender = sender.clone();
pool.execute(move || { pool.execute(move || {
let task = match handle_execute_command(world, path_map, params) { let (edit, cursor) = match handle_execute_command(world, path_map, params) {
Ok(req) => match to_value(req) { Ok(res) => res,
Err(e) => Task::Die(e.into()), Err(e) => return sender.send(Task::Die(e)),
Ok(params) => {
let request = RawRequest {
id: 0,
method: <req::ApplyWorkspaceEdit as req::ClientRequest>::METHOD.to_string(),
params,
};
Task::Request(request)
}
},
Err(e) => Task::Die(e),
}; };
sender.send(task) match to_value(edit) {
Err(e) => return sender.send(Task::Die(e.into())),
Ok(params) => {
let request = RawRequest {
id: 0,
method: <req::ApplyWorkspaceEdit as req::ClientRequest>::METHOD.to_string(),
params,
};
sender.send(Task::Request(request))
}
}
if let Some(cursor) = cursor {
let request = RawRequest {
id: 0,
method: <req::MoveCursor as req::ClientRequest>::METHOD.to_string(),
params: to_value(cursor).unwrap(),
};
sender.send(Task::Request(request))
}
}); });
Ok(()) Ok(())
})?; })?;

View file

@ -101,3 +101,11 @@ pub struct Decoration {
pub range: Range, pub range: Range,
pub tag: &'static str pub tag: &'static str
} }
pub enum MoveCursor {}
impl Request for MoveCursor {
type Params = Position;
type Result = ();
const METHOD: &'static str = "m/moveCursor";
}