# New Ticket Created by  Vyacheslav Matjukhin 
# Please include the string:  [perl #57884]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=57884 >


genfile() method at the Parrot::Configure::Compiler module generates wrong
vim syntax modelines, i.e., it prints ft=c always.
This patch fixes it by implementing new file_type option.
If file_type isn't specified, genfile() tries to automatically recognize
file type by looking at a target file name.
Also, understanding file_type allows method users to forget about a
comment_type option in most cases.
This obviously generalizes old makefile option, so i removed it.


Patch contents description follows.
(as required by "How To Submit A Patch" from submissions.pod; should i
really do this? It all seems pretty obvious...)

config/gen/config_h.pm, config/gen/crypto.pm, config/gen/makefiles.pm:
using new file_type option where needed, sometimes instead of makefile
option.

lib/Parrot/Configure/Compiler.pm:
file_type option implementation and pod; hash with description of possible
file_type's and corresponding comment_type's and vim_ft's.

t/configure/034-step.t:
trivial test updates.

PS. I should've refactored 034-step.t too, but this can be done next time.
There are no tests checking genfile() output, they all just check return
value...
Index: lib/Parrot/Configure/Compiler.pm
===================================================================
--- lib/Parrot/Configure/Compiler.pm	(revision 30189)
+++ lib/Parrot/Configure/Compiler.pm	(working copy)
@@ -34,6 +34,25 @@
     move_if_diff
 );
 
+our %file_types_info = (
+    makefile => {
+        comment_type    => '#',
+        vim_ft          => 'make',
+    },
+    c => {
+        comment_type    => '/*',
+        vim_ft          => 'c',
+    },
+    pmc => {
+        comment_type    => '/*',
+        vim_ft          => 'pmc',
+    },
+    perl => {
+        comment_type    => '#',
+        vim_ft          => 'perl',
+    },
+);
+
 =item C<cc_gen()>
 
     $conf->cc_gen($source)
@@ -46,7 +65,7 @@
     my $conf   = shift;
     my $source = shift;
 
-    $conf->genfile( $source, "test_$$.c" );
+    $conf->genfile( $source, "test_$$.c", file_type => 'none' );
 }
 
 =item C<cc_build()>
@@ -179,13 +198,15 @@
 
 =over 4
 
-=item makefile
+=item file_type
 
-If set to a true value, this flag sets (unless overridden) C<comment_type>
-to '#', C<replace_slashes> to enabled, and C<conditioned_lines> to enabled.
+If set to a C<makefile>, C<c> or C<perl> value, C<comment_type> will be set
+to corresponding value.
+Moreover, when set to a C<makefile> value, it will set C<replace_slashes> to
+enabled, and C<conditioned_lines> to enabled.
 
-If the name of the file being generated ends in C<Makefile>, this option
-defaults to true.
+Its value will be detected automatically by target file name unless you set
+it to a special value C<none>.
 
 =item conditioned_lines
 
@@ -272,21 +293,52 @@
     open my $in,  '<', $source       or die "Can't open $source: $!";
     open my $out, '>', "$target.tmp" or die "Can't open $target.tmp: $!";
 
-    if ( !exists $options{makefile} && $target =~ m/makefile$/i ) {
-        $options{makefile} = 1;
+    if ( !exists $options{file_type}) {
+        if ( $target =~ m/makefile$/i ) {
+            $options{file_type} = 'makefile';
+        }
+        elsif ($target =~ m/\.pl$/i ) {
+            $options{file_type} = 'perl';
+        }
+        elsif ($target =~ m/\.[hc]$/ ) {
+            $options{file_type} = 'c';
+        }
+        elsif ($target =~ m/\.pmc$/ ) {
+            $options{file_type} = 'pmc';
+        }
+    } elsif ( $options{file_type} eq 'none' ) {
+        delete $options{file_type};
     }
 
-    if ( $options{makefile} ) {
-        exists $options{comment_type}      or $options{comment_type}      = '#';
-        exists $options{replace_slashes}   or $options{replace_slashes}   = 1;
-        exists $options{conditioned_lines} or $options{conditioned_lines} = 1;
+    if ( !exists $options{file_type} && $target =~ m/makefile$/i ) {
+        $options{file_type} = 'makefile';
     }
 
+    if ( $options{file_type} ) {
+        unless ( exists $file_types_info{$options{file_type}} ) {
+            die "Unknown file_type '$options{file_type}'";
+        }
+        unless ( exists $options{comment_type} ) {
+            $options{comment_type} = $file_types_info{$options{file_type}}{comment_type};
+        }
+        if ( $options{file_type} eq 'makefile' ) {
+            exists $options{replace_slashes}   or $options{replace_slashes}   = 1;
+            exists $options{conditioned_lines} or $options{conditioned_lines} = 1;
+        }
+    }
+
     if ( $options{comment_type} ) {
-        my @comment = ( 'ex: set ro ft=c:',
+        my @comment = ( 'ex: set ro',
             'DO NOT EDIT THIS FILE',
             'Generated by ' . __PACKAGE__ . " from $source" );
 
+        if ($options{file_type}) {
+            $comment[0] .= ' ft=' . $file_types_info{$options{file_type}}{vim_ft} . ':';
+        }
+        else {
+            $comment[0] .= ':';
+        }
+
         if ( $options{comment_type} eq '#' ) {
             foreach my $line (@comment) {
                 $line = "# $line\n";
Index: t/configure/034-step.t
===================================================================
--- t/configure/034-step.t	(revision 30189)
+++ t/configure/034-step.t	(working copy)
@@ -33,9 +33,9 @@
     ok(
         $conf->genfile(
             $dummy   => 'CFLAGS',
-            makefile => 1,
+            file_type => 'makefile',
         ),
-        "genfile() returned true value with 'makefile' option"
+        "genfile() returned true value with 'file_type' option being set to 'makefile'"
     );
     unlink $dummy or croak "Unable to delete file after testing";
     chdir $cwd    or croak "Unable to change back to starting directory";
@@ -48,7 +48,7 @@
     open my $IN, '>', $dummy or croak "Unable to open temp file for writing";
     print $IN qq{Hello world\n};
     close $IN or croak "Unable to close temp file";
-    eval { $conf->genfile(  $dummy => 'CFLAGS', makefile => 1, comment_type => q{<!--}, ); };
+    eval { $conf->genfile(  $dummy => 'CFLAGS', file_type => 'makefile', comment_type => q{<!--}, ); };
     like(
         $@,
         qr/^Unknown comment type/,
@@ -68,7 +68,7 @@
     ok(
         $conf->genfile(
             $dummy       => 'CFLAGS',
-            makefile     => 1,
+            file_type    => 'makefile',
             feature_file => 0,
         ),
         "genfile() returned true value with false value for 'feature_file' option"
Index: config/gen/config_h.pm
===================================================================
--- config/gen/config_h.pm	(revision 30189)
+++ config/gen/config_h.pm	(working copy)
@@ -40,13 +40,11 @@
     my ( $self, $conf ) = @_;
 
     $conf->genfile($self->{templates}->{config_h}, 'include/parrot/config.h',
-        comment_type      => '/*',
         ignore_pattern    => 'PARROT_CONFIG_DATE',
         conditioned_lines => 1
     );
 
     $conf->genfile($self->{templates}->{feature_h}, 'include/parrot/feature.h',
-        comment_type   => '/*',
         ignore_pattern => 'PARROT_CONFIG_DATE',
         feature_file   => 1
     );
Index: config/gen/crypto.pm
===================================================================
--- config/gen/crypto.pm	(revision 30189)
+++ config/gen/crypto.pm	(working copy)
@@ -77,10 +77,7 @@
             ? '#if 0'
             : '#ifndef OPENSSL_NO_' . $md
         );
-        $conf->genfile(
-            $self->{digest_pmc_template} => "src/dynpmc/${file}.pmc",
-            comment_type                      => '/*',
-        );
+        $conf->genfile( $self->{digest_pmc_template} => "src/dynpmc/${file}.pmc" );
     }
 
     return 1;
Index: config/gen/makefiles.pm
===================================================================
--- config/gen/makefiles.pm	(revision 30189)
+++ config/gen/makefiles.pm	(working copy)
@@ -28,13 +28,8 @@
     $data{result}      = q{};
     $data{makefiles}   = {
         'Makefile' => { SOURCE => 'config/gen/makefiles/root.in' },
+        'ext/Makefile' => { SOURCE => 'config/gen/makefiles/ext.in', },
 
-        'ext/Makefile' => {
-            SOURCE            => 'config/gen/makefiles/ext.in',
-            commentType       => '#',
-            replace_slashes   => 1,
-            conditioned_lines => 1,
-        },
         'ext/Parrot-Embed/Makefile.PL' => {
             SOURCE            => 'config/gen/makefiles/parrot_embed.in',
             replace_slashes   => 0,
@@ -64,13 +59,11 @@
 
         'tools/build/dynpmc.pl' => {
             SOURCE            => 'config/gen/makefiles/dynpmc_pl.in',
-            comment_type      => '#',
             replace_slashes   => 0,
             conditioned_lines => 1,
         },
         'tools/build/dynoplibs.pl' => {
             SOURCE            => 'config/gen/makefiles/dynoplibs_pl.in',
-            comment_type      => '#',
             replace_slashes   => 0,
             conditioned_lines => 1,
         },

Reply via email to