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",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
"args": ["--extensionDevelopmentPath='./'"],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
"outFiles": [ "./out/src/**/*.js" ],
"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();
}

View file

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

View file

@ -171,21 +171,29 @@ fn on_request(
let path_map = path_map.clone();
let sender = sender.clone();
pool.execute(move || {
let task = match handle_execute_command(world, path_map, params) {
Ok(req) => match to_value(req) {
Err(e) => Task::Die(e.into()),
let (edit, cursor) = match handle_execute_command(world, path_map, params) {
Ok(res) => res,
Err(e) => return sender.send(Task::Die(e)),
};
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,
};
Task::Request(request)
sender.send(Task::Request(request))
}
},
Err(e) => Task::Die(e),
}
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)
sender.send(Task::Request(request))
}
});
Ok(())
})?;

View file

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