'use strict' ; import * as path from 'path' ; import { Node } from 'jsonc-parser' ; import { ExtensionContext , Uri , workspace , window , Range , TextDocument , Position , FileSystem } from 'vscode' ; import { FileType , PddlFileSystem , utils , PddlExtensionContext , PddlRange , PddlPosition , parser } from 'pddl-workspace' ; import { URI } from 'vscode-uri' ; export function createPddlExtensionContext ( context ) { return { asAbsolutePath : context . asAbsolutePath , extensionPath : context . extensionPath , storagePath : context . storagePath , storageUri : context . storageUri , subscriptions : context . subscriptions , pythonPath : function ( ) : string { return workspace . getConfiguration ( ) . get ( "python.pythonPath" , "python" ) ; } } ; } export function toPddlFileSystem ( fileSystem ) { return { readDirectory : async ( uri ) : Promise < [ string , FileType ] [ ] > => await fileSystem . readDirectory ( uri ) , readFile : async ( uri ) => await fileSystem . readFile ( uri ) } ; } export interface WebviewUriConverter { asWebviewUri ( localResource ) : Uri ; } export function sleep ( ms ) { return new Promise ( resolve => { setTimeout ( resolve , ms ) ; } ) ; } export function firstIndex < T > ( array , fn ) { for ( let i = 0 ; i < array . length ; i ++ ) { if ( fn ( array [ i ] ) ) { return i ; } } return - 1 ; } export function compareMaps ( map1 , map2 ) { let testVal ; if ( map1 . size !== map2 . size ) { return false ; } for ( const [ key , val ] of map1 ) { testVal = map2 . get ( key ) ; if ( testVal !== val || ( testVal === undefined && ! map2 . has ( key ) ) ) { return false ; } } return true ; } param time export 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 ; } export function showError ( reason ) { console . error ( reason ) ; window . showErrorMessage ( reason . message ) ; } export function throwForUndefined < T > ( part ) { throw new Error ( `No ${ part } defined.` ) ; } export function assertDefined < T > ( value , message ) { if ( value === undefined || value === null ) { throw new Error ( "Assertion error: " + message ) ; } else { return value ; } } param configuredPath export function ensureAbsoluteGlobalStoragePath ( configuredPath , context ) { if ( ! configuredPath ) { return undefined ; } if ( isHttp ( configuredPath ) ) { return configuredPath ; } if ( path . isAbsolute ( configuredPath ) || ! path . dirname ( configuredPath ) ) { return configuredPath ; } else { if ( configuredPath . startsWith ( path . join ( 'val' , 'Val-' ) ) ) { return path . join ( context . globalStoragePath , configuredPath ) ; } else { return configuredPath ; } } } export function isHttp ( path ) { return path . match ( /^http[s]?:/i ) !== null ; } export function toURI ( uri ) { return URI . parse ( uri . toString ( ) ) ; } export function toUri ( uri ) { return Uri . parse ( uri . toString ( ) ) ; } export function toRange ( pddlRange ) { return new Range ( toPosition ( pddlRange . start ) , toPosition ( pddlRange . end ) ) ; } export function nodeToRange ( document , node ) { return new Range ( document . positionAt ( node . getStart ( ) ) , document . positionAt ( node . getEnd ( ) ) ) ; } export function toPosition ( position ) { return new Position ( position . line , position . character ) ; } export function jsonNodeToRange ( document , node ) { return new Range ( document . positionAt ( node . offset ) , document . positionAt ( node . offset + node . length ) ) ; } export class UriMap < T > extends utils . StringifyingMap < Uri , T > { protected stringifyKey ( key ) { return key . toString ( ) ; } } export async function fileOrFolderExists ( fileOrFolderUri ) { try { await workspace . fs . stat ( fileOrFolderUri ) ; return true ; } catch ( err ) { return false ; } }