wordSelectionAtCell method

GhosttyTerminalSelection? wordSelectionAtCell(
  1. int row,
  2. int col, {
  3. GhosttyTerminalWordBoundaryPolicy policy = const GhosttyTerminalWordBoundaryPolicy(),
})

Returns an inclusive selection covering the word-like token at col.

Implementation

GhosttyTerminalSelection? wordSelectionAtCell(
  int row,
  int col, {
  GhosttyTerminalWordBoundaryPolicy policy =
      const GhosttyTerminalWordBoundaryPolicy(),
}) {
  if (runs.isEmpty || cellCount == 0) {
    return null;
  }

  final normalizedCol = col.clamp(0, cellCount - 1);
  final hyperlink = _hyperlinkInfoAtCell(normalizedCol);
  if (hyperlink != null) {
    return GhosttyTerminalSelection(
      base: GhosttyTerminalCellPosition(row: row, col: hyperlink.startCol),
      extent: GhosttyTerminalCellPosition(row: row, col: hyperlink.endCol),
    );
  }

  final cells = _lineCells();
  final classification = _classifyTerminalCharacter(
    cells[normalizedCol].text,
    policy: policy,
  );
  var start = normalizedCol;
  var end = normalizedCol;

  while (start > 0 &&
      _classifyTerminalCharacter(cells[start - 1].text, policy: policy) ==
          classification) {
    start--;
  }
  while (end + 1 < cells.length &&
      _classifyTerminalCharacter(cells[end + 1].text, policy: policy) ==
          classification) {
    end++;
  }

  return GhosttyTerminalSelection(
    base: GhosttyTerminalCellPosition(row: row, col: start),
    extent: GhosttyTerminalCellPosition(row: row, col: end),
  );
}