import * as vscode from 'vscode' ; import * as path from 'path' ; import { ensureDocument } from '../path-utils' ; import { SwitchFile , OrPromise } from '../types' ; interface IndexedQuickPickItem extends vscode . QuickPickItem { create : boolean ; index : number ; } export function openFile ( filename ) { return vscode . workspace . openTextDocument ( filename ) . then ( vscode . window . showTextDocument ) ; } export async function createFile ( switchFile ) { return openFile ( switchFile . filename ) ; } function existingQuickPickItem ( root ) { return ( switchFile , index ) : IndexedQuickPickItem => ( { label : switchFile . title , description : '' , detail : path . relative ( root , switchFile . filename ) , index , create : false , } ) ; } function createQuickPickItem ( root ) { return ( switchFile , index ) : IndexedQuickPickItem => ( { label : switchFile . title , description : `Create ${ switchFile . type }` , detail : path . relative ( root , switchFile . filename ) , index , create : true , } ) ; } function quickPickItem ( root ) { const existing = existingQuickPickItem ( root ) ; const create = createQuickPickItem ( root ) ; return ( switchFile , index ) => { if ( switchFile . checkedExists ) { return existing ( switchFile , index ) ; } else { return create ( switchFile , index ) ; } } ; } async function quickPickItems ( root , switchFiles ) { return ( await Promise . resolve ( switchFiles ) ) . map ( quickPickItem ( root ) ) ; } export async function showPicker ( root , switchFiles ) { const picked = await vscode . window . showQuickPick ( quickPickItems ( root , switchFiles ) ) ; if ( picked ) { const switchFile = switchFiles [ picked . index ] ; if ( picked . create ) { await createFile ( switchFile ) ; } else { await openFile ( switchFile . filename ) ; } return switchFile ; } } export async function showYesNo ( message ) { const yesItem = { title : 'Yes' , } ; const noItem = { title : 'No' , isCloseAffordance : true , } ; const response = await vscode . window . showInformationMessage ( message , yesItem , noItem ) ; return response === yesItem ; } export async function showCreateFile ( filename ) { const root = vscode . workspace . workspaceFolders [ 0 ] . uri . fsPath ; const display = path . relative ( root , filename ) ; const message = `Create ${ display }` ; if ( await showYesNo ( message ) ) { return ensureDocument ( filename ) ; } }