cjohansson / emacs-phps-mode

Emacs major mode for PHP with code intelligence
GNU General Public License v3.0
35 stars 2 forks source link

Unable to `narrow-to-defun` #47

Closed polaris64 closed 1 year ago

polaris64 commented 1 year ago

First of all, thanks for creating this package! I've always found php-mode to be problematic so I'm glad to see that there's work being done on an improved version!

I've encountered a couple of issues so far: -

  1. When using phps-mode with Eglot (in Emacs 29) and phpactor I see Flymake errors from all files in the project, not just the current file.
  2. I often use narrow-to-defun to narrow a PHP source buffer to a single function or method. This does not seem to work in phps-mode.

For reference, I'm using this following to load phps-mode: -

(use-package phps-mode
  :mode ("\\.php\\'" "\\.phtml\\'")
  :config
  (phps-mode-flymake-init)
  (setq phps-mode-async-process t)
  (setq phps-mode-async-process-using-async-el t))

Here's a screenshot showing the Flymake issue. Note that the referenced issue is from another file and the underlining is specific to that file, not the file loaded into the current buffer.

Emacs-Eglot-Flymake-phps-mode

polaris64 commented 1 year ago

Apologies: I've just gone back to php-mode temporarily and I noticed that the Eglot/Flymake/Phpactor issue seems to be present there too. I'm quite surprised because I'm sure I tested that before, but it indeed does not work correctly. It only shows up after opening a second project file though so perhaps that's why I missed it at first!

So it looks like issue 1 above is not related to phps-mode after all, but item 2 is still an issue.

cjohansson commented 1 year ago

Thanks for reporting that issue, I think I stumbled upon it sometime as well but forgot to document it. Should be fixed now in version 0.4.36

cjohansson commented 1 year ago

The automated test passes but when I test manually it doesn't work, back to the drawing-board

cjohansson commented 1 year ago

Ok now in 0.4.37 the issue should really be fixed

polaris64 commented 1 year ago

Hi @cjohansson, thanks for looking into this!

I've just tried again from the latest commit (a32f68a0d490555fc692f58a780fbdaec0df08b4) and this still doesn't appear to work for me.

In an example buffer with the following contents: -

<?php
// Before function

function test($a) {
    return $a + 1;
}

// After function

Placing the point somewhere inside the test function body and then running narrow-to-defun seems to do nothing. I would expect the buffer to be narrowed to the following: -

function test($a) {
    return $a + 1;
}
cjohansson commented 1 year ago

Try to use narrow-to-region instead, I think the issue rather is the structure of the imenu-index now, not sure what a defun is in the context of PHP? Like is it a namespace, class, interface, trait, enum, method, etc ?

polaris64 commented 1 year ago

I'm not sure exactly how php-mode defines it but it usually "just works"™ when I try to narrow to a particular function or method definition. Saying that though there are times when this fails so perhaps it's just a coincidence that it happens to work in php-mode?

I think ideally it would narrow to the innermost "thing" at point. If that's a class method then narrow to the method. If the point is within a class but outside a method then narrow to the class, etc.

cjohansson commented 1 year ago

Yeah I investigated this a bit and it should be pretty easy to add support for it in phps-mode

cjohansson commented 1 year ago

Hey - it should work now in version 0.4.38