Document Formatting

Document Formatting 请求

文档格式化请求从客户端发送到服务器,以格式化整个文档。

document-formatting

客户端能力(Client capability):

  • 属性路径: textDocument.formatting
  • 属性类型: DocumentFormattingClientCapabilities, 定义如下:
export interface DocumentFormattingClientCapabilities {
	/**
	 * Whether formatting supports dynamic registration.
	 */
	dynamicRegistration?: boolean;
}

服务端能力(Server capability):

  • 属性路径: documentFormattingProvider
  • 属性类型: boolean | DocumentFormattingOptions, 定义如下:
export interface DocumentFormattingOptions extends WorkDoneProgressOptions {
}

注册选项(Registration Options): DocumentFormattingRegistrationOptions, 定义如下:

export interface DocumentFormattingRegistrationOptions extends
	TextDocumentRegistrationOptions, DocumentFormattingOptions {
}

请求(Request):

  • method: "textDocument/formatting"
  • params: DocumentFormattingParams, 定义如下:
interface DocumentFormattingParams extends WorkDoneProgressParams {
	/**
	 * The document to format.
	 */
	textDocument: TextDocumentIdentifier;

	/**
	 * The format options.
	 */
	options: FormattingOptions;
}
/**
 * Value-object describing what options formatting should use.
 */
interface FormattingOptions {
	/**
	 * Size of a tab in spaces.
	 */
	tabSize: uinteger;

	/**
	 * Prefer spaces over tabs.
	 */
	insertSpaces: boolean;

	/**
	 * Trim trailing whitespace on a line.
	 *
	 * @since 3.15.0
	 */
	trimTrailingWhitespace?: boolean;

	/**
	 * Insert a newline character at the end of the file if one does not exist.
	 *
	 * @since 3.15.0
	 */
	insertFinalNewline?: boolean;

	/**
	 * Trim all newlines after the final newline at the end of the file.
	 *
	 * @since 3.15.0
	 */
	trimFinalNewlines?: boolean;

	/**
	 * Signature for further properties.
	 */
	[key: string]: boolean | integer | string;
}

响应(Response):

  • result: TextEdit[] | null, 描述对要格式化的文档的修改。
  • error: codemessage,以防在请求期间发生异常。

Document Range Formatting 请求

文档范围格式设置请求从客户端发送到服务器,以设置文档中给定范围的格式。

客户端能力(Client capability):

  • 属性路径: textDocument.rangeFormatting
  • 属性类型: DocumentRangeFormattingClientCapabilities, 定义如下:
export interface DocumentRangeFormattingClientCapabilities {
	/**
	 * Whether formatting supports dynamic registration.
	 */
	dynamicRegistration?: boolean;
}

服务端能力(Server capability):

  • 属性路径: documentRangeFormattingProvider
  • 属性类型: boolean | DocumentRangeFormattingOptions, 定义如下:
export interface DocumentRangeFormattingOptions extends
	WorkDoneProgressOptions {
}

注册选项(Registration Options): DocumentFormattingRegistrationOptions, 定义如下:

export interface DocumentRangeFormattingRegistrationOptions extends
	TextDocumentRegistrationOptions, DocumentRangeFormattingOptions {
}

请求(Request):

  • method: "textDocument/rangeFormatting"
  • params: DocumentRangeFormattingParams, 定义如下:
interface DocumentRangeFormattingParams extends WorkDoneProgressParams {
	/**
	 * The document to format.
	 */
	textDocument: TextDocumentIdentifier;

	/**
	 * The range to format
	 */
	range: Range;

	/**
	 * The format options
	 */
	options: FormattingOptions;
}

响应(Response):

  • result: TextEdit[] | null, 描述对要格式化的文档的修改。
  • error: codemessage,以防在请求期间发生异常。

Document on Type Formatting 请求

在键入过程中,文档格式化请求从客户端发送到服务器,以设置文档部分的格式。

客户端能力(Client capability):

  • 属性路径: textDocument.onTypeFormatting
  • 属性类型: DocumentOnTypeFormattingClientCapabilities, 定义如下:
export interface DocumentOnTypeFormattingClientCapabilities {
	/**
	 * Whether on type formatting supports dynamic registration.
	 */
	dynamicRegistration?: boolean;
}

服务端能力(Server capability):

  • 属性路径: documentOnTypeFormattingProvider
  • 属性类型: DocumentOnTypeFormattingOptions, 定义如下:
export interface DocumentOnTypeFormattingOptions {
	/**
	 * A character on which formatting should be triggered, like `{`.
	 */
	firstTriggerCharacter: string;

	/**
	 * More trigger characters.
	 */
	moreTriggerCharacter?: string[];
}

注册选项(Registration Options): DocumentOnTypeFormattingRegistrationOptions, 定义如下:

export interface DocumentOnTypeFormattingRegistrationOptions extends
	TextDocumentRegistrationOptions, DocumentOnTypeFormattingOptions {
}

请求(Request):

  • method: "textDocument/onTypeFormatting"
  • params: DocumentOnTypeFormattingParams, 定义如下:
interface DocumentOnTypeFormattingParams {

	/**
	 * The document to format.
	 */
	textDocument: TextDocumentIdentifier;

	/**
	 * The position around which the on type formatting should happen.
	 * This is not necessarily the exact position where the character denoted
	 * by the property `ch` got typed.
	 */
	position: Position;

	/**
	 * The character that has been typed that triggered the formatting
	 * on type request. That is not necessarily the last character that
	 * got inserted into the document since the client could auto insert
	 * characters as well (e.g. like automatic brace completion).
	 */
	ch: string;

	/**
	 * The formatting options.
	 */
	options: FormattingOptions;
}

响应(Response):

  • result: TextEdit[] | null
  • error: codemessage,以防在请求期间发生异常。