PublishDiagnostics 通知

诊断通知从服务器发送到客户端,以指示诊断运行的结果。

diagnostics

诊断由服务器“拥有”,因此服务器有责任在必要时清除它们。以下规则被 VS Code 服务器用于生成诊断:

  • 如果一种语言仅是单个文件(例如 HTML),则服务器会在关闭文件时清除诊断。请注意,打开/关闭事件不一定反映用户在用户界面中看到的内容。这些事件是所有权事件。因此,对于当前版本的规范,尽管文件在用户界面中不可见,但问题可能未清除,因为客户端尚未关闭文件。

  • 如果语言具有项目系统(例如 C#),则在文件关闭时不会清除诊断。打开项目时,将重新计算所有文件的所有诊断(或从缓存中读取)。

当文件发生更改时,服务器负责重新计算诊断并将其推送到客户端。如果计算集为空,则必须推送空数组以清除以前的诊断。新推送的诊断始终会替换以前推送的诊断。客户端不会发生合并。

客户端能力(Client capability):

  • 属性路径: textDocument.publishDiagnostics
  • 属性类型: PublishDiagnosticsClientCapabilities, 定义如下:
export interface PublishDiagnosticsClientCapabilities {
	/**
	 * Whether the clients accepts diagnostics with related information.
	 */
	relatedInformation?: boolean;

	/**
	 * Client supports the tag property to provide meta data about a diagnostic.
	 * Clients supporting tags have to handle unknown tags gracefully.
	 *
	 * @since 3.15.0
	 */
	tagSupport?: {
		/**
		 * The tags supported by the client.
		 */
		valueSet: DiagnosticTag[];
	};

	/**
	 * Whether the client interprets the version property of the
	 * `textDocument/publishDiagnostics` notification's parameter.
	 *
	 * @since 3.15.0
	 */
	versionSupport?: boolean;

	/**
	 * Client supports a codeDescription property
	 *
	 * @since 3.16.0
	 */
	codeDescriptionSupport?: boolean;

	/**
	 * Whether code action supports the `data` property which is
	 * preserved between a `textDocument/publishDiagnostics` and
	 * `textDocument/codeAction` request.
	 *
	 * @since 3.16.0
	 */
	dataSupport?: boolean;
}

通知(Notification):

  • method: "textDocument/publishDiagnostics"
  • params: PublishDiagnosticsParams, 定义如下:
interface PublishDiagnosticsParams {
	/**
	 * The URI for which diagnostic information is reported.
	 */
	uri: DocumentUri;

	/**
	 * Optional the version number of the document the diagnostics are published
	 * for.
	 *
	 * @since 3.15.0
	 */
	version?: integer;

	/**
	 * An array of diagnostic information items.
	 */
	diagnostics: Diagnostic[];
}