mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 20:05:03 +00:00
JoinLines frontend
This commit is contained in:
parent
18918769ba
commit
8ad586a44e
5 changed files with 60 additions and 1 deletions
|
@ -44,6 +44,10 @@
|
||||||
{
|
{
|
||||||
"command": "libsyntax-rust.parentModule",
|
"command": "libsyntax-rust.parentModule",
|
||||||
"title": "Rust Parent Module"
|
"title": "Rust Parent Module"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "libsyntax-rust.joinLines",
|
||||||
|
"title": "Rust Join Lines"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"keybindings": [
|
"keybindings": [
|
||||||
|
@ -61,6 +65,11 @@
|
||||||
"command": "libsyntax-rust.extendSelection",
|
"command": "libsyntax-rust.extendSelection",
|
||||||
"key": "ctrl+w",
|
"key": "ctrl+w",
|
||||||
"when": "editorTextFocus && editorLangId == rust"
|
"when": "editorTextFocus && editorLangId == rust"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "libsyntax-rust.joinLines",
|
||||||
|
"key": "ctrl+shift+j",
|
||||||
|
"when": "editorTextFocus && editorLangId == rust"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"problemMatchers": [
|
"problemMatchers": [
|
||||||
|
|
|
@ -51,6 +51,19 @@ export function activate(context: vscode.ExtensionContext) {
|
||||||
return new vscode.Selection(anchor, active)
|
return new vscode.Selection(anchor, active)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
registerCommand('libsyntax-rust.joinLines', async () => {
|
||||||
|
let editor = vscode.window.activeTextEditor
|
||||||
|
if (editor == null || editor.document.languageId != "rust") return
|
||||||
|
let request: JoinLinesParams = {
|
||||||
|
textDocument: { uri: editor.document.uri.toString() },
|
||||||
|
range: client.code2ProtocolConverter.asRange(editor.selection),
|
||||||
|
}
|
||||||
|
let response = await client.sendRequest<lc.TextEdit[]>("m/joinLines", request)
|
||||||
|
let edits = client.protocol2CodeConverter.asTextEdits(response)
|
||||||
|
let wsEdit = new vscode.WorkspaceEdit()
|
||||||
|
wsEdit.set(editor.document.uri, edits)
|
||||||
|
return vscode.workspace.applyEdit(wsEdit)
|
||||||
|
})
|
||||||
registerCommand('libsyntax-rust.parentModule', async () => {
|
registerCommand('libsyntax-rust.parentModule', async () => {
|
||||||
let editor = vscode.window.activeTextEditor
|
let editor = vscode.window.activeTextEditor
|
||||||
if (editor == null || editor.document.languageId != "rust") return
|
if (editor == null || editor.document.languageId != "rust") return
|
||||||
|
@ -237,6 +250,11 @@ interface FindMatchingBraceParams {
|
||||||
offsets: lc.Position[];
|
offsets: lc.Position[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface JoinLinesParams {
|
||||||
|
textDocument: lc.TextDocumentIdentifier;
|
||||||
|
range: lc.Range;
|
||||||
|
}
|
||||||
|
|
||||||
interface PublishDecorationsParams {
|
interface PublishDecorationsParams {
|
||||||
uri: string,
|
uri: string,
|
||||||
decorations: Decoration[],
|
decorations: Decoration[],
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||||
use languageserver_types::{
|
use languageserver_types::{
|
||||||
Diagnostic, DiagnosticSeverity, Url, DocumentSymbol,
|
Diagnostic, DiagnosticSeverity, Url, DocumentSymbol,
|
||||||
Command, TextDocumentIdentifier, WorkspaceEdit,
|
Command, TextDocumentIdentifier, WorkspaceEdit,
|
||||||
SymbolInformation, Position, Location,
|
SymbolInformation, Position, Location, TextEdit,
|
||||||
};
|
};
|
||||||
use libanalysis::{Query};
|
use libanalysis::{Query};
|
||||||
use libeditor;
|
use libeditor;
|
||||||
|
@ -58,6 +58,18 @@ pub fn handle_find_matching_brace(
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn handle_join_lines(
|
||||||
|
world: ServerWorld,
|
||||||
|
params: req::JoinLinesParams,
|
||||||
|
) -> Result<Vec<TextEdit>> {
|
||||||
|
let file_id = params.text_document.try_conv_with(&world)?;
|
||||||
|
let file = world.analysis().file_syntax(file_id)?;
|
||||||
|
let line_index = world.analysis().file_line_index(file_id)?;
|
||||||
|
let range = params.range.conv_with(&line_index);
|
||||||
|
let res = libeditor::join_lines(&file, range);
|
||||||
|
Ok(res.edit.conv_with(&line_index))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn handle_document_symbol(
|
pub fn handle_document_symbol(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
params: req::DocumentSymbolParams,
|
params: req::DocumentSymbolParams,
|
||||||
|
|
|
@ -27,6 +27,7 @@ use {
|
||||||
handle_goto_definition,
|
handle_goto_definition,
|
||||||
handle_find_matching_brace,
|
handle_find_matching_brace,
|
||||||
handle_parent_module,
|
handle_parent_module,
|
||||||
|
handle_join_lines,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -145,6 +146,9 @@ fn on_request(
|
||||||
handle_request_on_threadpool::<req::ParentModule>(
|
handle_request_on_threadpool::<req::ParentModule>(
|
||||||
&mut req, pool, world, sender, handle_parent_module,
|
&mut req, pool, world, sender, handle_parent_module,
|
||||||
)?;
|
)?;
|
||||||
|
handle_request_on_threadpool::<req::JoinLines>(
|
||||||
|
&mut req, pool, world, sender, handle_join_lines,
|
||||||
|
)?;
|
||||||
dispatch::handle_request::<req::ExecuteCommand, _>(&mut req, |params, resp| {
|
dispatch::handle_request::<req::ExecuteCommand, _>(&mut req, |params, resp| {
|
||||||
io.send(RawMsg::Response(resp.into_response(Ok(None))?));
|
io.send(RawMsg::Response(resp.into_response(Ok(None))?));
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ pub use languageserver_types::{
|
||||||
ExecuteCommandParams,
|
ExecuteCommandParams,
|
||||||
WorkspaceSymbolParams,
|
WorkspaceSymbolParams,
|
||||||
TextDocumentPositionParams,
|
TextDocumentPositionParams,
|
||||||
|
TextEdit,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,3 +118,18 @@ impl Request for ParentModule {
|
||||||
type Result = Vec<Location>;
|
type Result = Vec<Location>;
|
||||||
const METHOD: &'static str = "m/parentModule";
|
const METHOD: &'static str = "m/parentModule";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum JoinLines {}
|
||||||
|
|
||||||
|
impl Request for JoinLines {
|
||||||
|
type Params = JoinLinesParams;
|
||||||
|
type Result = Vec<TextEdit>;
|
||||||
|
const METHOD: &'static str = "m/joinLines";
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct JoinLinesParams {
|
||||||
|
pub text_document: TextDocumentIdentifier,
|
||||||
|
pub range: Range,
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue