validationPlanOffendingSlice function

String validationPlanOffendingSlice(
  1. String plan,
  2. String diagnostic
)

The EXACT text of plan a shell refused, given that shell's diagnostic.

A parse refusal that only says "invalid" moves the guessing instead of removing it, and a shell's own diagnostic names a LINE and a token, not a span an author can search for. This resolves one, in a fixed precedence so the same plan always names the same text:

  1. a balanced, complete $(…) substitution containing # — inside a command substitution a # opens a comment that swallows the closing paren, which parse-kills the whole gating line;
  2. a balanced, complete <(…)/>(…) process substitution — Bash-only, and it dies at PARSE under dash rather than at run time, so it surfaces as a harness throttle rather than as a bad plan;
  3. the smallest INTERIOR-apostrophe word (lane's) when the plan's single quotes do not pair — design prose carried into a single-quoted program is the field case, and the boundary quotes of the program itself are not the offender;
  4. the first normalized token of diagnostic that occurs verbatim in the plan;
  5. the complete trimmed plan — never an empty string.

Implementation

String validationPlanOffendingSlice(String plan, String diagnostic) {
  final trimmed = plan.trim();
  if (trimmed.isEmpty) return trimmed;
  for (final span in _balancedSpans(trimmed, const [r'$('])) {
    if (span.contains('#')) return span;
  }
  for (final span in _balancedSpans(trimmed, const ['<(', '>('])) {
    return span;
  }
  if (_hasUnpairedSingleQuote(trimmed)) {
    final word = _smallestInteriorApostropheWord(trimmed);
    if (word.isNotEmpty) return word;
  }
  for (final token in _diagnosticTokens(diagnostic)) {
    if (trimmed.contains(token)) return token;
  }
  return trimmed;
}