MCPcopy Create free account
hub / github.com/Koihik/LuaFormatter / visitIfStat

Method visitIfStat

src/FormatVisitor.cpp:490–538  ·  view source on GitHub ↗

IF exp THEN block (ELSEIF exp THEN block)* (ELSE block)? END;

Source from the content-addressed store, hash-verified

488
489// IF exp THEN block (ELSEIF exp THEN block)* (ELSE block)? END;
490antlrcpp::Any FormatVisitor::visitIfStat(LuaParser::IfStatContext* ctx) {
491 LOG_FUNCTION_BEGIN();
492 // keep if statement one line only when there is no elseif or else statement.
493 cur_writer() << ctx->IF()->getText();
494 cur_writer() << commentAfter(ctx->IF(), " ");
495 visitExp(ctx->exp().front());
496 cur_writer() << commentAfter(ctx->exp().front(), " ");
497 cur_writer() << ctx->THEN().front()->getText();
498 if (ctx->ELSEIF().empty() && ctx->ELSE() == nullptr) {
499 if (needKeepBlockOneLine(ctx->THEN().front(), ctx->block().front(), CONTROL_BLOCK)) {
500 cur_writer() << commentAfter(ctx->THEN().front(), " ");
501 bool temp = chop_down_block_;
502 chop_down_block_ = false;
503 visitBlock(ctx->block().front());
504 chop_down_block_ = temp;
505 if (!isBlockEmpty(ctx->block().front())) {
506 cur_writer() << commentAfter(ctx->block().front(), " ");
507 }
508 cur_writer() << ctx->END()->getText();
509 return nullptr;
510 }
511 }
512 cur_writer() << commentAfterNewLine(ctx->THEN().front(), INC_INDENT);
513 visitBlock(ctx->block().front());
514 cur_writer() << commentAfterNewLine(ctx->block().front(), DEC_INDENT);
515 int n = ctx->ELSEIF().size();
516 for (int i = 0; i < n; i++) {
517 cur_writer() << indent();
518 cur_writer() << ctx->ELSEIF()[i]->getText();
519 cur_writer() << commentAfter(ctx->ELSEIF()[i], " ");
520 visitExp(ctx->exp()[i + 1]);
521 cur_writer() << commentAfter(ctx->exp()[i + 1], " ");
522 cur_writer() << ctx->THEN()[i + 1]->getText();
523 cur_writer() << commentAfterNewLine(ctx->THEN()[i + 1], INC_INDENT);
524 visitBlock(ctx->block()[i + 1]);
525 cur_writer() << commentAfterNewLine(ctx->block()[i + 1], DEC_INDENT);
526 }
527 if (ctx->ELSE() != nullptr) {
528 cur_writer() << indent();
529 cur_writer() << ctx->ELSE()->getText();
530 cur_writer() << commentAfterNewLine(ctx->ELSE(), INC_INDENT);
531 visitBlock(ctx->block().back());
532 cur_writer() << commentAfterNewLine(ctx->block().back(), DEC_INDENT);
533 }
534 cur_writer() << indent(); //
535 cur_writer() << ctx->END()->getText();
536 LOG_FUNCTION_END();
537 return nullptr;
538}
539
540// FOR NAME EQL exp COMMA exp (COMMA exp)? DO block END;
541antlrcpp::Any FormatVisitor::visitForStat(LuaParser::ForStatContext* ctx) {

Callers

nothing calls this directly

Calls 7

IFMethod · 0.80
expMethod · 0.80
THENMethod · 0.80
ELSEIFMethod · 0.80
ELSEMethod · 0.80
blockMethod · 0.80
ENDMethod · 0.80

Tested by

no test coverage detected