documentFormatting function

Extension documentFormatting(
  1. DocumentFormatSource source, [
  2. DocumentFormattingOptions options = const DocumentFormattingOptions()
])

Set up document formatting support.

The source callback is called when the user requests "Format Document" (via Shift+Alt+F or a command). It should return a FormatResult with the edits to apply.

Example:

documentFormatting((state) async {
  final formatted = await lspClient.formatDocument(
    state.doc.toString(),
    tabSize: 2,
    insertSpaces: true,
  );
  if (formatted == null) return null;
  return FormatResult.replaceAll(formatted, state.doc.length);
})

Returns an extension that can be added to the editor state.

Implementation

Extension documentFormatting(
  DocumentFormatSource source, [
  DocumentFormattingOptions options = const DocumentFormattingOptions(),
]) {
  final config = DocumentFormattingConfig(
    documentSource: source,
    options: options,
  );

  return ExtensionList([
    documentFormattingFacet.of(config),
  ]);
}