diff options
author | ivan770 <[email protected]> | 2021-03-16 12:37:00 +0000 |
---|---|---|
committer | ivan770 <[email protected]> | 2021-03-18 09:22:27 +0000 |
commit | 7d604584954660d255ad0929d3be8ce03f879d0c (patch) | |
tree | 613fdfdfd7eeb170082800533fb8b669dc35d25b /editors/code | |
parent | d704750ba982153d92ccff90cf236121641b9da3 (diff) |
Item up and down movers
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/package.json | 10 | ||||
-rw-r--r-- | editors/code/src/commands.ts | 28 | ||||
-rw-r--r-- | editors/code/src/lsp_ext.ts | 13 | ||||
-rw-r--r-- | editors/code/src/main.ts | 2 |
4 files changed, 53 insertions, 0 deletions
diff --git a/editors/code/package.json b/editors/code/package.json index a2ed9b2d5..faec45276 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -208,6 +208,16 @@ | |||
208 | "command": "rust-analyzer.peekTests", | 208 | "command": "rust-analyzer.peekTests", |
209 | "title": "Peek related tests", | 209 | "title": "Peek related tests", |
210 | "category": "Rust Analyzer" | 210 | "category": "Rust Analyzer" |
211 | }, | ||
212 | { | ||
213 | "command": "rust-analyzer.moveItemUp", | ||
214 | "title": "Move item up", | ||
215 | "category": "Rust Analyzer" | ||
216 | }, | ||
217 | { | ||
218 | "command": "rust-analyzer.moveItemDown", | ||
219 | "title": "Move item down", | ||
220 | "category": "Rust Analyzer" | ||
211 | } | 221 | } |
212 | ], | 222 | ], |
213 | "keybindings": [ | 223 | "keybindings": [ |
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index bed1f0116..cc90fe889 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts | |||
@@ -134,6 +134,34 @@ export function joinLines(ctx: Ctx): Cmd { | |||
134 | }; | 134 | }; |
135 | } | 135 | } |
136 | 136 | ||
137 | export function moveItemUp(ctx: Ctx): Cmd { | ||
138 | return moveItem(ctx, ra.Direction.Up); | ||
139 | } | ||
140 | |||
141 | export function moveItemDown(ctx: Ctx): Cmd { | ||
142 | return moveItem(ctx, ra.Direction.Down); | ||
143 | } | ||
144 | |||
145 | export function moveItem(ctx: Ctx, direction: ra.Direction): Cmd { | ||
146 | return async () => { | ||
147 | const editor = ctx.activeRustEditor; | ||
148 | const client = ctx.client; | ||
149 | if (!editor || !client) return; | ||
150 | |||
151 | const edit: lc.TextDocumentEdit = await client.sendRequest(ra.moveItem, { | ||
152 | range: client.code2ProtocolConverter.asRange(editor.selection), | ||
153 | textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document), | ||
154 | direction | ||
155 | }); | ||
156 | |||
157 | await editor.edit((builder) => { | ||
158 | client.protocol2CodeConverter.asTextEdits(edit.edits).forEach((edit: any) => { | ||
159 | builder.replace(edit.range, edit.newText); | ||
160 | }); | ||
161 | }); | ||
162 | }; | ||
163 | } | ||
164 | |||
137 | export function onEnter(ctx: Ctx): Cmd { | 165 | export function onEnter(ctx: Ctx): Cmd { |
138 | async function handleKeypress() { | 166 | async function handleKeypress() { |
139 | const editor = ctx.activeRustEditor; | 167 | const editor = ctx.activeRustEditor; |
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 52de29e04..9af30cfdb 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts | |||
@@ -127,3 +127,16 @@ export const openCargoToml = new lc.RequestType<OpenCargoTomlParams, lc.Location | |||
127 | export interface OpenCargoTomlParams { | 127 | export interface OpenCargoTomlParams { |
128 | textDocument: lc.TextDocumentIdentifier; | 128 | textDocument: lc.TextDocumentIdentifier; |
129 | } | 129 | } |
130 | |||
131 | export const moveItem = new lc.RequestType<MoveItemParams, lc.TextDocumentEdit, void>("experimental/moveItem"); | ||
132 | |||
133 | export interface MoveItemParams { | ||
134 | textDocument: lc.TextDocumentIdentifier, | ||
135 | range: lc.Range, | ||
136 | direction: Direction | ||
137 | } | ||
138 | |||
139 | export const enum Direction { | ||
140 | Up = "Up", | ||
141 | Down = "Down" | ||
142 | } | ||
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 925103f56..643fb643f 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -114,6 +114,8 @@ async function tryActivate(context: vscode.ExtensionContext) { | |||
114 | ctx.registerCommand('openDocs', commands.openDocs); | 114 | ctx.registerCommand('openDocs', commands.openDocs); |
115 | ctx.registerCommand('openCargoToml', commands.openCargoToml); | 115 | ctx.registerCommand('openCargoToml', commands.openCargoToml); |
116 | ctx.registerCommand('peekTests', commands.peekTests); | 116 | ctx.registerCommand('peekTests', commands.peekTests); |
117 | ctx.registerCommand('moveItemUp', commands.moveItemUp); | ||
118 | ctx.registerCommand('moveItemDown', commands.moveItemDown); | ||
117 | 119 | ||
118 | defaultOnEnter.dispose(); | 120 | defaultOnEnter.dispose(); |
119 | ctx.registerCommand('onEnter', commands.onEnter); | 121 | ctx.registerCommand('onEnter', commands.onEnter); |