From 8ad586a44e2214a11c4e7d27e0d3c2d73e43f39f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 23 Aug 2018 22:14:51 +0300 Subject: JoinLines frontend --- code/package.json | 9 +++++++++ code/src/extension.ts | 18 ++++++++++++++++++ crates/server/src/main_loop/handlers.rs | 14 +++++++++++++- crates/server/src/main_loop/mod.rs | 4 ++++ crates/server/src/req.rs | 16 ++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/code/package.json b/code/package.json index 35367edb6..20a6ceee7 100644 --- a/code/package.json +++ b/code/package.json @@ -44,6 +44,10 @@ { "command": "libsyntax-rust.parentModule", "title": "Rust Parent Module" + }, + { + "command": "libsyntax-rust.joinLines", + "title": "Rust Join Lines" } ], "keybindings": [ @@ -61,6 +65,11 @@ "command": "libsyntax-rust.extendSelection", "key": "ctrl+w", "when": "editorTextFocus && editorLangId == rust" + }, + { + "command": "libsyntax-rust.joinLines", + "key": "ctrl+shift+j", + "when": "editorTextFocus && editorLangId == rust" } ], "problemMatchers": [ diff --git a/code/src/extension.ts b/code/src/extension.ts index fb6841fa0..134459f30 100644 --- a/code/src/extension.ts +++ b/code/src/extension.ts @@ -51,6 +51,19 @@ export function activate(context: vscode.ExtensionContext) { 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("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 () => { let editor = vscode.window.activeTextEditor if (editor == null || editor.document.languageId != "rust") return @@ -237,6 +250,11 @@ interface FindMatchingBraceParams { offsets: lc.Position[]; } +interface JoinLinesParams { + textDocument: lc.TextDocumentIdentifier; + range: lc.Range; +} + interface PublishDecorationsParams { uri: string, decorations: Decoration[], diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index b47cbc0fc..ae362ddaa 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use languageserver_types::{ Diagnostic, DiagnosticSeverity, Url, DocumentSymbol, Command, TextDocumentIdentifier, WorkspaceEdit, - SymbolInformation, Position, Location, + SymbolInformation, Position, Location, TextEdit, }; use libanalysis::{Query}; use libeditor; @@ -58,6 +58,18 @@ pub fn handle_find_matching_brace( Ok(res) } +pub fn handle_join_lines( + world: ServerWorld, + params: req::JoinLinesParams, +) -> Result> { + 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( world: ServerWorld, params: req::DocumentSymbolParams, diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs index 9499b826c..1e65041e2 100644 --- a/crates/server/src/main_loop/mod.rs +++ b/crates/server/src/main_loop/mod.rs @@ -27,6 +27,7 @@ use { handle_goto_definition, handle_find_matching_brace, handle_parent_module, + handle_join_lines, }, }; @@ -145,6 +146,9 @@ fn on_request( handle_request_on_threadpool::( &mut req, pool, world, sender, handle_parent_module, )?; + handle_request_on_threadpool::( + &mut req, pool, world, sender, handle_join_lines, + )?; dispatch::handle_request::(&mut req, |params, resp| { io.send(RawMsg::Response(resp.into_response(Ok(None))?)); diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index a8aa24629..c431deeb4 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs @@ -10,6 +10,7 @@ pub use languageserver_types::{ ExecuteCommandParams, WorkspaceSymbolParams, TextDocumentPositionParams, + TextEdit, }; @@ -117,3 +118,18 @@ impl Request for ParentModule { type Result = Vec; const METHOD: &'static str = "m/parentModule"; } + +pub enum JoinLines {} + +impl Request for JoinLines { + type Params = JoinLinesParams; + type Result = Vec; + const METHOD: &'static str = "m/joinLines"; +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct JoinLinesParams { + pub text_document: TextDocumentIdentifier, + pub range: Range, +} -- cgit v1.2.3