|
| 1 | +import type { LanguageServiceContext, LanguageServicePlugin } from '@volar/language-service'; |
| 2 | +import { VirtualGtsCode } from '../volar/gts-virtual-code.js'; |
| 3 | +import type * as vscode from 'vscode-languageserver-protocol'; |
| 4 | +import type { TextDocument } from 'vscode-languageserver-textdocument'; |
| 5 | +import { URI } from 'vscode-uri'; |
| 6 | + |
| 7 | +/** |
| 8 | + * This is a LanguageServicePlugin that provides language features for the top-level .gts/.gjs files. |
| 9 | + * For now, this just provides document symbols for `<template>` tags, which are a language |
| 10 | + * construct specific to .gts/.gjs files. Note that .gts/.gjs files will have TypeScript symbols |
| 11 | + * provided by our syntactic TS LanguageServicePlugin configured elsewhere, and these will be |
| 12 | + * combined with the symbols provided here. |
| 13 | + */ |
| 14 | +export function create(): LanguageServicePlugin { |
| 15 | + return { |
| 16 | + name: 'gts-gjs', |
| 17 | + capabilities: { |
| 18 | + documentSymbolProvider: true, |
| 19 | + }, |
| 20 | + create(context) { |
| 21 | + return { |
| 22 | + provideDocumentSymbols(document) { |
| 23 | + return worker(document, context, (root) => { |
| 24 | + const result: vscode.DocumentSymbol[] = []; |
| 25 | + const { transformedModule } = root; |
| 26 | + |
| 27 | + if (transformedModule) { |
| 28 | + const templateSymbols = transformedModule.templateSymbols(); |
| 29 | + for (const templateSymbol of templateSymbols) { |
| 30 | + result.push({ |
| 31 | + name: 'template', |
| 32 | + kind: 2 satisfies typeof vscode.SymbolKind.Module, |
| 33 | + range: { |
| 34 | + start: document.positionAt(templateSymbol.start), |
| 35 | + end: document.positionAt(templateSymbol.end), |
| 36 | + }, |
| 37 | + selectionRange: { |
| 38 | + start: document.positionAt(templateSymbol.start), |
| 39 | + end: document.positionAt(templateSymbol.startTagEnd), |
| 40 | + }, |
| 41 | + }); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return result; |
| 46 | + }); |
| 47 | + }, |
| 48 | + |
| 49 | + // TODO: this is copied from Vue; this might be the proper way for surfacing Glimmer syntax parsing errors to the top-level .gts file. |
| 50 | + // provideDiagnostics(document, token) { |
| 51 | + // return worker(document, context, async (root) => { |
| 52 | + // const { vueSfc, sfc } = root; |
| 53 | + // if (!vueSfc) { |
| 54 | + // return; |
| 55 | + // } |
| 56 | + |
| 57 | + // const originalResult = await htmlServiceInstance.provideDiagnostics?.(document, token); |
| 58 | + // const sfcErrors: vscode.Diagnostic[] = []; |
| 59 | + // const { template } = sfc; |
| 60 | + |
| 61 | + // const { startTagEnd = Infinity, endTagStart = -Infinity } = template ?? {}; |
| 62 | + |
| 63 | + // for (const error of vueSfc.errors) { |
| 64 | + // if ('code' in error) { |
| 65 | + // const start = error.loc?.start.offset ?? 0; |
| 66 | + // const end = error.loc?.end.offset ?? 0; |
| 67 | + // if (end < startTagEnd || start >= endTagStart) { |
| 68 | + // sfcErrors.push({ |
| 69 | + // range: { |
| 70 | + // start: document.positionAt(start), |
| 71 | + // end: document.positionAt(end), |
| 72 | + // }, |
| 73 | + // severity: 1 satisfies typeof vscode.DiagnosticSeverity.Error, |
| 74 | + // code: error.code, |
| 75 | + // source: 'vue', |
| 76 | + // message: error.message, |
| 77 | + // }); |
| 78 | + // } |
| 79 | + // } |
| 80 | + // } |
| 81 | + |
| 82 | + // return [...(originalResult ?? []), ...sfcErrors]; |
| 83 | + // }); |
| 84 | + // }, |
| 85 | + |
| 86 | + // TODO: this is copied from Vue; this might be a good place to implement auto-completing |
| 87 | + // the `<template>` tag and other top-level concerns for .gts files |
| 88 | + |
| 89 | + // async provideCompletionItems(document, position, context, token) { |
| 90 | + // const result = await htmlServiceInstance.provideCompletionItems?.( |
| 91 | + // document, |
| 92 | + // position, |
| 93 | + // context, |
| 94 | + // token, |
| 95 | + // ); |
| 96 | + // if (!result) { |
| 97 | + // return; |
| 98 | + // } |
| 99 | + // result.items = result.items.filter( |
| 100 | + // (item) => |
| 101 | + // item.label !== '!DOCTYPE' && item.label !== 'Custom Blocks' && item.label !== 'data-', |
| 102 | + // ); |
| 103 | + |
| 104 | + // const tags = sfcDataProvider?.provideTags(); |
| 105 | + |
| 106 | + // const scriptLangs = getLangs('script'); |
| 107 | + // const scriptItems = result.items.filter( |
| 108 | + // (item) => item.label === 'script' || item.label === 'script setup', |
| 109 | + // ); |
| 110 | + // for (const scriptItem of scriptItems) { |
| 111 | + // scriptItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File; |
| 112 | + // scriptItem.detail = '.js'; |
| 113 | + // for (const lang of scriptLangs) { |
| 114 | + // result.items.push({ |
| 115 | + // ...scriptItem, |
| 116 | + // detail: `.${lang}`, |
| 117 | + // kind: 17 satisfies typeof vscode.CompletionItemKind.File, |
| 118 | + // label: scriptItem.label + ' lang="' + lang + '"', |
| 119 | + // textEdit: scriptItem.textEdit |
| 120 | + // ? { |
| 121 | + // ...scriptItem.textEdit, |
| 122 | + // newText: scriptItem.textEdit.newText + ' lang="' + lang + '"', |
| 123 | + // } |
| 124 | + // : undefined, |
| 125 | + // }); |
| 126 | + // } |
| 127 | + // } |
| 128 | + |
| 129 | + // const styleLangs = getLangs('style'); |
| 130 | + // const styleItem = result.items.find((item) => item.label === 'style'); |
| 131 | + // if (styleItem) { |
| 132 | + // styleItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File; |
| 133 | + // styleItem.detail = '.css'; |
| 134 | + // for (const lang of styleLangs) { |
| 135 | + // result.items.push( |
| 136 | + // getStyleCompletionItem(styleItem, lang), |
| 137 | + // getStyleCompletionItem(styleItem, lang, 'scoped'), |
| 138 | + // getStyleCompletionItem(styleItem, lang, 'module'), |
| 139 | + // ); |
| 140 | + // } |
| 141 | + // } |
| 142 | + |
| 143 | + // const templateLangs = getLangs('template'); |
| 144 | + // const templateItem = result.items.find((item) => item.label === 'template'); |
| 145 | + // if (templateItem) { |
| 146 | + // templateItem.kind = 17 satisfies typeof vscode.CompletionItemKind.File; |
| 147 | + // templateItem.detail = '.html'; |
| 148 | + // for (const lang of templateLangs) { |
| 149 | + // if (lang === 'html') { |
| 150 | + // continue; |
| 151 | + // } |
| 152 | + // result.items.push({ |
| 153 | + // ...templateItem, |
| 154 | + // kind: 17 satisfies typeof vscode.CompletionItemKind.File, |
| 155 | + // detail: `.${lang}`, |
| 156 | + // label: templateItem.label + ' lang="' + lang + '"', |
| 157 | + // textEdit: templateItem.textEdit |
| 158 | + // ? { |
| 159 | + // ...templateItem.textEdit, |
| 160 | + // newText: templateItem.textEdit.newText + ' lang="' + lang + '"', |
| 161 | + // } |
| 162 | + // : undefined, |
| 163 | + // }); |
| 164 | + // } |
| 165 | + // } |
| 166 | + // return result; |
| 167 | + |
| 168 | + // function getLangs(label: string) { |
| 169 | + // return ( |
| 170 | + // tags |
| 171 | + // ?.find((tag) => tag.name === label) |
| 172 | + // ?.attributes.find((attr) => attr.name === 'lang') |
| 173 | + // ?.values?.map(({ name }) => name) ?? [] |
| 174 | + // ); |
| 175 | + // } |
| 176 | + // }, |
| 177 | + }; |
| 178 | + }, |
| 179 | + }; |
| 180 | + |
| 181 | + function worker<T>( |
| 182 | + document: TextDocument, |
| 183 | + context: LanguageServiceContext, |
| 184 | + callback: (root: VirtualGtsCode) => T, |
| 185 | + ): T | undefined { |
| 186 | + if (document.languageId !== 'glimmer-ts' && document.languageId !== 'glimmer-js') { |
| 187 | + return; |
| 188 | + } |
| 189 | + const uri = URI.parse(document.uri); |
| 190 | + const decoded = context.decodeEmbeddedDocumentUri(uri); |
| 191 | + const sourceScript = decoded && context.language.scripts.get(decoded[0]); |
| 192 | + const root = sourceScript?.generated?.root; |
| 193 | + if (root instanceof VirtualGtsCode) { |
| 194 | + return callback(root); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments