File Coverage

blib/lib/Code/TidyAll/Plugin/Perl/AlignMooseAttributes.pm
Criterion Covered Total %
statement 16 40 40.0
branch n/a
condition n/a
subroutine 6 9 66.6
pod 2 3 66.6
total 24 52 46.1


line stmt bran cond sub pod time code
1             package Code::TidyAll::Plugin::Perl::AlignMooseAttributes;
2             BEGIN {
3 1     1   1030 $Code::TidyAll::Plugin::Perl::AlignMooseAttributes::VERSION = '0.01';
4             }
5 1     1   889 use Text::Aligner qw(align);
  1         12870  
  1         73  
6 1     1   11 use List::Util qw(max);
  1         2  
  1         104  
7 1     1   5 use strict;
  1         1  
  1         33  
8 1     1   5 use warnings;
  1         1  
  1         35  
9 1     1   5 use base qw(Code::TidyAll::Plugin);
  1         2  
  1         1039  
10              
11             my $marker = '__AlignMooseAttributes_no_tidy';
12              
13             sub preprocess_source {
14 0     0 1   my ( $self, $source ) = @_;
15              
16             # Hide 'has' one-liners behind comments
17 0           $source =~ s/^( has .* \) \s* \; )$/\# $marker $1/gmx;
18              
19 0           return $source;
20             }
21              
22             sub postprocess_source {
23 0     0 1   my ( $self, $source ) = @_;
24              
25             # Reveal 'has' lines
26 0           $source =~ s/^\# $marker //gm;
27              
28             # For each group of 'has' one-liners: sort, remove multiple spaces and align equal signs
29 0           $source =~ s/((^ has \s+ \' .*? \) \s* \; \s* \n)+)/process_attr_block($1)/gmex;
  0            
30              
31 0           return $source;
32             }
33              
34             sub process_attr_block {
35 0     0 0   my ($block) = @_;
36 0           my @lines = grep { /\S/ } split( "\n", $block );
  0            
37 0           my @attrs = map { /has\s+'([^\']+)'/; $1 } @lines;
  0            
  0            
38 0           my $max_length = max( map { length($_) } @attrs );
  0            
39 0           foreach my $line (@lines) {
40 0           $line =~ s/ +/ /g;
41 0           $line =~
42 0           s/(?:has \s+ '([^\']+)') \s* => \s* \( \s* (.*?) \s* \) \s* ;/"has '$1'" . scalar(' ' x ($max_length - length($1))) . " => ( $2 );"/ex;
43 0           $line =~ s/=> \(\s*\)/=> \(\)/;
44 0           $line =~ s/,\s+\)/ \)/;
45             }
46 0           return join( "", sort(map { "$_\n" } @lines) ) . "\n";
  0            
47             }
48              
49             1;
50              
51              
52              
53             =pod
54              
55             =head1 NAME
56              
57             Code::TidyAll::Plugin::Perl::AlignMooseAttributes - Sort and align Moose-style
58             attributes with tidyall
59              
60             =head1 VERSION
61              
62             version 0.01
63              
64             =head1 SYNOPSIS
65              
66             use Code::TidyAll::Plugin::Perl::AlignMooseAttributes;
67              
68             =head1 DESCRIPTION
69              
70             This L plugin sorts and aligns consecutive Moose-style
71             attribute lines. e.g. this:
72              
73             has 'namespace' => ( is => 'ro', isa => 'Str', default => 'Default' );
74             has 'expires_at' => ( is => 'rw', default => CHI_Max_Time );
75             has 'storage' => ( is => 'ro' );
76             has 'label' => ( is => 'rw', lazy_build => 1 );
77             has 'chi_root_class' => ( is => 'ro' );
78              
79             becomes this:
80              
81             has 'chi_root_class' => ( is => 'ro' );
82             has 'expires_at' => ( is => 'rw', default => CHI_Max_Time );
83             has 'label' => ( is => 'rw', lazy_build => 1 );
84             has 'namespace' => ( is => 'ro', isa => 'Str', default => 'Default' );
85             has 'storage' => ( is => 'ro' );
86              
87             Only consecutive attributes, each on a single line, will be affected.
88             Multi-line attributes will not be affected.
89              
90             This plugin has a preprocess step that hides these lines to prevent perltidy
91             from splitting them into multiple lines.
92              
93             =head1 SUPPORT AND DOCUMENTATION
94              
95             Questions and feedback are welcome, and should be directed to the author.
96              
97             Bugs and feature requests will be tracked at RT:
98              
99             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Code-TidyAll-Plugin-Perl-AlignMooseAttributes
100             bug-code-tidyall-plugin-perl-alignmooseattributes@rt.cpan.org
101              
102             The latest source code can be browsed and fetched at:
103              
104             http://github.com/jonswar/perl-code-tidyall-plugin-perl-alignmooseattributes
105             git clone git://github.com/jonswar/perl-code-tidyall-plugin-perl-alignmooseattributes.git
106              
107             =head1 SEE ALSO
108              
109             L
110              
111             =head1 AUTHOR
112              
113             Jonathan Swartz
114              
115             =head1 COPYRIGHT AND LICENSE
116              
117             This software is copyright (c) 2011 by Jonathan Swartz.
118              
119             This is free software; you can redistribute it and/or modify it under
120             the same terms as the Perl 5 programming language system itself.
121              
122             =cut
123              
124              
125             __END__