Skip to content

Commit

Permalink
feat: send $/typescriptVersion notification with TypeScript version (
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Jan 30, 2023
1 parent 1daf209 commit b081112
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Maintained by a [community of contributors](https://github.com/typescript-langua
- [Rename File](#rename-file)
- [Configure plugin](#configure-plugin)
- [Inlay hints \(`textDocument/inlayHint`\)](#inlay-hints-textdocumentinlayhint)
- [TypeScript Version Notification](#typescript-version-notification)
- [Supported Protocol features](#supported-protocol-features)
- [Development](#development)
- [Build](#build)
Expand Down Expand Up @@ -499,6 +500,15 @@ export interface InlayHintsOptions extends UserPreferences {
}
```

## TypeScript Version Notification

Right after initializing, the server sends a custom `$/typescriptVersion` notification that carries information about the version of TypeScript that is utilized by the server. The editor can then display that information in the UI.

The `$/typescriptVersion` notification params include two properties:

- `version` - a semantic version (for example `4.8.4`)
- `source` - a string specifying whether used TypeScript version comes from the local workspace (`workspace`), is explicitly specified through a `initializationOptions.tsserver.path` setting (`user-setting`) or was bundled with the server (`bundled`)

## Supported Protocol features

- [x] textDocument/codeAction
Expand Down
10 changes: 10 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import * as lsp from 'vscode-languageserver';
import { SourceDefinitionCommand } from './features/source-definition.js';
import { TypeScriptVersionSource } from './tsServer/versionProvider.js';

export const Commands = {
APPLY_WORKSPACE_EDIT: '_typescript.applyWorkspaceEdit',
Expand All @@ -19,3 +21,11 @@ export const Commands = {
SELECT_REFACTORING: '_typescript.selectRefactoring',
SOURCE_DEFINITION: SourceDefinitionCommand.id,
};

type TypescriptVersionNotificationParams = {
version: string;
source: TypeScriptVersionSource;
};

export const TypescriptVersionNotification = new lsp.NotificationType<TypescriptVersionNotificationParams>('$/typescriptVersion');

5 changes: 5 additions & 0 deletions src/lsp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface LspClient {
logMessage(args: lsp.LogMessageParams): void;
applyWorkspaceEdit(args: lsp.ApplyWorkspaceEditParams): Promise<lsp.ApplyWorkspaceEditResult>;
rename(args: lsp.TextDocumentPositionParams): Promise<any>;
sendNotification<P>(type: lsp.NotificationType<P>, params: P): Promise<void>;
}

// Hack around the LSP library that makes it otherwise impossible to differentiate between Null and Client-initiated reporter.
Expand Down Expand Up @@ -69,4 +70,8 @@ export class LspClientImpl implements LspClient {
async rename(args: lsp.TextDocumentPositionParams): Promise<any> {
return this.connection.sendRequest(TypeScriptRenameRequest.type, args);
}

async sendNotification<P>(type: lsp.NotificationType<P>, params: P): Promise<void> {
await this.connection.sendNotification(type, params);
}
}
1 change: 1 addition & 0 deletions src/lsp-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function createLspConnection(options: LspConnectionOptions): lsp.Connecti
});

connection.onInitialize(server.initialize.bind(server));
connection.onInitialized(server.initialized.bind(server));
connection.onDidChangeConfiguration(server.didChangeConfiguration.bind(server));

connection.onDidOpenTextDocument(server.didOpenTextDocument.bind(server));
Expand Down
10 changes: 9 additions & 1 deletion src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { toDocumentHighlight, uriToPath, toSymbolKind, toLocation, toSelectionRa
import { LspDocuments, LspDocument } from './document.js';
import { asCompletionItem, asResolvedCompletionItem, getCompletionTriggerCharacter } from './completion.js';
import { asSignatureHelp, toTsTriggerReason } from './hover.js';
import { Commands } from './commands.js';
import { Commands, TypescriptVersionNotification } from './commands.js';
import { provideQuickFix } from './quickfix.js';
import { provideRefactors } from './refactor.js';
import { provideOrganizeImports } from './organize-imports.js';
Expand Down Expand Up @@ -265,6 +265,14 @@ export class LspServer {
return initializeResult;
}

public initialized(_: lsp.InitializedParams): void {
const { apiVersion, typescriptVersionSource } = this.tspClient;
this.options.lspClient.sendNotification(TypescriptVersionNotification, {
version: apiVersion.displayName,
source: typescriptVersionSource,
});
}

private findTypescriptVersion(userTsserverPath: string | undefined): TypeScriptVersion | null {
const typescriptVersionProvider = new TypeScriptVersionProvider(userTsserverPath || this.options.tsserverPath, this.logger);
// User-provided tsserver path.
Expand Down
4 changes: 4 additions & 0 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ export class TestLspClient implements LspClient {
rename(): Promise<void> {
throw new Error('unsupported');
}

sendNotification<P>(_type: lsp.NotificationType<P>, _params: P): Promise<void> {
throw new Error('unsupported');
}
}

export class TestLspServer extends LspServer {
Expand Down
4 changes: 3 additions & 1 deletion src/tsp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { ITypeScriptServer, TypeScriptServerExitEvent } from './tsServer/se
import { TypeScriptServerError } from './tsServer/serverError.js';
import { TypeScriptServerSpawner } from './tsServer/spawner.js';
import Tracer, { Trace } from './tsServer/tracer.js';
import type { TypeScriptVersion } from './tsServer/versionProvider.js';
import type { TypeScriptVersion, TypeScriptVersionSource } from './tsServer/versionProvider.js';
import type { LspClient } from './lsp-client.js';
import type { TsServerLogLevel } from './utils/configuration.js';

Expand Down Expand Up @@ -78,6 +78,7 @@ export interface TspClientOptions {

export class TspClient {
public apiVersion: API;
public typescriptVersionSource: TypeScriptVersionSource;
private primaryTsServer: ITypeScriptServer | null = null;
private logger: Logger;
private tsserverLogger: Logger;
Expand All @@ -86,6 +87,7 @@ export class TspClient {

constructor(private options: TspClientOptions) {
this.apiVersion = options.typescriptVersion.version || API.defaultVersion;
this.typescriptVersionSource = options.typescriptVersion.source;
this.logger = new PrefixingLogger(options.logger, '[tsclient]');
this.tsserverLogger = new PrefixingLogger(options.logger, '[tsserver]');
this.loadingIndicator = new ServerInitializingIndicator(options.lspClient);
Expand Down

0 comments on commit b081112

Please sign in to comment.