]> git.lizzy.rs Git - rust.git/commitdiff
Use DocumentProvider instead of Hover
authorEdwin Cheng <edwin0cheng@gmail.com>
Tue, 19 Nov 2019 17:06:10 +0000 (01:06 +0800)
committerEdwin Cheng <edwin0cheng@gmail.com>
Tue, 19 Nov 2019 17:06:10 +0000 (01:06 +0800)
editors/code/package.json
editors/code/src/commands/expand_macro.ts
editors/code/src/extension.ts

index ee997e58f5d09c099f63210215e65efd1419c345..94887674ba3309ad5d28352c285d28c1c2f7e7b5 100644 (file)
                 "title": "Show Syntax Tree",
                 "category": "Rust Analyzer"
             },
+            {
+                "command": "rust-analyzer.expandMacro",
+                "title": "Expand macro recursively",
+                "category": "Rust Analyzer"
+            },
             {
                 "command": "rust-analyzer.matchingBrace",
                 "title": "Find matching brace",
index 3fc3e0391725f7a321e8464206ab3827fde3b059..1fa2cf7393feaeabe960d444995a3d545ecf09e4 100644 (file)
@@ -2,47 +2,82 @@ import * as vscode from 'vscode';
 import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
 import { Server } from '../server';
 
-interface ExpandedMacro {
-    name: string,
-    expansion: string,
-}
+export const expandMacroUri = vscode.Uri.parse(
+    'rust-analyzer://expandMacro/[EXPANSION].rs'
+);
 
-function code_format(expanded: ExpandedMacro): vscode.MarkdownString {
-    const markdown = new vscode.MarkdownString(
-        `#### Recursive expansion of ${expanded.name}! macro`
-    );
-    markdown.appendCodeblock(expanded.expansion, 'rust');
-    return markdown;
-}
+export class ExpandMacroContentProvider
+    implements vscode.TextDocumentContentProvider {
+    public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
 
-export class ExpandMacroHoverProvider implements vscode.HoverProvider {
-    public provideHover(
-        document: vscode.TextDocument,
-        position: vscode.Position,
-        token: vscode.CancellationToken
-    ): Thenable<vscode.Hover | null> | null {
+    public provideTextDocumentContent(
+        uri: vscode.Uri
+    ): vscode.ProviderResult<string> {
         async function handle() {
+            const editor = vscode.window.activeTextEditor;
+            if (editor == null) {
+                return '';
+            }
+
+            const position = editor.selection.active;
             const request: MacroExpandParams = {
-                textDocument: { uri: document.uri.toString() },
+                textDocument: { uri: editor.document.uri.toString() },
                 position
             };
-            const result = await Server.client.sendRequest<ExpandedMacro>(
+            const expanded = await Server.client.sendRequest<ExpandedMacro>(
                 'rust-analyzer/expandMacro',
                 request
             );
-            if (result != null) {
-                const formated = code_format(result);
-                return new vscode.Hover(formated);
+
+            if (expanded == null) {
+                return 'Not available';
             }
 
-            return null;
+            return code_format(expanded);
         }
 
         return handle();
     }
+
+    get onDidChange(): vscode.Event<vscode.Uri> {
+        return this.eventEmitter.event;
+    }
+}
+
+// Opens the virtual file that will show the syntax tree
+//
+// The contents of the file come from the `TextDocumentContentProvider`
+export function createHandle(provider: ExpandMacroContentProvider) {
+    return async () => {
+        const uri = expandMacroUri;
+
+        const document = await vscode.workspace.openTextDocument(uri);
+
+        provider.eventEmitter.fire(uri);
+
+        return vscode.window.showTextDocument(
+            document,
+            vscode.ViewColumn.Two,
+            true
+        );
+    };
 }
 
 interface MacroExpandParams {
     textDocument: TextDocumentIdentifier;
     position: Position;
 }
+
+interface ExpandedMacro {
+    name: string;
+    expansion: string;
+}
+
+function code_format(expanded: ExpandedMacro): string {
+    let result = `// Recursive expansion of ${expanded.name}! macro\n`;
+    result += '='.repeat(result.length);
+    result += '\n\n';
+    result += expanded.expansion;
+
+    return result;
+}
index 8654b603007bcbf3f30fd80fb6c7c7d0843f263b..683497dfd4dc9d7e3911b47b5f835c3049cd40af 100644 (file)
@@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient';
 
 import * as commands from './commands';
 import { CargoWatchProvider } from './commands/cargo_watch';
-import { ExpandMacroHoverProvider } from './commands/expand_macro';
+import { ExpandMacroContentProvider } from './commands/expand_macro';
 import { HintsUpdater } from './commands/inlay_hints';
 import {
     interactivelyStartCargoWatch,
@@ -98,6 +98,7 @@ export function activate(context: vscode.ExtensionContext) {
         ]
     ];
     const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
+    const expandMacroContentProvider = new ExpandMacroContentProvider();
 
     // The events below are plain old javascript events, triggered and handled by vscode
     vscode.window.onDidChangeActiveTextEditor(
@@ -110,11 +111,21 @@ export function activate(context: vscode.ExtensionContext) {
             syntaxTreeContentProvider
         )
     );
+    disposeOnDeactivation(
+        vscode.workspace.registerTextDocumentContentProvider(
+            'rust-analyzer',
+            expandMacroContentProvider
+        )
+    );
 
     registerCommand(
         'rust-analyzer.syntaxTree',
         commands.syntaxTree.createHandle(syntaxTreeContentProvider)
     );
+    registerCommand(
+        'rust-analyzer.expandMacro',
+        commands.expandMacro.createHandle(expandMacroContentProvider)
+    );
 
     vscode.workspace.onDidChangeTextDocument(
         events.changeTextDocument.createHandler(syntaxTreeContentProvider),
@@ -122,15 +133,6 @@ export function activate(context: vscode.ExtensionContext) {
         context.subscriptions
     );
 
-    const expandMacroContentProvider = new ExpandMacroHoverProvider();
-
-    disposeOnDeactivation(
-        vscode.languages.registerHoverProvider(
-            'rust',
-            expandMacroContentProvider
-        )
-    );
-
     const startServer = () => Server.start(allNotifications);
     const reloadCommand = () => reloadServer(startServer);