All files / src vscodeNative.js

59.09% Statements 26/44
47.61% Branches 10/21
47.05% Functions 8/17
66.66% Lines 26/39

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 77 78 79 80 81 82 83 84 85 869x 9x   9x 6x 15x 15x 15x 15x 5x 5x             5x 5x 5x 5x 5x       9x       1x 1x       1x 1x       2x       2x                 1x 1x               1x 1x                                              
const isVscode = typeof window.vscode !== 'undefined';
const pendingDialogs = new Map();
 
if (isVscode) {
  window.addEventListener('message', (event) => {
    const msg = event.data;
    Eif (['dialogResult', 'secretValue', 'secretStored', 'configValues'].includes(msg.type) && msg.id) {
      const handler = pendingDialogs.get(msg.id);
      if (handler) {
        handler(msg);
        pendingDialogs.delete(msg.id);
      }
    }
  });
}
 
function postAndWait(message) {
  Iif (!isVscode) return Promise.resolve({ result: undefined });
  return new Promise((resolve) => {
    const id = Math.random().toString(36).substr(2, 9);
    pendingDialogs.set(id, resolve);
    window.vscode.postMessage({ ...message, id });
  });
}
 
export const vscodeNative = {
  isVscode,
 
  notify(message, level = 'info') {
    Iif (!isVscode) return;
    window.vscode.postMessage({ type: 'notify', message, level });
  },
 
  async confirm(message, ...actions) {
    const result = await postAndWait({ type: 'showConfirm', message, actions });
    return result.result;
  },
 
  async pickFolder() {
    const result = await postAndWait({
      type: 'showOpenDialog',
      options: { canSelectFolders: true, canSelectFiles: false, canSelectMany: false },
    });
    return result.result?.[0];
  },
 
  async quickPick(items, options = {}) {
    const result = await postAndWait({ type: 'showQuickPick', items, options });
    return result.result;
  },
 
  async getSecret(key) {
    const result = await postAndWait({ type: 'getSecret', key });
    return result.value;
  },
 
  async setSecret(key, value) {
    await postAndWait({ type: 'setSecret', key, value });
  },
 
  async getConfig() {
    const result = await postAndWait({ type: 'getConfig' });
    return result.values;
  },
 
  startProgress(title, cancellable = false) {
    if (!isVscode) return { report() {}, done() {} };
    const id = Math.random().toString(36).substr(2, 9);
    window.vscode.postMessage({ type: 'showProgress', id, title, cancellable });
    return {
      report: (report) => window.vscode.postMessage({ type: 'progressUpdate', id, report }),
      done: () => window.vscode.postMessage({ type: 'progressDone', id }),
    };
  },
 
  updateStatusBar(updates) {
    if (!isVscode) return;
    window.vscode.postMessage({ type: 'updateStatusBar', ...updates });
  },
 
  executeCommand(command, ...args) {
    if (!isVscode) return;
    window.vscode.postMessage({ type: 'openExternal', command, args });
  },
};