Line data Source code
1 : import 'package:enum_assist/src/util/string_helpers.dart'; 2 : 3 : export 'package:change_case/change_case.dart'; 4 : 5 : /// extension to [StringBuffer] 6 : extension StringBufferX on StringBuffer { 7 : /// {@macro tab_indentation} 8 1 : void writelnTab(String s, [int n = 1]) { 9 2 : writeln(tab(s, n)); 10 : } 11 : 12 : /// {@macro tab_indentation} 13 1 : void writeTab(String s, [int n = 1]) { 14 2 : write(tab(s, n)); 15 : } 16 : 17 : /// accepts a header string to name the section 18 : /// 19 : /// wraps the header with brakets 20 1 : void writeobj( 21 : String s, { 22 : required void Function(StringBuffer, int) body, 23 : int tab = 0, 24 : String? open, 25 : String? close, 26 : bool appendNewLine = true, 27 : bool includeSpaceBetweenOpen = true, 28 : }) { 29 1 : final openAndCloseAreNullOrNotNull = (open == null) ^ (close == null); 30 : if (openAndCloseAreNullOrNotNull) { 31 0 : throw ArgumentError( 32 : 'open and close must both be null or both be non-null', 33 : ); 34 : } 35 : 36 : final opener = open ?? '{'; 37 : final closer = close ?? '}'; 38 : final spacer = includeSpaceBetweenOpen ? ' ' : ''; 39 : 40 2 : writelnTab('$s$spacer$opener', tab); 41 1 : body(this, tab + 1); 42 : if (appendNewLine) { 43 1 : writelnTab(closer, tab); 44 : } else { 45 1 : writeTab(closer, tab); 46 : } 47 : } 48 : }