学习了 lsp 的代码补全之后, 我们可以尝试搭建一套可以运行的 lsp 的系统.
在此之前, 我们再将一些细节夯实一下.
我们在第 4 节曾经介绍过 LSP 的初始化的握手过程.
我们可以在 connection 的 onInitialize 函数中来接收客户端的初始化参数, 比如客户端的能力.
- connection.onInitialize((params: InitializeParams) => {
- let capabilities = params.capabilities;
- return {
- capabilities: {
- textDocumentSync: documents.syncKind,
- // Tell the client that the server supports code completion
- completionProvider: {
- resolveProvider: true
- }
- }
- };
- })
我们先用一张图来看一下 lsp 初始化参数所包含的内容:
下面我们看下这些定义:
- interface InitializeParams {
- /**
- * The process Id of the parent process that started
- * the server. Is null if the process has not been started by another process.
- * If the parent process is not alive then the server should exit (see exit notification) its process.
- */
- processId: number | null;
- /**
- * The rootPath of the workspace. Is null
- * if no folder is open.
- *
- * @deprecated in favour of rootUri.
- */
- rootPath?: string | null;
- /**
- * The rootUri of the workspace. Is null if no
- * folder is open. If both `rootPath` and `rootUri` are set
- * `rootUri` wins.
- */
- rootUri: DocumentUri | null;
- /**
- * User provided initialization options.
- */
- initializationOptions?: any;
- /**
- * The capabilities provided by the client (editor or tool)
- */
- capabilities: ClientCapabilities;
- /**
- * The initial trace setting. If omitted trace is disabled ('off').
- */
- trace?: 'off' | 'messages' | 'verbose';
- /**
- * The workspace folders configured in the client when the server starts.
- * This property is only available if the client supports workspace folders.
- * It can be `null` if the client supports workspace folders but none are
- * configured.
- *
- * Since 3.6.0
- */
- workspaceFolders?: WorkspaceFolder[] | null;
- }
我们将上面的信息分下类, 如下图所示:
主要有三种信息:
一是运行环境相关的信息, 如路径相关信息和进程相关信息.
二是能力信息.
三是一些辅助信息, 包括 trace 设置和用户自定义信息.
路径信息是只有在插件运行的 vscode 中已经打开了一个目录时才可以获取到.
假设我打开的目录是:/Users/ziyingliuziying/working/GitLab/cafmode/server, 那么 rootPath 返回的结果是:/Users/ziyingliuziying/working/GitLab/cafmode/server. 而 rootUri 返回的结果是: file:///Users/ziyingliuziying/working/GitLab/cafmode/server.
workspaceFolders 是一个由 WorkspaceFolder 组成的数组. 每一个 WorkspaceFolder 是由 name 和 uri 组成的对象. 以上面的例子为例: name 为 server, uri 是 file:///Users/ziyingliuziying/working/GitLab/cafmode/server
来源: https://yq.aliyun.com/articles/704464