diff options
author | Aleksey Kladov <[email protected]> | 2019-12-30 18:05:41 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-12-30 18:07:59 +0000 |
commit | ca5c59507f76b8e30658d6c815b823c9636d786a (patch) | |
tree | c953cf7eaaf7f6892e7cff63b5b96ff3060826a8 /editors | |
parent | ac3d0e83403be22ec31d62a1501d726f6e6f81e1 (diff) |
Refactor show syntax tree action
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/src/commands/index.ts | 2 | ||||
-rw-r--r-- | editors/code/src/commands/syntaxTree.ts | 76 | ||||
-rw-r--r-- | editors/code/src/commands/syntax_tree.ts | 106 | ||||
-rw-r--r-- | editors/code/src/ctx.ts | 4 | ||||
-rw-r--r-- | editors/code/src/events/change_active_text_editor.ts | 9 | ||||
-rw-r--r-- | editors/code/src/events/change_text_document.ts | 24 | ||||
-rw-r--r-- | editors/code/src/events/index.ts | 3 | ||||
-rw-r--r-- | editors/code/src/main.ts | 26 |
8 files changed, 116 insertions, 134 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index a7f3bc4c1..8f91b3b7d 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts | |||
@@ -5,10 +5,10 @@ import { matchingBrace } from './matching_brace'; | |||
5 | import { joinLines } from './join_lines'; | 5 | import { joinLines } from './join_lines'; |
6 | import { onEnter } from './on_enter'; | 6 | import { onEnter } from './on_enter'; |
7 | import { parentModule } from './parent_module'; | 7 | import { parentModule } from './parent_module'; |
8 | import { syntaxTree } from './syntax_tree'; | ||
8 | import * as expandMacro from './expand_macro'; | 9 | import * as expandMacro from './expand_macro'; |
9 | import * as inlayHints from './inlay_hints'; | 10 | import * as inlayHints from './inlay_hints'; |
10 | import * as runnables from './runnables'; | 11 | import * as runnables from './runnables'; |
11 | import * as syntaxTree from './syntaxTree'; | ||
12 | 12 | ||
13 | function collectGarbage(ctx: Ctx): Cmd { | 13 | function collectGarbage(ctx: Ctx): Cmd { |
14 | return async () => { | 14 | return async () => { |
diff --git a/editors/code/src/commands/syntaxTree.ts b/editors/code/src/commands/syntaxTree.ts deleted file mode 100644 index 89a80550c..000000000 --- a/editors/code/src/commands/syntaxTree.ts +++ /dev/null | |||
@@ -1,76 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; | ||
3 | |||
4 | import { Server } from '../server'; | ||
5 | |||
6 | export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree'); | ||
7 | |||
8 | export class SyntaxTreeContentProvider | ||
9 | implements vscode.TextDocumentContentProvider { | ||
10 | public eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
11 | public syntaxTree: string = 'Not available'; | ||
12 | |||
13 | public provideTextDocumentContent( | ||
14 | uri: vscode.Uri, | ||
15 | ): vscode.ProviderResult<string> { | ||
16 | const editor = vscode.window.activeTextEditor; | ||
17 | if (editor == null) { | ||
18 | return ''; | ||
19 | } | ||
20 | |||
21 | let range: Range | undefined; | ||
22 | |||
23 | // When the range based query is enabled we take the range of the selection | ||
24 | if (uri.query === 'range=true') { | ||
25 | range = editor.selection.isEmpty | ||
26 | ? undefined | ||
27 | : Server.client.code2ProtocolConverter.asRange( | ||
28 | editor.selection, | ||
29 | ); | ||
30 | } | ||
31 | |||
32 | const request: SyntaxTreeParams = { | ||
33 | textDocument: { uri: editor.document.uri.toString() }, | ||
34 | range, | ||
35 | }; | ||
36 | return Server.client.sendRequest<SyntaxTreeResult>( | ||
37 | 'rust-analyzer/syntaxTree', | ||
38 | request, | ||
39 | ); | ||
40 | } | ||
41 | |||
42 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
43 | return this.eventEmitter.event; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | interface SyntaxTreeParams { | ||
48 | textDocument: TextDocumentIdentifier; | ||
49 | range?: Range; | ||
50 | } | ||
51 | |||
52 | type SyntaxTreeResult = string; | ||
53 | |||
54 | // Opens the virtual file that will show the syntax tree | ||
55 | // | ||
56 | // The contents of the file come from the `TextDocumentContentProvider` | ||
57 | export function createHandle(provider: SyntaxTreeContentProvider) { | ||
58 | return async () => { | ||
59 | const editor = vscode.window.activeTextEditor; | ||
60 | const rangeEnabled = !!(editor && !editor.selection.isEmpty); | ||
61 | |||
62 | const uri = rangeEnabled | ||
63 | ? vscode.Uri.parse(`${syntaxTreeUri.toString()}?range=true`) | ||
64 | : syntaxTreeUri; | ||
65 | |||
66 | const document = await vscode.workspace.openTextDocument(uri); | ||
67 | |||
68 | provider.eventEmitter.fire(uri); | ||
69 | |||
70 | return vscode.window.showTextDocument( | ||
71 | document, | ||
72 | vscode.ViewColumn.Two, | ||
73 | true, | ||
74 | ); | ||
75 | }; | ||
76 | } | ||
diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts new file mode 100644 index 000000000..e61fb36df --- /dev/null +++ b/editors/code/src/commands/syntax_tree.ts | |||
@@ -0,0 +1,106 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as lc from 'vscode-languageclient'; | ||
3 | |||
4 | import { Ctx, Cmd } from '../ctx'; | ||
5 | |||
6 | // Opens the virtual file that will show the syntax tree | ||
7 | // | ||
8 | // The contents of the file come from the `TextDocumentContentProvider` | ||
9 | export function syntaxTree(ctx: Ctx): Cmd { | ||
10 | const stcp = new SyntaxTreeContentProvider(ctx); | ||
11 | |||
12 | ctx.pushCleanup( | ||
13 | vscode.workspace.registerTextDocumentContentProvider( | ||
14 | 'rust-analyzer', | ||
15 | stcp, | ||
16 | ), | ||
17 | ); | ||
18 | |||
19 | vscode.workspace.onDidChangeTextDocument( | ||
20 | (event: vscode.TextDocumentChangeEvent) => { | ||
21 | const doc = event.document; | ||
22 | if (doc.languageId !== 'rust') return; | ||
23 | afterLs(() => stcp.eventEmitter.fire(stcp.uri)); | ||
24 | }, | ||
25 | ctx.subscriptions, | ||
26 | ); | ||
27 | |||
28 | vscode.window.onDidChangeActiveTextEditor( | ||
29 | (editor: vscode.TextEditor | undefined) => { | ||
30 | if (!editor || editor.document.languageId !== 'rust') return; | ||
31 | stcp.eventEmitter.fire(stcp.uri); | ||
32 | }, | ||
33 | ctx.subscriptions, | ||
34 | ); | ||
35 | |||
36 | return async () => { | ||
37 | const editor = vscode.window.activeTextEditor; | ||
38 | const rangeEnabled = !!(editor && !editor.selection.isEmpty); | ||
39 | |||
40 | const uri = rangeEnabled | ||
41 | ? vscode.Uri.parse(`${stcp.uri.toString()}?range=true`) | ||
42 | : stcp.uri; | ||
43 | |||
44 | const document = await vscode.workspace.openTextDocument(uri); | ||
45 | |||
46 | stcp.eventEmitter.fire(uri); | ||
47 | |||
48 | return vscode.window.showTextDocument( | ||
49 | document, | ||
50 | vscode.ViewColumn.Two, | ||
51 | true, | ||
52 | ); | ||
53 | }; | ||
54 | } | ||
55 | |||
56 | // We need to order this after LS updates, but there's no API for that. | ||
57 | // Hence, good old setTimeout. | ||
58 | function afterLs(f: () => any) { | ||
59 | setTimeout(f, 10); | ||
60 | } | ||
61 | |||
62 | interface SyntaxTreeParams { | ||
63 | textDocument: lc.TextDocumentIdentifier; | ||
64 | range?: lc.Range; | ||
65 | } | ||
66 | |||
67 | export class SyntaxTreeContentProvider | ||
68 | implements vscode.TextDocumentContentProvider { | ||
69 | ctx: Ctx; | ||
70 | uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); | ||
71 | eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
72 | syntaxTree: string = 'Not available'; | ||
73 | |||
74 | constructor(ctx: Ctx) { | ||
75 | this.ctx = ctx; | ||
76 | } | ||
77 | |||
78 | provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> { | ||
79 | const editor = vscode.window.activeTextEditor; | ||
80 | if (editor == null) return ''; | ||
81 | |||
82 | let range: lc.Range | undefined; | ||
83 | |||
84 | // When the range based query is enabled we take the range of the selection | ||
85 | if (uri.query === 'range=true') { | ||
86 | range = editor.selection.isEmpty | ||
87 | ? undefined | ||
88 | : this.ctx.client.code2ProtocolConverter.asRange( | ||
89 | editor.selection, | ||
90 | ); | ||
91 | } | ||
92 | |||
93 | const request: SyntaxTreeParams = { | ||
94 | textDocument: { uri: editor.document.uri.toString() }, | ||
95 | range, | ||
96 | }; | ||
97 | return this.ctx.client.sendRequest<string>( | ||
98 | 'rust-analyzer/syntaxTree', | ||
99 | request, | ||
100 | ); | ||
101 | } | ||
102 | |||
103 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
104 | return this.eventEmitter.event; | ||
105 | } | ||
106 | } | ||
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 22af5ef32..c3a3583b5 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts | |||
@@ -49,6 +49,10 @@ export class Ctx { | |||
49 | } | 49 | } |
50 | } | 50 | } |
51 | 51 | ||
52 | get subscriptions(): { dispose(): any }[] { | ||
53 | return this.extCtx.subscriptions; | ||
54 | } | ||
55 | |||
52 | pushCleanup(d: { dispose(): any }) { | 56 | pushCleanup(d: { dispose(): any }) { |
53 | this.extCtx.subscriptions.push(d); | 57 | this.extCtx.subscriptions.push(d); |
54 | } | 58 | } |
diff --git a/editors/code/src/events/change_active_text_editor.ts b/editors/code/src/events/change_active_text_editor.ts index 74b91bd48..4384ee567 100644 --- a/editors/code/src/events/change_active_text_editor.ts +++ b/editors/code/src/events/change_active_text_editor.ts | |||
@@ -1,21 +1,14 @@ | |||
1 | import { TextEditor } from 'vscode'; | 1 | import { TextEditor } from 'vscode'; |
2 | import { TextDocumentIdentifier } from 'vscode-languageclient'; | 2 | import { TextDocumentIdentifier } from 'vscode-languageclient'; |
3 | |||
4 | import { | ||
5 | SyntaxTreeContentProvider, | ||
6 | syntaxTreeUri, | ||
7 | } from '../commands/syntaxTree'; | ||
8 | import { Decoration } from '../highlighting'; | 3 | import { Decoration } from '../highlighting'; |
9 | import { Server } from '../server'; | 4 | import { Server } from '../server'; |
10 | 5 | ||
11 | export function makeHandler(syntaxTreeProvider: SyntaxTreeContentProvider) { | 6 | export function makeHandler() { |
12 | return async function handle(editor: TextEditor | undefined) { | 7 | return async function handle(editor: TextEditor | undefined) { |
13 | if (!editor || editor.document.languageId !== 'rust') { | 8 | if (!editor || editor.document.languageId !== 'rust') { |
14 | return; | 9 | return; |
15 | } | 10 | } |
16 | 11 | ||
17 | syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri); | ||
18 | |||
19 | if (!Server.config.highlightingOn) { | 12 | if (!Server.config.highlightingOn) { |
20 | return; | 13 | return; |
21 | } | 14 | } |
diff --git a/editors/code/src/events/change_text_document.ts b/editors/code/src/events/change_text_document.ts deleted file mode 100644 index 2e998e889..000000000 --- a/editors/code/src/events/change_text_document.ts +++ /dev/null | |||
@@ -1,24 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | |||
3 | import { | ||
4 | SyntaxTreeContentProvider, | ||
5 | syntaxTreeUri, | ||
6 | } from '../commands/syntaxTree'; | ||
7 | |||
8 | export function createHandler(syntaxTreeProvider: SyntaxTreeContentProvider) { | ||
9 | return (event: vscode.TextDocumentChangeEvent) => { | ||
10 | const doc = event.document; | ||
11 | if (doc.languageId !== 'rust') { | ||
12 | return; | ||
13 | } | ||
14 | afterLs(() => { | ||
15 | syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri); | ||
16 | }); | ||
17 | }; | ||
18 | } | ||
19 | |||
20 | // We need to order this after LS updates, but there's no API for that. | ||
21 | // Hence, good old setTimeout. | ||
22 | function afterLs(f: () => any) { | ||
23 | setTimeout(f, 10); | ||
24 | } | ||
diff --git a/editors/code/src/events/index.ts b/editors/code/src/events/index.ts index 4c154563f..be135474d 100644 --- a/editors/code/src/events/index.ts +++ b/editors/code/src/events/index.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | import * as changeActiveTextEditor from './change_active_text_editor'; | 1 | import * as changeActiveTextEditor from './change_active_text_editor'; |
2 | import * as changeTextDocument from './change_text_document'; | ||
3 | 2 | ||
4 | export { changeActiveTextEditor, changeTextDocument }; | 3 | export { changeActiveTextEditor }; |
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 55fedd8bb..d92cd164f 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -4,7 +4,6 @@ import * as lc from 'vscode-languageclient'; | |||
4 | import * as commands from './commands'; | 4 | import * as commands from './commands'; |
5 | import { ExpandMacroContentProvider } from './commands/expand_macro'; | 5 | import { ExpandMacroContentProvider } from './commands/expand_macro'; |
6 | import { HintsUpdater } from './commands/inlay_hints'; | 6 | import { HintsUpdater } from './commands/inlay_hints'; |
7 | import { SyntaxTreeContentProvider } from './commands/syntaxTree'; | ||
8 | import { StatusDisplay } from './commands/watch_status'; | 7 | import { StatusDisplay } from './commands/watch_status'; |
9 | import * as events from './events'; | 8 | import * as events from './events'; |
10 | import * as notifications from './notifications'; | 9 | import * as notifications from './notifications'; |
@@ -20,6 +19,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
20 | ctx.registerCommand('matchingBrace', commands.matchingBrace); | 19 | ctx.registerCommand('matchingBrace', commands.matchingBrace); |
21 | ctx.registerCommand('joinLines', commands.joinLines); | 20 | ctx.registerCommand('joinLines', commands.joinLines); |
22 | ctx.registerCommand('parentModule', commands.parentModule); | 21 | ctx.registerCommand('parentModule', commands.parentModule); |
22 | ctx.registerCommand('syntaxTree', commands.syntaxTree); | ||
23 | 23 | ||
24 | function disposeOnDeactivation(disposable: vscode.Disposable) { | 24 | function disposeOnDeactivation(disposable: vscode.Disposable) { |
25 | context.subscriptions.push(disposable); | 25 | context.subscriptions.push(disposable); |
@@ -55,10 +55,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
55 | disposeOnDeactivation(watchStatus); | 55 | disposeOnDeactivation(watchStatus); |
56 | 56 | ||
57 | // Notifications are events triggered by the language server | 57 | // Notifications are events triggered by the language server |
58 | const allNotifications: Iterable<[ | 58 | const allNotifications: [string, lc.GenericNotificationHandler][] = [ |
59 | string, | ||
60 | lc.GenericNotificationHandler, | ||
61 | ]> = [ | ||
62 | [ | 59 | [ |
63 | 'rust-analyzer/publishDecorations', | 60 | 'rust-analyzer/publishDecorations', |
64 | notifications.publishDecorations.handle, | 61 | notifications.publishDecorations.handle, |
@@ -68,42 +65,25 @@ export async function activate(context: vscode.ExtensionContext) { | |||
68 | params => watchStatus.handleProgressNotification(params), | 65 | params => watchStatus.handleProgressNotification(params), |
69 | ], | 66 | ], |
70 | ]; | 67 | ]; |
71 | const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); | ||
72 | const expandMacroContentProvider = new ExpandMacroContentProvider(); | 68 | const expandMacroContentProvider = new ExpandMacroContentProvider(); |
73 | 69 | ||
74 | // The events below are plain old javascript events, triggered and handled by vscode | 70 | // The events below are plain old javascript events, triggered and handled by vscode |
75 | vscode.window.onDidChangeActiveTextEditor( | 71 | vscode.window.onDidChangeActiveTextEditor( |
76 | events.changeActiveTextEditor.makeHandler(syntaxTreeContentProvider), | 72 | events.changeActiveTextEditor.makeHandler(), |
77 | ); | 73 | ); |
78 | 74 | ||
79 | disposeOnDeactivation( | 75 | disposeOnDeactivation( |
80 | vscode.workspace.registerTextDocumentContentProvider( | 76 | vscode.workspace.registerTextDocumentContentProvider( |
81 | 'rust-analyzer', | 77 | 'rust-analyzer', |
82 | syntaxTreeContentProvider, | ||
83 | ), | ||
84 | ); | ||
85 | disposeOnDeactivation( | ||
86 | vscode.workspace.registerTextDocumentContentProvider( | ||
87 | 'rust-analyzer', | ||
88 | expandMacroContentProvider, | 78 | expandMacroContentProvider, |
89 | ), | 79 | ), |
90 | ); | 80 | ); |
91 | 81 | ||
92 | registerCommand( | 82 | registerCommand( |
93 | 'rust-analyzer.syntaxTree', | ||
94 | commands.syntaxTree.createHandle(syntaxTreeContentProvider), | ||
95 | ); | ||
96 | registerCommand( | ||
97 | 'rust-analyzer.expandMacro', | 83 | 'rust-analyzer.expandMacro', |
98 | commands.expandMacro.createHandle(expandMacroContentProvider), | 84 | commands.expandMacro.createHandle(expandMacroContentProvider), |
99 | ); | 85 | ); |
100 | 86 | ||
101 | vscode.workspace.onDidChangeTextDocument( | ||
102 | events.changeTextDocument.createHandler(syntaxTreeContentProvider), | ||
103 | null, | ||
104 | context.subscriptions, | ||
105 | ); | ||
106 | |||
107 | const startServer = () => Server.start(allNotifications); | 87 | const startServer = () => Server.start(allNotifications); |
108 | const reloadCommand = () => reloadServer(startServer); | 88 | const reloadCommand = () => reloadServer(startServer); |
109 | 89 | ||