JSON-RPC
> JSON-RPC is a remote procedure call protocol encoded in JSON. It is similar to the XML-RPC protocol, defining only a few data types and commands. JSON-RPC allows for notifications (data sent to the https://en.wikipedia.org/wiki/JSON-RPC
\r\n
で分割される。 Content-Length: <コンテント部のバイト数>
Content-Type: <コンテント部のmine type>
デフォルトは application/vscode-jsonrpc; charset=utf-8
Content-Length: ...\r\n
\r\n
{
"jsonrpc": "2.0",
"id": 1,
"method": "textDocument/didOpen",
"params": {
...
}
}
jsonrpc
フィールドを含む。LSPでは常に "2.0"
。interface Message {
jsonrpc: string;
}
interface RequestMessage extends Message {
/**
* リクエストID。
*/
id: number | string;
/**
* 実行されるメソッド。
*/
method: string;
/**
* メソッドのパラメータ。
*/
params?: array | object;
}
result
プロパティは null
となる。interface ResponseMessage extends Message {
/**
* リクエストID。
*/
id: number | string | null;
/**
* リクエストの結果。成功時は必須である。
* メソッドがエラーを返した場合はこのプロパティは存在してはならない。
*/
result?: string | number | boolean | object | null;
/**
* リクエストが失敗した場合のエラー。
*/
error?: ResponseError;
}
interface ResponseError {
/**
* 発生したエラー種別を表す数字。
*/
code: number;
/**
* エラーの概要を表す文字列。
*/
message: string;
/**
* エラーについての情報を付加するプリミティブまたは構造化された値。
* 省略可能。
*/
data?: string | number | boolean | array | object | null;
}
export namespace ErrorCodes {
// JSON RPC で定義されたもの。
export const ParseError: number = -32700;
export const InvalidRequest: number = -32600;
export const MethodNotFound: number = -32601;
export const InvalidParams: number = -32602;
export const InternalError: number = -32603;
export const serverErrorStart: number = -32099;
export const serverErrorEnd: number = -32000;
export const ServerNotInitialized: number = -32002;
export const UnknownErrorCode: number = -32001;
// このプロトコルで定義されたもの。
export const RequestCancelled: number = -32800;
export const ContentModified: number = -32801;
}
interface NotificationMessage extends Message {
/**
* 実行されるメソッド。
*/
method: string;
/**
* 通知のパラメータ。
*/
params?: array | object;
}
$/
で始まるメッセージは実装依存。通知の場合は無視していい。リクエストの場合、 MethodNotFound
エラーをレスポンスする。 a𐐀b
を考えると、 a
は0、 𐐀
は1、 b
は3となる。 initialize
リクエストを送る。 params
は以下の InitializeParams
interface InitializeParams {
/**
* サーバを起動した親プロセスのプロセス ID。プロセスが他のプロセスから起動さ
* れていない場合は null となる。
* 親プロセスが死んでいる場合は、サーバは自身のプロセスを終了すべきである
* (`exit` 通知を参照)。
*/
processId: number | null;
/**
* クライアントについての情報
*
* 3.15.0
*/
clientInfo?: {
/**
* クライアント自身により定義されたクライアントの名前。
*/
name: string;
/**
* クライアント自身により定義されたクライアントのバージョン。
*/
version?: string;
};
/**
* ワークスペースの `rootPath`。フォルダを開いていない場合は null となる。
*
* rootUri を用いる
*/
rootPath?: string | null;
/**
* ワークスペースの `rootUri`。フォルダを開いていない場合は null となる。
* `rootPath` と `rootUri` が指定されている場合は `rootUri` が優先される。
*/
rootUri: DocumentUri | null;
/**
* ユーザが提供する初期化オプション。
*/
initializationOptions?: any;
/**
* クライアント(エディタまたはツール)から提供される機能
*/
capabilities: ClientCapabilities;
/**
* トレースの初期設定。省略した場合、トレースは無効('off')となる。
*/
trace?: 'off' | 'messages' | 'verbose';
/**
* サーバ開始時にクライアアントで設定されるワークスペースフォルダ。
* このプロパティはクライアントがワークスペースフォルダをサポートするときのみ
* 表われる。
* クライアントがワークスペースフォルダをサポートしているが設定しない場合は
* `null` を指定できる。
*
* 3.6.0 から
*/
workspaceFolders?: WorkspaceFolder[] | null;
}
interface InitializeResult {
/**
* サーバが提供する機能。
*/
capabilities: ServerCapabilities;
/**
* サーバについての情報。
*
* 3.15.0
*/
serverInfo?: {
/**
* サーバ自身により定義されるサーバの名前。
*/
name: string;
/**
* サーバ自身により定義されるサーバのバージョン。
*/
version?: string;
};
}
initialize
リクエストの結果を受け取ってからその他のリクエストまたは通知を送る前に、 initialized
通知を送る。 initialized
params: {}