やりたいこと
Rubyのファイルを編集中に指定のキーを押したとき、カーソル行を対象にRSpecを実行したい。
つまりターミナル内で下記コマンドを実行したい:
$ bundle exec rspec -f d ファイル名:行番号
手順
1. VSCode拡張Copy Relative Path and Line Numbersをインストール
この拡張は
Ctrl+l (MacOS)
Alt+l (Windows/Linux)
というキーバインドを提供し、ファイル名(相対パス):行番号
という形式でカーソル行の行番号をクリップボードにコピーしてくれる。
2. RSpec実行のキーバインド設定
keybindings.jsonに記述:
{
"key": "ctrl+r",
"command": "runCommands",
"args": {
"commands": [
"copy-relative-path-and-line-numbers.both",
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "bundle exec rspec -f d "
},
},
"workbench.action.terminal.paste",
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "\u000d" // Enter
},
}
]
},
"when": "editorLangId == 'ruby' && editorTextFocus",
},
これで、Rubyのファイルを編集中にCtrl+r
を押すと、ターミナルでrspecを実行するようになる。
他の方法
vscode-run-rspec-fileという拡張にも同じような機能があるようだ(試してない)。