import * as child_process from 'child_process' ; import { promises as fs } from 'fs' ; import { URL } from 'url' ; import { promisify } from 'util' ; import { TextDocument } from 'vscode-languageserver-textdocument' ; import { Range } from 'vscode-languageserver-types' ; import * as path from 'path' ; export async function fileExists ( fileUri ) { return ! ! ( await fs . stat ( new URL ( fileUri ) ) . catch ( ( ) => false ) ) ; } export const asyncExec = promisify ( child_process . exec ) ; export function toLspRange ( range , textDocument ) { const start = textDocument . positionAt ( range [ 0 ] ) ; const end = textDocument . positionAt ( range [ 1 ] ) ; return Range . create ( start , end ) ; } export function hasOwnProperty < X , Y extends PropertyKey > ( obj , prop ) { return isObject ( obj ) && obj . hasOwnProperty ( prop ) ; } /**
 * Checks whether `obj` is a non-null object.
 */ export function isObject < X > ( obj ) { return obj && typeof obj === 'object' ; } export function insert ( str , index , val ) { return `${ str . substring ( 0 , index ) }${ val }${ str . substring ( index ) }` ; } /**
 * Adjusts the command and environment in case the interpreter path is provided.
 */ export function withInterpreter ( executable , args , interpreterPath , activationScript ) { let command = `${ executable } ${ args }` ; if ( activationScript ) { command = `bash -c 'source ${ activationScript } && ${ executable } ${ args }'` ; return [ command , undefined ] ; } if ( interpreterPath ) { const virtualEnv = path . resolve ( interpreterPath , '../..' ) ; const pathEntry = path . join ( virtualEnv , 'bin' ) ; if ( path . isAbsolute ( executable ) ) { command = `${ interpreterPath } ${ executable } ${ args }` ; } const envOverride = { VIRTUAL_ENV : virtualEnv , PATH : `${ pathEntry }:${ process . env . PATH }` , } ; const newEnv = Object . assign ( { } , process . env , envOverride ) ; delete newEnv . PYTHONHOME ; return [ command , newEnv ] ; } else { return [ command , undefined ] ; } }