xcwen / ac-php

emacs auto-complete & company-mode for php
165 stars 35 forks source link

Possibility to find references #75

Open ilyaguy opened 6 years ago

ilyaguy commented 6 years ago

Is there any possibility to find references to symbol at point? For example, I want to search "all child of the class".

tam5 commented 6 years ago

@xcwen any ideas on how to implement this?

I know some other simple implementations of this would just do a grep for things that match the symbol name, though that usually has a number of false positives.

Maybe we could start with that and then narrow that list by using ac-php-find-symbol-at-point on each result and checking if it in fact should be considered a match.

Seems like there might be a better way, but maybe this could work? Thoughts?

(Actually, that alone wouldn't solve the use case mentioned above, but maybe the idea could be extend further for that)

Another possibility is to try and use cscope.

ilyaguy commented 6 years ago

I solved this functionality.

  1. Added this code to the ~/.emacs/init.el (or ~/.emacs)
    (require 'semantic/symref/grep)
    (add-to-list 'semantic-symref-filepattern-alist  '(php-mode "*.php" "*.tpl"))
  2. Added (semantic-mode) to php-mode-hook.
  3. Then use xref-find-references when I want to search symbol at point. Default binding is M-?.
tam5 commented 6 years ago

@ilyaguy thanks for your solution. I'm going to try it out for a few days.

At a first glance, it seems like its definitely useful and better than just a raw search, but it does have a bunch of shortcomings.

1) I get some false positives and it seems like its just doing a search for the name of the symbol at point. It even matched the word in strings, though I can't seem to figure out what it considers a match vs not. I haven't used semantic nor xref before though, so possibly there is something I can do to tweak it? 2) I assume related to 1, but it seems pretty slow. 3) Also because 1 & 2 it doesn't seem like it would be a reliable way of determining child/parent hierarchy for classes and methods.

ilyaguy commented 6 years ago

@tam5 yes, it is just grep over TAGS file. So, it has some false positives. But it is faster than grep -rni over the whole project.

xref may use different backends for search, but at this moment I don't know how to customise it or how to write new backend.

ilyaguy commented 6 years ago

@tam5 I've found https://github.com/dedi/gxref And it works much better than default one. It finds correct references to token (classname, method, property, etc..) and ignores comments, strings and other such things. But it uses own tags system. So, now I have in my project 3 different tags:

  1. from ctags
  2. from global tags
  3. from ac-php And only ac-php tags are updated automatically.
cyrialize commented 6 years ago

@ilyaguy You can try out ggtags: https://github.com/leoliu/ggtags

The installation method recommends installing an older version of GNU Global and ctags - I've written up instructions but haven't merged them in yet. The instructions are available on the read me of my .emacs.d: https://github.com/jonnycyr/.emacs.d

cmatzenbach commented 5 years ago

@jonnycyr So are the gtags better than the php tags generated through the suggestions in this packages README? Also, do you need both GNU global and ctags? I thought ctags was just for c/c++ so you would need GNU global and something to generate php tags (or does ctags do that?) Also, I checkout out your setup and was surprised to see you using lsp-php. The company completions are unusably slow (I commented on this ticket about it here ). Do you just work on small projects, or how on earth did you get it to be usable?

We need lsp and the different implementations to be finished to we can put the world of tags behind us

cyrialize commented 5 years ago

@cmatzenbach The ggtags package just gives you the ability to jump to variables, see occurrences of variables, etc. ac-php let’s you jump to function definitions but not variable definitions (yet).

I haven’t used ggtags in a while actually, but I believe ctags helps with php generation, I’m not quite sure.

Also, what I linked is now my old configuration. My newer one is here: https://github.com/jonnycyr/.emacs.d

I used to use lsp-php, but I stopped because of what you said - it’s really slow. It might be worth checking out eglot. I’ve been trying to use it in my spare time but I’ve run into difficulties trying to install it (I use straight.el instead of the default package manager, that may be why).

I also used to use ggtags. I was running into lagging issues so I stopped. I haven’t had time to try and fix it since then. I believe this package is the best to develop php in, I haven’t really felt the need for lsp. Then again, I haven’t used it much, so I don’t know what I’m missing.

I’d recommend checking out friends of Emacs php development, I think they’re just Emacs-php on GitHub. There’s a ton of packages being developed, phpactor (I believe) acts like lsp.

I apologize for the lack of links, I’m on mobile!