Skip to content

Instantly share code, notes, and snippets.

@andyl
Created February 18, 2011 19:08
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save andyl/834215 to your computer and use it in GitHub Desktop.
Save andyl/834215 to your computer and use it in GitHub Desktop.
Generates Ctags and Vim Paths from Gemfile.lock
#!/usr/bin/ruby
require 'rubygems'
require 'bundler'
=begin
This script was written to incorporate Bundler and Gemfile.lock into
Vim's tag and file-finding tools.
=end
# This code generates ctags. If a Gemfile.lock is found
# in the current directory, tags will be generated for
# every gem referenced in the Gemfile.
bundler_paths = []
if File.exist?('Gemfile.lock')
lockfile_contents = Bundler.read_file('Gemfile.lock')
lockfile = Bundler::LockfileParser.new(lockfile_contents)
bundler_paths = lockfile.specs.map do |spec|
spec.__materialize__
spec.full_gem_path
end
end
tags_file = ".tags"
opts = "--extra=+f -R -f #{tags_file}"
exclude_dirs = "--exclude=.git --exclude=log --exclude=.svn"
ctag_paths = "* #{bundler_paths.map {|x| x + '/*'}.join(' ')}"
command = "ctags #{opts} #{exclude_dirs} #{ctag_paths} 2> /dev/null"
puts "Processing ctags..."
system command
puts "Wrote ctags to '#{tags_file}'"
# This code generates a script that loads Bundler paths into vim.
# To use this script in vim, put this command into your .vimrc:
#
# autocmd BufNewFile,BufRead *.rb silent! source .paths.vim<cr>
system "rm -f .paths.vim"
unless bundler_paths.empty?
gem_paths = Gem.all_load_paths
vim_paths = bundler_paths.map {|x| x = x + '/lib'} - gem_paths
paths_file = ".paths.vim"
File.open(paths_file, 'w') do |file|
vim_paths.each {|x| file.puts "silent! set path+=#{x}" }
end
puts "Wrote #{vim_paths.length} vim paths to '#{paths_file}'"
end
@gerardc
Copy link

gerardc commented May 27, 2011

Sweet!

@skyeagle
Copy link

How about a make a vim plugin ? It would be great.

@tpope
Copy link

tpope commented Aug 25, 2011

@skyeagle
Copy link

Awesome! Thanks for your excellent work.

@andyl
Copy link
Author

andyl commented Aug 25, 2011

@tpope - I reviewed your vim-bundler code. for me, who doesn't know vim-script, the code is hard to read. Since you write plugins for Ruby, I'm curious why you write in vim-script, rather than using vim's capability for scripting in ruby.

ps thanks for all your work - esp pathogen which really made it easy to use vim plugins.

@tpope
Copy link

tpope commented Aug 25, 2011

Lots of reasons.

  1. The Vim Ruby interface sucks. It's a copy and paste of the Vim Perl interface, which is, as I recall, a copy and paste of the nvi Perl interface. So it was designed for another language to interface with another editor. The API is atrocious and only supports a couple of dozen methods, the most useful being eval, which means most of your plugin will end up being evaling Vimscript.
  2. It's buggy. I used it back in the day in rails.vim, and the maintainer of rubycomplete.vim started getting bug reports about stack overflows. Why? It turns out that because I first called the Ruby interface a few levels deep inside a Vim function, the interpreter was getting the short stick on stack space.
  3. It limits what versions of Vim you can use. This is not the end of the world for a plugin designed for Ruby users, but this means, for example, OS X users who use the command line Vim that ships with the OS are left out in the cold.
  4. It limits you to the system Ruby. Like 90% of the people I know use RVM nowadays, and this plugin would force them to sudo /usr/bin/gem update --system and sudo /usr/bin/gem install bundler.

Vimscript is a horrible, warty language. But it gets the job done.

@andyl
Copy link
Author

andyl commented Aug 25, 2011

Thanks for that explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment