Cezar Halmagean
Cezar Halmagean

Reputation: 902

Generating a better ctags file for Rails projects

How can I generate a good ctags file for a rails project ? One that includes all the gems, ruby libs and sorts them in the right order (like when you search for “Rails” it doesn’t go to the airbrake gem or whatever)

I'm using this right now, but I don't like it for the reason mentioned above:

ctags --tag-relative -Rf.git/tags.$$ --exclude=.git --exclude=*.html --exclude=tmp --exclude=public --exclude=app/assets --languages=-javascript,js,sql,html `bundle show --paths` $MY_RUBY_HOME .

Upvotes: 4

Views: 2215

Answers (3)

ohhh
ohhh

Reputation: 707

This is what I'm doing and it works okay:

#!/bin/bash

project_dir="~/<project>"

# Delete existing tags to start fresh
rm -f tags gemtags evrthngelse

# Generate tags for Ruby gems
ripper-tags -R -f gemtags <path to project gemfile>/gems

# Generate tags for the project, excluding certain directories and files
ctags -R -f evrthngelse --exclude=vendor --exclude=.git --exclude=log --exclude=tmp --exclude='*.rb' --exclude='*.erb' .
ripper-tags -R --exclude=vendor --exclude=.git --exclude=log --exclude=tmp .

# Concatenate temporary tag files into the main 'tags' file
cat evrthngelse >> tags
cat gemtags >> tags

# Remove temporary tag files
rm -f gemtags evrthngelse

# De-duplicate the tags file, preserving order
awk '!seen[$0]++' tags > temp_tags && mv temp_tags tags

I know the root issue is Ruby's loosey goosey dynamic class/module/method definitions, but it still feels like it should work better. I was hopeful for ripper-tags, but it's a lot worse than an IDE in terms of predicting which tag definitions you want to go to.

Upvotes: 0

Cezar Halmagean
Cezar Halmagean

Reputation: 902

So it turns out the better option (better than ctags) is either Robe for Emacs or a combination of vim + tmux + vmux + pry (to get show-source and edit).

irb-config is an interesting example of vim setup.

Upvotes: 1

romainl
romainl

Reputation: 196606

Neither Vim nor ctags understand or can be taught to understand your code and/or your thoughts. If you want to jump to a different tag than the first matching one in your tags file, use the proper commands:

:tselect foo        " list all tags 'foo'
:tselect /foo       " list all tags containing 'foo'
g]                  " list all tags matching the word under your cursor

Also, note that you can make "your" ctags command a little shorter by whitelisting filetypes:

ctags --tag-relative -Rf.git/tags.$$ --exclude=.git --exclude=tmp --exclude=public --exclude=app/assets --languages=ruby `bundle show --paths` $MY_RUBY_HOME .

Upvotes: 1

Related Questions