在Atom中設定rails的自動補齊
2015-11-24 09:12:00

UPDATE 2017-03-30

使用guard來自動產生tag有幾個問題,一個是每次修改檔案都會重建所有的tag,這不太對。另一點是要開guard才能生tag,但有些專案又不用guard,這樣有點尷尬,所以這個方法顯然不太好。後來我發現atom-ctags本身就會做產生tag的事,只是如果要整合ripper-tags,就必須要做一些修改。詳細內容可以參考:整合atom-ctags與ripper-tags來支援ruby的專案

=== 以下是舊的文章內容 ===

確認系統裡有安裝ctags

$ brew install ctags

試著跑一下ctags

在rails專案中執行下面這個指令:

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

如果要連bundle的gem也要做tag的話,可以加上bundle list --path:

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

這時候有一個tags的file就會被建立。

使用guard讓tag可以在檔案儲存時自動產生

如果每次改檔案都要手動重新建立tag也實在太麻煩啦,這時候guard就非常好用了。首先在Gemfile中加入guard-ctags-bundler:

Gemfile
group :development do
  gem 'guard-ctags-bundler'
end

bundle install完之後,執行guard init加入Guardfile的設定。

$ guard init ctags-bundler

這時候你會發現產生的Guardfile會長的像下面這個樣子:

Guardfile
guard 'ctags-bundler', :src_path => ["app", "lib", "spec/support"] do
  watch(/^(app|lib|spec\/support)\/.*\.rb$/)
  watch('Gemfile.lock')
end

guard-ctags-bundler還有提供很多選項可以做調整,請參考它的文件ivalkeen/guard-ctags-bundler

之後,在寫程式前執行guard,就可以自動產生tags了。

$ guard

讓Atom可以吃ctags來做到自動補齊

確認有安裝下列的package,就ok了。

  • autocomplete-plus
  • atom-ctags

記得將fuzzy的設定打開:在autocomplete-plus中的DefaultProvider設定選Fuzzy。

UPDATE 2016-05-27

官方的ctags對於ruby的支援 不是很好,所以可以改用 ripper-tags來建立ctag的tag檔,這時候Guardfile也要做一下調整:

Gemfile
group :development do
  gem 'ripper-tags'
end
Guardfile
guard 'ctags-bundler',
  src_path: ["app", "lib", "spec/support"],
  binary: 'ripper-tags',
  arguments: '-R --exclude=vendor' do
  watch(/^(app|lib|spec\/support)\/.*\.rb$/)
  watch('Gemfile.lock')
end