|
|
@ -1,18 +1,25 @@
|
|
|
|
const LINE = {};
|
|
|
|
enum ChunkType {
|
|
|
|
const BLOCK = {};
|
|
|
|
Line,
|
|
|
|
|
|
|
|
Block
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default class CodeBuilder {
|
|
|
|
export default class CodeBuilder {
|
|
|
|
|
|
|
|
result: string
|
|
|
|
|
|
|
|
first: ChunkType
|
|
|
|
|
|
|
|
last: ChunkType
|
|
|
|
|
|
|
|
lastCondition: string
|
|
|
|
|
|
|
|
|
|
|
|
constructor ( str = '' ) {
|
|
|
|
constructor ( str = '' ) {
|
|
|
|
this.result = str;
|
|
|
|
this.result = str;
|
|
|
|
|
|
|
|
|
|
|
|
const initial = str ? ( /\n/.test( str ) ? BLOCK : LINE ) : null;
|
|
|
|
const initial = str ? ( /\n/.test( str ) ? ChunkType.Block : ChunkType.Line ) : null;
|
|
|
|
this.first = initial;
|
|
|
|
this.first = initial;
|
|
|
|
this.last = initial;
|
|
|
|
this.last = initial;
|
|
|
|
|
|
|
|
|
|
|
|
this.lastCondition = null;
|
|
|
|
this.lastCondition = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addConditionalLine ( condition, line ) {
|
|
|
|
addConditionalLine ( condition: string, line: string ) {
|
|
|
|
if ( condition === this.lastCondition ) {
|
|
|
|
if ( condition === this.lastCondition ) {
|
|
|
|
this.result += `\n\t${line}`;
|
|
|
|
this.result += `\n\t${line}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -24,41 +31,41 @@ export default class CodeBuilder {
|
|
|
|
this.lastCondition = condition;
|
|
|
|
this.lastCondition = condition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.last = BLOCK;
|
|
|
|
this.last = ChunkType.Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addLine ( line ) {
|
|
|
|
addLine ( line: string ) {
|
|
|
|
if ( this.lastCondition ) {
|
|
|
|
if ( this.lastCondition ) {
|
|
|
|
this.result += `\n}`;
|
|
|
|
this.result += `\n}`;
|
|
|
|
this.lastCondition = null;
|
|
|
|
this.lastCondition = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( this.last === BLOCK ) {
|
|
|
|
if ( this.last === ChunkType.Block ) {
|
|
|
|
this.result += `\n\n${line}`;
|
|
|
|
this.result += `\n\n${line}`;
|
|
|
|
} else if ( this.last === LINE ) {
|
|
|
|
} else if ( this.last === ChunkType.Line ) {
|
|
|
|
this.result += `\n${line}`;
|
|
|
|
this.result += `\n${line}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.result += line;
|
|
|
|
this.result += line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.last = LINE;
|
|
|
|
this.last = ChunkType.Line;
|
|
|
|
if ( !this.first ) this.first = LINE;
|
|
|
|
if ( !this.first ) this.first = ChunkType.Line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addLineAtStart ( line ) {
|
|
|
|
addLineAtStart ( line: string ) {
|
|
|
|
if ( this.first === BLOCK ) {
|
|
|
|
if ( this.first === ChunkType.Block ) {
|
|
|
|
this.result = `${line}\n\n${this.result}`;
|
|
|
|
this.result = `${line}\n\n${this.result}`;
|
|
|
|
} else if ( this.first === LINE ) {
|
|
|
|
} else if ( this.first === ChunkType.Line ) {
|
|
|
|
this.result = `${line}\n${this.result}`;
|
|
|
|
this.result = `${line}\n${this.result}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.result += line;
|
|
|
|
this.result += line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.first = LINE;
|
|
|
|
this.first = ChunkType.Line;
|
|
|
|
if ( !this.last ) this.last = LINE;
|
|
|
|
if ( !this.last ) this.last = ChunkType.Line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addBlock ( block ) {
|
|
|
|
addBlock ( block: string ) {
|
|
|
|
if ( this.lastCondition ) {
|
|
|
|
if ( this.lastCondition ) {
|
|
|
|
this.result += `\n}`;
|
|
|
|
this.result += `\n}`;
|
|
|
|
this.lastCondition = null;
|
|
|
|
this.lastCondition = null;
|
|
|
@ -70,19 +77,19 @@ export default class CodeBuilder {
|
|
|
|
this.result += block;
|
|
|
|
this.result += block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.last = BLOCK;
|
|
|
|
this.last = ChunkType.Block;
|
|
|
|
if ( !this.first ) this.first = BLOCK;
|
|
|
|
if ( !this.first ) this.first = ChunkType.Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addBlockAtStart ( block ) {
|
|
|
|
addBlockAtStart ( block: string ) {
|
|
|
|
if ( this.result ) {
|
|
|
|
if ( this.result ) {
|
|
|
|
this.result = `${block}\n\n${this.result}`;
|
|
|
|
this.result = `${block}\n\n${this.result}`;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.result += block;
|
|
|
|
this.result += block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.first = BLOCK;
|
|
|
|
this.first = ChunkType.Block;
|
|
|
|
if ( !this.last ) this.last = BLOCK;
|
|
|
|
if ( !this.last ) this.last = ChunkType.Block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isEmpty () {
|
|
|
|
isEmpty () {
|
|
|
|