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

Method visitVarSuffix

src/FormatVisitor.cpp:1231–1297  ·  view source on GitHub ↗

nameAndArgs* (LSB exp RSB | DOT NAME);

Source from the content-addressed store, hash-verified

1229
1230// nameAndArgs* (LSB exp RSB | DOT NAME);
1231antlrcpp::Any FormatVisitor::visitVarSuffix(LuaParser::VarSuffixContext* ctx) {
1232 LOG_FUNCTION_BEGIN();
1233 chainedMethodCallIsFirst_.push_back(false);
1234 chainedMethodCallHasIncIndent_.push_back(false);
1235 for (auto* na : ctx->nameAndArgs()) {
1236 visitNameAndArgs(na);
1237 cur_writer() << commentAfter(na, "");
1238 }
1239 if (ctx->exp() != nullptr) {
1240 std::string expString = ctx->exp()->getText();
1241 // if table key is a nested string, keep the whitespace
1242 // example:
1243 // x = {}
1244 // x[ [[key]] ] = "value"
1245 if (!expString.empty() && expString[0] == '[') {
1246 cur_writer() << ctx->LSB()->getText();
1247 cur_writer() << commentAfter(ctx->LSB(), " ");
1248 visitExp(ctx->exp());
1249 cur_writer() << commentAfter(ctx->exp(), " ");
1250 cur_writer() << ctx->RSB()->getText();
1251 } else {
1252 cur_writer() << ctx->LSB()->getText();
1253 cur_writer() << commentAfter(ctx->LSB(), "");
1254 visitExp(ctx->exp());
1255 cur_writer() << commentAfter(ctx->exp(), "");
1256 cur_writer() << ctx->RSB()->getText();
1257 }
1258 } else {
1259 auto* varCtx = dynamic_cast<LuaParser::VarContext*>(ctx->parent);
1260 const std::vector<LuaParser::VarSuffixContext*>& arr = varCtx->varSuffix();
1261 int index = find(arr.begin(), arr.end(), ctx) - arr.begin();
1262 if (index != 0) {
1263 int length = calcASTLengthAndLines(ctx->NAME(), [=]() {
1264 cur_writer() << ctx->DOT()->getText();
1265 cur_writer() << commentAfter(ctx->DOT(), "");
1266 cur_writer() << ctx->NAME()->getText();
1267 visitNextNameAndArgs(ctx);
1268 }).first;
1269 bool beyondLimit = cur_columns() + length > config_.get<int>("column_limit");
1270 if (beyondLimit) {
1271 cur_writer() << "\n";
1272 if (chainedMethodCallHasIncIndent_.empty()) {
1273 // chainedMethodCallHasIncIndent_ is empty when visit var in varlist
1274 cur_writer() << indentWithAlign();
1275 } else {
1276 if (chainedMethodCallHasIncIndent_.back()) {
1277 cur_writer() << indentWithAlign();
1278 } else {
1279 incContinuationIndent();
1280 cur_writer() << indentWithAlign();
1281 chainedMethodCallHasIncIndent_[chainedMethodCallHasIncIndent_.size() - 1] = true;
1282 }
1283 }
1284 }
1285 }
1286 cur_writer() << ctx->DOT()->getText();
1287 cur_writer() << commentAfter(ctx->DOT(), "");
1288 cur_writer() << ctx->NAME()->getText();

Callers

nothing calls this directly

Calls 7

nameAndArgsMethod · 0.80
expMethod · 0.80
LSBMethod · 0.80
RSBMethod · 0.80
varSuffixMethod · 0.80
NAMEMethod · 0.80
DOTMethod · 0.80

Tested by

no test coverage detected