getWordBoundary method

BufferRangeLine? getWordBoundary(
  1. CellOffset position
)

Implementation

BufferRangeLine? getWordBoundary(CellOffset position) {
  var separators = wordSeparators ?? defaultWordSeparators;
  if (position.y >= lines.length) {
    return null;
  }

  var startLine = position.y;
  var start = position.x;
  var endLine = position.y;
  var end = position.x;

  do {
    if (start == 0) {
      if (!_lineContinuesFromPrevious(startLine)) break;
      startLine--;
      start = viewWidth;
    }
    final line = lines[startLine];
    var previous = start - 1;
    if (previous > 0 &&
        line.getWidth(previous) == 0 &&
        line.getWidth(previous - 1) == 2) {
      previous--;
    }
    final char = line.getCodePoint(previous);
    if (separators.contains(char)) {
      break;
    }
    start = previous;
  } while (true);

  do {
    if (end >= viewWidth) {
      if (!_lineContinuesToNext(endLine)) break;
      endLine++;
      end = 0;
    }
    final line = lines[endLine];
    final width = line.getWidth(end);
    if (width == 0 && end > 0 && line.getWidth(end - 1) == 2) {
      end++;
      continue;
    }
    final char = line.getCodePoint(end);
    if (separators.contains(char)) {
      break;
    }
    end += switch (width) {
      2 => 2,
      _ => 1,
    };
  } while (true);

  if (start == end && startLine == endLine) {
    return null;
  }

  return BufferRangeLine(
    CellOffset(start, startLine),
    CellOffset(end, endLine),
  );
}