Skip to Content

ctags for Ruby libs

Posted on

“Go to definition” and tags

Most IDE (and some Editor) support a function called: “Go to definition”, allow us quick jump to class/function. Basicly, the IDE/Editor use a mapping file called tags with format like that:

DynamoDBError	lib/aws/dynamodb_operator.rb	/^  class DynamoDBError < StandardError; end$/;"	c	class:Aws
DynamodbHelper	spec/support/dynamodb_helper.rb	/^module DynamodbHelper$/;"	m

This includes:

  • identifier: The name of the identifier (eg: function, method, variable, ..)
  • file_path: The path to the file where the identifier is defined.
  • address: The byte offset, or the regex to find the location of this identifier in the file.
  • language_specifier: Specifies the language of the source code (eg: c ~ Class, m ~ Module, …)

tags in Rails + Vim

To use this feature for Vim/NeoVim, we can call ctags to generate tags file, and use Vim plugin to navigate based on this file.

ctags -R --languages=ruby --exclude=.git --exclude=log --exclude=tmp .

tags for Rails library

Same above methods, we can generate tags for all Rails libraries (which are installed for current Ruby version) by add the lib paths to command

ctags -R --languages=ruby --exclude=.git --exclude=log --exclude=tmp . $(bundle list --paths)

Now in Vim/Neovim, we can go to definition in Rails/Ruby libraries :D

References

comments powered by Disqus