Skip to content

Commit 9b41ca1

Browse files
committed
fix(language-service): html custom data not working
close #3975
1 parent 1c206e7 commit 9b41ca1

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

packages/language-service/lib/plugins/vue-template.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { TextDocument } from 'vscode-languageserver-textdocument';
99
import { getNameCasing } from '../ideFeatures/nameCasing';
1010
import { AttrNameCasing, ServicePlugin, TagNameCasing, VueCompilerOptions } from '../types';
1111
import { loadModelModifiersData, loadTemplateData } from './data';
12+
import { URI, Utils } from 'vscode-uri';
1213

1314
let builtInData: html.HTMLDataV1;
1415
let modelData: html.HTMLDataV1;
@@ -21,6 +22,7 @@ export function create(
2122
): ServicePlugin {
2223

2324
let customData: html.IHTMLDataProvider[] = [];
25+
let extraCustomData: html.IHTMLDataProvider[] = [];
2426

2527
const onDidChangeCustomDataListeners = new Set<() => void>();
2628
const onDidChangeCustomData = (listener: () => void): Disposable => {
@@ -34,7 +36,10 @@ export function create(
3436
const baseServicePlugin = mode === 'pug' ? createPugService : createHtmlService;
3537
const baseService = baseServicePlugin({
3638
getCustomData() {
37-
return customData;
39+
return [
40+
...customData,
41+
...extraCustomData,
42+
];
3843
},
3944
onDidChangeCustomData,
4045
});
@@ -83,10 +88,19 @@ export function create(
8388
}
8489
}
8590

91+
const disposable = context.env.onDidChangeConfiguration?.(() => initializing = undefined);
92+
93+
let initializing: Promise<void> | undefined;
94+
8695
return {
8796

8897
...baseServiceInstance,
8998

99+
dispose() {
100+
baseServiceInstance.dispose?.();
101+
disposable?.dispose();
102+
},
103+
90104
async provideCompletionItems(document, position, completionContext, token) {
91105

92106
if (!isSupportedDocument(document))
@@ -241,7 +255,7 @@ export function create(
241255
return;
242256

243257
if (context.documents.getVirtualCodeByUri(document.uri)[0])
244-
updateCustomData([]);
258+
updateExtraCustomData([]);
245259

246260
return baseServiceInstance.provideHover?.(document, position, token);
247261
},
@@ -307,6 +321,8 @@ export function create(
307321

308322
async function provideHtmlData(sourceDocumentUri: string, vueCode: VueGeneratedCode) {
309323

324+
await (initializing ??= initialize());
325+
310326
const casing = await getNameCasing(context, sourceDocumentUri, tsPluginClient);
311327

312328
if (builtInData.tags) {
@@ -337,7 +353,7 @@ export function create(
337353
let components: string[] | undefined;
338354
let templateContextProps: string[] | undefined;
339355

340-
updateCustomData([
356+
updateExtraCustomData([
341357
html.newHTMLDataProvider('vue-template-built-in', builtInData),
342358
{
343359
getId: () => 'vue-template',
@@ -675,7 +691,31 @@ export function create(
675691
}
676692
}
677693

678-
updateCustomData([]);
694+
updateExtraCustomData([]);
695+
}
696+
697+
698+
async function initialize() {
699+
customData = await getHtmlCustomData();
700+
}
701+
702+
async function getHtmlCustomData() {
703+
const customData: string[] = await context.env.getConfiguration?.('html.customData') ?? [];
704+
const newData: html.IHTMLDataProvider[] = [];
705+
for (const customDataPath of customData) {
706+
const uri = Utils.resolvePath(URI.parse(context.env.workspaceFolder), customDataPath);
707+
const json = await context.env.fs?.readFile?.(uri.toString());
708+
if (json) {
709+
try {
710+
const data = JSON.parse(json);
711+
newData.push(html.newHTMLDataProvider(customDataPath, data));
712+
}
713+
catch (error) {
714+
console.error(error);
715+
}
716+
}
717+
}
718+
return newData;
679719
}
680720
},
681721
};
@@ -692,8 +732,8 @@ export function create(
692732
}
693733
}
694734

695-
function updateCustomData(extraData: html.IHTMLDataProvider[]) {
696-
customData = extraData;
735+
function updateExtraCustomData(extraData: html.IHTMLDataProvider[]) {
736+
extraCustomData = extraData;
697737
onDidChangeCustomDataListeners.forEach(l => l());
698738
}
699739

0 commit comments

Comments
 (0)