formatSelection function

String formatSelection(
  1. Map<String, Object?> payload, {
  2. required String jsonPath,
  3. bool multiline = false,
})

Builds the string that goes on the clipboard for one written selection.

payload is the map writeSelection wrote to disk (target, geometry, resolvedProps, ancestors, and a screenshot path already resolved). jsonPath is that same selection's own .json file, referenced at the end so a competent agent can read it for the full payload without polluting the chat (PLAN.md §5.1).

Implementation

String formatSelection(Map<String, Object?> payload, {required String jsonPath, bool multiline = false}) {
  final Map<String, Object?> target = payload['target'] as Map<String, Object?>? ?? const <String, Object?>{};
  final String widget = (target['widget'] as String?) ?? 'Widget';
  final String file = (target['file'] as String?) ?? '?';
  final Object? line = target['line'];

  final List<String> middle = <String>[
    if (_sizeSegment(payload) case final String s) s,
    if (_propSegment(payload, 'backgroundColor', 'bg') case final String s) s,
    if (_propSegment(payload, 'borderRadius', 'radius') case final String s) s,
    if (_parentSegment(payload) case final String s) s,
  ];

  if (!multiline) {
    final List<String> segments = <String>[...middle, 'details: $jsonPath'];
    return '[ref] $widget @ $file:$line${segments.isEmpty ? '' : ' · ${segments.join(' · ')}'}';
  }

  final StringBuffer buffer = StringBuffer('[ref] $widget · $file:$line');
  if (middle.isNotEmpty) buffer.write('\n${middle.join(' · ')}');
  buffer.write('\n→ $jsonPath');
  return buffer.toString();
}