import { REDSPOT_PARAM_DEFINITIONS } from '../core/params/redspot-params' ; import type redspot from '../lib/redspot-lib' ; import { ArgumentsParser } from './ArgumentsParser' ; interface CompletionEnv { line : string ; point : number ; } export async function complete ( { line , point } ) { let hre ; try { process . env . TS_NODE_TRANSPILE_ONLY = '1' ; require ( '../../register' ) ; const { RedspotContext } = require ( '../context' ) ; const context = RedspotContext . getRedspotContext ( ) ; hre = context . getRedspotRuntimeEnvironment ( ) ; } catch ( e ) { return [ ] ; } const words = line . split ( /\s+/ ) . filter ( ( x ) => x . length > 0 ) ; const wordsBeforeCursor = line . slice ( 0 , point ) . split ( /\s+/ ) ; const [ prev , last ] = wordsBeforeCursor . slice ( - 2 ) ; const coreParams = Object . values ( REDSPOT_PARAM_DEFINITIONS ) . map ( ( x ) => x . name ) . map ( ArgumentsParser . paramNameToCLA ) . filter ( ( x ) => ! words . includes ( x ) ) ; let task ; let index = 1 ; while ( index < words . length ) { if ( isGlobalFlag ( words [ index ] ) ) { index += 1 ; } else if ( isGlobalParam ( words [ index ] ) ) { index += 2 ; } else if ( words [ index ] . startsWith ( '--' ) ) { index += 1 ; } else { task = words [ index ] ; break ; } } if ( task === last ) { task = undefined ; } if ( prev === '--network' ) { return Object . keys ( hre . config . networks ) ; } if ( prev . startsWith ( '-' ) ) { const paramName = ArgumentsParser . cLAToParamName ( prev ) ; const globalParam = ( REDSPOT_PARAM_DEFINITIONS as any ) [ paramName ] ; if ( globalParam !== undefined && ! globalParam . isFlag ) { return [ ] ; } } if ( task === undefined || hre . tasks [ task ] === undefined ) { const tasks = Object . values ( hre . tasks ) . map ( ( x ) => x . name ) . filter ( ( x ) => ! x . includes ( ':' ) ) ; if ( last . startsWith ( '-' ) ) { return coreParams ; } return tasks ; } if ( ! last . startsWith ( '-' ) ) { return [ ] ; } const taskParams = Object . values ( hre . tasks [ task ] . paramDefinitions ) . map ( ( x ) => x . name ) . map ( ArgumentsParser . paramNameToCLA ) . filter ( ( x ) => ! words . includes ( x ) ) ; return [ ... taskParams , ... coreParams ] ; } function isGlobalFlag ( param ) { const paramName = ArgumentsParser . cLAToParamName ( param ) ; return ( REDSPOT_PARAM_DEFINITIONS as any ) [ paramName ] ?. isFlag === true ; } function isGlobalParam ( param ) { const paramName = ArgumentsParser . cLAToParamName ( param ) ; return ( REDSPOT_PARAM_DEFINITIONS as any ) [ paramName ] ?. isFlag === false ; }