Configuring Perl::Tidy to handle custom keywords
Asked Answered
D

1

11

Is there a straightforward way to configure Perl::Tidy so that it handles correctly custom keywords created with Devel::Declare?

Specifically, I am using Sub::Curried, which creates a keyword curry that works more or less like sub with method signature in 5.20, but with facilities for currying:

curry add ($x, $y) {
    return $x + $y;
}

Notice that a ; is not required, which Perl::Tidy doesn't know since it gives:

curry add( $x, $y ) {
    return $x + $y;
  }

With a longer subroutine, it gets worse, with or without ;:

curry add( $x, $y ) {
    my $z = $x + $y;
      return $z;
};

I suppose telling Perl::Tidy to treat curry exactly as sub shouldn't be impossible. Alternatively, is there a way at least to have it disregard those sections that it doesn't understand and still tidy up the rest of the file?

Divot answered 23/5, 2014 at 18:2 Comment(0)
S
7

The only way I know how to do it is as follows from the man page:

Skipping Selected Sections of Code

Selected lines of code may be passed verbatim to the output without any formatting. This feature is enabled by default but can be disabled with the --noformat-skipping or -nfs flag. It should be used sparingly to avoid littering code with markers, but it might be helpful for working around occasional problems. For example it might be useful for keeping the indentation of old commented code unchanged, keeping indentation of long blocks of aligned comments unchanged, keeping certain list formatting unchanged, or working around a glitch in perltidy.

-fs, --format-skipping

This flag, which is enabled by default, causes any code between special beginning and ending comment markers to be passed to the output without formatting. The default beginning marker is #<<< and the default ending marker is #>>> but they may be changed (see next items below). Additional text may appear on these special comment lines provided that it is separated from the marker by at least one space. For example

 #<<<  do not let perltidy touch this
    my @list = (1,
                1, 1,
                1, 2, 1,
                1, 3, 3, 1,
                1, 4, 6, 4, 1,);
 #>>>

The comment markers may be placed at any location that a block comment may appear. If they do not appear to be working, use the -log flag and examine the .LOG file. Use -nfs to disable this feature.

So in your case the code would be as follows:

#<<<
curry add ($x, $y) {
    return $x + $y;
}
#>>>

Update

I realized that you may want to set custom special comment markers, which is covered with the following:

-fsb=string, --format-skipping-begin=string

The -fsb=string parameter may be used to change the beginning marker for format skipping. The default is equivalent to -fsb='#<<<'. The string that you enter must begin with a # and should be in quotes as necessary to get past the command shell of your system. It is actually the leading text of a pattern that is constructed by appending a '\s', so you must also include backslashes for characters to be taken literally rather than as patterns.

Some examples show how example strings become patterns:

 -fsb='#\{\{\{' becomes /^#\{\{\{\s/  which matches  #{{{ but not #{{{{
 -fsb='#\*\*'   becomes /^#\*\*\s/    which matches  #** but not #***
 -fsb='#\*{2,}' becomes /^#\*{2,}\s/  which matches  #** and #*****

You then need to set the ending special comment string:

-fse=string, --format-skipping-end=string

The -fsb=string is the corresponding parameter used to change the ending marker for format skipping. The default is equivalent to -fse='#<<<'.

Note The special comment strings do have to be comments so they always have to start with #

Surpass answered 24/5, 2014 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.