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

refactor(language-server): simplify startServer abstraction #113

Merged
merged 3 commits into from
Dec 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 8 additions & 35 deletions packages/language-server/browser.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { FileType } from '@volar/language-service';
import * as vscode from 'vscode-languageserver/browser';
import { startLanguageServerBase } from './lib/server';
import type { ServerPlugin, ServerProjectProvider } from './lib/types';
import httpSchemaRequestHandler from './lib/schemaRequestHandlers/http';
import { URI } from 'vscode-uri';
import { FsReadFileRequest, FsReadDirectoryRequest, FsStatRequest } from './protocol';
import { FileType } from '@volar/language-service';
import { createSimpleProjectProvider, type WorkspacesContext } from './lib/project/simpleProjectProvider';
import { createTypeScriptProjectProvider } from './lib/project/typescriptProjectProvider';
import httpSchemaRequestHandler from './lib/schemaRequestHandlers/http';
import { createServer } from './lib/server';
import { FsReadDirectoryRequest, FsReadFileRequest, FsStatRequest } from './protocol';

export * from 'vscode-languageserver/browser';
export * from './index';
export * from './lib/project/simpleProjectProvider';
export * from './lib/project/typescriptProjectProvider';

export function createConnection() {

Expand All @@ -20,34 +19,8 @@ export function createConnection() {
return connection;
}

export function startSimpleServer(
connection: vscode.Connection,
...plugins: ServerPlugin[]
) {
return startServer(
connection,
createSimpleProjectProvider,
...plugins,
);
}

export function startTypeScriptServer(
connection: vscode.Connection,
...plugins: ServerPlugin[]
) {
return startServer(
connection,
createTypeScriptProjectProvider,
...plugins,
);
}

function startServer(
connection: vscode.Connection,
createProjectProvider: (context: WorkspacesContext, plugins: ReturnType<ServerPlugin>[]) => ServerProjectProvider,
...plugins: ServerPlugin[]
) {
startLanguageServerBase(connection, plugins, createProjectProvider, () => ({
export function createBrowserServer(connection: vscode.Connection) {
return createServer(connection, () => ({
uriToFileName,
fileNameToUri,
console: connection.console,
Expand Down
45 changes: 0 additions & 45 deletions packages/language-server/lib/config.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/language-server/lib/project/simpleProject.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { LanguageService, ServiceEnvironment, createFileProvider, createLanguageService } from '@volar/language-service';
import { getConfig } from '../config';
import type { ServerProject, ServerPlugin } from '../types';
import type { ServerOptions } from '../server';
import type { ServerProject } from '../types';
import type { WorkspacesContext } from './simpleProjectProvider';

export async function createSimpleServerProject(
context: WorkspacesContext,
plugins: ReturnType<ServerPlugin>[],
serviceEnv: ServiceEnvironment,
serverOptions: ServerOptions,
): Promise<ServerProject> {

let languageService: LanguageService | undefined;

const config = await getConfig(context, plugins, serviceEnv, {});
const { languagePlugins, servicePlugins } = await serverOptions.getProjectSetup(serviceEnv, {});

return {
serviceEnv,
Expand All @@ -24,7 +24,7 @@ export async function createSimpleServerProject(

function getLanguageService() {
if (!languageService) {
const files = createFileProvider(Object.values(config.languages ?? {}), false, fileName => {
const files = createFileProvider(languagePlugins, false, fileName => {
const uri = context.server.runtimeEnv.fileNameToUri(fileName);
const script = context.workspaces.documents.get(uri);
if (script) {
Expand All @@ -36,7 +36,7 @@ export async function createSimpleServerProject(
});
languageService = createLanguageService(
{ files },
Object.values(config.services ?? {}),
servicePlugins,
serviceEnv,
);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/language-server/lib/project/simpleProjectProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { SnapshotDocument } from '@volar/snapshot-document';
import type * as ts from 'typescript/lib/tsserverlibrary';
import type { TextDocuments } from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import type { ServerContext } from '../server';
import type { InitializationOptions, ServerProject, ServerProjectProvider, ServerPlugin } from '../types';
import type { ServerContext, ServerOptions } from '../server';
import type { InitializationOptions, ServerProject, ServerProjectProvider } from '../types';
import { isFileInDir } from '../utils/isFileInDir';
import type { WorkspaceFolderManager } from '../workspaceFolderManager';
import { createSimpleServerProject } from './simpleProject';
Expand All @@ -21,7 +21,7 @@ export interface WorkspacesContext extends ServerContext {
};
}

export function createSimpleProjectProvider(context: WorkspacesContext, plugins: ReturnType<ServerPlugin>[]): ServerProjectProvider {
export function createSimpleProjectProvider(context: WorkspacesContext, serverOptions: ServerOptions): ServerProjectProvider {

const projects = new Map<ServiceEnvironment['workspaceFolder'], Promise<ServerProject>>();

Expand All @@ -33,7 +33,7 @@ export function createSimpleProjectProvider(context: WorkspacesContext, plugins:
let projectPromise = projects.get(workspaceFolder);
if (!projectPromise) {
const serviceEnv = createServiceEnvironment(context, workspaceFolder);
projectPromise = createSimpleServerProject(context, plugins, serviceEnv);
projectPromise = createSimpleServerProject(context, serviceEnv, serverOptions);
projects.set(workspaceFolder, projectPromise);
}

Expand Down
20 changes: 10 additions & 10 deletions packages/language-server/lib/project/typescriptProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { createLanguage, createSys } from '@volar/typescript';
import * as path from 'path-browserify';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as vscode from 'vscode-languageserver';
import { getConfig } from '../config';
import type { ServerProject, ServerPlugin } from '../types';
import type { ProjectContext, ServerProject } from '../types';
import { UriMap, createUriMap } from '../utils/uriMap';
import type { WorkspacesContext } from './simpleProjectProvider';
import type { ServerOptions } from '../server';

export interface TypeScriptServerProject extends ServerProject {
askedFiles: UriMap<boolean>;
Expand All @@ -17,8 +17,8 @@ export interface TypeScriptServerProject extends ServerProject {
export async function createTypeScriptServerProject(
tsconfig: string | ts.CompilerOptions,
context: WorkspacesContext,
plugins: ReturnType<ServerPlugin>[],
serviceEnv: ServiceEnvironment,
serverOptions: ServerOptions,
): Promise<TypeScriptServerProject> {

if (!context.workspaces.ts) {
Expand Down Expand Up @@ -50,12 +50,13 @@ export async function createTypeScriptServerProject(
let projectVersion = 0;
let languageService: LanguageService | undefined;

const config = await getConfig(context, plugins, serviceEnv, {
const projectContext: ProjectContext = {
typescript: {
parsedCommandLine: parsedCommandLine!,
configFileName: typeof tsconfig === 'string' ? tsconfig : undefined,
},
});
};
const { languagePlugins, servicePlugins } = await serverOptions.getProjectSetup(serviceEnv, projectContext);
const askedFiles = createUriMap<boolean>(fileNameToUri);
const docChangeWatcher = context.workspaces.documents.onDidChangeContent(() => {
projectVersion++;
Expand Down Expand Up @@ -85,7 +86,7 @@ export async function createTypeScriptServerProject(
sys,
uriToFileName(serviceEnv.workspaceFolder.uri.toString()),
tsconfig,
plugins,
serverOptions.typescript?.extraFileExtensions ?? [],
);
return parsedCommandLine.fileNames;
}
Expand All @@ -94,13 +95,13 @@ export async function createTypeScriptServerProject(
const language = createLanguage(
ts,
sys,
Object.values(config.languages ?? {}),
languagePlugins,
typeof tsconfig === 'string' ? tsconfig : undefined,
host,
);
languageService = createLanguageService(
language,
Object.values(config.services ?? {}),
servicePlugins,
serviceEnv,
);
}
Expand Down Expand Up @@ -129,9 +130,8 @@ async function createParsedCommandLine(
sys: ReturnType<typeof createSys>,
workspacePath: string,
tsconfig: string | ts.CompilerOptions,
plugins: ReturnType<ServerPlugin>[],
extraFileExtensions: ts.FileExtensionInfo[],
): Promise<ts.ParsedCommandLine> {
const extraFileExtensions = plugins.map(plugin => plugin.typescript?.extraFileExtensions ?? []).flat();
let content: ts.ParsedCommandLine = {
errors: [],
fileNames: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import * as path from 'path-browserify';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as vscode from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import type { ServerProjectProvider, ServerPlugin } from '../types';
import type { ServerProjectProvider } from '../types';
import { isFileInDir } from '../utils/isFileInDir';
import { createUriMap } from '../utils/uriMap';
import { getInferredCompilerOptions } from './inferredCompilerOptions';
import { WorkspacesContext, createServiceEnvironment, getWorkspaceFolder } from './simpleProjectProvider';
import { TypeScriptServerProject, createTypeScriptServerProject } from './typescriptProject';
import type { ServerOptions } from '../server';

const rootTsConfigNames = ['tsconfig.json', 'jsconfig.json'];

export function createTypeScriptProjectProvider(
context: WorkspacesContext,
plugins: ReturnType<ServerPlugin>[]
): ServerProjectProvider {
export function createTypeScriptProjectProvider(context: WorkspacesContext, serverOptions: ServerOptions): ServerProjectProvider {

const { fileNameToUri, uriToFileName, fs } = context.server.runtimeEnv;
const configProjects = createUriMap<Promise<TypeScriptServerProject>>(fileNameToUri);
Expand Down Expand Up @@ -227,7 +225,7 @@ export function createTypeScriptProjectProvider(
if (!projectPromise) {
const workspaceFolder = getWorkspaceFolder(fileNameToUri(tsconfig), context.workspaces.workspaceFolders, uriToFileName);
const serviceEnv = createServiceEnvironment(context, workspaceFolder);
projectPromise = createTypeScriptServerProject(tsconfig, context, plugins, serviceEnv);
projectPromise = createTypeScriptServerProject(tsconfig, context, serviceEnv, serverOptions);
configProjects.pathSet(tsconfig, projectPromise);
}
return projectPromise;
Expand All @@ -239,7 +237,7 @@ export function createTypeScriptProjectProvider(
inferredProjects.uriSet(workspaceFolder.uri.toString(), (async () => {
const inferOptions = await getInferredCompilerOptions(context.server.configurationHost);
const serviceEnv = createServiceEnvironment(context, workspaceFolder);
return createTypeScriptServerProject(inferOptions, context, plugins, serviceEnv);
return createTypeScriptServerProject(inferOptions, context, serviceEnv, serverOptions);
})());
}

Expand Down
Loading
Loading