xref: /llvm-project/mlir/utils/vscode/src/PDLL/commands/viewOutput.ts (revision f7b8a70e7a1738e0fc6574e3cf8faa4fa1f34eba)
1import * as vscode from 'vscode'
2
3import {Command} from '../../command';
4import {MLIRContext} from '../../mlirContext';
5
6/**
7 * The parameters to the pdll/viewOutput command. These parameters are:
8 * - `uri`: The URI of the file to view.
9 * - `kind`: The kind of the output to generate.
10 */
11type ViewOutputParams = Partial<{uri : string, kind : string}>;
12
13/**
14 * The output of the commands:
15 * - `output`: The output string of the command, e.g. a .mlir PDL string.
16 */
17type ViewOutputResult = Partial<{output : string}>;
18
19/**
20 * A command that displays the output of the current PDLL document.
21 */
22export class ViewPDLLCommand extends Command {
23  constructor(context: MLIRContext) { super('mlir.viewPDLLOutput', context); }
24
25  async execute() {
26    const editor = vscode.window.activeTextEditor;
27    if (editor.document.languageId != 'pdll')
28      return;
29
30    // Check to see if a language client is active for this document.
31    const pdllClient =
32        this.context.getLanguageClient(editor.document.uri, "pdll");
33    if (!pdllClient) {
34      return;
35    }
36
37    // Ask the user for the desired output type.
38    const outputType =
39        await vscode.window.showQuickPick([ 'ast', 'mlir', 'cpp' ]);
40    if (!outputType) {
41      return;
42    }
43
44    // If we have the language client, ask it to try compiling the document.
45    let outputParams: ViewOutputParams = {
46      uri : editor.document.uri.toString(),
47      kind : outputType,
48    };
49    const result: ViewOutputResult|undefined =
50        await pdllClient.sendRequest('pdll/viewOutput', outputParams);
51    if (!result || result.output.length === 0) {
52      return;
53    }
54
55    // Display the output in a new editor.
56    let outputFileType = 'plaintext';
57    if (outputType == 'mlir') {
58      outputFileType = 'mlir';
59    } else if (outputType == 'cpp') {
60      outputFileType = 'cpp';
61    }
62    await vscode.workspace.openTextDocument(
63        {language : outputFileType, content : result.output});
64  }
65}
66