LoginSignup
9
9

More than 5 years have passed since last update.

rubocopのチェック結果をpecoで選択して、vimで該当行にジャンプした状態で開く

Posted at

下記関数を.bashrcなりに追加して、virc <ソースDIR> すれば(出力フォーマットをカスタマイズしていなければ)コードのpathと行番号が出力されている部分を選択すると、その行をvimで開くことが出来る

# rubocopの結果をvimで開く
function virc {
    DIR="$1"

    F=$(rubocop ${DIR} | peco | awk -F ":" '{print "-c "$2" "$1}')
    if [ $? -eq 0 -a "${F}" != "" ]
    then
      eval "vi ${F}"
    fi
}
lhmのソースでの実行例
QUERY>                                                                                                                                                                                    IgnoreCase [239 (1/5)]
Inspecting 17 files
.CW.CC.CCC.CCC...
Offenses:
lib/lhm/atomic_switcher.rb:38:27: C: Space inside string interpolation detected.  # この行を選択
        "rename table `#{ @origin.name }` to `#{ @migration.archive_name }`, " \
                          ^^^^^^^^^^^^
lib/lhm/atomic_switcher.rb:38:50: C: Space inside string interpolation detected.
        "rename table `#{ @origin.name }` to `#{ @migration.archive_name }`, " \
                                                 ^^^^^^^^^^^^^^^^^^^^^^^
lib/lhm/atomic_switcher.rb:39:14: C: Space inside string interpolation detected.
        "`#{ @destination.name }` to `#{ @origin.name }`"
             ^^^^^^^^^^^^^^^^^
lib/lhm/atomic_switcher.rb:39:42: C: Space inside string interpolation detected.
        "`#{ @destination.name }` to `#{ @origin.name }`"
                                         ^^^^^^^^^^^^
lib/lhm/atomic_switcher.rb:46:20: C: Space inside string interpolation detected.
        error "`#{ @origin.name }` and `#{ @destination.name }` must exist"
                   ^^^^^^^^^^^^
lib/lhm/atomic_switcher.rb:46:44: C: Space inside string interpolation detected.
        error "`#{ @origin.name }` and `#{ @destination.name }` must exist"
                                           ^^^^^^^^^^^^^^^^^
lib/lhm/atomic_switcher.rb:53:7: C: Redundant begin block detected.
      begin
      ^^^^^
:
:
atomic_switcher.rb
 22     def initialize(migration, connection = nil)
 23       @migration = migration
 24       @connection = connection
 25       @origin = migration.origin
 26       @destination = migration.destination
 27       @retries = 0
 28       @max_retries = MAX_RETRIES
 29       @retry_sleep_time = RETRY_SLEEP_TIME
 30     end
 31
 32     def statements
 33       atomic_switch
 34     end
 35
 36     def atomic_switch
 37       [
 38         "rename table `#{ @origin.name }` to `#{   #@migration.archive_name }`, " \  # この行が選択された状態で開く
 39         "`#{ @destination.name }` to `#{ @origin.name }`"
 40       ]
 41     end
 42
 43     def validate
 44       unless @connection.table_exists?(@origin.name) &&
 45              @connection.table_exists?(@destination.name)
 46         error "`#{ @origin.name }` and `#{ @destination.name }` must exist"
 47       end
 48     end
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9