import { CaseStyle } from "../Languages/Casing/CaseStyle" ; 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 { addLineEnder } from "./Utilities" ; /**
 * Starts any kind of member function.
 */ export abstract class MemberFunctionDeclareCommand extends Command { param parameters public render ( parameters ) { let declaration = "" ; if ( ! this . canRender ( ) ) { return LineResults . newSingleLine ( declaration ) ; } const imports = [ ] ; const publicity = parameters [ 1 ] ; const functionName = parameters [ 2 ] ; const throwsIndex = parameters . indexOf ( KeywordNames . Throws ) ; const functionParameters = this . collectFunctionParameters ( parameters , throwsIndex ) ; const returnTypeLine = this . context . convertParsed ( [ CommandNames . Type , parameters [ 3 ] ] ) ; const returnType = returnTypeLine . commandResults [ 0 ] . text ; let output ; imports . push ( ... returnTypeLine . addedImports ) ; declaration += this . getPublicity ( publicity ) ; declaration += this . getAfterPublicity ( ) ; if ( this . language . syntax . functions . explicitReturns && ! this . language . syntax . functions . returnTypeAfterName ) { declaration += returnType + " " ; } declaration += this . getPublicityPrefix ( publicity ) ; declaration += this . context . convertStringToCase ( functionName , this . getPublicityCase ( publicity ) ) ; declaration += "(" ; if ( this . language . syntax . classes . members . functions . includeThisReference ) { declaration += this . language . syntax . classes . this ; if ( functionParameters . length !== 0 ) { declaration += ", " ; } } for ( let i = 0 ; i < functionParameters . length ; i += 2 ) { const nextTypeLine = this . generateParameterVariable ( functionParameters , i ) ; imports . push ( ... nextTypeLine . addedImports ) ; if ( i !== 0 ) { declaration += ", " ; } declaration += nextTypeLine . commandResults [ 0 ] . text ; } declaration += ")" ; if ( this . language . syntax . functions . explicitReturns && this . language . syntax . functions . returnTypeAfterName ) { declaration += this . language . syntax . functions . returnTypeMarker ; declaration += returnType ; } if ( this . language . syntax . functions . explicitThrows && throwsIndex !== - 1 ) { declaration += this . language . syntax . functions . throwsMarker ; for ( let i = throwsIndex + 1 ; i < parameters . length ; i += 1 ) { declaration += parameters [ i ] ; if ( i !== parameters . length - 1 ) { declaration += ", " ; } } } output = [ new CommandResult ( declaration , 0 ) ] ; addLineEnder ( output , this . getEnder ( ) , this . getIndentation ( ) ) ; return new LineResults ( output ) . withImports ( imports ) ; } returns protected abstract canRender ( ) ; returns protected abstract getAfterPublicity ( ) ; returns protected abstract getEnder ( ) ; returns protected abstract getIndentation ( ) ; private collectFunctionParameters ( parameters , throwsIndex ) { if ( throwsIndex === - 1 ) { return parameters . slice ( 4 ) ; } return parameters . slice ( 4 , throwsIndex ) ; } 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 ] ) . withImports ( parameterTypeLine . addedImports ) ; } param publicity returns private getPublicity ( publicity ) { if ( publicity === KeywordNames . Private ) { return this . language . syntax . classes . members . functions . private ; } if ( publicity === KeywordNames . Protected ) { return this . language . syntax . classes . members . functions . protected ; } return this . language . syntax . classes . members . functions . public ; } param publicity returns private getPublicityCase ( publicity ) { if ( publicity === KeywordNames . Private ) { return this . language . syntax . classes . members . functions . privateCase ; } if ( publicity === KeywordNames . Protected ) { return this . language . syntax . classes . members . functions . protectedCase ; } return this . language . syntax . classes . members . functions . publicCase ; } param publicity returns private getPublicityPrefix ( publicity ) { if ( publicity === KeywordNames . Private ) { return this . language . syntax . classes . members . functions . privatePrefix ; } if ( publicity === KeywordNames . Protected ) { return this . language . syntax . classes . members . functions . protectedPrefix ; } return this . language . syntax . classes . members . functions . publicPrefix ; } }