All files / src/commands previewCodeCommand.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                5x 5x 1x 1x     4x 4x 5x 5x 5x 5x   5x      
import * as vscode from "vscode";
import { CanvasPanel, type PreviewCodeFile } from "../canvas/CanvasPanel";
import { getOutputDirectory } from "../config/ConfigReader";
import { generatePreviewJavaFiles } from "../generator/JavaGenerator";
import { inferJavaPackage, resolveOutputDirectory } from "../utils/JavaPackageInference";
import { detectJavaProject } from "../utils/JavaProjectDetector";
 
export function registerPreviewCodeCommand(): vscode.Disposable {
  return vscode.commands.registerCommand("swingGuiBuilder.previewCode", async () => {
    if (!CanvasPanel.currentPanel) {
      vscode.window.showErrorMessage("No canvas is open. Open a canvas first.");
      return;
    }
 
    const state = CanvasPanel.currentPanel.getCanvasState();
    const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
    const projectStructure = workspaceRoot ? detectJavaProject(workspaceRoot) : undefined;
    const outputDir = resolveOutputDirectory(getOutputDirectory(), projectStructure);
    const javaPackage = inferJavaPackage(outputDir, projectStructure);
    const previewFiles: PreviewCodeFile[] = generatePreviewJavaFiles(state, javaPackage);
 
    await CanvasPanel.currentPanel.postPreviewCode(previewFiles);
  });
}