vscode插件快餐教程(6) - LSP协议的初始化参数

vscode插件快餐教程(6) - LSP协议的初始化参数

学习了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初始化参数所包含的内容:
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;
}

我们将上面的信息分下类,如下图所示:
InitializeParams

主要有三种信息:

  • 一是运行环境相关的信息,如路径相关信息和进程相关信息。
  • 二是能力信息。
  • 三是一些辅助信息,包括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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jtag特工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值