import { LineResults } from "../LineResults" ; import { CommandNames } from "../Names/CommandNames" ; import { Command } from "./Command" ; import { CommandResult } from "./CommandResult" ; import { CommandMetadata } from "./Metadata/CommandMetadata" ; import { RepeatingParameters } from "./Metadata/Parameters/RepeatingParameters" ; import { SingleParameter } from "./Metadata/Parameters/SingleParameter" ; /**
 * Line within a documentation tag.
 */ export class CommentDocTagCommand extends Command { todo todo private static maximumLineLength = 70 ; /**
     * Metadata on the command.
     */ private static metadata : CommandMetadata = new CommandMetadata ( CommandNames . CommentDocTag ) . withDescription ( "Line within a documentation tag" ) . withParameters ( [ new SingleParameter ( "tag" , "The name of the tag." , true ) , new SingleParameter ( "parameter" , "An optional descriptor for the tag." , false ) , new RepeatingParameters ( "Comments regarding the tag" , [ new SingleParameter ( "contents" , "Contents of the comment" , true ) ] ) , ] ) ; returns public getMetadata ( ) { return CommentDocTagCommand . metadata ; } param parameters returns public render ( parameters ) { if ( this . language . syntax . comments . docAsXml ) { return this . renderXmlDoc ( parameters ) ; } return this . renderJsDoc ( parameters ) ; } param tag param contents returns private padContentsWithTag ( tag , contents ) { const results = [ ] ; const starter = this . language . syntax . comments . docLineStart ; results . push ( starter + tag + contents [ 0 ] ) ; for ( let i = 1 ; i < contents . length ; i += 1 ) { results . push ( starter + this . padLeft ( contents [ i ] , tag . length ) ) ; } return results ; } param text param spaces returns private padLeft ( text , spaces ) { let padder = "" ; for ( let i = 0 ; i < spaces ; i += 1 ) { padder += " " ; } return padder + text ; } param tagRaw returns private parseTag ( tagRaw ) { if ( { } . hasOwnProperty . call ( this . language . syntax . comments . docTagAliases , tagRaw ) ) { return this . language . syntax . comments . docTagAliases [ tagRaw ] ; } return tagRaw ; } param tagRaw returns private parseTagParameter ( tag , parameter ) { if ( { } . hasOwnProperty . call ( this . language . syntax . comments . docTagsWithParameters , tag ) ) { return this . language . syntax . comments . docTagsWithParameters [ tag ] ; } return parameter ; } param parameters returns private renderJsDoc ( parameters ) { const tagRaw = parameters [ 1 ] ; const tagParsed = this . parseTag ( tagRaw ) ; let tag = tagParsed + this . language . syntax . comments . docTagEnd ; let contentsRaw ; if ( tagParsed === "\0" ) { tag = this . language . syntax . comments . docTagStart ; } else if ( tagParsed !== "" ) { tag = this . language . syntax . comments . docTagStart + tag ; } if ( { } . hasOwnProperty . call ( this . language . syntax . comments . docTagsWithParameters , tagRaw ) ) { const parameterInfo = this . language . syntax . comments . docTagsWithParameters [ tagRaw ] ; if ( parameterInfo === "\0" ) { contentsRaw = parameters . slice ( 2 ) . join ( " " ) ; tag = "" ; } else { contentsRaw = parameters . slice ( 3 ) . join ( " " ) ; tag += parameters [ 2 ] ; tag += this . language . syntax . comments . docTagEnd ; tag += this . language . syntax . comments . docTagSpaceAfter ; } } else { contentsRaw = parameters . slice ( 2 ) . join ( " " ) ; } if ( tag === this . language . syntax . comments . docTagEnd ) { tag = "" ; } const contents = this . wrapTagContents ( tag , contentsRaw ) ; const contentsPadded = this . padContentsWithTag ( tag , contents ) ; const commandResults = [ ] ; for ( const contentPadded of contentsPadded ) { commandResults . push ( new CommandResult ( contentPadded , 0 ) ) ; } return new LineResults ( commandResults ) ; } param parameters returns private renderXmlDoc ( parameters ) { const lineStart = this . language . syntax . comments . docLineStart ; const tagRaw = parameters [ 1 ] ; const tag = this . parseTag ( tagRaw ) ; const commandResults = [ ] ; let contentsRaw ; let starter = lineStart + "<" + tag ; if ( { } . hasOwnProperty . call ( this . language . syntax . comments . docTagsWithParameters , tagRaw ) ) { starter += " " + this . parseTagParameter ( tagRaw , parameters [ 2 ] ) + '="' ; starter += parameters [ 2 ] ; starter += '">' ; contentsRaw = parameters . slice ( 3 ) . join ( " " ) ; } else { starter += ">" ; contentsRaw = parameters . slice ( 2 ) . join ( " " ) ; } commandResults . push ( new CommandResult ( starter , 0 ) ) ; const contents = this . wrapTagContents ( lineStart , contentsRaw ) ; const contentsPadded = this . padContentsWithTag ( "" , contents ) ; for ( const contentPadded of contentsPadded ) { commandResults . push ( new CommandResult ( contentPadded , 0 ) ) ; } let ender = this . language . syntax . comments . docLineStart ; ender += "</" + tag + ">" ; commandResults . push ( new CommandResult ( ender , 0 ) ) ; return new LineResults ( commandResults ) ; } param text param length returns private splitTextToLength ( text , length ) { const textSplit = text . split ( " " ) ; const lines = [ ] ; let line = "" ; for ( const split of textSplit ) { if ( line . length + 1 + split . length > length ) { lines . push ( line ) ; line = split ; } else { if ( line !== "" ) { line += " " ; } line += split ; } } lines . push ( line ) ; return lines ; } param tag param contentsRaw private wrapTagContents ( tag , contentsRaw ) { let maximumContentsLength = CommentDocTagCommand . maximumLineLength - tag . length - this . language . syntax . comments . docLineEnd . length ; if ( ! this . language . syntax . comments . docAsXml ) { maximumContentsLength -= this . language . syntax . comments . docLineStart . length ; } if ( contentsRaw . length <= maximumContentsLength ) { return [ contentsRaw ] ; } return this . splitTextToLength ( contentsRaw , maximumContentsLength ) ; } }