aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorivan770 <[email protected]>2021-03-16 14:57:14 +0000
committerivan770 <[email protected]>2021-03-18 09:22:27 +0000
commita154ef7ca1010b890e7dbe913d00140ed029945c (patch)
tree4f077e645da76b97e25159dac77de0d0d4f7c909
parent7d604584954660d255ad0929d3be8ce03f879d0c (diff)
Remove movable array, improve client code
-rw-r--r--crates/ide/src/move_item.rs36
-rw-r--r--editors/code/src/commands.ts6
-rw-r--r--editors/code/src/lsp_ext.ts2
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(
33} 33}
34 34
35fn find_ancestors(item: SyntaxElement, direction: Direction) -> Option<TextEdit> { 35fn find_ancestors(item: SyntaxElement, direction: Direction) -> Option<TextEdit> {
36 let movable = [
37 SyntaxKind::MATCH_ARM,
38 // https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/ide/actions/mover/RsStatementUpDownMover.kt
39 SyntaxKind::LET_STMT,
40 SyntaxKind::EXPR_STMT,
41 SyntaxKind::MATCH_EXPR,
42 // https://github.com/intellij-rust/intellij-rust/blob/master/src/main/kotlin/org/rust/ide/actions/mover/RsItemUpDownMover.kt
43 SyntaxKind::TRAIT,
44 SyntaxKind::IMPL,
45 SyntaxKind::MACRO_CALL,
46 SyntaxKind::MACRO_DEF,
47 SyntaxKind::STRUCT,
48 SyntaxKind::ENUM,
49 SyntaxKind::MODULE,
50 SyntaxKind::USE,
51 SyntaxKind::FN,
52 SyntaxKind::CONST,
53 SyntaxKind::TYPE_ALIAS,
54 ];
55
56 let root = match item { 36 let root = match item {
57 NodeOrToken::Node(node) => node, 37 NodeOrToken::Node(node) => node,
58 NodeOrToken::Token(token) => token.parent(), 38 NodeOrToken::Token(token) => token.parent(),
@@ -60,17 +40,18 @@ fn find_ancestors(item: SyntaxElement, direction: Direction) -> Option<TextEdit>
60 40
61 let ancestor = once(root.clone()) 41 let ancestor = once(root.clone())
62 .chain(root.ancestors()) 42 .chain(root.ancestors())
63 .filter(|ancestor| movable.contains(&ancestor.kind())) 43 .filter_map(|ancestor| kind_priority(ancestor.kind()).map(|priority| (priority, ancestor)))
64 .max_by_key(|ancestor| kind_priority(ancestor.kind()))?; 44 .max_by_key(|(priority, _)| *priority)
45 .map(|(_, ancestor)| ancestor)?;
65 46
66 move_in_direction(&ancestor, direction) 47 move_in_direction(&ancestor, direction)
67} 48}
68 49
69fn kind_priority(kind: SyntaxKind) -> i32 { 50fn kind_priority(kind: SyntaxKind) -> Option<i32> {
70 match kind { 51 match kind {
71 SyntaxKind::MATCH_ARM => 4, 52 SyntaxKind::MATCH_ARM => Some(4),
72 53
73 SyntaxKind::LET_STMT | SyntaxKind::EXPR_STMT | SyntaxKind::MATCH_EXPR => 3, 54 SyntaxKind::LET_STMT | SyntaxKind::EXPR_STMT | SyntaxKind::MATCH_EXPR => Some(3),
74 55
75 SyntaxKind::TRAIT 56 SyntaxKind::TRAIT
76 | SyntaxKind::IMPL 57 | SyntaxKind::IMPL
@@ -82,10 +63,9 @@ fn kind_priority(kind: SyntaxKind) -> i32 {
82 | SyntaxKind::USE 63 | SyntaxKind::USE
83 | SyntaxKind::FN 64 | SyntaxKind::FN
84 | SyntaxKind::CONST 65 | SyntaxKind::CONST
85 | SyntaxKind::TYPE_ALIAS => 2, 66 | SyntaxKind::TYPE_ALIAS => Some(2),
86 67
87 // Placeholder for items, that are non-movable, and filtered even before kind_priority call 68 _ => None,
88 _ => 1,
89 } 69 }
90} 70}
91 71
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 {
148 const client = ctx.client; 148 const client = ctx.client;
149 if (!editor || !client) return; 149 if (!editor || !client) return;
150 150
151 const edit: lc.TextDocumentEdit = await client.sendRequest(ra.moveItem, { 151 const edit = await client.sendRequest(ra.moveItem, {
152 range: client.code2ProtocolConverter.asRange(editor.selection), 152 range: client.code2ProtocolConverter.asRange(editor.selection),
153 textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document), 153 textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document),
154 direction 154 direction
155 }); 155 });
156 156
157 if(!edit) return;
158
157 await editor.edit((builder) => { 159 await editor.edit((builder) => {
158 client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => { 160 client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => {
159 builder.replace(edit.range, edit.newText); 161 builder.replace(edit.range, edit.newText);
160 }); 162 });
163 }).then(() => {
164 editor.selection = new vscode.Selection(editor.selection.end, editor.selection.end);
161 }); 165 });
162 }; 166 };
163} 167}
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 {
128 textDocument: lc.TextDocumentIdentifier; 128 textDocument: lc.TextDocumentIdentifier;
129} 129}
130 130
131export const moveItem = new lc.RequestType<MoveItemParams, lc.TextDocumentEdit, void>("experimental/moveItem"); 131export const moveItem = new lc.RequestType<MoveItemParams, lc.TextDocumentEdit | void, void>("experimental/moveItem");
132 132
133export interface MoveItemParams { 133export interface MoveItemParams {
134 textDocument: lc.TextDocumentIdentifier, 134 textDocument: lc.TextDocumentIdentifier,