refactor document format command

This commit is contained in:
全卓 2022-11-02 20:15:36 +08:00
parent 71fa3852b5
commit 0d5f3ec0bb
1 changed files with 56 additions and 196 deletions

View File

@ -141,6 +141,36 @@ export class FormatListener extends CMakeListener {
return result; return result;
} }
private exitBeginBlockCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex;
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
++this._indentLevel;
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
}
private exitEndBlockCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex;
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
this.addNewLineAfterBlock();
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
}
enterFile(ctx: any): void { enterFile(ctx: any): void {
let hasComment = false; let hasComment = false;
for (const token of this._tokenStream.tokens) { for (const token of this._tokenStream.tokens) {
@ -164,81 +194,34 @@ export class FormatListener extends CMakeListener {
} }
exitIfCmd(ctx: any): void { exitIfCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitBeginBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
++this._indentLevel;
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterElseIfCmd(ctx: any): void { enterElseIfCmd(ctx: any): void {
--this._indentLevel; --this._indentLevel;
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("elseif", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("elseif", index);
} }
exitElseIfCmd(ctx: any): void { exitElseIfCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitBeginBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
++this._indentLevel;
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterElseCmd(ctx: any): void { enterElseCmd(ctx: any): void {
--this._indentLevel; --this._indentLevel;
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("else", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("else", index);
} }
exitElseCmd(ctx: any): void { exitElseCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitBeginBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
++this._indentLevel;
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterEndIfCmd(ctx: any): void { enterEndIfCmd(ctx: any): void {
--this._indentLevel; --this._indentLevel;
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("endif", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("endif", index);
} }
exitEndIfCmd(ctx: any): void { exitEndIfCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitEndBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
this.addNewLineAfterBlock();
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterWhileCmd(ctx: any): void { enterWhileCmd(ctx: any): void {
@ -248,39 +231,16 @@ export class FormatListener extends CMakeListener {
} }
exitWhileCmd(ctx: any): void { exitWhileCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitBeginBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
++this._indentLevel;
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterEndWhileCmd(ctx: any): void { enterEndWhileCmd(ctx: any): void {
--this._indentLevel; --this._indentLevel;
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("endwhile", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("endwhile", index);
} }
exitEndWhileCmd(ctx: any): void { exitEndWhileCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitEndBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
this.addNewLineAfterBlock();
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterForeachCmd(ctx: any): void { enterForeachCmd(ctx: any): void {
@ -290,75 +250,32 @@ export class FormatListener extends CMakeListener {
} }
exitForeachCmd(ctx: any): void { exitForeachCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitBeginBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
++this._indentLevel;
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterEndForeachCmd(ctx: any): void { enterEndForeachCmd(ctx: any): void {
--this._indentLevel; --this._indentLevel;
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("endforeach", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("endforeach", index);
} }
exitEndForeachCmd(ctx: any): void { exitEndForeachCmd(ctx: any): void {
const index = ctx.RParen().getSymbol().tokenIndex; this.exitEndBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command sperator
this._formatted += "\n";
this.addNewLineAfterBlock();
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterBreakCmd(ctx: any): void { enterBreakCmd(ctx: any): void {
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("break", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("break", index);
} }
exitBreakCmd(ctx: any): void { exitBreakCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitOtherCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterContinueCmd(ctx: any): void { enterContinueCmd(ctx: any): void {
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("continue", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("continue", index);
} }
exitContinueCmd(ctx: any): void { exitContinueCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitOtherCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterFunctionCmd(ctx: any): void { enterFunctionCmd(ctx: any): void {
@ -368,39 +285,16 @@ export class FormatListener extends CMakeListener {
} }
exitFunctionCmd(ctx: any): void { exitFunctionCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitBeginBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
++this._indentLevel;
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterEndFunctionCmd(ctx: any): void { enterEndFunctionCmd(ctx: any): void {
--this._indentLevel; --this._indentLevel;
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("endfunction", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("endfunction", index);
} }
exitEndFunctionCmd(ctx: any): void { exitEndFunctionCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitEndBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
this.addNewLineAfterBlock();
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterMacroCmd(ctx: any): void { enterMacroCmd(ctx: any): void {
@ -410,44 +304,20 @@ export class FormatListener extends CMakeListener {
} }
exitMacroCmd(ctx: any): void { exitMacroCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitBeginBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
++this._indentLevel;
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterEndMacroCmd(ctx: any): void { enterEndMacroCmd(ctx: any): void {
--this._indentLevel; --this._indentLevel;
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("endmacro", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("endmacro", index);
} }
exitEndMacroCmd(ctx: any): void { exitEndMacroCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitEndBlockCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
this.addNewLineAfterBlock();
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterOtherCmd(ctx: any): void { enterOtherCmd(ctx: any): void {
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg(ctx.ID().getText(), ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg(ctx.ID().getText(), index);
} }
exitOtherCmd(ctx: any): void { exitOtherCmd(ctx: any): void {
@ -464,21 +334,11 @@ export class FormatListener extends CMakeListener {
} }
enterSetCmd(ctx: any): void { enterSetCmd(ctx: any): void {
const index: number = ctx.LParen().getSymbol().tokenIndex; this._formatted += this.getTextBeforeFirstArg("set", ctx.LParen().getSymbol().tokenIndex);
this._formatted += this.getTextBeforeFirstArg("set", index);
} }
exitSetCmd(ctx: any): void { exitSetCmd(ctx: any): void {
const index: number = ctx.RParen().getSymbol().tokenIndex; this.exitOtherCmd(ctx);
const text: string = this.getTextAfterLastArg(index);
this._formatted += text;
// append a newline as command seprator
this._formatted += "\n";
// comments after the newline
const nlIndex: number = text === ")" ? index + 1 : index + 2;
this.addCommentsAfterSeprator(nlIndex);
} }
enterArgument(ctx: any): void { enterArgument(ctx: any): void {