formatDocument function

Future<TransactionSpec?> formatDocument(
  1. EditorState state
)

Utility to format and apply in one step.

Returns a future that completes with the transaction to dispatch, or null if no formatting was available.

Implementation

Future<TransactionSpec?> formatDocument(EditorState state) async {
  final configs = state.facet(documentFormattingFacet);
  // ignore: avoid_print
  print('[formatDocument] configs.length = ${configs.length}');

  for (final config in configs) {
    // ignore: avoid_print
    print('[formatDocument] checking config, documentSource=${config.documentSource != null}');
    if (config.documentSource != null) {
      try {
        // ignore: avoid_print
        print('[formatDocument] calling documentSource...');
        final result = await Future.value(config.documentSource!(state));
        // ignore: avoid_print
        print('[formatDocument] documentSource returned: ${result?.edits.length ?? "null"} edits');
        if (result != null && result.isNotEmpty) {
          return applyFormatEdits(state, result);
        }
      } catch (e, stack) {
        // Log error instead of silently swallowing
        // ignore: avoid_print
        print('[formatDocument] Error: $e');
        // ignore: avoid_print
        print('[formatDocument] Stack: $stack');
      }
    }
  }

  return null;
}