getCurrentWordPrefix method

String getCurrentWordPrefix(
  1. String text,
  2. int offset
)

Returns the identifier prefix immediately preceding the given offset.

This method extracts a contiguous sequence of identifier characters (ASCII letters, digits and underscore) that ends at offset (or at the current buffer cursor when a buffer is active). The extracted prefix is only returned if its first character is a letter (A–Z or a–z) or an underscore; prefixes that start with a digit are considered invalid and yield an empty string.

Behavior details:

  • If isBufferActive is true, the method uses bufferLineText and bufferCursorColumn instead of the provided text and offset.
  • offset is clamped to the range 0, text.length before processing.
  • If the effective cursor/column is out of range (<= 0 or greater than the line length) the method returns an empty string.
  • Identifier characters considered: '0'–'9', 'A'–'Z', 'a'–'z', and '_'.
  • The first character of the returned prefix must be a letter ('A'–'Z' or 'a'–'z') or an underscore; otherwise an empty string is returned.
  • If there is no identifier character immediately before the offset, or the computed start equals the offset, an empty string is returned.

Parameters:

  • text: The full text to inspect (ignored when a buffer is active).
  • offset: The exclusive character index (cursor position) in text at which to look backwards for an identifier prefix.

Returns:

  • The identifier prefix ending at the given offset (or buffer cursor), or an empty string if no valid prefix exists.

Examples:

 getCurrentWordPrefix("hello world", 5) -> "hello"
 getCurrentWordPrefix("123abc", 6) -> ""  (prefix starts with a digit)
  • When buffer is active, the method behaves the same but uses the buffer's current line and column instead of text/offset.

Implementation

String getCurrentWordPrefix(String text, int offset) {
  final safeOffset = offset.clamp(0, text.length);
  if (isBufferActive) {
    final lineText = bufferLineText ?? '';
    final col = bufferCursorColumn;
    if (col <= 0) return '';
    if (col > lineText.length) return '';
    int i = col - 1;
    while (i >= 0) {
      final code = lineText.codeUnitAt(i);
      final isIdentChar =
          (code >= 48 && code <= 57) ||
          (code >= 65 && code <= 90) ||
          (code >= 97 && code <= 122) ||
          code == 95;
      if (!isIdentChar) break;
      i--;
    }
    final start = i + 1;
    if (start >= col) return '';
    final firstCode = lineText.codeUnitAt(start);
    final isStartOk =
        (firstCode >= 65 && firstCode <= 90) ||
        (firstCode >= 97 && firstCode <= 122) ||
        firstCode == 95;
    if (!isStartOk) return '';
    return lineText.substring(start, col);
  }

  if (safeOffset == 0) return '';
  int i = safeOffset - 1;
  while (i >= 0) {
    final code = text.codeUnitAt(i);
    final isIdentChar =
        (code >= 48 && code <= 57) ||
        (code >= 65 && code <= 90) ||
        (code >= 97 && code <= 122) ||
        code == 95;
    if (!isIdentChar) break;
    i--;
  }
  final start = i + 1;
  if (start >= safeOffset) return '';
  final firstCode = text.codeUnitAt(start);
  final isStartOk =
      (firstCode >= 65 && firstCode <= 90) ||
      (firstCode >= 97 && firstCode <= 122) ||
      firstCode == 95;
  if (!isStartOk) return '';
  return text.substring(start, safeOffset);
}