DEV Community

Kay Gosho
Kay Gosho

Posted on

When developing Laravel app with Vim, you can still jump to Magic Methods

image

I am developing Laravel app with my favorite editor, Vim.

My compulsion used to be that I cannot jump to Laravel's magic method definitions such as getIsActiveAttribute, and scopeIsActive.

So I set proper ctags configuration to jump to them by C-].

~/.ctags

--regex-php=/get([a-z|A-Z|0-9]+)Attribute/\1/
--regex-php=/scope([a-z|A-Z|0-9]+)/\1/
Enter fullscreen mode Exit fullscreen mode

Then,

  • Run ctags command in your project root. Recent ctag read our configuration file automatically.
  • Open a file with vim (of course)
  • Set the cursor to a magic method
  • Type C-]

I strongly recommend customize your ctags. I set the following configuration to jump to the definition of class method of JavaScript.

--langmap=javascript:.js.es6.es.jsx
--javascript-kinds=-c-f-m-p-v
--regex-javascript=/^[ \t]*([a-z]+[ \t]+)*class[ \t]+([A-Za-z0-9_]+)[ \t]*([^)])/\2/c,class,classes/
--regex-javascript=/^[ \t]*([a-z]+[ \t]+)*const[ \t]+([A-Za-z0-9_]+)[ \t]*([^)])/\2/c,const/
--regex-javascript=/^[ \t]*([a-z]+[ \t]+)*type[ \t]+([A-Za-z0-9_]+)[ \t]*([^)])/\2/c,type/
--regex-javascript=/^[ \t]*([a-z]+[ \t]+)*[ \t]+([A-Za-z0-9_]+) ?\(\)[ \t]*([^)])/\2/c,method/
--regex-javascript=/^[ \t]*([a-z|A-Z]+)\(\)/\1/
--regex-javascript=/^[ \t]*([a-z]+[ \t]+)*const[ \t]+([A-Za-z0-9_]+)[ \t]*([^)])/\2/c,class,classes/
Enter fullscreen mode Exit fullscreen mode

Happy hacking with Vim!

Top comments (1)

Collapse
 
michaelvickersuk profile image
Michael • Edited

Lovely stuff, although my editor (Atom) still wouldn't jump to the scope or accessor as the first character of the tag will be uppercase, and this doesn't match with the search term the cursor will be positioned at.

My solution was to prefix these tags with a placeholder.

--regex-php=/get([a-z|A-Z|0-9]+)Attribute/_FIRST_CHAR_TO_LOWER_\1/
--regex-php=/scope([a-z|A-Z|0-9]+)/_FIRST_CHAR_TO_LOWER_\1/
Enter fullscreen mode Exit fullscreen mode

Then post-process the tags using sed and convert the uppercase first character to lowercase.

#!/usr/bin/env bash

ctags

sed --in-place \
--regexp-extended \
--expression='s/_FIRST_CHAR_TO_LOWER_(.)([a-z|A-Z|0-9]+)\t/\l\1\2\t/g' \
tags

sort tags --output=tags
Enter fullscreen mode Exit fullscreen mode