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

Method visitExplist

src/FormatVisitor.cpp:878–1006  ·  view source on GitHub ↗

exp (COMMA exp)*;

Source from the content-addressed store, hash-verified

876
877// exp (COMMA exp)*;
878antlrcpp::Any FormatVisitor::visitExplist(LuaParser::ExplistContext* ctx) {
879 LOG_FUNCTION_BEGIN();
880 int n = ctx->COMMA().size();
881 auto p = calcASTLengthAndLines(ctx->exp().front(), [=]() { visitExp(ctx->exp().front()); });
882 int expLength = p.first;
883 int lines = p.second;
884 if (n > 0) {
885 expLength++; // calc a ',' if exp > 1
886 }
887 bool beyondLimit = cur_columns() + expLength > config_.get<int>("column_limit");
888 // if lines > 1 and current column is already too long, then break
889 // example:
890 // var:foo(xxxx .. xxxx) -- no break
891 // longlonglonglongvar:foo(
892 // xxxx .. xxxx) -- break
893 int column_limit = config_.get<int>("column_limit");
894 if (lines > 1 && cur_columns() > column_limit / 2 && expLength >= column_limit / 4) {
895 beyondLimit = true;
896 }
897 if (!functioncallLpHasBreak_.empty() && functioncallLpHasBreak_.back()) {
898 beyondLimit = false;
899 }
900 bool hasIncIndent = false;
901 int firstArgsIndent = 0;
902 if (beyondLimit) {
903 if (cur_writer().isLastCharWhiteSpace()) {
904 cur_writer().ss().seekp(-1, std::ios::end);
905 }
906 cur_writer() << "\n";
907 incContinuationIndent();
908 cur_writer() << indentWithAlign();
909 hasIncIndent = true;
910 visitExp(ctx->exp().front());
911 } else {
912 firstArgsIndent = cur_columns() - indent_ - indentForAlign_;
913 incIndentForAlign(firstArgsIndent);
914 visitExp(ctx->exp().front());
915 decIndentForAlign(firstArgsIndent);
916 }
917 if (n > 0) {
918 cur_writer() << commentAfter(ctx->exp().front(), "");
919 }
920 for (int i = 0; i < n; i++) {
921 cur_writer() << ctx->COMMA()[i]->getText();
922 bool beyondLimit = false;
923 pushWriter();
924 cur_writer() << commentAfter(ctx->COMMA()[i], " ");
925 visitExp(ctx->exp()[i + 1]);
926 int expLength = cur_writer().firstLineColumn();
927 int lines = cur_writer().lines();
928 popWriter();
929 if (i != n - 1) {
930 expLength++; // calc a ',' if exp > 1
931 }
932 beyondLimit = cur_columns() + expLength > config_.get<int>("column_limit");
933 if (beyondLimit && lines == 1) {
934 // lines is 1 means expression is enough short. But with current columns,
935 // it beyonds column limit, need a line break here.

Callers

nothing calls this directly

Calls 5

COMMAMethod · 0.80
expMethod · 0.80
isLastCharWhiteSpaceMethod · 0.80
firstLineColumnMethod · 0.80
linesMethod · 0.80

Tested by

no test coverage detected