/* -------------------------------------------------------------------------------------------- * Copyright (c) Jan Dolejsi. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileOrFolderExists = exports.UriMap = exports.jsonNodeToRange = exports.toPosition = exports.nodeToRange = exports.toRange = exports.toUri = exports.toURI = exports.isHttp = exports.ensureAbsoluteGlobalStoragePath = exports.assertDefined = exports.throwForUndefined = exports.showError = exports.toFuzzyRelativeTime = exports.compareMaps = exports.firstIndex = exports.sleep = exports.toPddlFileSystem = exports.createPddlExtensionContext = void 0; const path = require("path"); const vscode_1 = require("vscode"); const pddl_workspace_1 = require("pddl-workspace"); const vscode_uri_1 = require("vscode-uri"); function createPddlExtensionContext(context) { return { asAbsolutePath: context.asAbsolutePath, extensionPath: context.extensionPath, storagePath: context.storagePath, storageUri: context.storageUri, subscriptions: context.subscriptions, pythonPath: function () { return vscode_1.workspace.getConfiguration().get("python.pythonPath", "python"); } }; } exports.createPddlExtensionContext = createPddlExtensionContext; function toPddlFileSystem(fileSystem) { return { readDirectory: async (uri) => await fileSystem.readDirectory(uri), readFile: async (uri) => await fileSystem.readFile(uri) }; } exports.toPddlFileSystem = toPddlFileSystem; function sleep(ms) { return new Promise(resolve => { setTimeout(resolve, ms); }); } exports.sleep = sleep; function firstIndex(array, fn) { for (let i = 0; i < array.length; i++) { if (fn(array[i])) { return i; } } return -1; } exports.firstIndex = firstIndex; function compareMaps(map1, map2) { let testVal; if (map1.size !== map2.size) { return false; } for (const [key, val] of map1) { testVal = map2.get(key); // in cases of an undefined value, make sure the key // actually exists on the object so there are no false positives if (testVal !== val || (testVal === undefined && !map2.has(key))) { return false; } } return true; } exports.compareMaps = compareMaps; /** * Relative fuzzy time in a human readable form. * @param time date time */ function toFuzzyRelativeTime(time) { const delta = Math.round((+new Date - time) / 1000); const minute = 60, hour = minute * 60, day = hour * 24; let fuzzy; if (delta < 30) { fuzzy = 'just now'; } else if (delta < minute) { fuzzy = delta + ' seconds ago'; } else if (delta < 2 * minute) { fuzzy = 'a minute ago'; } else if (delta < hour) { fuzzy = Math.floor(delta / minute) + ' minutes ago'; } else if (Math.floor(delta / hour) === 1) { fuzzy = '1 hour ago'; } else if (delta < day) { fuzzy = Math.floor(delta / hour) + ' hours ago'; } else if (delta < day * 2) { fuzzy = 'yesterday'; } else { fuzzy = Math.floor(delta / day) + ' days ago'; } return fuzzy; } exports.toFuzzyRelativeTime = toFuzzyRelativeTime; function showError(reason) { console.error(reason); vscode_1.window.showErrorMessage(reason.message); } exports.showError = showError; function throwForUndefined(part) { throw new Error(`No ${part} defined.`); } exports.throwForUndefined = throwForUndefined; function assertDefined(value, message) { if (value === undefined || value === null) { throw new Error("Assertion error: " + message); } else { return value; } } exports.assertDefined = assertDefined; /** * Absolute path, unless it relied on a %path% location (i.e. there was no dirname). * * This is here merely for background compatibility with previous val configuration storage * @param configuredPath a configured path to an executable */ function ensureAbsoluteGlobalStoragePath(configuredPath, context) { if (!configuredPath) { return undefined; } if (isHttp(configuredPath)) { return configuredPath; } // if the path is absolute, or contains just the name of an executable (obviously relying on the %path&), return it as is if (path.isAbsolute(configuredPath) || !path.dirname(configuredPath)) { return configuredPath; } else { // this is here merely for background compatibility with previous val configuration storage if (configuredPath.startsWith(path.join('val', 'Val-'))) { return path.join(context.globalStoragePath, configuredPath); } else { return configuredPath; } } } exports.ensureAbsoluteGlobalStoragePath = ensureAbsoluteGlobalStoragePath; function isHttp(path) { return path.match(/^http[s]?:/i) !== null; } exports.isHttp = isHttp; function toURI(uri) { return vscode_uri_1.URI.parse(uri.toString()); } exports.toURI = toURI; function toUri(uri) { return vscode_1.Uri.parse(uri.toString()); } exports.toUri = toUri; function toRange(pddlRange) { return new vscode_1.Range(toPosition(pddlRange.start), toPosition(pddlRange.end)); } exports.toRange = toRange; function nodeToRange(document, node) { return new vscode_1.Range(document.positionAt(node.getStart()), document.positionAt(node.getEnd())); } exports.nodeToRange = nodeToRange; function toPosition(position) { return new vscode_1.Position(position.line, position.character); } exports.toPosition = toPosition; function jsonNodeToRange(document, node) { return new vscode_1.Range(document.positionAt(node.offset), document.positionAt(node.offset + node.length)); } exports.jsonNodeToRange = jsonNodeToRange; class UriMap extends pddl_workspace_1.utils.StringifyingMap { stringifyKey(key) { return key.toString(); } } exports.UriMap = UriMap; async function fileOrFolderExists(fileOrFolderUri) { try { await vscode_1.workspace.fs.stat(fileOrFolderUri); return true; } catch (err) { return false; } } exports.fileOrFolderExists = fileOrFolderExists; //# sourceMappingURL=utils.js.map