int _completeLine()

in lib/src/line_writer.dart [134:200]


  int _completeLine(int newlines, int indent, int start, int end,
      {required bool flushLeft}) {
    // Write the newlines required by the previous line.
    for (var j = 0; j < newlines; j++) {
      _buffer.write(_lineEnding);
    }

    var chunks = _chunks.sublist(start, end);

    if (debug.traceLineWriter) {
      debug.log(debug.green('\nWriting:'));
      debug.dumpChunks(0, chunks);
      debug.log();
    }

    // Run the line splitter.
    var splitter = LineSplitter(this, chunks, _blockIndentation, indent,
        flushLeft: flushLeft);
    var splits = splitter.apply();

    // Write the indentation of the first line.
    if (!flushLeft) {
      _buffer.write(' ' * (indent + _blockIndentation));
    }

    // Write each chunk with the appropriate splits between them.
    for (var i = 0; i < chunks.length; i++) {
      var chunk = chunks[i];
      _writeChunk(chunk);

      if (chunk.isBlock) {
        if (!splits.shouldSplitAt(i)) {
          // This block didn't split (which implies none of the child blocks
          // of that block split either, recursively), so write them all inline.
          _writeChunksUnsplit(chunk);
        } else {
          // Include the formatted block contents.
          var block = formatBlock(chunk, splits.getColumn(i));

          // If this block contains one of the selection markers, tell the
          // writer where it ended up in the final output.
          if (block.selectionStart != null) {
            _selectionStart = length + block.selectionStart!;
          }

          if (block.selectionEnd != null) {
            _selectionEnd = length + block.selectionEnd!;
          }

          _buffer.write(block.text);
        }
      }

      if (i == chunks.length - 1) {
        // Don't write trailing whitespace after the last chunk.
      } else if (splits.shouldSplitAt(i)) {
        _buffer.write(_lineEnding);
        if (chunk.isDouble) _buffer.write(_lineEnding);

        _buffer.write(' ' * (splits.getColumn(i)));
      } else {
        if (chunk.spaceWhenUnsplit) _buffer.write(' ');
      }
    }

    return splits.cost;
  }