BreakAtParentheses attempts to break long lines at parentheses for function calls
(expression string)
| 407 | |
| 408 | // BreakAtParentheses attempts to break long lines at parentheses for function calls |
| 409 | func BreakAtParentheses(expression string) []string { |
| 410 | if len(expression) <= int(constants.MaxExpressionLineLength) { |
| 411 | return []string{expression} |
| 412 | } |
| 413 | |
| 414 | expressionsLog.Printf("Breaking expression at parentheses: length=%d", len(expression)) |
| 415 | |
| 416 | var lines []string |
| 417 | var current strings.Builder |
| 418 | parenDepth := 0 |
| 419 | |
| 420 | for i := 0; i < len(expression); i++ { |
| 421 | char := expression[i] |
| 422 | current.WriteByte(char) |
| 423 | |
| 424 | switch char { |
| 425 | case '(': |
| 426 | parenDepth++ |
| 427 | case ')': |
| 428 | parenDepth-- |
| 429 | |
| 430 | // If we're back to zero depth and the line is getting long, consider a break |
| 431 | if parenDepth == 0 && current.Len() > 80 && i < len(expression)-1 { |
| 432 | // Look ahead to see if there's a logical operator |
| 433 | j := i + 1 |
| 434 | for j < len(expression) && (expression[j] == ' ' || expression[j] == '\t') { |
| 435 | j++ |
| 436 | } |
| 437 | |
| 438 | if j+1 < len(expression) && (expression[j:j+2] == "||" || expression[j:j+2] == "&&") { |
| 439 | // Add the operator to current line and break |
| 440 | current.WriteString(expression[i+1 : j+2]) |
| 441 | lines = append(lines, strings.TrimSpace(current.String())) |
| 442 | current.Reset() |
| 443 | i = j + 2 - 1 // Set to j+2-1 so the loop increment makes i = j+2 |
| 444 | |
| 445 | // Skip whitespace after operator |
| 446 | for i+1 < len(expression) && (expression[i+1] == ' ' || expression[i+1] == '\t') { |
| 447 | i++ |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // Add remaining part |
| 455 | if trimmed := strings.TrimSpace(current.String()); trimmed != "" { |
| 456 | lines = append(lines, trimmed) |
| 457 | } |
| 458 | |
| 459 | return lines |
| 460 | } |
| 461 | |
| 462 | // hasNewlineInStringLiteral returns true if s contains an actual newline character (\n) |
| 463 | // that appears inside a single-quoted GitHub Actions expression string literal. |