All files / src/commands openCommand.ts

100% Statements 32/32
100% Branches 22/22
100% Functions 4/4
100% Lines 32/32

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76          4x                   7x 2x     5x 5x 2x     3x 3x 2x     1x             12x 12x 12x 1x 1x     11x       11x 11x 4x 4x 12x 12x   7x 4x 4x     4x       3x 3x 3x       8x 8x 7x        
import * as vscode from "vscode";
import { CanvasPanel } from "../canvas/CanvasPanel";
import type { CanvasState } from "../components/ComponentModel";
 
function createDefaultCanvasState(): CanvasState {
  return {
    className: "MainWindow",
    frameTitle: "MainWindow",
    frameWidth: 800,
    frameHeight: 600,
    components: [],
  };
}
 
function isMissingLayoutError(error: unknown): boolean {
  if (!error || typeof error !== "object") {
    return false;
  }
 
  const code = Reflect.get(error, "code");
  if (code === "FileNotFound" || code === "ENOENT") {
    return true;
  }
 
  const message = Reflect.get(error, "message");
  if (typeof message === "string" && /file not found|enoent|not exist|cannot find/i.test(message)) {
    return true;
  }
 
  return false;
}
 
export function registerOpenCommand(
  context: vscode.ExtensionContext,
  outputChannel: vscode.OutputChannel,
): vscode.Disposable {
  return vscode.commands.registerCommand("swingGuiBuilder.open", async () => {
    const workspaceFolders = vscode.workspace.workspaceFolders;
    if (!workspaceFolders) {
      vscode.window.showErrorMessage("No workspace folder open.");
      return;
    }
 
    const filePath = vscode.Uri.joinPath(workspaceFolders[0].uri, ".swingbuilder-layout.json");
 
    let state: CanvasState;
 
    try {
      const fileContent = await vscode.workspace.fs.readFile(filePath);
      const parsedState = JSON.parse(Buffer.from(fileContent).toString("utf-8")) as CanvasState;
      const className = parsedState.className || "MainWindow";
      state = { ...parsedState, className, frameTitle: parsedState.frameTitle ?? className };
      vscode.window.showInformationMessage("Canvas loaded from .swingbuilder-layout.json");
    } catch (error) {
      if (isMissingLayoutError(error)) {
        state = createDefaultCanvasState();
        outputChannel.appendLine(
          "Layout file .swingbuilder-layout.json was not found. Opened a new empty MainWindow layout.",
        );
        vscode.window.showInformationMessage(
          "No .swingbuilder-layout.json found. Opened a new empty MainWindow layout.",
        );
      } else {
        outputChannel.appendLine(`Error opening layout file: ${error}`);
        vscode.window.showErrorMessage("Could not read .swingbuilder-layout.json.");
        return;
      }
    }
 
    CanvasPanel.createOrShow(context.extensionUri, state.className);
    if (CanvasPanel.currentPanel) {
      CanvasPanel.currentPanel.loadState(state);
    }
  });
}