All files / vscode-extension/src extension.ts

100% Statements 34/34
100% Branches 18/18
100% Functions 9/9
100% Lines 30/30

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                13x 13x 13x   4x 4x 4x 4x 4x             4x   4x 3x   4x 2x   4x 2x   4x 1x 1x 1x   4x     13x           3x 3x 1x 1x   2x     4x 4x 4x     13x    
import * as vscode from 'vscode';
import { getPreviewHtml } from './previewHtml.js';
import { createPreviewUpdate } from './previewData.js';
import { isReadyMessage } from './protocol.js';
import { registerConvertCommands } from './convert.js';
import { registerLanguageFeatures } from './languageProviders.js';
 
export function activate(context: vscode.ExtensionContext): void {
  registerConvertCommands(context);
  const mediaRoot = vscode.Uri.joinPath(context.extensionUri, 'media');
  const provider: vscode.CustomTextEditorProvider = {
    async resolveCustomTextEditor(document, panel) {
      panel.webview.options = { enableScripts: true, localResourceRoots: [mediaRoot] };
      const script = panel.webview.asWebviewUri(vscode.Uri.joinPath(mediaRoot, 'preview.js'));
      const style = panel.webview.asWebviewUri(vscode.Uri.joinPath(mediaRoot, 'preview.css'));
      const update = (): void => {
        const message = createPreviewUpdate(
          document.getText(),
          /\.ldt$/i.test(document.uri.path) ? 'ldt' : 'ies',
          document.uri.path.split('/').at(-1),
        );
        // VS Code's Webview.postMessage has no targetOrigin argument.
        // oxlint-disable-next-line unicorn/require-post-message-target-origin
        void panel.webview.postMessage(message);
      };
      const messages = panel.webview.onDidReceiveMessage((message: unknown) => {
        if (isReadyMessage(message)) update();
      });
      const changes = vscode.workspace.onDidChangeTextDocument((event) => {
        if (event.document.uri.toString() === document.uri.toString()) update();
      });
      const visibility = panel.onDidChangeViewState(() => {
        if (panel.visible) update();
      });
      panel.onDidDispose(() => {
        messages.dispose();
        changes.dispose();
        visibility.dispose();
      });
      panel.webview.html = getPreviewHtml(panel.webview.cspSource, script.toString(), style.toString());
    },
  };
  context.subscriptions.push(
    vscode.window.registerCustomEditorProvider('ies.lightPreview', provider, {
      webviewOptions: { retainContextWhenHidden: true },
      supportsMultipleEditorsPerDocument: true,
    }),
    vscode.commands.registerCommand('ies.openPreview', async (uri?: vscode.Uri) => {
      const target = uri ?? vscode.window.activeTextEditor?.document.uri;
      if (!target) {
        void vscode.window.showInformationMessage('Open an IES or LDT file to preview its lighting distribution.');
        return;
      }
      await vscode.commands.executeCommand('vscode.openWith', target, 'ies.lightPreview');
    }),
    vscode.commands.registerCommand('ies.openAsText', async (uri?: vscode.Uri) => {
      const input = vscode.window.tabGroups.activeTabGroup.activeTab?.input;
      const target = uri ?? (input instanceof vscode.TabInputCustom ? input.uri : undefined);
      if (target) await vscode.commands.executeCommand('vscode.openWith', target, 'default');
    }),
  );
  registerLanguageFeatures(context);
}