BreakLongExpression breaks a long expression into multiple lines at logical points such as after || and && operators for better readability
(expression string)
| 322 | // BreakLongExpression breaks a long expression into multiple lines at logical points |
| 323 | // such as after || and && operators for better readability |
| 324 | func BreakLongExpression(expression string) []string { |
| 325 | // If the expression is not too long, return as-is |
| 326 | if len(expression) <= int(constants.MaxExpressionLineLength) { |
| 327 | return []string{expression} |
| 328 | } |
| 329 | |
| 330 | expressionsLog.Printf("Breaking long expression: length=%d", len(expression)) |
| 331 | |
| 332 | var lines []string |
| 333 | var current strings.Builder |
| 334 | i := 0 |
| 335 | |
| 336 | for i < len(expression) { |
| 337 | char := expression[i] |
| 338 | |
| 339 | // Handle quoted strings - don't break inside quotes |
| 340 | // Support single quotes ('), double quotes ("), and backticks (`) |
| 341 | if char == '\'' || char == '"' || char == '`' { |
| 342 | quote := char |
| 343 | current.WriteByte(char) |
| 344 | i++ |
| 345 | |
| 346 | // Continue until closing quote |
| 347 | for i < len(expression) { |
| 348 | current.WriteByte(expression[i]) |
| 349 | if expression[i] == quote { |
| 350 | i++ |
| 351 | break |
| 352 | } |
| 353 | if expression[i] == '\\' && i+1 < len(expression) { |
| 354 | i++ // Skip escaped character |
| 355 | if i < len(expression) { |
| 356 | current.WriteByte(expression[i]) |
| 357 | } |
| 358 | } |
| 359 | i++ |
| 360 | } |
| 361 | continue |
| 362 | } |
| 363 | |
| 364 | // Look for logical operators as break points |
| 365 | if i+2 <= len(expression) { |
| 366 | next2 := expression[i : i+2] |
| 367 | if next2 == "||" || next2 == "&&" { |
| 368 | current.WriteString(next2) |
| 369 | i += 2 |
| 370 | |
| 371 | // If the current line is getting long (>ExpressionBreakThreshold chars), break here |
| 372 | if trimmed := strings.TrimSpace(current.String()); len(trimmed) > int(constants.ExpressionBreakThreshold) { |
| 373 | lines = append(lines, trimmed) |
| 374 | current.Reset() |
| 375 | // Skip whitespace after operator |
| 376 | for i < len(expression) && (expression[i] == ' ' || expression[i] == '\t') { |
| 377 | i++ |
| 378 | } |
| 379 | continue |
| 380 | } |
| 381 | continue |