简体   繁体   English

如何在使用闭包时修复php模式缩进

[英]how to fix php-mode indenting when using closures

I use php-mode in Emacs, it work fine execept when I use closures as argument like this: 我在Emacs中使用php-mode,当我使用闭包作为参数时,它工作得很好:

$app->get('/', function() use ($app) {
        echo "foo";
    });

It seams that when function is inside function invocation the indentation is doubled. 它接缝当函数在函数调用内部时,缩进加倍。 How to fix this? 如何解决这个问题?

EDIT 编辑

How to make it look like this (the same as javascript-mode handle anonymous functions). 如何使它看起来像这样(与javascript模式处理匿名函数相同)。

$app->get('/', function() use ($app) {
    echo "foo";
});

If you put point at the end of the first line and press Cc Co, you can see what syntactic construct cc-mode thinks you're in the middle of, and customize how it indents that construct. 如果你把点放在第一行的末尾并按下Cc Co,你可以看到cc-mode认为你在中间的语法结构,并自定义它如何缩进该结构。 I don't have php-mode on my current computer, but I think setting this to zero worked reasonably well back when I was doing PHP. 我当前的计算机上没有php模式,但是当我在做PHP时,我认为设置为零可以很好地工作。

With help from @amalloy I manage to create this fix that solve the problem: @amalloy的帮助下,我设法创建了解决问题的解决方案:

(defun unident-closure ()
  (let ((syntax (mapcar 'car c-syntactic-context)))
    (if (and (member 'arglist-cont-nonempty syntax)
             (or
              (member 'statement-block-intro syntax)
              (member 'block-close syntax)))
        (save-excursion
          (beginning-of-line)
          (delete-char c-basic-offset)))))

(add-hook 'php-mode-hook (lambda ()
                           (add-hook 'c-special-indent-hook 'unident-closure))

not sure If it need to be in php-mode-hook or not 不确定如果它需要在php-mode-hook中

Great help was Cc Cs that show what will be in c-syntactic-context variable when c-special-indent-hook is fired (and it's after indent was done). 很大的帮助是Cc Cs显示c-special-indent-hook被触发时将在c-syntactic-context变量中的内容(并且在缩进完成之后)。

CC-Mode Manual also help CC模式手册也有帮助

UPDATE UPDATE

I found this code that is invalid 我发现这段代码无效

array('tags' => array_map($rows,function($row) {
....    return array(
....        'name' => $row['name'],
....        'size' => $normalize($row['tag_count']);
....    );
....});

(dots are the spaces that need to be removed) (点是需要删除的空格)

and another one 还有一个

return array('tags' =>
             array_map($rows, function($row) use ($normalize) {
             ....    return array(
             ....        'name' => $row['name'],
             ....        'size' => $normalize($row['tag_count']);
             ....    );
             ....});

so I need to modify the function, Cc Cs shows that arglist-cont-nonempty appear twice (each additional arglist-cont-nonempty add more indent that need to be removed) 所以我需要修改函数, Cc Cs显示arglist-cont-nonempty出现两次(每个额外的arglist-cont-nonempty添加更多需要删除的缩进)

(defun unident-closure ()
  (let ((syntax (mapcar 'car c-syntactic-context)))
    (if (and (member 'arglist-cont-nonempty syntax)
             (or
              (member 'statement-block-intro syntax)
              (member 'brace-list-intro syntax)
              (member 'brace-list-close syntax)
              (member 'block-close syntax)))
        (save-excursion
          (beginning-of-line)
          (delete-char (* (count 'arglist-cont-nonempty syntax)
                          c-basic-offset))))))

I tried the code in jcubic's answer and it no longer workers. 我在jcubic的答案中尝试了代码,它不再是工作者。 Based on amalloy's helpful answer, I've modified jcubic's workaround to the following, which works. 根据amalloy的有用答案,我已经将jcubic的解决方法修改为以下内容,这是有效的。

(defun unident-closure ()
  (let ((syntax (mapcar 'car c-syntactic-context)))
    (if (and (member 'arglist-cont-nonempty syntax)
             (or
              (member 'statement-block-intro syntax)
              (member 'brace-list-intro syntax)
              (member 'brace-list-close syntax)
              (member 'block-close syntax)
              (member 'defun-block-intro syntax)
              (member 'defun-close syntax)))
        (save-excursion
          (beginning-of-line)
          (delete-char (* (count 'arglist-cont-nonempty syntax)
                          c-basic-offset))))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM