|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { Code } from '../src/code'; |
| 3 | + |
| 4 | +describe('Code.getWordAtPosition', () => { |
| 5 | + it('should return correct words under cursor', () => { |
| 6 | + const code = new Code('const find_tree = 123;\n another_word\n', 'test.js', 'javascript'); |
| 7 | + |
| 8 | + // Line 0: "const find_tree = 123;" |
| 9 | + // "const" is cols 0-5 |
| 10 | + expect(code.getWordAtPosition(0, 0)).toEqual({ text: 'const', token: null }); |
| 11 | + expect(code.getWordAtPosition(0, 3)).toEqual({ text: 'const', token: null }); |
| 12 | + expect(code.getWordAtPosition(0, 5)).toEqual({ text: 'const', token: null }); |
| 13 | + |
| 14 | + // Space at col 5 is adjacent to both "const" and "find_tree" (since index 5 is space and index 4 is 't') |
| 15 | + // Actually, let's verify if index 5 is space: |
| 16 | + // 'c'(0), 'o'(1), 'n'(2), 's'(3), 't'(4), ' '(5) |
| 17 | + // At col 5, index 5 is ' ', index 4 is 't' (word char). So it touches "const" |
| 18 | + expect(code.getWordAtPosition(0, 5)).toEqual({ text: 'const', token: null }); |
| 19 | + |
| 20 | + // Col 6 is 'f'. Touches "find_tree" |
| 21 | + expect(code.getWordAtPosition(0, 6)).toEqual({ text: 'find_tree', token: null }); |
| 22 | + expect(code.getWordAtPosition(0, 10)).toEqual({ text: 'find_tree', token: null }); // 't' in tree |
| 23 | + expect(code.getWordAtPosition(0, 15)).toEqual({ text: 'find_tree', token: null }); // right after 'e' (touches it) |
| 24 | + // Space at col 16: "const find_tree = 123;" |
| 25 | + // Index 15 is '=' (not word char), index 16 is ' '. Neither is word char. |
| 26 | + expect(code.getWordAtPosition(0, 16)).toBe(null); |
| 27 | + |
| 28 | + // "123" is cols 18-21 |
| 29 | + expect(code.getWordAtPosition(0, 18)).toEqual({ text: '123', token: null }); |
| 30 | + expect(code.getWordAtPosition(0, 21)).toEqual({ text: '123', token: null }); |
| 31 | + |
| 32 | + // Line 1: " another_word" |
| 33 | + expect(code.getWordAtPosition(1, 0)).toBe(null); // too far from word |
| 34 | + expect(code.getWordAtPosition(1, 1)).toBe(null); // too far |
| 35 | + expect(code.getWordAtPosition(1, 2)).toEqual({ text: 'another_word', token: null }); // at 'a' |
| 36 | + }); |
| 37 | + |
| 38 | + it('should retrieve words by offset', () => { |
| 39 | + const code = new Code('abc def', 'test.js', 'javascript'); |
| 40 | + // 'a'(0), 'b'(1), 'c'(2), ' '(3), 'd'(4), 'e'(5), 'f'(6) |
| 41 | + expect(code.getWordAtOffset(0)).toEqual({ text: 'abc', token: null }); |
| 42 | + expect(code.getWordAtOffset(1)).toEqual({ text: 'abc', token: null }); |
| 43 | + expect(code.getWordAtOffset(3)).toEqual({ text: 'abc', token: null }); // touches 'c' |
| 44 | + expect(code.getWordAtOffset(4)).toEqual({ text: 'def', token: null }); |
| 45 | + expect(code.getWordAtOffset(7)).toEqual({ text: 'def', token: null }); // touches 'f' |
| 46 | + expect(code.getWordAtOffset(8)).toBe(null); // out of range |
| 47 | + }); |
| 48 | + |
| 49 | + it('should detect unicode words (cyrillic and cjk)', () => { |
| 50 | + const code = new Code('привет 你好 _x42', 'test.js', 'javascript'); |
| 51 | + |
| 52 | + // Cyrillic |
| 53 | + expect(code.getWordAtPosition(0, 0)).toEqual({ text: 'привет', token: null }); |
| 54 | + expect(code.getWordAtPosition(0, 3)).toEqual({ text: 'привет', token: null }); |
| 55 | + expect(code.getWordAtPosition(0, 6)).toEqual({ text: 'привет', token: null }); // right boundary touch |
| 56 | + |
| 57 | + // CJK |
| 58 | + expect(code.getWordAtPosition(0, 7)).toEqual({ text: '你好', token: null }); |
| 59 | + expect(code.getWordAtPosition(0, 8)).toEqual({ text: '你好', token: null }); |
| 60 | + expect(code.getWordAtPosition(0, 9)).toEqual({ text: '你好', token: null }); // right boundary touch |
| 61 | + |
| 62 | + // underscore + digits still works |
| 63 | + expect(code.getWordAtPosition(0, 10)).toEqual({ text: '_x42', token: null }); |
| 64 | + expect(code.getWordAtPosition(0, 14)).toEqual({ text: '_x42', token: null }); // right boundary touch |
| 65 | + }); |
| 66 | + |
| 67 | + it('should not treat emoji grapheme clusters as word characters', () => { |
| 68 | + const code = new Code('foo 👨👩👧👦 bar', 'test.js', 'javascript'); |
| 69 | + |
| 70 | + expect(code.getWordAtPosition(0, 1)).toEqual({ text: 'foo', token: null }); |
| 71 | + expect(code.getWordAtPosition(0, 3)).toEqual({ text: 'foo', token: null }); // boundary touch |
| 72 | + expect(code.getWordAtPosition(0, 5)).toBe(null); // emoji start |
| 73 | + expect(code.getWordAtPosition(0, 10)).toBe(null); // inside emoji cluster (code unit index) |
| 74 | + expect(code.getWordAtPosition(0, 15)).toBe(null); // whitespace after emoji cluster |
| 75 | + expect(code.getWordAtPosition(0, 16)).toEqual({ text: 'bar', token: null }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments