import { Import } from "../Languages/Imports/Import" ; import { LineResults } from "../LineResults" ; import { CommandNames } from "../Names/CommandNames" ; import { Command } from "./Command" ; import { CommandResult } from "./CommandResult" ; import { CommandMetadata } from "./Metadata/CommandMetadata" ; import { SingleParameter } from "./Metadata/Parameters/SingleParameter" ; import { addLineEnder } from "./Utilities" ; /**
 * Starts a foreach loop over a container's keys.
 */ export class ForEachKeyStartCommand extends Command { /**
     * Metadata on the command.
     */ private static metadata : CommandMetadata = new CommandMetadata ( CommandNames . ForEachKeyStart ) . withDescription ( "Starts a foreach loop over a container's keys" ) . withIndentation ( [ 1 ] ) . withParameters ( [ new SingleParameter ( "container" , "A container to iterate over." , true ) , new SingleParameter ( "keyName" , "The name of the iteration key variable." , true ) , new SingleParameter ( "keyType" , "The type of the iteration key variable." , true ) , ] ) ; returns public getMetadata ( ) { return ForEachKeyStartCommand . metadata ; } param parameters returns public render ( parameters ) { if ( this . language . syntax . loops . forEachAsMethod ) { return this . renderForEachAsMethod ( parameters ) ; } return this . renderForEachAsLoop ( parameters ) ; } param parameters returns remarks public renderForEachAsLoop ( parameters ) { let line = this . language . syntax . loops . foreach ; let output ; const imports = [ ] ; line += this . language . syntax . conditionals . startLeft ; if ( this . language . syntax . variables . declarationRequired ) { line += this . language . syntax . variables . declaration ; if ( this . language . syntax . loops . forEachPairsTypedPair ) { const variableInline = this . context . convertParsed ( [ CommandNames . VariableInline , parameters [ 2 ] , parameters [ 3 ] ] ) ; line += variableInline . commandResults [ 0 ] . text ; imports . push ( ... variableInline . addedImports ) ; } else { line += parameters [ 2 ] ; } } else { line += parameters [ 2 ] ; } line += this . language . syntax . loops . forEachMiddle ; line += parameters [ 1 ] ; line += this . language . syntax . loops . forEachGetKeys ; line += this . language . syntax . loops . forEachRight ; output = [ new CommandResult ( line , 0 ) ] ; addLineEnder ( output , this . language . syntax . conditionals . startRight , 1 ) ; return new LineResults ( output ) . withImports ( imports ) ; } param parameters returns remarks public renderForEachAsMethod ( parameters ) { let output = parameters [ 1 ] ; output += this . language . syntax . loops . forEachGetKeys ; output += parameters [ 2 ] ; output += this . language . syntax . loops . forEachRight ; return new LineResults ( [ new CommandResult ( output , 1 ) ] ) ; } }