mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-24 12:24:11 +00:00
Remove movable array, improve client code
This commit is contained in:
parent
7d60458495
commit
a154ef7ca1
3 changed files with 14 additions and 30 deletions
|
@ -33,26 +33,6 @@ pub(crate) fn move_item(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_ancestors(item: SyntaxElement, direction: Direction) -> Option<TextEdit> {
|
fn find_ancestors(item: SyntaxElement, direction: Direction) -> Option<TextEdit> {
|
||||||
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 {
|
let root = match item {
|
||||||
NodeOrToken::Node(node) => node,
|
NodeOrToken::Node(node) => node,
|
||||||
NodeOrToken::Token(token) => token.parent(),
|
NodeOrToken::Token(token) => token.parent(),
|
||||||
|
@ -60,17 +40,18 @@ fn find_ancestors(item: SyntaxElement, direction: Direction) -> Option<TextEdit>
|
||||||
|
|
||||||
let ancestor = once(root.clone())
|
let ancestor = once(root.clone())
|
||||||
.chain(root.ancestors())
|
.chain(root.ancestors())
|
||||||
.filter(|ancestor| movable.contains(&ancestor.kind()))
|
.filter_map(|ancestor| kind_priority(ancestor.kind()).map(|priority| (priority, ancestor)))
|
||||||
.max_by_key(|ancestor| kind_priority(ancestor.kind()))?;
|
.max_by_key(|(priority, _)| *priority)
|
||||||
|
.map(|(_, ancestor)| ancestor)?;
|
||||||
|
|
||||||
move_in_direction(&ancestor, direction)
|
move_in_direction(&ancestor, direction)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kind_priority(kind: SyntaxKind) -> i32 {
|
fn kind_priority(kind: SyntaxKind) -> Option<i32> {
|
||||||
match kind {
|
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::TRAIT
|
||||||
| SyntaxKind::IMPL
|
| SyntaxKind::IMPL
|
||||||
|
@ -82,10 +63,9 @@ fn kind_priority(kind: SyntaxKind) -> i32 {
|
||||||
| SyntaxKind::USE
|
| SyntaxKind::USE
|
||||||
| SyntaxKind::FN
|
| SyntaxKind::FN
|
||||||
| SyntaxKind::CONST
|
| 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
|
_ => None,
|
||||||
_ => 1,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -148,16 +148,20 @@ export function moveItem(ctx: Ctx, direction: ra.Direction): Cmd {
|
||||||
const client = ctx.client;
|
const client = ctx.client;
|
||||||
if (!editor || !client) return;
|
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),
|
range: client.code2ProtocolConverter.asRange(editor.selection),
|
||||||
textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document),
|
textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document),
|
||||||
direction
|
direction
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if(!edit) return;
|
||||||
|
|
||||||
await editor.edit((builder) => {
|
await editor.edit((builder) => {
|
||||||
client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => {
|
client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => {
|
||||||
builder.replace(edit.range, edit.newText);
|
builder.replace(edit.range, edit.newText);
|
||||||
});
|
});
|
||||||
|
}).then(() => {
|
||||||
|
editor.selection = new vscode.Selection(editor.selection.end, editor.selection.end);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,7 +128,7 @@ export interface OpenCargoTomlParams {
|
||||||
textDocument: lc.TextDocumentIdentifier;
|
textDocument: lc.TextDocumentIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const moveItem = new lc.RequestType<MoveItemParams, lc.TextDocumentEdit, void>("experimental/moveItem");
|
export const moveItem = new lc.RequestType<MoveItemParams, lc.TextDocumentEdit | void, void>("experimental/moveItem");
|
||||||
|
|
||||||
export interface MoveItemParams {
|
export interface MoveItemParams {
|
||||||
textDocument: lc.TextDocumentIdentifier,
|
textDocument: lc.TextDocumentIdentifier,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue