Skip to content

Commit

Permalink
feat: Config petite-vue, VitePress supports in settings (#1878)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Sep 16, 2022
1 parent 9a88ce0 commit 1520a66
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 161 deletions.
7 changes: 5 additions & 2 deletions examples/svelte/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import useCssPlugin from '@volar-plugins/css';
import useTsPlugin, { getSemanticTokenLegend } from '@volar-plugins/typescript';
import { LanguageServerPlugin } from '@volar/language-server';
import { createLanguageServer, EmbeddedLanguageModule, SourceFile } from '@volar/language-server/node';

const blocksReg = /\<(script|style)[\s\S]*?\>([\s\S]*?)\<\/\1\>/g;
Expand Down Expand Up @@ -109,7 +110,7 @@ function getEmbeddeds(fileName: string, text: string) {
});
}

createLanguageServer([{
const plugin: LanguageServerPlugin = () => ({
extensions: ['.svelte'],
languageService: {
semanticTokenLegend: getSemanticTokenLegend(),
Expand All @@ -134,4 +135,6 @@ createLanguageServer([{
];
}
},
}]);
});

createLanguageServer([plugin]);
13 changes: 8 additions & 5 deletions extensions/vscode-vue-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@
}
},
"contributes": {
"configurationDefaults": {
"[markdown]": {
"editor.quickSuggestions": true
}
},
"views": {
"explorer": [
{
Expand Down Expand Up @@ -307,6 +302,14 @@
"default": "off",
"description": "Traces the communication between VS Code and the language server."
},
"volar.vueserver.petiteVue.processHtmlFile": {
"type": "boolean",
"default": false
},
"volar.vueserver.vitePress.processMdFile": {
"type": "boolean",
"default": false
},
"volar.vueserver.textDocumentSync": {
"type": "string",
"default": "incremental",
Expand Down
52 changes: 32 additions & 20 deletions extensions/vscode-vue-language-features/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as doctor from './features/doctor';
import * as fileReferences from './features/fileReferences';
import * as reloadProject from './features/reloadProject';
import * as serverSys from './features/serverSys';
import { ServerInitializationOptions } from '@volar/vue-language-server';
import { VueServerInitializationOptions } from '@volar/vue-language-server';

let apiClient: lsp.BaseLanguageClient | undefined;
let docClient: lsp.BaseLanguageClient | undefined;
Expand All @@ -28,7 +28,7 @@ type CreateLanguageClient = (
id: string,
name: string,
documentSelector: lsp.DocumentSelector,
initOptions: ServerInitializationOptions,
initOptions: VueServerInitializationOptions,
port: number,
) => Promise<lsp.BaseLanguageClient>;

Expand All @@ -47,7 +47,7 @@ export async function activate(context: vscode.ExtensionContext, createLc: Creat
}

const currentlangId = vscode.window.activeTextEditor.document.languageId;
if (currentlangId === 'vue' || currentlangId === 'markdown' || currentlangId === 'html') {
if (currentlangId === 'vue' || (currentlangId === 'markdown' && processMd()) || (currentlangId === 'html' && processHtml())) {
doActivate(context, createLc);
stopCheck.dispose();
}
Expand All @@ -67,18 +67,14 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
const takeOverMode = takeOverModeEnabled();
const languageFeaturesDocumentSelector: lsp.DocumentSelector = takeOverMode ?
[
{ /* scheme: 'file', */ language: 'vue' },
{ /* scheme: 'file', */ language: 'markdown' },
{ /* scheme: 'file', */ language: 'html' },
{ /* scheme: 'file', */ language: 'javascript' },
{ /* scheme: 'file', */ language: 'typescript' },
{ /* scheme: 'file', */ language: 'javascriptreact' },
{ /* scheme: 'file', */ language: 'typescriptreact' },
{ /* scheme: 'file', */ language: 'json' },
{ language: 'vue' },
{ language: 'javascript' },
{ language: 'typescript' },
{ language: 'javascriptreact' },
{ language: 'typescriptreact' },
{ language: 'json' },
] : [
{ /* scheme: 'file', */ language: 'vue' },
{ /* scheme: 'file', */ language: 'markdown' },
{ /* scheme: 'file', */ language: 'html' },
{ language: 'vue' },
];
const documentFeaturesDocumentSelector: lsp.DocumentSelector = takeOverMode ?
[
Expand All @@ -91,10 +87,16 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
{ language: 'vue' },
];

if (enabledDocumentFeaturesInHtml()) {
if (processHtml()) {
languageFeaturesDocumentSelector.push({ language: 'html' });
documentFeaturesDocumentSelector.push({ language: 'html' });
}

if (processMd()) {
languageFeaturesDocumentSelector.push({ language: 'markdown' });
documentFeaturesDocumentSelector.push({ language: 'markdown' });
}

const _useSecondServer = useSecondServer();
const _serverMaxOldSpaceSize = serverMaxOldSpaceSize();

Expand Down Expand Up @@ -210,10 +212,6 @@ export function takeOverModeEnabled() {
return false;
}

function enabledDocumentFeaturesInHtml() {
return !vscode.extensions.getExtension('vscode.html-language-features');
}

function useSecondServer() {
return !!vscode.workspace.getConfiguration('volar').get<boolean>('vueserver.useSecondServer');
}
Expand All @@ -222,13 +220,27 @@ function serverMaxOldSpaceSize() {
return vscode.workspace.getConfiguration('volar').get<number | null>('vueserver.maxOldSpaceSize');
}

function processHtml() {
return !!vscode.workspace.getConfiguration('volar').get<boolean>('vueserver.petiteVue.processHtmlFile');
}

function processMd() {
return !!vscode.workspace.getConfiguration('volar').get<boolean>('vueserver.vitePress.processMdFile');
}

function getInitializationOptions(
context: vscode.ExtensionContext,
mode: 'main-language-features' | 'second-language-features' | 'document-features',
useSecondServer: boolean,
) {
const textDocumentSync = vscode.workspace.getConfiguration('volar').get<'incremental' | 'full' | 'none'>('vueserver.textDocumentSync');
const initializationOptions: ServerInitializationOptions = {
const initializationOptions: VueServerInitializationOptions = {
petiteVue: {
processHtmlFile: processHtml(),
},
vitePress: {
processMdFile: processMd(),
},
textDocumentSync: textDocumentSync ? {
incremental: lsp.TextDocumentSyncKind.Incremental,
full: lsp.TextDocumentSyncKind.Full,
Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/features/customFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { LanguageServerPlugin } from '../types';
export function register(
connection: vscode.Connection,
projects: Workspaces,
plugins: LanguageServerPlugin[],
plugins: ReturnType<LanguageServerPlugin>[],
) {
connection.onRequest(GetMatchTsConfigRequest.type, async params => {
return (await projects.getProject(params.uri))?.tsconfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as vscode from 'vscode-languageserver';
export function register(
features: NonNullable<ServerInitializationOptions['languageFeatures']>,
server: vscode.ServerCapabilities,
plugins: LanguageServerPlugin[],
plugins: ReturnType<LanguageServerPlugin>[],
) {
if (features.references) {
server.referencesProvider = true;
Expand Down
4 changes: 3 additions & 1 deletion packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createWorkspaces } from './utils/workspaces';
export function createCommonLanguageServer(
connection: vscode.Connection,
runtimeEnv: RuntimeEnvironment,
plugins: LanguageServerPlugin[],
_plugins: LanguageServerPlugin[],
) {

let params: vscode.InitializeParams;
Expand All @@ -19,13 +19,15 @@ export function createCommonLanguageServer(
let projects: ReturnType<typeof createWorkspaces> | undefined;
let documentServiceHost: ReturnType<typeof createDocumentServiceHost> | undefined;
let configHost: ReturnType<typeof createConfigurationHost> | undefined;
let plugins: ReturnType<LanguageServerPlugin>[];

const documents = createSnapshots(connection);

connection.onInitialize(async _params => {

params = _params;
options = params.initializationOptions as any;
plugins = _plugins.map(plugin => plugin(options));

if (params.capabilities.workspace?.workspaceFolders && params.workspaceFolders) {
roots = params.workspaceFolders.map(folder => URI.parse(folder.uri));
Expand Down
14 changes: 9 additions & 5 deletions packages/language-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export interface RuntimeEnvironment {
) => FileSystemHost,
}

export type LanguageServerPlugin<A extends embedded.LanguageServiceHost = embedded.LanguageServiceHost, B = embeddedLS.LanguageService> = {
export type LanguageServerPlugin<
A extends ServerInitializationOptions = ServerInitializationOptions,
B extends embedded.LanguageServiceHost = embedded.LanguageServiceHost,
C = embeddedLS.LanguageService
> = (initOptions: A) => {

extensions: string[],

Expand All @@ -51,18 +55,18 @@ export type LanguageServerPlugin<A extends embedded.LanguageServiceHost = embedd
sys: FileSystem,
tsConfig: string | ts.CompilerOptions,
host: embedded.LanguageServiceHost,
): A,
): B,

getLanguageModules?(host: A): embedded.EmbeddedLanguageModule[],
getLanguageModules?(host: B): embedded.EmbeddedLanguageModule[],

getServicePlugins?(
host: A,
host: B,
service: embeddedLS.LanguageService,
): embeddedLS.EmbeddedLanguageServicePlugin[],

onInitialize?(
connection: vscode.Connection,
getLangaugeService: (uri: string) => Promise<B>,
getLangaugeService: (uri: string) => Promise<C>,
): void,
},

Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/utils/documentServiceHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type * as _ from 'vscode-languageserver-textdocument';

export function createDocumentServiceHost(
runtimeEnv: RuntimeEnvironment,
plugins: LanguageServerPlugin[],
plugins: ReturnType<LanguageServerPlugin>[],
ts: typeof import('typescript/lib/tsserverlibrary'),
configHost: ConfigurationHost | undefined,
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/language-server/src/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface Project extends ReturnType<typeof createProject> { }

export async function createProject(
runtimeEnv: RuntimeEnvironment,
plugins: LanguageServerPlugin[],
plugins: ReturnType<LanguageServerPlugin>[],
fsHost: FileSystemHost,
sys: FileSystem,
ts: typeof import('typescript/lib/tsserverlibrary'),
Expand Down Expand Up @@ -238,7 +238,7 @@ function createParsedCommandLine(
sys: FileSystem,
rootPath: string,
tsConfig: string | ts.CompilerOptions,
plugins: LanguageServerPlugin[],
plugins: ReturnType<LanguageServerPlugin>[],
): ts.ParsedCommandLine {
const extraExts = plugins.map(plugin => plugin.extensions).flat();
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/utils/workspaceProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const rootTsConfigNames = ['tsconfig.json', 'jsconfig.json'];

export async function createWorkspaceProjects(
runtimeEnv: RuntimeEnvironment,
plugins: LanguageServerPlugin[],
plugins: ReturnType<LanguageServerPlugin>[],
fsHost: FileSystemHost,
rootUri: URI,
ts: typeof import('typescript/lib/tsserverlibrary'),
Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/utils/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Workspaces extends ReturnType<typeof createWorkspaces> { }

export function createWorkspaces(
runtimeEnv: RuntimeEnvironment,
plugins: LanguageServerPlugin[],
plugins: ReturnType<LanguageServerPlugin>[],
fsHost: FileSystemHost,
configurationHost: ConfigurationHost | undefined,
ts: typeof import('typescript/lib/tsserverlibrary'),
Expand Down
1 change: 1 addition & 0 deletions packages/vue-component-meta/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function createComponentMetaCheckerBase(tsconfigPath: string, parsedCommandLine:
host.getCurrentDirectory(),
host.getCompilationSettings(),
host.getVueCompilationSettings(),
['.vue']
);
const core = embedded.createEmbeddedLanguageServiceHost(host, [vueLanguageModule]);
const proxyApis: Partial<ts.LanguageServiceHost> = checkerOptions.forceUseTs ? {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/languageModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export function createEmbeddedLanguageModule(
rootDir: string,
compilerOptions: ts.CompilerOptions,
_vueCompilerOptions: VueCompilerOptions,
exts: string[],
extraPlugins: VueLanguagePlugin[] = [],
exts: string[] = ['.vue', '.html', '.md'],
): embedded.EmbeddedLanguageModule {

const vueLanguagePlugin = getDefaultVueLanguagePlugins(
Expand Down

0 comments on commit 1520a66

Please sign in to comment.