Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support volar.vueserver.json.customBlockSchemaUrls #2079

Merged
merged 1 commit into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions extensions/vscode-vue-language-features/package.json
Expand Up @@ -299,6 +299,9 @@
"type": "boolean",
"default": false
},
"volar.vueserver.json.customBlockSchemaUrls": {
"type": "object"
},
"volar.vueserver.textDocumentSync": {
"type": "string",
"default": "incremental",
Expand Down
3 changes: 3 additions & 0 deletions extensions/vscode-vue-language-features/src/common.ts
Expand Up @@ -278,6 +278,9 @@ function getInitializationOptions(
vitePress: {
processMdFile: processMd(),
},
json: {
customBlockSchemaUrls: vscode.workspace.getConfiguration('volar').get<Record<string, string>>('vueserver.json.customBlockSchemaUrls')
},
noProjectReferences: noProjectReferences(),
additionalExtensions: additionalExtensions()
};
Expand Down
5 changes: 4 additions & 1 deletion plugins/json/src/index.ts
Expand Up @@ -3,7 +3,7 @@ import * as json from 'vscode-json-languageservice';
import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';

export default function (): LanguageServicePlugin {
export default function (settings?: json.LanguageSettings): LanguageServicePlugin {

const jsonDocuments = new WeakMap<TextDocument, [number, json.JSONDocument]>();

Expand All @@ -15,6 +15,9 @@ export default function (): LanguageServicePlugin {
setup(_context) {
context = _context;
jsonLs = json.getLanguageService({ schemaRequestService: _context.env.schemaRequestService });
if (settings) {
jsonLs.configure(settings);
}
},

complete: {
Expand Down
Expand Up @@ -51,7 +51,18 @@ const plugin: LanguageServerPlugin<VueServerInitializationOptions, vue.LanguageS
return [vueLanguageModule];
},
getServicePlugins(host, service) {
return vue.getLanguageServicePlugins(host, service);
const settings: vue.Settings = {};
if (initOptions.json) {
settings.json = { schemas: [] };
for (const blockType in initOptions.json.customBlockSchemaUrls) {
const url = initOptions.json.customBlockSchemaUrls[blockType];
settings.json.schemas?.push({
fileMatch: [`*.customBlock_${blockType}_*.json*`],
uri: new URL(url, service.context.pluginContext.env.rootUri.toString() + '/').toString(),
});
}
}
return vue.getLanguageServicePlugins(host, service, settings);
},
onInitialize(connection, getService) {

Expand Down
13 changes: 8 additions & 5 deletions vue-language-tools/vue-language-server/src/types.ts
Expand Up @@ -2,13 +2,16 @@ import { LanguageServerInitializationOptions } from "@volar/language-server";

export type VueServerInitializationOptions = LanguageServerInitializationOptions & {
petiteVue?: {
processHtmlFile: boolean,
},
processHtmlFile: boolean;
};
vitePress?: {
processMdFile: boolean,
},
processMdFile: boolean;
};
json?: {
customBlockSchemaUrls?: Record<string, string>;
};
/**
* @example ['vue1', 'vue2']
*/
additionalExtensions?: string[],
additionalExtensions?: string[];
};
10 changes: 8 additions & 2 deletions vue-language-tools/vue-language-service/src/languageService.ts
Expand Up @@ -20,9 +20,14 @@ import useTwoslashQueries from './plugins/vue-twoslash-queries';
import useVueTemplateLanguagePlugin from './plugins/vue-template';
import type { Data } from '@volar-plugins/typescript/src/services/completions/basic';

export interface Settings {
json?: Parameters<typeof useJsonPlugin>[0];
}

export function getLanguageServicePlugins(
host: vue.LanguageServiceHost,
apis: embeddedLS.LanguageService,
settings?: Settings,
): embeddedLS.LanguageServicePlugin[] {

// plugins
Expand Down Expand Up @@ -144,7 +149,7 @@ export function getLanguageServicePlugins(
getVueDocument: (document) => apis.context.documents.get(document.uri),
});
const cssPlugin = useCssPlugin();
const jsonPlugin = useJsonPlugin();
const jsonPlugin = useJsonPlugin(settings?.json);
const emmetPlugin = useEmmetPlugin();
const autoDotValuePlugin = useAutoDotValuePlugin();
const referencesCodeLensPlugin = useReferencesCodeLensPlugin({
Expand Down Expand Up @@ -219,6 +224,7 @@ export function createLanguageService(
host: LanguageServiceHost,
env: embeddedLS.LanguageServicePluginContext['env'],
documentRegistry?: ts.DocumentRegistry,
settings?: Settings,
) {

const vueLanguageModule = vue.createLanguageModule(
Expand All @@ -233,7 +239,7 @@ export function createLanguageService(
host,
context: core,
getPlugins() {
return getLanguageServicePlugins(host, languageService);
return getLanguageServicePlugins(host, languageService, settings);
},
documentRegistry,
});
Expand Down