import { Disposable , ShellExecution , Task , TaskGroup , TaskProvider , tasks , workspace , WorkspaceFolder , } from 'vscode' ; /**
 * Displayed identifier associated with each task.
 */ const TASK_SOURCE = 'Rust' ; /**
 * Internal VSCode task type (namespace) under which extensions register their
 * tasks. We only use `cargo` task type.
 */ const TASK_TYPE = 'cargo' ; /**
 * Command execution parameters sent by the RLS (as of 1.35).
 */ export interface Execution { deprecated binary ? : string ; command ? : string ; args : string [ ] ; env ? : { [ key ] : string ; } ; cwd ? : string ; } /**
 * Creates a Task-used `ShellExecution` from a unified `Execution` interface.
 */ function createShellExecution ( execution ) { const { binary , command , args , cwd , env } = execution ; const cmdLine = `${ command || binary } ${ args . join ( ' ' ) }` ; return new ShellExecution ( cmdLine , { cwd , env } ) ; } export function activateTaskProvider ( target ) { const provider = { provideTasks : ( ) => detectCargoTasks ( target ) , resolveTask : ( ) => undefined , } ; return tasks . registerTaskProvider ( TASK_TYPE , provider ) ; } function detectCargoTasks ( target ) { return [ { subcommand : 'build' , group : TaskGroup . Build } , { subcommand : 'check' , group : TaskGroup . Build } , { subcommand : 'test' , group : TaskGroup . Test } , { subcommand : 'clean' , group : TaskGroup . Clean } , { subcommand : 'run' , group : undefined } , ] . map ( ( { subcommand , group } ) => ( { definition : { subcommand , type : TASK_TYPE } , label : `cargo ${ subcommand } - ${ target . name }` , execution : createShellExecution ( { command : 'cargo' , args : [ subcommand ] , cwd : target . uri . fsPath , } ) , group , problemMatchers : [ '$rustc' ] , } ) ) . map ( task => { const vscodeTask = new Task ( task . definition , target , task . label , TASK_SOURCE , task . execution , task . problemMatchers ) ; vscodeTask . group = task . group ; return vscodeTask ; } ) ; } export function runRlsCommand ( folder , execution ) { const shellExecution = createShellExecution ( execution ) ; const problemMatchers = [ '$rustc' ] ; return tasks . executeTask ( new Task ( { type : 'shell' } , folder , 'External RLS command' , TASK_SOURCE , shellExecution , problemMatchers ) ) ; } /**
 * Starts a shell command as a VSCode task, resolves when a task is finished.
 * Useful in tandem with setup commands, since the task window is reusable and
 * also capable of displaying ANSI terminal colors. Exit codes are not
 * supported, however.
 */ export async function runTaskCommand ( { command , args , env , cwd } , displayName , folder ? ) { const commandLine = `${ command } ${ args . join ( ' ' ) }` ; const task = new Task ( { type : 'shell' } , folder || workspace . workspaceFolders ! [ 0 ] , displayName , TASK_SOURCE , new ShellExecution ( commandLine , { cwd : cwd || ( folder && folder . uri . fsPath ) , env , } ) ) ; return new Promise ( resolve => { const disposable = tasks . onDidEndTask ( ( { execution } ) => { const taskExecution = execution . task . execution ; if ( taskExecution instanceof ShellExecution && taskExecution . commandLine === commandLine ) { disposable . dispose ( ) ; resolve ( ) ; } } ) ; tasks . executeTask ( task ) ; } ) ; }