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

feat!: Update the client/server api #2859

Merged
merged 12 commits into from
Oct 10, 2023
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
"<node_internals>/**"
]
},
{
"name": "Client: Launch Extension -- All extensions",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/packages/client/dist/**/*.js"
],
"smartStep": true,
"skipFiles": [
"<node_internals>/**"
]
},
{
"name": "Client: Launch Extension - .gitignore",
"type": "extensionHost",
Expand Down
1 change: 1 addition & 0 deletions cspell-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ codeblock
codicon
codicons
colorscheme
comms
cosmiconfig
darkblue
dcopy
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/__utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "Shared Utils between server and client",
"private": true,
"type": "commonjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down
31 changes: 11 additions & 20 deletions packages/_server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,15 @@
},
"type": "module",
"main": "dist/main.cjs",
"typings": "dist/api.d.cts",
"exports": {
".": "./dist/main.cjs",
"./api": "./dist/api.cjs"
},
"typesVersions": {
"*": {
"*.d.cts": [
"dist/*.d.cts"
],
"api": [
"dist/api.d.cts"
],
"dist/api.d.cts": [
"dist/api.d.cts"
],
"*": [
"dist/*.d.cts"
]
".": {
"import": "./dist/main.cjs",
"require": "./dist/main.cjs"
},
"./api": {
"types": "./dist/api.d.cts",
"import": "./dist/api.cjs",
"require": "./dist/api.cjs"
}
},
"devDependencies": {
Expand All @@ -52,14 +42,15 @@
"cspell-glob": "^7.3.7",
"cspell-lib": "^7.3.7",
"gensequence": "^6.0.0",
"json-rpc-api": "file:../json-rpc-api",
"node-watch": "^0.7.4",
"rxjs": "^7.8.1",
"utils-disposables": "file:../utils-disposables",
"utils-logger": "file:../utils-logger",
"vscode-languageserver": "^9.0.1",
"vscode-languageserver-textdocument": "^1.0.11",
"vscode-languageserver-types": "^3.17.5",
"vscode-uri": "^3.0.8",
"vscode-webview-rpc": "file:../webview-rpc"
"vscode-uri": "^3.0.8"
},
"scripts": {
"build": "npm run build:esbuild && npm run build:api && npm run build:tsc && npm run build-schema",
Expand Down
11 changes: 11 additions & 0 deletions packages/_server/src/api/CommandsToClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { ConfigurationTarget } from './apiModels.js';
import type { OrPromise } from './types.js';

export interface CommandsToClient {
addWordsToVSCodeSettingsFromServer: (words: string[], documentUri: string, target: ConfigurationTarget) => void;
addWordsToDictionaryFileFromServer: (words: string[], documentUri: string, dict: { uri: string; name: string }) => void;
addWordsToConfigFileFromServer: (words: string[], documentUri: string, config: { uri: string; name: string }) => void;
}
export type ClientSideCommandHandlerApi = {
[command in keyof CommandsToClient as `cSpell.${command}`]: (...params: Parameters<CommandsToClient[command]>) => OrPromise<void>;
};
111 changes: 111 additions & 0 deletions packages/_server/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import type {
ApiPrefix,
ApplyNotificationAPI,
ApplyRequestAPI,
ClientAPIDef,
ClientSideMethods,
Logger,
MessageConnection,
RpcAPI,
ServerAPIDef,
ServerSideMethods,
} from 'json-rpc-api';
import { createClientApi, createServerApi } from 'json-rpc-api';

import type {
GetConfigurationForDocumentRequest,
GetConfigurationForDocumentResult,
IsSpellCheckEnabledResult,
OnSpellCheckDocumentStep,
SpellingSuggestionsResult,
SplitTextIntoWordsResult,
TextDocumentInfo,
WorkspaceConfigForDocumentRequest,
WorkspaceConfigForDocumentResponse,
} from './apiModels.js';

export type { Logger, MessageConnection } from 'json-rpc-api';

/** Requests that can be made to the server */
export interface ServerRequestsAPI {
getConfigurationForDocument(req: GetConfigurationForDocumentRequest): GetConfigurationForDocumentResult;
isSpellCheckEnabled(req: TextDocumentInfo): IsSpellCheckEnabledResult;
splitTextIntoWords(req: string): SplitTextIntoWordsResult;
spellingSuggestions(req: TextDocumentInfo): SpellingSuggestionsResult;
}

/** Notifications that can be sent to the server */
export interface ServerNotificationsAPI {
notifyConfigChange: () => void;
registerConfigurationFile: (path: string) => void;
}

/**
* Requests that can be made from the server to the client(vscode extension)
* Note: RPC requests to the client/extension is rare.
*/
export interface ClientRequestsAPI {
// addWordsToVSCodeSettingsFromServer: (words: string[], documentUri: string, target: ConfigurationTarget) => void;
// addWordsToDictionaryFileFromServer: (words: string[], documentUri: string, dict: { uri: string; name: string }) => void;
// addWordsToConfigFileFromServer: (words: string[], documentUri: string, config: { uri: string; name: string }) => void;
onWorkspaceConfigForDocumentRequest: (req: WorkspaceConfigForDocumentRequest) => WorkspaceConfigForDocumentResponse;
}

/** Notifications from the server to the client(vscode extension) */
export interface ClientNotificationsAPI {
onSpellCheckDocument(step: OnSpellCheckDocumentStep): void;
}

export interface SpellCheckerServerAPI extends RpcAPI {
serverRequests: ApplyRequestAPI<ServerRequestsAPI>;
serverNotifications: ApplyNotificationAPI<ServerNotificationsAPI>;
clientRequests: ApplyRequestAPI<ClientRequestsAPI>;
clientNotifications: ApplyNotificationAPI<ClientNotificationsAPI>;
}

/**
* Used on the server side to communicate with the client(extension).
*/
export interface ServerSideApi extends ServerSideMethods<SpellCheckerServerAPI> {}
/**
* Used in the client(extension) to communicate with the server.
*/
export interface ClientSideApi extends ClientSideMethods<SpellCheckerServerAPI> {}

export type ServerSideApiDef = ServerAPIDef<SpellCheckerServerAPI>;
export type ClientSideApiDef = ClientAPIDef<SpellCheckerServerAPI>;

export interface ServerSideHandlers {
serverRequests: DefineHandlers<ServerSideApiDef['serverRequests']>;
serverNotifications: DefineHandlers<ServerSideApiDef['serverNotifications']>;
}

// todo: make '' when all old apis are removed.
const pfx = '_';

const apiPrefix: ApiPrefix = {
serverNotifications: pfx,
serverRequests: pfx,
clientNotifications: pfx,
clientRequests: pfx,
};

export function createServerSideApi(
connection: MessageConnection,
api: ServerAPIDef<SpellCheckerServerAPI>,
logger: Logger | undefined,
): ServerSideApi {
return createServerApi(connection, api, logger, apiPrefix);
}

export function createClientSideApi(
connection: MessageConnection,
api: ClientAPIDef<SpellCheckerServerAPI>,
logger: Logger | undefined,
): ClientSideApi {
return createClientApi(connection, api, logger, apiPrefix);
}

type DefineHandlers<T> = {
[P in keyof T]: Exclude<T[P], boolean>;
};
Loading