|
| 1 | +import { describe, afterEach, beforeEach, it, expect, vi } from 'vitest'; |
| 2 | +import { CompletionsProvider } from '../CompletionsProvider'; |
| 3 | +import { DocumentManager } from '../../documents'; |
| 4 | +import { MetafieldDefinitionMap } from '@shopify/theme-check-common'; |
| 5 | +import { TextEdit, InsertTextFormat } from 'vscode-languageserver-protocol'; |
| 6 | +import { TextDocument } from 'vscode-languageserver-textdocument'; |
| 7 | +import { CURSOR } from '../params'; |
| 8 | + |
| 9 | +vi.mock('./data/contentForParameterCompletionOptions', async () => { |
| 10 | + const actual = (await vi.importActual( |
| 11 | + './data/contentForParameterCompletionOptions', |
| 12 | + )) as typeof import('./data/contentForParameterCompletionOptions'); |
| 13 | + return { |
| 14 | + DEFAULT_COMPLETION_OPTIONS: { |
| 15 | + ...actual.DEFAULT_COMPLETION_OPTIONS, |
| 16 | + // Add another option here so we can properly test some scenarios that |
| 17 | + // we wouldn't be able to otherwise. |
| 18 | + testOption: '', |
| 19 | + }, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +describe('Module: ContentForBlockParameterCompletionProvider', async () => { |
| 24 | + let provider: CompletionsProvider; |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + provider = new CompletionsProvider({ |
| 28 | + documentManager: new DocumentManager(), |
| 29 | + themeDocset: { |
| 30 | + filters: async () => [], |
| 31 | + objects: async () => [], |
| 32 | + tags: async () => [], |
| 33 | + systemTranslations: async () => ({}), |
| 34 | + }, |
| 35 | + getMetafieldDefinitions: async (_rootUri: string) => ({} as MetafieldDefinitionMap), |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + vi.restoreAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('offers a full list of completion items', async () => { |
| 44 | + await expect(provider).to.complete('{% content_for "block", █ %}', [ |
| 45 | + 'type', |
| 46 | + 'id', |
| 47 | + 'closest', |
| 48 | + 'testOption', |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('uses text edits to insert the completion item', async () => { |
| 53 | + // char 24 ⌄ |
| 54 | + const context = `{% content_for "block", █ %}`; |
| 55 | + |
| 56 | + const textEdit: TextEdit = { |
| 57 | + newText: "type: '$1'", |
| 58 | + range: { |
| 59 | + end: { line: 0, character: 24 }, |
| 60 | + start: { line: 0, character: 24 }, |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + await expect(provider).to.complete( |
| 65 | + context, |
| 66 | + expect.arrayContaining([ |
| 67 | + expect.objectContaining({ |
| 68 | + label: 'type', |
| 69 | + insertTextFormat: InsertTextFormat.Snippet, |
| 70 | + textEdit, |
| 71 | + }), |
| 72 | + ]), |
| 73 | + ); |
| 74 | + |
| 75 | + const textDocument = TextDocument.create('', 'liquid', 0, context.replace(CURSOR, '')); |
| 76 | + |
| 77 | + expect(TextDocument.applyEdits(textDocument, [textEdit])).toBe( |
| 78 | + `{% content_for "block", type: '$1' %}`, |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('provides a different style of completion for "closest"', async () => { |
| 83 | + // char 24 ⌄ |
| 84 | + const context = `{% content_for "block", █ %}`; |
| 85 | + |
| 86 | + const textEdit: TextEdit = { |
| 87 | + newText: 'closest.', |
| 88 | + range: { |
| 89 | + end: { line: 0, character: 24 }, |
| 90 | + start: { line: 0, character: 24 }, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + await expect(provider).to.complete( |
| 95 | + context, |
| 96 | + expect.arrayContaining([ |
| 97 | + expect.objectContaining({ |
| 98 | + label: 'closest', |
| 99 | + insertTextFormat: InsertTextFormat.PlainText, |
| 100 | + textEdit, |
| 101 | + }), |
| 102 | + ]), |
| 103 | + ); |
| 104 | + |
| 105 | + const textDocument = TextDocument.create('', 'liquid', 0, context.replace(CURSOR, '')); |
| 106 | + |
| 107 | + expect(TextDocument.applyEdits(textDocument, [textEdit])).toBe( |
| 108 | + `{% content_for "block", closest. %}`, |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("when we're completing for blocks we only allow `closest`", () => { |
| 113 | + it('does something', async () => { |
| 114 | + await expect(provider).to.complete('{% content_for "blocks", █ %}', ['closest']); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('when the user has already started typing the name of the parameter', () => { |
| 119 | + it('filters the completion options to only include ones that match', async () => { |
| 120 | + await expect(provider).to.complete('{% content_for "block", t█ %}', ['type', 'testOption']); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('when the user has already typed out a parameter name', () => { |
| 125 | + describe('and the cursor is in the middle of the parameter', () => { |
| 126 | + it('changes the range depending on the completion item', async () => { |
| 127 | + // char 24 ⌄ ⌄ char 38 |
| 128 | + const context = `{% content_for "block", t█ype: "button" %}`; |
| 129 | + // ⌃ char 28 |
| 130 | + |
| 131 | + const typeTextEdit: TextEdit = { |
| 132 | + newText: 'type', |
| 133 | + range: { |
| 134 | + end: { line: 0, character: 28 }, |
| 135 | + start: { line: 0, character: 24 }, |
| 136 | + }, |
| 137 | + }; |
| 138 | + |
| 139 | + const testTextEdit: TextEdit = { |
| 140 | + newText: "testOption: '$1'", |
| 141 | + range: { |
| 142 | + end: { line: 0, character: 38 }, |
| 143 | + start: { line: 0, character: 24 }, |
| 144 | + }, |
| 145 | + }; |
| 146 | + |
| 147 | + await expect(provider).to.complete(context, [ |
| 148 | + expect.objectContaining({ |
| 149 | + label: 'type', |
| 150 | + insertTextFormat: InsertTextFormat.PlainText, |
| 151 | + textEdit: expect.objectContaining(typeTextEdit), |
| 152 | + }), |
| 153 | + expect.objectContaining({ |
| 154 | + label: 'testOption', |
| 155 | + insertTextFormat: InsertTextFormat.Snippet, |
| 156 | + textEdit: expect.objectContaining(testTextEdit), |
| 157 | + }), |
| 158 | + ]); |
| 159 | + |
| 160 | + const textDocument = TextDocument.create('', 'liquid', 0, context.replace(CURSOR, '')); |
| 161 | + |
| 162 | + expect(TextDocument.applyEdits(textDocument, [testTextEdit])).toBe( |
| 163 | + `{% content_for "block", testOption: '$1' %}`, |
| 164 | + ); |
| 165 | + |
| 166 | + expect(TextDocument.applyEdits(textDocument, [typeTextEdit])).toBe( |
| 167 | + `{% content_for "block", type: "button" %}`, |
| 168 | + ); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe('and the cursor is at the beginning of the parameter', () => { |
| 173 | + it('offers a full list of completion items', async () => { |
| 174 | + const context = `{% content_for "block", █type: "button" %}`; |
| 175 | + |
| 176 | + await expect(provider).to.complete(context, ['type', 'id', 'closest', 'testOption']); |
| 177 | + }); |
| 178 | + |
| 179 | + it('does not replace the existing text', async () => { |
| 180 | + // char 24 ⌄ |
| 181 | + const context = `{% content_for "block", █type: "button" %}`; |
| 182 | + |
| 183 | + const textEdit: TextEdit = { |
| 184 | + newText: "testOption: '$1', ", |
| 185 | + range: { |
| 186 | + end: { line: 0, character: 24 }, |
| 187 | + start: { line: 0, character: 24 }, |
| 188 | + }, |
| 189 | + }; |
| 190 | + |
| 191 | + await expect(provider).to.complete( |
| 192 | + context, |
| 193 | + expect.arrayContaining([ |
| 194 | + expect.objectContaining({ |
| 195 | + label: 'testOption', |
| 196 | + insertTextFormat: InsertTextFormat.Snippet, |
| 197 | + textEdit, |
| 198 | + }), |
| 199 | + ]), |
| 200 | + ); |
| 201 | + |
| 202 | + const textDocument = TextDocument.create('', 'liquid', 0, context.replace(CURSOR, '')); |
| 203 | + |
| 204 | + expect(TextDocument.applyEdits(textDocument, [textEdit])).toBe( |
| 205 | + `{% content_for "block", testOption: '$1', type: "button" %}`, |
| 206 | + ); |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + describe('and the cursor is at the end of the parameter', () => { |
| 211 | + it('offers only the same completion item', async () => { |
| 212 | + const context = `{% content_for "block", type█: "button" %}`; |
| 213 | + |
| 214 | + await expect(provider).to.complete(context, ['type']); |
| 215 | + }); |
| 216 | + |
| 217 | + it('only replaces the parameter name', async () => { |
| 218 | + // char 24 ⌄ ⌄ char 28 |
| 219 | + const context = `{% content_for "block", type█: "button" %}`; |
| 220 | + |
| 221 | + const textEdit: TextEdit = { |
| 222 | + newText: 'type', |
| 223 | + range: { |
| 224 | + end: { line: 0, character: 28 }, |
| 225 | + start: { line: 0, character: 24 }, |
| 226 | + }, |
| 227 | + }; |
| 228 | + |
| 229 | + await expect(provider).to.complete( |
| 230 | + context, |
| 231 | + expect.arrayContaining([ |
| 232 | + expect.objectContaining({ |
| 233 | + label: 'type', |
| 234 | + insertTextFormat: InsertTextFormat.PlainText, |
| 235 | + textEdit, |
| 236 | + }), |
| 237 | + ]), |
| 238 | + ); |
| 239 | + |
| 240 | + const textDocument = TextDocument.create('', 'liquid', 0, context.replace(CURSOR, '')); |
| 241 | + |
| 242 | + expect(TextDocument.applyEdits(textDocument, [textEdit])).toBe( |
| 243 | + `{% content_for "block", type: "button" %}`, |
| 244 | + ); |
| 245 | + }); |
| 246 | + }); |
| 247 | + }); |
| 248 | +}); |
0 commit comments