close method

OpStreamEnd close()

Ends the stream: anything still open is reported.

Implementation

OpStreamEnd close() {
  final out = <StreamedOp>[];
  // A scalar at the very end (a bare number) completes now.
  if (_scalarStart >= 0) _endScalar(out);
  String? truncated;
  if (!_started) {
    if (_pos > 0 && _cleanIsBlankRaw) {
      _diagnostics.add(EditDiagnostic.of(EditCodes.missingField, -1, 'The tool input is empty: send {"ops": [...]}.', path: '/ops'));
    } else if (_pos > 0) {
      _diagnostics.add(EditDiagnostic.of(EditCodes.invalidJson, -1, 'The tool input has no JSON object or list: send {"ops": [...]}.',
          path: '/ops'));
    } else {
      _diagnostics.add(EditDiagnostic.of(EditCodes.missingField, -1,
          'The tool input is empty: the turn ended without ops. Call ui_apply with {"ops": [...]}.',
          path: '/ops'));
    }
  }
  final opFrame = _stack.indexWhere((f) => f.role == _Role.op);
  if (opFrame >= 0) {
    final f = _stack[opFrame];
    truncated = _cleanText(f.cleanStart, _clean.length);
    _diagnostics.add(EditDiagnostic.of(EditCodes.truncatedOp, f.index,
        'The input ended inside op ${f.index} (${_pos - f.start} characters arrived): it was not applied${f.yielded.isEmpty ? '' : ', except the ${f.yielded.length} nodes of it that had arrived whole'}. Resend it.',
        path: '/ops/${f.index}'));
  } else if (_stack.isNotEmpty) {
    _repair(-1, _pos, 'the input ended with ${_stack.length} bracket${_stack.length == 1 ? '' : 's'} still open (every op in it was read)');
  }
  if (_repairs > maxReports) {
    _diagnostics.add(EditDiagnostic.of(EditCodes.malformedInput, -1,
        '${_repairs - maxReports} more repairs of malformed JSON were not listed.'));
  }
  return OpStreamEnd(
    diagnostics: List.unmodifiable(_diagnostics),
    baseRev: _baseRev,
    atomic: _atomic,
    truncated: truncated,
    complete: _started && _stack.isEmpty,
    opCount: _opCount,
    repairs: _repairs,
    recovered: _recovered,
  );
}