|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { LSPClient } from '../lsp/lsp' |
| 3 | +import { isErr } from '@bus/result' |
| 4 | +import { RenderModelEntry } from '../lsp/custom' |
| 5 | + |
| 6 | +export function renderModel(lspClient?: LSPClient) { |
| 7 | + return async () => { |
| 8 | + // Get the current active editor |
| 9 | + const activeEditor = vscode.window.activeTextEditor |
| 10 | + |
| 11 | + if (!activeEditor) { |
| 12 | + vscode.window.showErrorMessage('No active editor found') |
| 13 | + return |
| 14 | + } |
| 15 | + |
| 16 | + if (!lspClient) { |
| 17 | + vscode.window.showErrorMessage('LSP client not available') |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + // Get the current document URI |
| 22 | + const documentUri = activeEditor.document.uri.toString(true) |
| 23 | + |
| 24 | + // Call the render model API |
| 25 | + const result = await lspClient.call_custom_method('sqlmesh/render_model', { |
| 26 | + textDocumentUri: documentUri, |
| 27 | + }) |
| 28 | + |
| 29 | + if (isErr(result)) { |
| 30 | + vscode.window.showErrorMessage(`Failed to render model: ${result.error}`) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // Check if we got any models |
| 35 | + if (!result.value.models || result.value.models.length === 0) { |
| 36 | + vscode.window.showInformationMessage( |
| 37 | + 'No models found in the current file', |
| 38 | + ) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + // If multiple models, let user choose |
| 43 | + let selectedModel: RenderModelEntry |
| 44 | + if (result.value.models.length > 1) { |
| 45 | + const items = result.value.models.map((model: RenderModelEntry) => ({ |
| 46 | + label: model.name, |
| 47 | + description: model.fqn, |
| 48 | + detail: model.description, |
| 49 | + model: model, |
| 50 | + })) |
| 51 | + |
| 52 | + const selected = await vscode.window.showQuickPick(items, { |
| 53 | + placeHolder: 'Select a model to render', |
| 54 | + }) |
| 55 | + |
| 56 | + if (!selected) { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + selectedModel = selected.model |
| 61 | + } else { |
| 62 | + selectedModel = result.value.models[0] |
| 63 | + } |
| 64 | + |
| 65 | + // Create a new untitled document with the rendered SQL |
| 66 | + const document = await vscode.workspace.openTextDocument({ |
| 67 | + language: 'sql', |
| 68 | + content: selectedModel.rendered_query, |
| 69 | + }) |
| 70 | + |
| 71 | + // Determine the view column for side-by-side display |
| 72 | + let viewColumn: vscode.ViewColumn |
| 73 | + if (activeEditor) { |
| 74 | + // Open beside the current editor |
| 75 | + viewColumn = activeEditor.viewColumn |
| 76 | + ? activeEditor.viewColumn + 1 |
| 77 | + : vscode.ViewColumn.Two |
| 78 | + } else { |
| 79 | + // If no active editor, open in column two |
| 80 | + viewColumn = vscode.ViewColumn.Two |
| 81 | + } |
| 82 | + |
| 83 | + // Open the document in the editor as a preview (preview: true is default) |
| 84 | + await vscode.window.showTextDocument(document, { |
| 85 | + viewColumn: viewColumn, |
| 86 | + preview: true, |
| 87 | + preserveFocus: false, |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments