From: Matt S Trout Date: Thu, 5 Apr 2018 19:21:18 +0000 (+0000) Subject: much better indentation on traces X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FDX.git;a=commitdiff_plain;h=6162b00196cbcc997f20714de9d21d1963cc2adf much better indentation on traces --- diff --git a/lib/DX/Resolution.pm b/lib/DX/Resolution.pm index 3606633..35059dd 100644 --- a/lib/DX/Resolution.pm +++ b/lib/DX/Resolution.pm @@ -13,8 +13,8 @@ sub remainder { () } sub for_deparse { my ($self) = @_; - [ statement => [ - [ symbol => 'resolution' ], + [ word_and_body => [ + 'resolution', [ pairs => [ (@{$self->actions} ? [ actions => [ block => $self->actions ] ] diff --git a/lib/DX/ResolutionStrategy.pm b/lib/DX/ResolutionStrategy.pm index ac54832..389c406 100644 --- a/lib/DX/ResolutionStrategy.pm +++ b/lib/DX/ResolutionStrategy.pm @@ -43,8 +43,8 @@ sub remainder { sub for_deparse { my ($self) = @_; - [ statement => [ - [ symbol => 'resolution_strategy' ], + [ word_and_body => [ + 'resolution_strategy', [ pairs => [ [ action_prototypes => [ block => [ map { @@ -56,9 +56,9 @@ sub for_deparse { ] ] } @{$self->action_prototypes} ] ] ], - [ implementation_candidates => [ block => [ - map [ block => [ - map [ block => [ + [ implementation_candidates => [ list => [ + map [ list => [ + map [ list => [ map +($_->value_path ? [ value_path => $_->value_path ] : $_), @$_ ] ], @$_ ] ], @{$self->implementation_candidates} diff --git a/lib/DX/Step/ResolveProposition.pm b/lib/DX/Step/ResolveProposition.pm index 3969867..0e28efc 100644 --- a/lib/DX/Step/ResolveProposition.pm +++ b/lib/DX/Step/ResolveProposition.pm @@ -66,11 +66,11 @@ sub apply_to { : ()), [ statement => [ [ symbol => 'depends_on' ], - [ pairs => [ - map [ - (split '::', ${$_->[0]})[-1], + [ block => [ + map [ statement => [ + [ symbol => (split '::', ${$_->[0]})[-1] ], [ value_path => [ @{$_}[1..$#$_] ] ] - ], @{$self->depends_on} + ] ], @{$self->depends_on} ] ], ] ], ] ] diff --git a/lib/DX/TraceFormatter.pm b/lib/DX/TraceFormatter.pm index 17bfeb7..31ee922 100644 --- a/lib/DX/TraceFormatter.pm +++ b/lib/DX/TraceFormatter.pm @@ -5,12 +5,17 @@ use curry; use List::Util qw(min); use DX::Class; +our $WS; +our $Extra = 0; + has ambient_indent_level => ( is => 'rwp', lazy => 1, clearer => 1, default => 0 ); sub indent_by { ' ' } +sub max_width { 78 } + sub format { my ($self, $thing) = @_; local our $Indent_Level = $self->ambient_indent_level; @@ -24,11 +29,31 @@ sub format { sub _format { my ($self, $thing) = @_; my ($as, $data) = @{blessed($thing) ? $thing->for_deparse : $thing}; - $self->${\"_format_as_${as}"}($data); + local $WS = ' '; + my $spaced = $self->${\"_format_as_${as}"}($data);# + if ($spaced =~ /\n/ + or (length($spaced) + > ($self->max_width - + ((length($self->indent_by) * our $Indent_Level) + $Extra))) + ) { + local $WS = "\n"; + local $Extra = 0; + return $self->${\"_format_as_${as}"}($data); + } + return $spaced; +} + +sub _format_as_word_and_body { + my ($self, $wb) = @_; + my ($word, $body) = @$wb; + my $word_f = $self->_format_as_maybe_bareword($word).' '; + local $Extra = length($word_f); + return $word_f.$self->_format($body); } sub _format_indented { my ($self, $cb) = @_; + return $cb->() if $WS eq ' '; our $Indent_Level; local $Indent_Level = $Indent_Level + 1; my $unindented = $cb->(); @@ -62,16 +87,18 @@ sub _format_as_unset { 'unset' } sub _format_as_array { my ($self, $members) = @_; - join ' ', '{[', (map $self->_format($_), @$members), ']}'; + join $WS, + '{[', + (map $self->_format_indented($self->curry::_format($_)), @$members) + , ']}'; } sub _format_as_dict { my ($self, $members) = @_; - join ' ', '{{', ( - map +( - $self->_format_as_maybe_bareword($_), - $self->_format($members->{$_}), - ), sort keys %$members + join $WS, '{{', ( + map $self->_format_indented($self->curry::_format( + [ word_and_body => [ $_, $members->{$_} ] ], + )), sort keys %$members ), '}}'; } @@ -87,25 +114,25 @@ sub _format_as_value_path { sub _format_as_list { my ($self, $members) = @_; - join "\n", '{', ( + join $WS, '{', ( map $self->_format_indented($self->curry::_format($_)), @$members ), '}'; } sub _format_as_pairs { my ($self, $members) = @_; - join "\n", '{', ( - map $self->_format_indented(sub { - $self->_format_as_maybe_bareword($_->[0]) - .' '.$self->_format($_->[1]) - }), @$members + join $WS, '{', ( + map $self->_format_indented($self->curry::_format( + [ word_and_body => $_ ] + )), @$members ), '}'; } sub _format_as_block { my ($self, $members) = @_; - join "\n", '{', ( - map $self->_format_indented($self->curry::_format($_)), @$members + join $WS, '{', ( + join +($WS eq ' ' ? '; ' : $WS), + map $self->_format_indented($self->curry::_format($_)), @$members ), '}'; }