如何防止 PerlTidy 对齐分配但继续添加单个空格?

发布于 2024-10-09 10:52:51 字数 600 浏览 5 评论 0原文

如何防止 PerlTidy 对齐分配但继续添加单个空格?

这个问题类似于如何防止 PerlTidy 对齐我的作业? 但我想在指示的地方添加单个空格。由于这个 -naws 开关对我不起作用。我只是不想插入多个空格。可以使用 perltidy 或其他工具吗?

Perl 整洁的更改:

my $a    = 1;
my $aa = 2;
my $aaa= 3;

使用

my $a   = 1;
my $aa  = 2;
my $aaa = 3;

-naws 后它保持不变:

my $a    = 1;
my $aa = 2;
my $aaa= 3;

我希望将此代码格式化为:

my $a = 1;
my $aa = 2;
my $aaa = 3;

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

This question is similar to How can I prevent PerlTidy from aligning my assignments? but I would like single spaces to be added where directed. Due to this -naws switch does not work for me. I just do not want multiple spaces to be inserted. Is it possibe with perltidy or some other tool?

Perl tidy changes:

my $a    = 1;
my $aa = 2;
my $aaa= 3;

into

my $a   = 1;
my $aa  = 2;
my $aaa = 3;

with -naws it remains unchanged:

my $a    = 1;
my $aa = 2;
my $aaa= 3;

I would like this code to be formatted as:

my $a = 1;
my $aa = 2;
my $aaa = 3;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2024-10-16 10:52:51

有一个未记录的标志 --no-valign 似乎可以在不修改 perltidy 源的情况下实现两全其美。

正如您所指出的, --no-add-whitespace 过于激进,会阻止在其他所需位置(运算符周围等)添加空格。使用 --no-valign perltidy 仍在将 my ($arg)=@_; 更正为 my ($arg) = @_; code> 但不会尝试跨行垂直对齐运算符。该设置不会完全禁用垂直对齐器,因此您仍然可以在其他地方获得一些好处(例如侧面注释)。

到目前为止,我发现的唯一问题是,侧面注释块的第一个侧面注释与后续注释不一致:

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
);

它仅尊重 --minimum-space-to-comment.我不确定为什么后续(第三和第四)行可以正常工作。我不太使用旁注,因此这不是一个主要问题(您可以在此类块上使用 --format-skipping )。

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).

蓝色星空 2024-10-16 10:52:51

以下补丁对我有用:

--- 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] = '';
             }

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] = '';
             }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文