|
| 1 | +import { Completion, CompletionContext } from '@codemirror/autocomplete'; |
| 2 | +import { EditorState } from '@codemirror/state'; |
| 3 | + |
| 4 | +import { createIdentifierCompletionSource } from '../utils'; |
| 5 | + |
| 6 | +const TEST_COMPLETIONS: Completion[] = [ |
| 7 | + { label: 'column1', type: 'variable' }, |
| 8 | + { label: 'column2', type: 'variable' }, |
| 9 | + { label: 'SELECT', type: 'keyword' }, |
| 10 | + { label: 'count', type: 'function', apply: 'count(' }, |
| 11 | +]; |
| 12 | + |
| 13 | +/** |
| 14 | + * Simulates what CodeMirror shows the user: calls the completion source, |
| 15 | + * extracts the typed prefix (text from `result.from` to cursor), and |
| 16 | + * filters options by case-insensitive prefix match on the label. |
| 17 | + * |
| 18 | + * Returns the filtered labels, or null when the source suppresses completions. |
| 19 | + */ |
| 20 | +function getSuggestionLabels( |
| 21 | + doc: string, |
| 22 | + { |
| 23 | + pos, |
| 24 | + explicit = false, |
| 25 | + completions = TEST_COMPLETIONS, |
| 26 | + }: { pos?: number; explicit?: boolean; completions?: Completion[] } = {}, |
| 27 | +): string[] | null { |
| 28 | + const source = createIdentifierCompletionSource(completions); |
| 29 | + const state = EditorState.create({ doc }); |
| 30 | + const cursorPos = pos ?? doc.length; |
| 31 | + const context = new CompletionContext(state, cursorPos, explicit); |
| 32 | + const result = source(context); |
| 33 | + |
| 34 | + if (result == null) return null; |
| 35 | + |
| 36 | + const typedPrefix = doc.slice(result.from, cursorPos).toLowerCase(); |
| 37 | + return result.options |
| 38 | + .filter(o => o.label.toLowerCase().startsWith(typedPrefix)) |
| 39 | + .map(o => o.label); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Returns the range [from, to] that CodeMirror would replace when the user |
| 44 | + * accepts a suggestion. This lets us verify that the entire identifier |
| 45 | + * (including any trailing characters after the cursor) gets replaced. |
| 46 | + */ |
| 47 | +function getReplacementRange( |
| 48 | + doc: string, |
| 49 | + pos: number, |
| 50 | +): { from: number; to: number } | null { |
| 51 | + const source = createIdentifierCompletionSource(TEST_COMPLETIONS); |
| 52 | + const state = EditorState.create({ doc }); |
| 53 | + const context = new CompletionContext(state, pos, false); |
| 54 | + const result = source(context); |
| 55 | + if (result == null) return null; |
| 56 | + return { from: result.from, to: result.to ?? pos }; |
| 57 | +} |
| 58 | + |
| 59 | +describe('Auto-Complete source', () => { |
| 60 | + it.each([ |
| 61 | + { doc: 'SELECT col', expected: ['column1', 'column2'] }, |
| 62 | + { doc: 'sel', expected: ['SELECT'] }, |
| 63 | + { doc: 'SELECT xyz', expected: [] }, |
| 64 | + { doc: 'SELECT count(*) AS total, col', expected: ['column1', 'column2'] }, |
| 65 | + ])('suggests matching completions for "$doc"', ({ doc, expected }) => { |
| 66 | + expect(getSuggestionLabels(doc)).toEqual(expected); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns all options when prefix is empty (Ctrl+Space)', () => { |
| 70 | + const labels = getSuggestionLabels('', { explicit: true }); |
| 71 | + expect(labels).toEqual(['column1', 'column2', 'SELECT', 'count']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns null when there is no prefix and no Ctrl+Space', () => { |
| 75 | + expect(getSuggestionLabels('')).toBeNull(); |
| 76 | + }); |
| 77 | + |
| 78 | + it.each([ |
| 79 | + { |
| 80 | + name: 'dots and brackets', |
| 81 | + doc: "ResourceAttributes['service.name']", |
| 82 | + completions: [ |
| 83 | + { label: "ResourceAttributes['service.name']", type: 'variable' }, |
| 84 | + ], |
| 85 | + expected: ["ResourceAttributes['service.name']"], |
| 86 | + }, |
| 87 | + { |
| 88 | + name: '$ macros', |
| 89 | + doc: 'WHERE $__date', |
| 90 | + completions: [ |
| 91 | + { label: '$__dateFilter', type: 'variable' }, |
| 92 | + { label: 'column1', type: 'variable' }, |
| 93 | + ], |
| 94 | + expected: ['$__dateFilter'], |
| 95 | + }, |
| 96 | + { |
| 97 | + name: 'curly braces and colons', |
| 98 | + doc: 'WHERE {name:', |
| 99 | + completions: [ |
| 100 | + { label: '{name:String}', type: 'variable' }, |
| 101 | + { label: 'column1', type: 'variable' }, |
| 102 | + ], |
| 103 | + expected: ['{name:String}'], |
| 104 | + }, |
| 105 | + ])('supports identifiers with $name', ({ doc, completions, expected }) => { |
| 106 | + expect(getSuggestionLabels(doc, { completions })).toEqual(expected); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('AS keyword suppression', () => { |
| 110 | + it.each([ |
| 111 | + { name: 'AS (uppercase)', doc: 'SELECT count(*) AS ali' }, |
| 112 | + { name: 'as (lowercase)', doc: 'SELECT count(*) as ali' }, |
| 113 | + { name: 'AS with extra whitespace', doc: 'SELECT count(*) AS ali' }, |
| 114 | + ])('returns null after $name', ({ doc }) => { |
| 115 | + expect(getSuggestionLabels(doc)).toBeNull(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('does not suppress when AS is part of a larger word', () => { |
| 119 | + expect(getSuggestionLabels('SELECT CAST')).toEqual([]); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('mid-identifier completion', () => { |
| 124 | + it.each([ |
| 125 | + { |
| 126 | + name: "cursor before trailing ']", |
| 127 | + doc: "ResourceAttributes['host.']", |
| 128 | + pos: 25, // after 'host.' |
| 129 | + expectedRange: { from: 0, to: 27 }, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: 'cursor in middle of a word', |
| 133 | + doc: 'SELECT column1', |
| 134 | + pos: 10, // after 'col' |
| 135 | + expectedRange: { from: 7, to: 14 }, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: 'cursor at end of identifier (no trailing chars)', |
| 139 | + doc: 'SELECT column1', |
| 140 | + pos: 14, |
| 141 | + expectedRange: { from: 7, to: 14 }, |
| 142 | + }, |
| 143 | + ])( |
| 144 | + 'replacement range covers full identifier when $name', |
| 145 | + ({ doc, pos, expectedRange }) => { |
| 146 | + expect(getReplacementRange(doc, pos)).toEqual(expectedRange); |
| 147 | + }, |
| 148 | + ); |
| 149 | + }); |
| 150 | +}); |
0 commit comments