import { Import } from "../Languages/Imports/Import" ; import { LineResults } from "../LineResults" ; import { CommandNames } from "../Names/CommandNames" ; import { KeywordNames } from "../Names/KeywordNames" ; import { Command } from "./Command" ; import { CommandResult } from "./CommandResult" ; import { CommandMetadata } from "./Metadata/CommandMetadata" ; import { KeywordParameter } from "./Metadata/Parameters/KeywordParameter" ; import { RepeatingParameters } from "./Metadata/Parameters/RepeatingParameters" ; import { SingleParameter } from "./Metadata/Parameters/SingleParameter" ; import { addLineEnder } from "./Utilities" ; /**
 * Starts a constructor.
 */ export class ConstructorStartCommand extends Command { /**
     * Metadata on the command.
     */ private static metadata : CommandMetadata = new CommandMetadata ( CommandNames . ConstructorStart ) . withDescription ( "Starts a constructor" ) . withIndentation ( [ 1 ] ) . withParameters ( [ new KeywordParameter ( KeywordNames . Privacies , "The privacy of the constructor." , true ) , new SingleParameter ( "className" , "The name of the class." , true ) , new RepeatingParameters ( "Function parameters" , [ new SingleParameter ( "parameterName" , "A named parameter for the constructor." , true ) , new SingleParameter ( "parameterType" , "The type of the parameter." , true ) , ] ) , new KeywordParameter ( [ KeywordNames . Base ] , "Keyword to call a base class constructor." , false ) , new RepeatingParameters ( "Function parameters" , [ new SingleParameter ( "parameter" , "Argument for the base constructor." , true ) ] ) , ] ) ; returns public getMetadata ( ) { return ConstructorStartCommand . metadata ; } param parameters returns public render ( parameters ) { const imports = [ ] ; const privacy = parameters [ 1 ] ; const className = parameters [ 2 ] ; const baseIndex = parameters . indexOf ( KeywordNames . Base ) ; let declaration = this . getPublicity ( privacy ) ; if ( this . language . syntax . classes . constructors . useKeyword ) { declaration += this . language . syntax . classes . constructors . keyword ; } else { declaration += className ; } declaration += "(" ; let endOfParameters = baseIndex ; if ( endOfParameters === - 1 ) { endOfParameters = parameters . length ; } if ( this . language . syntax . classes . constructors . takeThis ) { declaration += this . language . syntax . classes . this ; if ( endOfParameters > 3 ) { declaration += ", " ; } } for ( let i = 3 ; i < endOfParameters ; i += 2 ) { const parameterLine = this . generateParameterVariable ( parameters , i ) ; declaration += parameterLine . commandResults [ 0 ] . text ; imports . push ( ... parameterLine . addedImports ) ; if ( i !== endOfParameters - 2 ) { declaration += ", " ; } } const output = [ new CommandResult ( declaration + ")" , 0 ) ] ; let addSemicolon = false ; if ( baseIndex === - 1 ) { addLineEnder ( output , this . language . syntax . functions . defineStartRight , 1 ) ; } else if ( this . language . syntax . classes . constructors . baseShorthand ) { let nextLine = "\n    : " + this . language . syntax . classes . constructors . baseConstructor + "(" ; for ( let i = baseIndex + 1 ; i < parameters . length ; i += 1 ) { nextLine += parameters [ i ] ; if ( i !== parameters . length - 1 ) { nextLine += ", " ; } } nextLine += ")" ; addLineEnder ( output , nextLine , 1 ) ; output [ output . length - 1 ] . indentation -= 1 ; addLineEnder ( output , "\n{" , 1 ) ; } else { addSemicolon = true ; let startLine = this . language . syntax . functions . defineStartRight + "\n" ; startLine += this . language . syntax . classes . constructors . baseConstructor + "(" ; addLineEnder ( output , startLine , 0 ) ; output [ output . length - 2 ] . indentation += 1 ; let nextLine = "" ; for ( let i = baseIndex + 1 ; i < parameters . length ; i += 1 ) { nextLine += parameters [ i ] ; if ( i !== parameters . length - 1 ) { nextLine += ", " ; } } addLineEnder ( output , nextLine + ")" , 0 ) ; } return new LineResults ( output ) . withAddSemicolon ( addSemicolon ) . withImports ( imports ) ; } param publicity returns private getPublicity ( publicity ) { if ( publicity === KeywordNames . Private ) { return this . language . syntax . classes . constructors . private ; } if ( publicity === KeywordNames . Protected ) { return this . language . syntax . classes . constructors . protected ; } return this . language . syntax . classes . constructors . public ; } param parameters param i remarks private generateParameterVariable ( parameters , i ) { if ( ! this . language . syntax . variables . declarationRequired ) { return LineResults . newSingleLine ( parameters [ i ] ) ; } const parameterName = parameters [ i ] ; const parameterTypeLine = this . context . convertParsed ( [ CommandNames . Type , parameters [ i + 1 ] ] ) ; const parameterType = parameterTypeLine . commandResults [ 0 ] . text ; return this . context . convertParsed ( [ CommandNames . VariableInline , parameterName , parameterType ] ) ; } }