From c70c3f881b69e7f9ee43ead6fd1e51b0bd107ccf Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Fri, 13 Dec 2024 02:46:29 +0800 Subject: [PATCH 1/2] feat: mapping sfc compiler error outside of template inner content --- .../language-service/lib/plugins/vue-sfc.ts | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/language-service/lib/plugins/vue-sfc.ts b/packages/language-service/lib/plugins/vue-sfc.ts index 849f59bf55..652778beb7 100644 --- a/packages/language-service/lib/plugins/vue-sfc.ts +++ b/packages/language-service/lib/plugins/vue-sfc.ts @@ -10,7 +10,7 @@ import { loadLanguageBlocks } from './data'; let sfcDataProvider: html.IHTMLDataProvider | undefined; export function create(): LanguageServicePlugin { - const htmlPlugin = createHtmlService({ + const htmlService = createHtmlService({ documentSelector: ['vue-root-tags'], useDefaultDataProvider: false, getCustomData(context) { @@ -41,10 +41,17 @@ export function create(): LanguageServicePlugin { }, }); return { - ...htmlPlugin, + ...htmlService, name: 'vue-sfc', + capabilities: { + ...htmlService.capabilities, + diagnosticProvider: { + interFileDependencies: false, + workspaceDiagnostics: false, + } + }, create(context) { - const htmlPluginInstance = htmlPlugin.create(context); + const htmlPluginInstance = htmlService.create(context); return { @@ -73,6 +80,48 @@ export function create(): LanguageServicePlugin { return options; }, + provideDiagnostics(document, token) { + return worker(document, context, async vueSourceFile => { + const vueSfc = vueSourceFile._vueSfc.get(); + if (!vueSfc) { + return; + } + + const originalResult = await htmlPluginInstance.provideDiagnostics?.(document, token); + const sfcErrors: vscode.Diagnostic[] = []; + const { template } = vueSourceFile._sfc; + + const { + startTagEnd = Infinity, + endTagStart = -Infinity + } = template ?? {}; + + for (const error of vueSfc.errors) { + if ('code' in error) { + const start = error.loc?.start.offset ?? 0; + const end = error.loc?.end.offset ?? 0; + if (end < startTagEnd || start >= endTagStart) { + sfcErrors.push({ + range: { + start: document.positionAt(start), + end: document.positionAt(end), + }, + severity: 1 satisfies typeof vscode.DiagnosticSeverity.Error, + code: error.code, + source: 'vue', + message: error.message, + }); + } + } + } + + return [ + ...originalResult ?? [], + ...sfcErrors + ]; + }); + }, + provideDocumentSymbols(document) { return worker(document, context, vueSourceFile => { From 9029ecb0d6d0f4a5c3959c874674b3306b9d9762 Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Fri, 13 Dec 2024 02:52:05 +0800 Subject: [PATCH 2/2] refactor: rename --- packages/language-service/lib/plugins/vue-sfc.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/language-service/lib/plugins/vue-sfc.ts b/packages/language-service/lib/plugins/vue-sfc.ts index 652778beb7..34ae8de656 100644 --- a/packages/language-service/lib/plugins/vue-sfc.ts +++ b/packages/language-service/lib/plugins/vue-sfc.ts @@ -51,11 +51,11 @@ export function create(): LanguageServicePlugin { } }, create(context) { - const htmlPluginInstance = htmlService.create(context); + const htmlServiceInstance = htmlService.create(context); return { - ...htmlPluginInstance, + ...htmlServiceInstance, provideDocumentLinks: undefined, @@ -87,7 +87,7 @@ export function create(): LanguageServicePlugin { return; } - const originalResult = await htmlPluginInstance.provideDiagnostics?.(document, token); + const originalResult = await htmlServiceInstance.provideDiagnostics?.(document, token); const sfcErrors: vscode.Diagnostic[] = []; const { template } = vueSourceFile._sfc; @@ -211,7 +211,7 @@ export function create(): LanguageServicePlugin { }, async provideCompletionItems(document, position, context, token) { - const result = await htmlPluginInstance.provideCompletionItems?.(document, position, context, token); + const result = await htmlServiceInstance.provideCompletionItems?.(document, position, context, token); if (!result) { return; }