How can I prevent PerlTidy from aligning assignments but keep adding single spaces?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 20:13:26

The following patch worked for me:

--- Tidy.pm.org 2009-06-16 22:00:50.000000000 +0200
+++ Tidy.pm 2010-12-28 09:43:19.625000000 +0100
@@ -12404,7 +12404,7 @@
         # accept vertical alignment.

         # nothing to do if we aren't allowed to change whitespace
-        if ( !$rOpts_add_whitespace ) {
+        if ( 1 || !$rOpts_add_whitespace ) {
             for my $i ( 0 .. $max_index_to_go ) {
                 $matching_token_to_go[$i] = '';
             }

There is an undocumented flag --no-valign which appears to achieve the best of both worlds without modifying the perltidy source.

As you point out, --no-add-whitespace is too aggressive and prevents whitespace from being added in other, desirable locations (around operators etc.). With --no-valign perltidy is still correcting things like my ($arg)=@_; to my ($arg) = @_; but does not attempt to vertically align operators across lines. The setting does not completely disable the vertical aligner, so you still get some benefits in other places (e.g. side-comments).

The only problem I have found with this so far is that the first side-comment of a block of side-comments is not aligned with the subsequent ones:

my @DISAGREE_NONFATAL = grep { exists $warnings::Offsets{$_} } (
    'newline', # stat on nonexistent file with a newline in it
    'experimental', # no reason for these to be fatal
    'deprecated',   # unfortunately can't make these fatal
    'portable',     # everything worked fine here, just may not elsewhere
);

It is only respecting --minimum-space-to-comment. I'm not sure why the subsequent (third and fourth) lines work properly. I don't use side-comments much so it's not a major issue (and you could use --format-skipping on such blocks).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!