aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-30 15:43:34 +0000
committerAleksey Kladov <[email protected]>2019-12-30 18:07:59 +0000
commit5aebf1081dced95a71c674aba65fb5b3e40e6ff1 (patch)
treebacedf66912eee5a366a3c93c60a39ba29744f92 /editors/code/src/commands
parent83d2527880d86653ce00940c65620319b36afcff (diff)
Refactor applySourceChange
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r--editors/code/src/commands/analyzer_status.ts8
-rw-r--r--editors/code/src/commands/apply_source_change.ts54
-rw-r--r--editors/code/src/commands/index.ts4
-rw-r--r--editors/code/src/commands/join_lines.ts12
-rw-r--r--editors/code/src/commands/on_enter.ts49
5 files changed, 31 insertions, 96 deletions
diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts
index c9d32fe07..849c2ec6c 100644
--- a/editors/code/src/commands/analyzer_status.ts
+++ b/editors/code/src/commands/analyzer_status.ts
@@ -40,11 +40,10 @@ export function analyzerStatus(ctx: Ctx): Cmd {
40class TextDocumentContentProvider 40class TextDocumentContentProvider
41 implements vscode.TextDocumentContentProvider { 41 implements vscode.TextDocumentContentProvider {
42 42
43 ctx: Ctx
43 uri = vscode.Uri.parse('rust-analyzer-status://status'); 44 uri = vscode.Uri.parse('rust-analyzer-status://status');
44 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 45 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
45 46
46 ctx: Ctx
47
48 constructor(ctx: Ctx) { 47 constructor(ctx: Ctx) {
49 this.ctx = ctx 48 this.ctx = ctx
50 } 49 }
@@ -53,9 +52,8 @@ class TextDocumentContentProvider
53 _uri: vscode.Uri, 52 _uri: vscode.Uri,
54 ): vscode.ProviderResult<string> { 53 ): vscode.ProviderResult<string> {
55 const editor = vscode.window.activeTextEditor; 54 const editor = vscode.window.activeTextEditor;
56 if (editor == null) { 55 if (editor == null) return '';
57 return ''; 56
58 }
59 return this.ctx.client.sendRequest<string>( 57 return this.ctx.client.sendRequest<string>(
60 'rust-analyzer/analyzerStatus', 58 'rust-analyzer/analyzerStatus',
61 null, 59 null,
diff --git a/editors/code/src/commands/apply_source_change.ts b/editors/code/src/commands/apply_source_change.ts
deleted file mode 100644
index 8167398b1..000000000
--- a/editors/code/src/commands/apply_source_change.ts
+++ /dev/null
@@ -1,54 +0,0 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3
4import { Server } from '../server';
5
6export interface SourceChange {
7 label: string;
8 workspaceEdit: lc.WorkspaceEdit;
9 cursorPosition?: lc.TextDocumentPositionParams;
10}
11
12export async function handle(change: SourceChange) {
13 const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit(
14 change.workspaceEdit,
15 );
16 let created;
17 let moved;
18 if (change.workspaceEdit.documentChanges) {
19 for (const docChange of change.workspaceEdit.documentChanges) {
20 if (lc.CreateFile.is(docChange)) {
21 created = docChange.uri;
22 } else if (lc.RenameFile.is(docChange)) {
23 moved = docChange.newUri;
24 }
25 }
26 }
27 const toOpen = created || moved;
28 const toReveal = change.cursorPosition;
29 await vscode.workspace.applyEdit(wsEdit);
30 if (toOpen) {
31 const toOpenUri = vscode.Uri.parse(toOpen);
32 const doc = await vscode.workspace.openTextDocument(toOpenUri);
33 await vscode.window.showTextDocument(doc);
34 } else if (toReveal) {
35 const uri = Server.client.protocol2CodeConverter.asUri(
36 toReveal.textDocument.uri,
37 );
38 const position = Server.client.protocol2CodeConverter.asPosition(
39 toReveal.position,
40 );
41 const editor = vscode.window.activeTextEditor;
42 if (!editor || editor.document.uri.toString() !== uri.toString()) {
43 return;
44 }
45 if (!editor.selection.isEmpty) {
46 return;
47 }
48 editor.selection = new vscode.Selection(position, position);
49 editor.revealRange(
50 new vscode.Range(position, position),
51 vscode.TextEditorRevealType.Default,
52 );
53 }
54}
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 8090c7e5b..0a0a36e23 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -3,10 +3,9 @@ import { Ctx, Cmd } from '../ctx'
3import { analyzerStatus } from './analyzer_status'; 3import { analyzerStatus } from './analyzer_status';
4import { matchingBrace } from './matching_brace'; 4import { matchingBrace } from './matching_brace';
5import { joinLines } from './join_lines'; 5import { joinLines } from './join_lines';
6import * as applySourceChange from './apply_source_change'; 6import { onEnter } from './on_enter';
7import * as expandMacro from './expand_macro'; 7import * as expandMacro from './expand_macro';
8import * as inlayHints from './inlay_hints'; 8import * as inlayHints from './inlay_hints';
9import * as onEnter from './on_enter';
10import * as parentModule from './parent_module'; 9import * as parentModule from './parent_module';
11import * as runnables from './runnables'; 10import * as runnables from './runnables';
12import * as syntaxTree from './syntaxTree'; 11import * as syntaxTree from './syntaxTree';
@@ -17,7 +16,6 @@ function collectGarbage(ctx: Ctx): Cmd {
17 16
18export { 17export {
19 analyzerStatus, 18 analyzerStatus,
20 applySourceChange,
21 expandMacro, 19 expandMacro,
22 joinLines, 20 joinLines,
23 matchingBrace, 21 matchingBrace,
diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts
index 7952fb0c0..1a4b8a2d8 100644
--- a/editors/code/src/commands/join_lines.ts
+++ b/editors/code/src/commands/join_lines.ts
@@ -1,16 +1,14 @@
1import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; 1import { Range, TextDocumentIdentifier } from 'vscode-languageclient';
2import { Ctx, Cmd } from '../ctx'; 2import { Ctx, Cmd } from '../ctx';
3import { 3import {
4 handle as applySourceChange, 4 applySourceChange, SourceChange
5 SourceChange, 5} from '../source_change';
6} from './apply_source_change';
7 6
8export function joinLines(ctx: Ctx): Cmd { 7export function joinLines(ctx: Ctx): Cmd {
9 return async () => { 8 return async () => {
10 const editor = ctx.activeRustEditor; 9 const editor = ctx.activeRustEditor;
11 if (!editor) { 10 if (!editor) return;
12 return; 11
13 }
14 const request: JoinLinesParams = { 12 const request: JoinLinesParams = {
15 range: ctx.client.code2ProtocolConverter.asRange(editor.selection), 13 range: ctx.client.code2ProtocolConverter.asRange(editor.selection),
16 textDocument: { uri: editor.document.uri.toString() }, 14 textDocument: { uri: editor.document.uri.toString() },
@@ -19,7 +17,7 @@ export function joinLines(ctx: Ctx): Cmd {
19 'rust-analyzer/joinLines', 17 'rust-analyzer/joinLines',
20 request, 18 request,
21 ); 19 );
22 await applySourceChange(change); 20 await applySourceChange(ctx, change);
23 } 21 }
24} 22}
25 23
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts
index 772c64b3c..4503e13f0 100644
--- a/editors/code/src/commands/on_enter.ts
+++ b/editors/code/src/commands/on_enter.ts
@@ -1,33 +1,28 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 1import * as lc from 'vscode-languageclient';
3import { Server } from '../server';
4import { 2import {
5 handle as applySourceChange, 3 applySourceChange,
6 SourceChange, 4 SourceChange,
7} from './apply_source_change'; 5} from '../source_change';
6import { Cmd, Ctx } from '../ctx';
8 7
9export async function handle(event: { text: string }): Promise<boolean> { 8export function onEnter(ctx: Ctx): Cmd {
10 const editor = vscode.window.activeTextEditor; 9 return async (event: { text: string }) => {
11 if ( 10 const editor = ctx.activeRustEditor;
12 editor == null || 11 if (!editor || event.text !== '\n') return false;
13 editor.document.languageId !== 'rust' || 12
14 event.text !== '\n' 13 const request: lc.TextDocumentPositionParams = {
15 ) { 14 textDocument: { uri: editor.document.uri.toString() },
16 return false; 15 position: ctx.client.code2ProtocolConverter.asPosition(
17 } 16 editor.selection.active,
18 const request: lc.TextDocumentPositionParams = { 17 ),
19 textDocument: { uri: editor.document.uri.toString() }, 18 };
20 position: Server.client.code2ProtocolConverter.asPosition( 19 const change = await ctx.client.sendRequest<undefined | SourceChange>(
21 editor.selection.active, 20 'rust-analyzer/onEnter',
22 ), 21 request,
23 }; 22 );
24 const change = await Server.client.sendRequest<undefined | SourceChange>( 23 if (!change) return false;
25 'rust-analyzer/onEnter', 24
26 request, 25 await applySourceChange(ctx, change);
27 ); 26 return true;
28 if (!change) {
29 return false;
30 } 27 }
31 await applySourceChange(change);
32 return true;
33} 28}