Skip to content

Commit

Permalink
feat: redesign additional extensions, VitePress, PetiteVue support (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Apr 28, 2024
1 parent 80118ac commit f49e1b8
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 303 deletions.
49 changes: 16 additions & 33 deletions extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@
{
"name": "typescript-vue-plugin-bundle",
"enableForWorkspaceTypeScriptVersions": true,
"configNamespace": "typescript",
"languages": [
"vue"
]
"configNamespace": "typescript"
}
],
"grammars": [
Expand Down Expand Up @@ -236,32 +233,20 @@
],
"description": "Vue language server only handles CSS and HTML language support, and tsserver takes over TS language support via TS plugin."
},
"vue.server.includeLanguages": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"vue"
]
},
"vue.server.maxFileSize": {
"type": "number",
"default": 20971520,
"description": "Maximum file size for Vue Language Server to load. (default: 20MB)"
},
"vue.server.petiteVue.supportHtmlFile": {
"type": "boolean",
"default": false
},
"vue.server.vitePress.supportMdFile": {
"type": "boolean",
"default": false
},
"vue.server.diagnosticModel": {
"type": "string",
"default": "push",
"enum": [
"push",
"pull"
],
"enumDescriptions": [
"Diagnostic push by language server.",
"Diagnostic pull by language client."
],
"description": "Diagnostic update model."
},
"vue.server.maxOldSpaceSize": {
"type": [
"number",
Expand All @@ -270,14 +255,6 @@
"default": null,
"description": "Set --max-old-space-size option on server process. If you have problem on frequently \"Request textDocument/** failed.\" error, try setting higher memory(MB) on it."
},
"vue.server.additionalExtensions": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"description": "List any additional file extensions that should be processed as Vue files (requires restart)."
},
"vue.doctor.status": {
"type": "boolean",
"default": true,
Expand All @@ -290,6 +267,9 @@
},
"vue.splitEditors.layout.left": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"script",
"scriptSetup",
Expand All @@ -298,6 +278,9 @@
},
"vue.splitEditors.layout.right": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"template",
"customBlocks"
Expand Down
54 changes: 17 additions & 37 deletions extensions/vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let client: lsp.BaseLanguageClient;
type CreateLanguageClient = (
id: string,
name: string,
langs: lsp.DocumentFilter[],
langs: lsp.DocumentSelector,
initOptions: VueInitializationOptions,
port: number,
outputChannel: vscode.OutputChannel,
Expand All @@ -27,11 +27,7 @@ export async function activate(context: vscode.ExtensionContext, createLc: Creat
tryActivate();

function tryActivate() {
if (
vscode.window.visibleTextEditors.some(editor => editor.document.languageId === 'vue')
|| (config.server.vitePress.supportMdFile && vscode.window.visibleTextEditors.some(editor => editor.document.languageId === 'markdown'))
|| (config.server.petiteVue.supportHtmlFile && vscode.window.visibleTextEditors.some(editor => editor.document.languageId === 'html'))
) {
if (vscode.window.visibleTextEditors.some(editor => config.server.includeLanguages.includes(editor.document.languageId))) {
doActivate(context, createLc);
stopCheck.dispose();
}
Expand Down Expand Up @@ -210,30 +206,22 @@ function getCurrentHybridModeStatus(report = false) {

async function doActivate(context: vscode.ExtensionContext, createLc: CreateLanguageClient) {

vscode.commands.executeCommand('setContext', 'vue.activated', true);

getCurrentHybridModeStatus(true);

const outputChannel = vscode.window.createOutputChannel('Vue Language Server');

vscode.commands.executeCommand('setContext', 'vue.activated', true);
const selectors = config.server.includeLanguages;

client = createLc(
'vue',
'Vue',
getDocumentSelector(),
selectors,
await getInitializationOptions(context, enabledHybridMode),
6009,
outputChannel
);

const selectors: vscode.DocumentFilter[] = [{ language: 'vue' }];

if (config.server.petiteVue.supportHtmlFile) {
selectors.push({ language: 'html' });
}
if (config.server.vitePress.supportMdFile) {
selectors.push({ language: 'markdown' });
}

activateConfigWatcher();
activateRestartRequest();

Expand Down Expand Up @@ -410,6 +398,16 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
);
}
}
else if (e.affectsConfiguration('vue.server')) {
if (enabledHybridMode) {
if (e.affectsConfiguration('vue.server.includeLanguages')) {
requestReloadVscode('Please reload VSCode to apply the new language settings.');
}
}
else {
vscode.commands.executeCommand('vue.action.restartServer', false);
}
}
else if (e.affectsConfiguration('vue')) {
vscode.commands.executeCommand('vue.action.restartServer', false);
}
Expand All @@ -434,25 +432,12 @@ export function deactivate(): Thenable<any> | undefined {
return client?.stop();
}

export function getDocumentSelector(): lsp.DocumentFilter[] {
const selectors: lsp.DocumentFilter[] = [];
selectors.push({ language: 'vue' });
if (config.server.petiteVue.supportHtmlFile) {
selectors.push({ language: 'html' });
}
if (config.server.vitePress.supportMdFile) {
selectors.push({ language: 'markdown' });
}
return selectors;
}

async function getInitializationOptions(
context: vscode.ExtensionContext,
hybridMode: boolean,
): Promise<VueInitializationOptions> {
return {
// volar
diagnosticModel: config.server.diagnosticModel === 'pull' ? DiagnosticModel.Pull : DiagnosticModel.Push,
diagnosticModel: enabledHybridMode ? DiagnosticModel.Pull : DiagnosticModel.Push,
typescript: { tsdk: (await lsp.getTsdk(context)).tsdk },
maxFileSize: config.server.maxFileSize,
semanticTokensLegend: {
Expand All @@ -461,11 +446,6 @@ async function getInitializationOptions(
},
vue: {
hybridMode,
additionalExtensions: [
...config.server.additionalExtensions,
...!config.server.petiteVue.supportHtmlFile ? [] : ['html'],
...!config.server.vitePress.supportMdFile ? [] : ['md'],
],
},
};
};
9 changes: 1 addition & 8 deletions extensions/vscode/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@ export const config = {
return _config().get('doctor')!;
},
get server(): Readonly<{
includeLanguages: string[];
hybridMode: 'auto' | 'typeScriptPluginOnly' | boolean;
maxOldSpaceSize: number;
maxFileSize: number;
diagnosticModel: 'push' | 'pull';
additionalExtensions: string[];
vitePress: {
supportMdFile: boolean;
};
petiteVue: {
supportHtmlFile: boolean;
};
}> {
return _config().get('server')!;
},
Expand Down
17 changes: 14 additions & 3 deletions extensions/vscode/src/features/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,20 @@ export async function register(context: vscode.ExtensionContext, client: BaseLan
});
}

// check outdated vue language plugins
// check node_modules has more than one vue versions
// check ESLint, Prettier...
if (
vscode.workspace.getConfiguration('vue').has('server.additionalExtensions')
|| vscode.workspace.getConfiguration('vue').has('server.petiteVue.supportHtmlFile')
|| vscode.workspace.getConfiguration('vue').has('server.vitePress.supportMdFile')
) {
problems.push({
title: 'Deprecated configuration',
message: [
'`vue.server.additionalExtensions`, `vue.server.petiteVue.supportHtmlFile`, and `vue.server.vitePress.supportMdFile` are deprecated. Please remove them from your settings.',
'',
'- PR: https://github.com/vuejs/language-tools/pull/4321',
].join('\n'),
});
}

return problems;
}
Expand Down
6 changes: 1 addition & 5 deletions extensions/vscode/src/features/nameCasing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,7 @@ export async function activate(_context: vscode.ExtensionContext, client: BaseLa
}

async function update(document: vscode.TextDocument | undefined) {
if (
document?.languageId === 'vue'
|| (config.server.vitePress.supportMdFile && document?.languageId === 'markdown')
|| (config.server.petiteVue.supportHtmlFile && document?.languageId === 'html')
) {
if (document && vscode.languages.match(selector, document)) {
let detected: Awaited<ReturnType<typeof detect>> | undefined;
let attrNameCasing = attrNameCasings.get(document.uri.toString());
let tagNameCasing = tagNameCasings.get(document.uri.toString());
Expand Down
13 changes: 6 additions & 7 deletions extensions/vscode/src/nodeClientMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function activate(context: vscode.ExtensionContext) {
runOptions.execArgv.push("--max-old-space-size=" + config.server.maxOldSpaceSize);
}
const debugOptions: lsp.ForkOptions = { execArgv: ['--nolazy', '--inspect=' + port] };
let serverOptions: lsp.ServerOptions = {
const serverOptions: lsp.ServerOptions = {
run: {
module: serverModule.fsPath,
transport: lsp.TransportKind.ipc,
Expand All @@ -55,7 +55,7 @@ export async function activate(context: vscode.ExtensionContext) {
isTrusted: true,
supportHtml: true,
},
outputChannel
outputChannel,
};
const client = new _LanguageClient(
id,
Expand Down Expand Up @@ -145,18 +145,17 @@ try {
s => s + `.filter(p=>p.name!=='typescript-vue-plugin-bundle')`
);
}
else if (!enabledHybridMode) {
else if (enabledHybridMode) {
// patch readPlugins
text = text.replace(
'languages:Array.isArray(e.languages)',
[
'languages:',
`e.name==='typescript-vue-plugin-bundle'?[]:`,
'Array.isArray(e.languages)',
`e.name==='typescript-vue-plugin-bundle'?[${config.server.includeLanguages.map(lang => `"${lang}"`).join(',')}]`,
':Array.isArray(e.languages)',
].join(''),
);
}
else {

// VSCode < 1.87.0
text = text.replace('t.$u=[t.$r,t.$s,t.$p,t.$q]', s => s + '.concat("vue")'); // patch jsTsLanguageModes
text = text.replace('.languages.match([t.$p,t.$q,t.$r,t.$s]', s => s + '.concat("vue")'); // patch isSupportedLanguageMode
Expand Down
6 changes: 6 additions & 0 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ function createCheckerWorker(
if (parsedCommandLine.vueOptions.extensions.some(ext => fileName.endsWith(ext))) {
return 'vue';
}
if (parsedCommandLine.vueOptions.vitePressExtensions.some(ext => fileName.endsWith(ext))) {
return 'markdown';
}
if (parsedCommandLine.vueOptions.petiteVueExtensions.some(ext => fileName.endsWith(ext))) {
return 'html';
}
return vue.resolveCommonLanguageId(fileName);
},
scriptIdToFileName: id => id,
Expand Down

0 comments on commit f49e1b8

Please sign in to comment.