|
| 1 | +import { InsertReplaceEdit, TextDocument, TextEdit } from '@volar/language-server'; |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import { URI } from 'vscode-uri'; |
| 4 | +import { getLanguageServer, testWorkspacePath } from './server.js'; |
| 5 | + |
| 6 | +describe('Completions', async () => { |
| 7 | + |
| 8 | + it('Directives', async () => { |
| 9 | + await assertCompletion('fixture.vue', 'vue', `<template><div v-ht|></div></template>`, 'v-html'); |
| 10 | + await assertCompletion('fixture.vue', 'vue', `<template><div v-cl|></div></template>`, 'v-cloak'); |
| 11 | + await assertCompletion('fixture.vue', 'vue', `<template><div v-el|></div></template>`, 'v-else'); |
| 12 | + await assertCompletion('fixture.vue', 'vue', `<template><div v-p|></div></template>`, 'v-pre'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('$event argument', async () => { |
| 16 | + await assertCompletion('fixture.vue', 'vue', `<template><div @click="console.log($eve|)"></div></template>`, '$event'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('<script setup>', async () => { |
| 20 | + await assertCompletion('fixture.vue', 'vue', ` |
| 21 | + <template>{{ f| }}</template> |
| 22 | +
|
| 23 | + <script lang="ts" setup> |
| 24 | + const foo = 1; |
| 25 | + </script> |
| 26 | + `, 'foo'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('Slot name', async () => { |
| 30 | + await assertCompletion('fixture.vue', 'vue', ` |
| 31 | + <template> |
| 32 | + <Foo> |
| 33 | + <template #|></template> |
| 34 | + </Foo> |
| 35 | + </template> |
| 36 | +
|
| 37 | + <script lang="ts" setup> |
| 38 | + let Foo: new () => { |
| 39 | + $slots: { |
| 40 | + default: any; |
| 41 | + }; |
| 42 | + }; |
| 43 | + </script> |
| 44 | + `, 'default'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('#2454', async () => { |
| 48 | + await assertCompletion('fixture.vue', 'vue', ` |
| 49 | + <script setup lang="ts"> |
| 50 | + let vLoading: any; |
| 51 | + </script> |
| 52 | +
|
| 53 | + <template> |
| 54 | + <div v-load|="vLoading"></div> |
| 55 | + </template> |
| 56 | + `, 'v-loading'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('#2511', async () => { |
| 60 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 61 | + await openDocument('tsconfigProject/component-for-auto-import.vue', 'vue', `<script setup lang="ts"></script>`); |
| 62 | + await assertCompletion('tsconfigProject/fixture.vue', 'vue', ` |
| 63 | + <script setup lang="ts"> |
| 64 | + import componentFor| |
| 65 | + </script> |
| 66 | + `, 'ComponentForAutoImport'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('#3658', async () => { |
| 70 | + await assertCompletion('fixture.vue', 'vue', ` |
| 71 | + <template> |
| 72 | + <Comp> |
| 73 | + <template #foo="foo"> |
| 74 | + {{ fo| }} |
| 75 | + </template> |
| 76 | + </Comp> |
| 77 | + </template> |
| 78 | + `, 'foo'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('#4639', async () => { |
| 82 | + await assertCompletion('fixture.vue', 'vue', ` |
| 83 | + <template> |
| 84 | + <div @click.| /> |
| 85 | + </template> |
| 86 | + `, 'capture'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('Alias path', async () => { |
| 90 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 91 | + await assertCompletion('tsconfigProject/fixture.vue', 'vue', ` |
| 92 | + <script setup lang="ts"> |
| 93 | + import Component from '@/|'; |
| 94 | + </script> |
| 95 | + `, 'empty.vue'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('Relative path', async () => { |
| 99 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 100 | + await assertCompletion('tsconfigProject/fixture.vue', 'vue', ` |
| 101 | + <script setup lang="ts"> |
| 102 | + import Component from './|'; |
| 103 | + </script> |
| 104 | + `, 'empty.vue'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('Component auto import', async () => { |
| 108 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 109 | + await openDocument('tsconfigProject/ComponentForAutoImport.vue', 'vue', `<script setup lang="ts"></script>`); |
| 110 | + await assertCompletion('tsconfigProject/fixture.vue', 'vue', ` |
| 111 | + <script setup lang="ts"> |
| 112 | + </script> |
| 113 | +
|
| 114 | + <template> |
| 115 | + <ComponentForA| /> |
| 116 | + </template> |
| 117 | + `, 'ComponentForAutoImport'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('core#8811', async () => { |
| 121 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 122 | + await assertCompletion('tsconfigProject/fixture.vue', 'vue', ` |
| 123 | + <script setup lang="ts"> |
| 124 | + declare const Foo: new () => { |
| 125 | + $props: { |
| 126 | + FooBar: string; |
| 127 | + }; |
| 128 | + }; |
| 129 | + </script> |
| 130 | +
|
| 131 | + <template> |
| 132 | + <Foo :-| ></Foo> |
| 133 | + </template> |
| 134 | + `, ':-foo-bar'); |
| 135 | + }); |
| 136 | + |
| 137 | + const openedDocuments: TextDocument[] = []; |
| 138 | + |
| 139 | + afterEach(async () => { |
| 140 | + const server = await getLanguageServer(); |
| 141 | + for (const document of openedDocuments) { |
| 142 | + await server.closeTextDocument(document.uri); |
| 143 | + } |
| 144 | + openedDocuments.length = 0; |
| 145 | + }); |
| 146 | + |
| 147 | + /** |
| 148 | + * @deprecated Remove this when #4717 fixed. |
| 149 | + */ |
| 150 | + async function ensureGlobalTypesHolder(folderName: string) { |
| 151 | + const document = await openDocument(`${folderName}/globalTypesHolder.vue`, 'vue', ''); |
| 152 | + const server = await getLanguageServer(); |
| 153 | + await server.sendDocumentDiagnosticRequest(document.uri); |
| 154 | + } |
| 155 | + |
| 156 | + async function assertCompletion(fileName: string, languageId: string, content: string, itemLabel: string) { |
| 157 | + const offset = content.indexOf('|'); |
| 158 | + content = content.slice(0, offset) + content.slice(offset + 1); |
| 159 | + |
| 160 | + const server = await getLanguageServer(); |
| 161 | + let document = await openDocument(fileName, languageId, content); |
| 162 | + |
| 163 | + const position = document.positionAt(offset); |
| 164 | + const completions = await server.sendCompletionRequest(document.uri, position); |
| 165 | + |
| 166 | + let completion = completions?.items.find(item => item.label === itemLabel); |
| 167 | + expect(completion).toBeDefined(); |
| 168 | + |
| 169 | + completion = await server.sendCompletionResolveRequest(completion!); |
| 170 | + expect(completion).toBeDefined(); |
| 171 | + |
| 172 | + const edits: TextEdit[] = []; |
| 173 | + |
| 174 | + if (completion.textEdit) { |
| 175 | + if (InsertReplaceEdit.is(completion.textEdit)) { |
| 176 | + edits.push({ newText: completion.textEdit.newText, range: completion.textEdit.replace }); |
| 177 | + } |
| 178 | + else { |
| 179 | + edits.push(completion.textEdit); |
| 180 | + } |
| 181 | + } |
| 182 | + else { |
| 183 | + edits.push({ |
| 184 | + newText: completion.insertText ?? completion.label, |
| 185 | + range: { start: position, end: position }, |
| 186 | + }); |
| 187 | + } |
| 188 | + if (completion.additionalTextEdits) { |
| 189 | + edits.push(...completion.additionalTextEdits); |
| 190 | + } |
| 191 | + if (edits.length === 0) { |
| 192 | + console.log(completion); |
| 193 | + } |
| 194 | + expect(edits.length).toBeGreaterThan(0); |
| 195 | + |
| 196 | + document = await server.updateTextDocument(document.uri, edits); |
| 197 | + |
| 198 | + expect(document.getText()).toMatchSnapshot(); |
| 199 | + } |
| 200 | + |
| 201 | + async function openDocument(fileName: string, languageId: string, content: string) { |
| 202 | + const server = await getLanguageServer(); |
| 203 | + const uri = URI.file(`${testWorkspacePath}/${fileName}`); |
| 204 | + const document = await server.openInMemoryDocument(uri.toString(), languageId, content); |
| 205 | + if (openedDocuments.every(d => d.uri !== document.uri)) { |
| 206 | + openedDocuments.push(document); |
| 207 | + } |
| 208 | + return document; |
| 209 | + } |
| 210 | +}); |
0 commit comments