From a154ef7ca1010b890e7dbe913d00140ed029945c Mon Sep 17 00:00:00 2001 From: ivan770 Date: Tue, 16 Mar 2021 16:57:14 +0200 Subject: Remove movable array, improve client code --- crates/ide/src/move_item.rs | 36 ++++++++---------------------------- editors/code/src/commands.ts | 6 +++++- editors/code/src/lsp_ext.ts | 2 +- 3 files changed, 14 insertions(+), 30 deletions(-) diff --git a/crates/ide/src/move_item.rs b/crates/ide/src/move_item.rs index be62d008d..5a0faaf7b 100644 --- a/crates/ide/src/move_item.rs +++ b/crates/ide/src/move_item.rs @@ -33,26 +33,6 @@ pub(crate) fn move_item( } fn find_ancestors(item: SyntaxElement, direction: Direction) -> Option { - let movable = [ - SyntaxKind::MATCH_ARM, - // https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/ide/actions/mover/RsStatementUpDownMover.kt - SyntaxKind::LET_STMT, - SyntaxKind::EXPR_STMT, - SyntaxKind::MATCH_EXPR, - // https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/ide/actions/mover/RsItemUpDownMover.kt - SyntaxKind::TRAIT, - SyntaxKind::IMPL, - SyntaxKind::MACRO_CALL, - SyntaxKind::MACRO_DEF, - SyntaxKind::STRUCT, - SyntaxKind::ENUM, - SyntaxKind::MODULE, - SyntaxKind::USE, - SyntaxKind::FN, - SyntaxKind::CONST, - SyntaxKind::TYPE_ALIAS, - ]; - let root = match item { NodeOrToken::Node(node) => node, NodeOrToken::Token(token) => token.parent(), @@ -60,17 +40,18 @@ fn find_ancestors(item: SyntaxElement, direction: Direction) -> Option let ancestor = once(root.clone()) .chain(root.ancestors()) - .filter(|ancestor| movable.contains(&ancestor.kind())) - .max_by_key(|ancestor| kind_priority(ancestor.kind()))?; + .filter_map(|ancestor| kind_priority(ancestor.kind()).map(|priority| (priority, ancestor))) + .max_by_key(|(priority, _)| *priority) + .map(|(_, ancestor)| ancestor)?; move_in_direction(&ancestor, direction) } -fn kind_priority(kind: SyntaxKind) -> i32 { +fn kind_priority(kind: SyntaxKind) -> Option { match kind { - SyntaxKind::MATCH_ARM => 4, + SyntaxKind::MATCH_ARM => Some(4), - SyntaxKind::LET_STMT | SyntaxKind::EXPR_STMT | SyntaxKind::MATCH_EXPR => 3, + SyntaxKind::LET_STMT | SyntaxKind::EXPR_STMT | SyntaxKind::MATCH_EXPR => Some(3), SyntaxKind::TRAIT | SyntaxKind::IMPL @@ -82,10 +63,9 @@ fn kind_priority(kind: SyntaxKind) -> i32 { | SyntaxKind::USE | SyntaxKind::FN | SyntaxKind::CONST - | SyntaxKind::TYPE_ALIAS => 2, + | SyntaxKind::TYPE_ALIAS => Some(2), - // Placeholder for items, that are non-movable, and filtered even before kind_priority call - _ => 1, + _ => None, } } diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index cc90fe889..ad0b533b9 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -148,16 +148,20 @@ export function moveItem(ctx: Ctx, direction: ra.Direction): Cmd { const client = ctx.client; if (!editor || !client) return; - const edit: lc.TextDocumentEdit = await client.sendRequest(ra.moveItem, { + const edit = await client.sendRequest(ra.moveItem, { range: client.code2ProtocolConverter.asRange(editor.selection), textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document), direction }); + if(!edit) return; + await editor.edit((builder) => { client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => { builder.replace(edit.range, edit.newText); }); + }).then(() => { + editor.selection = new vscode.Selection(editor.selection.end, editor.selection.end); }); }; } diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 9af30cfdb..4c070beb0 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -128,7 +128,7 @@ export interface OpenCargoTomlParams { textDocument: lc.TextDocumentIdentifier; } -export const moveItem = new lc.RequestType("experimental/moveItem"); +export const moveItem = new lc.RequestType("experimental/moveItem"); export interface MoveItemParams { textDocument: lc.TextDocumentIdentifier, -- cgit v1.2.3