How do I configure ctags to index PHP namespaces and their aliases?
Asked Answered
C

3

11

when using PHP namespaces the code I work on uses a large number of namespace aliases, e.g.

<?php
namespace foo;

use bar\baz\qux as bazQux;

...

$a = new bazQux();

Is it possible to configure Exuberant Ctags to index the use of these namespace aliases so that I can jump from a line where the alias is used (the instantiation above) straight to the real class? If that's not possible, what's the best compromise that can be achieved?

I've seen somewhere a not-yet-integrated patch for ctags to use a PHP tokenizer for PHP support instead of the current regex-based implementation; I'd like to continue to use a stock ctags if possible though, rather than having to patch and compile a version myself.

Carlie answered 22/8, 2013 at 7:47 Comment(2)
It doesn't answer your question (it doesn't help inter-class navigation) but I recently found phpctags, which is very helpful if you also use the vim tagbar plugin: #11290852Mcfarland
@Mcfarland Thanks for that info; phpctags looks interesting.Carlie
V
4

This is the official changelog: http://ctags.sourceforge.net/news.html

They don't seem to merged yet this patch:

http://sourceforge.net/mailarchive/message.php?msg_id=30749245

So I don't think you can find a prebuilt package with php namespace support.

Vermifuge answered 28/10, 2013 at 19:7 Comment(2)
I suspect you're right, which is a shame. It looks like ctags isn't being actively maintained any longer; the last commit was 11 months ago. It's a shame this PHP parser patch has been waiting in limbo for so long.Carlie
github.com/fishman/ctags is a fork that has updated the php parser. It may work.Sleepwalk
B
2

I don't know well about php but I guess

use bar/baz/qux as bazQux;

should be

use bar\baz\qux as bazQux;

After replacing / with \ Universal-ctags(https://ctags.io) can capture bazQux well:

[jet@localhost]~/var/ctags% cat foo.php
cat foo.php
<?php
namespace foo;

use bar\baz\qux as bazQux;

[jet@localhost]~/var/ctags% ./ctags -o - foo.php
./ctags -o - foo.php
bazQux  foo.php /^use bar\\baz\\qux as bazQux;$/;"  a   namespace:foo   typeref:unknown:bar\\baz\\qux
foo foo.php /^namespace foo;$/;"    n
Beadroll answered 27/4, 2017 at 19:44 Comment(1)
Thanks for catching the syntax error with the namespace separators.Carlie
D
1

For those, who don't wait in limbo, there is solution:

You can try improved PHP omni complete for ViM: https://github.com/shawncplus/phpcomplete.vim.

This project supports also things like "use" keyword and namespaces, but it unfortunately depends on patched version of CTags.

Here are instructions how to prepare ctags version, that support namespaces: https://github.com/shawncplus/phpcomplete.vim/wiki/Patched-ctags

I know it's not a braindead solution, and it requires making your hands dirty with your own compilation of ctags, but it worked for me perfectly :)

Decoupage answered 30/6, 2015 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.