Skip to content

Commit 9d59cb9

Browse files
committed
Reduce duplicate code
Merge the `SyntaxRoutingTsServer` and `ProjectLoadingRoutingSyntaxTsServer` classes since these only differ routing to the syntax server while a project is loading
1 parent d0c1d2e commit 9d59cb9

3 files changed

Lines changed: 36 additions & 96 deletions

File tree

extensions/typescript-language-features/src/tsServer/server.ts

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -379,75 +379,6 @@ class RequestRouter {
379379
}
380380
}
381381

382-
383-
const syntaxAlwaysCommands: ReadonlySet<keyof TypeScriptRequests> = new Set<keyof TypeScriptRequests>([
384-
'navtree',
385-
'getOutliningSpans',
386-
'jsxClosingTag',
387-
'selectionRange',
388-
'format',
389-
'formatonkey',
390-
'docCommentTemplate',
391-
]);
392-
393-
export class SyntaxRoutingTsServer extends Disposable implements ITypeScriptServer {
394-
395-
private readonly syntaxServer: ITypeScriptServer;
396-
private readonly semanticServer: ITypeScriptServer;
397-
private readonly router: RequestRouter;
398-
399-
public constructor(
400-
servers: { syntax: ITypeScriptServer, semantic: ITypeScriptServer },
401-
delegate: TsServerDelegate,
402-
) {
403-
super();
404-
405-
this.syntaxServer = servers.syntax;
406-
this.semanticServer = servers.semantic;
407-
408-
this.router = new RequestRouter(
409-
[
410-
{ server: this.syntaxServer, canRun: (command) => syntaxAlwaysCommands.has(command) },
411-
{ server: this.semanticServer, canRun: undefined /* gets all other commands */ }
412-
],
413-
delegate);
414-
415-
this._register(this.syntaxServer.onEvent(e => this._onEvent.fire(e)));
416-
this._register(this.semanticServer.onEvent(e => this._onEvent.fire(e)));
417-
418-
this._register(this.semanticServer.onExit(e => {
419-
this._onExit.fire(e);
420-
this.syntaxServer.kill();
421-
}));
422-
this._register(this.semanticServer.onError(e => this._onError.fire(e)));
423-
}
424-
425-
private readonly _onEvent = this._register(new vscode.EventEmitter<Proto.Event>());
426-
public readonly onEvent = this._onEvent.event;
427-
428-
private readonly _onExit = this._register(new vscode.EventEmitter<any>());
429-
public readonly onExit = this._onExit.event;
430-
431-
private readonly _onError = this._register(new vscode.EventEmitter<any>());
432-
public readonly onError = this._onError.event;
433-
434-
public get onReaderError() { return this.semanticServer.onReaderError; }
435-
436-
public get tsServerLogFile() { return this.semanticServer.tsServerLogFile; }
437-
438-
public kill(): void {
439-
this.syntaxServer.kill();
440-
this.semanticServer.kill();
441-
}
442-
443-
public executeImpl(command: keyof TypeScriptRequests, args: any, executeInfo: { isAsync: boolean, token?: vscode.CancellationToken, expectsResult: false, lowPriority?: boolean }): undefined;
444-
public executeImpl(command: keyof TypeScriptRequests, args: any, executeInfo: { isAsync: boolean, token?: vscode.CancellationToken, expectsResult: boolean, lowPriority?: boolean }): Promise<ServerResponse.Response<Proto.Response>>;
445-
public executeImpl(command: keyof TypeScriptRequests, args: any, executeInfo: { isAsync: boolean, token?: vscode.CancellationToken, expectsResult: boolean, lowPriority?: boolean }): Promise<ServerResponse.Response<Proto.Response>> | undefined {
446-
return this.router.execute(command, args, executeInfo);
447-
}
448-
}
449-
450-
451382
export class GetErrRoutingTsServer extends Disposable implements ITypeScriptServer {
452383

453384
private static readonly diagnosticEvents = new Set<string>([
@@ -525,7 +456,20 @@ export class GetErrRoutingTsServer extends Disposable implements ITypeScriptServ
525456
}
526457

527458

528-
export class ProjectLoadingRoutingSyntaxTsServer extends Disposable implements ITypeScriptServer {
459+
export class SyntaxRoutingTsServer extends Disposable implements ITypeScriptServer {
460+
461+
/**
462+
* Commands that should always be run on the syntax server.
463+
*/
464+
private static readonly syntaxAlwaysCommands = new Set<keyof TypeScriptRequests>([
465+
'navtree',
466+
'getOutliningSpans',
467+
'jsxClosingTag',
468+
'selectionRange',
469+
'format',
470+
'formatonkey',
471+
'docCommentTemplate',
472+
]);
529473

530474
/**
531475
* Commands that should always be run on the semantic server.
@@ -539,7 +483,7 @@ export class ProjectLoadingRoutingSyntaxTsServer extends Disposable implements I
539483
/**
540484
* Commands that can be run on the syntax server but would benefit from being upgraded to the semantic server.
541485
*/
542-
private syntaxAllowedCommands = new Set<keyof TypeScriptRequests>([
486+
private static readonly syntaxAllowedCommands = new Set<keyof TypeScriptRequests>([
543487
'completions',
544488
'completionEntryDetails',
545489
'completionInfo',
@@ -563,6 +507,7 @@ export class ProjectLoadingRoutingSyntaxTsServer extends Disposable implements I
563507
public constructor(
564508
servers: { syntax: ITypeScriptServer, semantic: ITypeScriptServer },
565509
delegate: TsServerDelegate,
510+
enableDynamicRouting: boolean,
566511
) {
567512
super();
568513

@@ -574,13 +519,13 @@ export class ProjectLoadingRoutingSyntaxTsServer extends Disposable implements I
574519
{
575520
server: this.syntaxServer,
576521
canRun: (command) => {
577-
if (syntaxAlwaysCommands.has(command)) {
522+
if (SyntaxRoutingTsServer.syntaxAlwaysCommands.has(command)) {
578523
return true;
579524
}
580-
if (ProjectLoadingRoutingSyntaxTsServer.semanticCommands.has(command)) {
525+
if (SyntaxRoutingTsServer.semanticCommands.has(command)) {
581526
return false;
582527
}
583-
if (this.projectLoading && this.syntaxAllowedCommands.has(command)) {
528+
if (enableDynamicRouting && this.projectLoading && SyntaxRoutingTsServer.syntaxAllowedCommands.has(command)) {
584529
return true;
585530
}
586531
return false;

extensions/typescript-language-features/src/tsServer/spawner.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as stream from 'stream';
99
import * as vscode from 'vscode';
1010
import type * as Proto from '../protocol';
1111
import API from '../utils/api';
12-
import { TsServerLogLevel, TypeScriptServiceConfiguration, SeparateSyntaxServerConfigration } from '../utils/configuration';
12+
import { SeparateSyntaxServerConfiguration, TsServerLogLevel, TypeScriptServiceConfiguration } from '../utils/configuration';
1313
import * as electron from '../utils/electron';
1414
import LogDirectoryProvider from '../utils/logDirectoryProvider';
1515
import Logger from '../utils/logger';
@@ -18,7 +18,7 @@ import { PluginManager } from '../utils/plugins';
1818
import { TelemetryReporter } from '../utils/telemetry';
1919
import Tracer from '../utils/tracer';
2020
import { TypeScriptVersion, TypeScriptVersionProvider } from '../utils/versionProvider';
21-
import { ITypeScriptServer, PipeRequestCanceller, ProcessBasedTsServer, SyntaxRoutingTsServer, TsServerProcess, TsServerDelegate, GetErrRoutingTsServer, ProjectLoadingRoutingSyntaxTsServer } from './server';
21+
import { GetErrRoutingTsServer, ITypeScriptServer, PipeRequestCanceller, ProcessBasedTsServer, SyntaxRoutingTsServer, TsServerDelegate, TsServerProcess } from './server';
2222

2323
const enum ServerKind {
2424
Main = 'main',
@@ -34,7 +34,7 @@ const enum CompositeServerType {
3434
/** Run a separate server for syntax commands */
3535
SeparateSyntax,
3636

37-
/** Use a separate suntax server while the project is loading */
37+
/** Use a separate syntax server while the project is loading */
3838
DynamicSeparateSyntax,
3939
}
4040

@@ -55,21 +55,16 @@ export class TypeScriptServerSpawner {
5555
delegate: TsServerDelegate,
5656
): ITypeScriptServer {
5757
let primaryServer: ITypeScriptServer;
58-
switch (this.getCompositeServerType(version, configuration)) {
58+
const serverType = this.getCompositeServerType(version, configuration);
59+
switch (serverType) {
5960
case CompositeServerType.SeparateSyntax:
60-
{
61-
primaryServer = new SyntaxRoutingTsServer({
62-
syntax: this.spawnTsServer(ServerKind.Syntax, version, configuration, pluginManager),
63-
semantic: this.spawnTsServer(ServerKind.Semantic, version, configuration, pluginManager)
64-
}, delegate);
65-
break;
66-
}
6761
case CompositeServerType.DynamicSeparateSyntax:
6862
{
69-
primaryServer = new ProjectLoadingRoutingSyntaxTsServer({
63+
const enableDynamicRouting = serverType === CompositeServerType.DynamicSeparateSyntax;
64+
primaryServer = new SyntaxRoutingTsServer({
7065
syntax: this.spawnTsServer(ServerKind.Syntax, version, configuration, pluginManager),
7166
semantic: this.spawnTsServer(ServerKind.Semantic, version, configuration, pluginManager)
72-
}, delegate);
67+
}, delegate, enableDynamicRouting);
7368
break;
7469
}
7570
case CompositeServerType.Single:
@@ -94,13 +89,13 @@ export class TypeScriptServerSpawner {
9489
configuration: TypeScriptServiceConfiguration,
9590
): CompositeServerType {
9691
switch (configuration.separateSyntaxServer) {
97-
case SeparateSyntaxServerConfigration.Disabled:
92+
case SeparateSyntaxServerConfiguration.Disabled:
9893
return CompositeServerType.Single;
9994

100-
case SeparateSyntaxServerConfigration.Enabled:
95+
case SeparateSyntaxServerConfiguration.Enabled:
10196
return version.apiVersion?.gte(API.v340) ? CompositeServerType.SeparateSyntax : CompositeServerType.Single;
10297

103-
case SeparateSyntaxServerConfigration.Dynamic:
98+
case SeparateSyntaxServerConfiguration.Dynamic:
10499
return version.apiVersion?.gte(API.v400) ? CompositeServerType.DynamicSeparateSyntax : CompositeServerType.Single;
105100
}
106101
}

extensions/typescript-language-features/src/utils/configuration.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export namespace TsServerLogLevel {
4646
}
4747
}
4848

49-
export const enum SeparateSyntaxServerConfigration {
49+
export const enum SeparateSyntaxServerConfiguration {
5050
Disabled,
5151
Enabled,
5252
Dynamic,
@@ -62,7 +62,7 @@ export class TypeScriptServiceConfiguration {
6262
public readonly checkJs: boolean;
6363
public readonly experimentalDecorators: boolean;
6464
public readonly disableAutomaticTypeAcquisition: boolean;
65-
public readonly separateSyntaxServer: SeparateSyntaxServerConfigration;
65+
public readonly separateSyntaxServer: SeparateSyntaxServerConfiguration;
6666
public readonly enableProjectDiagnostics: boolean;
6767
public readonly maxTsServerMemory: number;
6868
public readonly enablePromptUseWorkspaceTsdk: boolean;
@@ -163,15 +163,15 @@ export class TypeScriptServiceConfiguration {
163163
return configuration.get<string | null>('typescript.locale', null);
164164
}
165165

166-
private static readUseSeparateSyntaxServer(configuration: vscode.WorkspaceConfiguration): SeparateSyntaxServerConfigration {
166+
private static readUseSeparateSyntaxServer(configuration: vscode.WorkspaceConfiguration): SeparateSyntaxServerConfiguration {
167167
const value = configuration.get('typescript.tsserver.useSeparateSyntaxServer', true);
168168
if (value === true) {
169-
return SeparateSyntaxServerConfigration.Enabled;
169+
return SeparateSyntaxServerConfiguration.Enabled;
170170
}
171171
if (value === 'dynamic') {
172-
return SeparateSyntaxServerConfigration.Dynamic;
172+
return SeparateSyntaxServerConfiguration.Dynamic;
173173
}
174-
return SeparateSyntaxServerConfigration.Disabled;
174+
return SeparateSyntaxServerConfiguration.Disabled;
175175
}
176176

177177
private static readEnableProjectDiagnostics(configuration: vscode.WorkspaceConfiguration): boolean {

0 commit comments

Comments
 (0)