GetWordAtPosition returns the word at the given position. This method extracts the identifier or keyword at a specific cursor position, which is used for hover documentation and completion filtering. The method uses rune-based indexing to properly handle UTF-8 encoded SQL identifiers that may cont
(pos Position)
| 353 | // This method is safe for concurrent use as it operates on document fields |
| 354 | // without modifying state. |
| 355 | func (doc *Document) GetWordAtPosition(pos Position) string { |
| 356 | if pos.Line >= len(doc.Lines) { |
| 357 | return "" |
| 358 | } |
| 359 | |
| 360 | line := doc.Lines[pos.Line] |
| 361 | runes := []rune(line) |
| 362 | |
| 363 | if pos.Character >= len(runes) { |
| 364 | return "" |
| 365 | } |
| 366 | |
| 367 | // Find word boundaries using rune indexing for UTF-8 safety |
| 368 | start := pos.Character |
| 369 | end := pos.Character |
| 370 | |
| 371 | // Move start backwards to find word start |
| 372 | for start > 0 && isWordChar(runes[start-1]) { |
| 373 | start-- |
| 374 | } |
| 375 | |
| 376 | // Move end forwards to find word end |
| 377 | for end < len(runes) && isWordChar(runes[end]) { |
| 378 | end++ |
| 379 | } |
| 380 | |
| 381 | if start == end { |
| 382 | return "" |
| 383 | } |
| 384 | |
| 385 | return string(runes[start:end]) |
| 386 | } |
| 387 | |
| 388 | // isWordChar returns true if c is a valid word character |
| 389 | func isWordChar(c rune) bool { |