Skip to content

Commit

Permalink
Add force option to :RspecJump command (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mogulla3 authored Sep 9, 2023
1 parent 0250994 commit f2da341
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 deletions.
4 changes: 2 additions & 2 deletions PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@

## Ideas

- [ ] Go to the spec file corresponding to the open buffer (and vice versa)
- [ ] If the spec file corresponding to the open buffer does not exist, create a spec file.
- [x] Go to the spec file corresponding to the open buffer (and vice versa)
- [x] If the spec file corresponding to the open buffer does not exist, create a spec file.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Then, you can use the following commands.
|`:RSpecOnlyFailures`|Run rspec on the current file with `--only-failures` option. [^1]|
|`:RSpecShowLastResult`|Show last spec result on floating window.|
|`:RSpecAbort`|Abort running rspec.|
|`:RSpecJump`|Jump from product code file to spec file (or vice versa).|
|`:RSpecJump`, `:RSpecJump!`|Jump from product code file to spec file (or vice versa). With `!`, if the file to jump to does not exist, attempt to create and then jump to it.|

Below is the recommended key mappings.

Expand All @@ -108,7 +108,10 @@ vim.keymap.set("n", "<leader>rs", ":RSpecShowLastResult<CR>", { noremap = true,
And below is the recommended user command.

```lua
vim.api.nvim_create_user_command("RJ", "RSpecJump", {})
-- This is a shortcut command for the RSpecJump and RSpecJump!.
vim.api.nvim_create_user_command('RJ', function(args)
rspec.jump({ force = args.bang })
end, { bang = true })
```

## Smart selection of rspec command and execution path
Expand Down
8 changes: 5 additions & 3 deletions lua/rspec/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ function M.abort()
runner.abort()
end

function M.jump()
jumper.jump()
function M.jump(options)
jumper.jump(options)
end

---@param user_config table
Expand All @@ -73,7 +73,9 @@ function M.setup(user_config)
vim.cmd("command! RSpecRerun lua require('rspec').rerun()<CR>")
vim.cmd("command! RSpecShowLastResult lua require('rspec').show_last_result()<CR>")
vim.cmd("command! RSpecAbort lua require('rspec').abort()<CR>")
vim.cmd("command! RSpecJump lua require('rspec').jump()<CR>")
vim.api.nvim_create_user_command('RSpecJump', function(args)
jumper.jump({ force = args.bang })
end, { bang = true })
end

return M
77 changes: 64 additions & 13 deletions lua/rspec/jumper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ local function infer_product_code_paths(bufname, project_root)
return results
end

--- Jump to the path passed in the argument
---
---@param path string
local function jump_to_file(path)
vim.api.nvim_command(config.jump_command .. " " .. path)
end

--- Jump to the path passed in the argument
--- If the directory does not exist, create it.
---
---@param path string
local function force_jump_to_file(path)
if vim.fn.filereadable(path) ~= 1 then
vim.fn.mkdir(vim.fs.dirname(path), "p")
end

jump_to_file(path)
end

--- Jump between specs and product code.
---
--- If the current buffer is a product code, it jumps to the related specs.
Expand All @@ -199,7 +218,12 @@ end
---
--- The inferred jump destination files have a priority order.
--- The files are searched in order of priority and the first file found is jumped to.
function Jumper.jump()
---
--- If the force option is enabled, create the inferred file if it does not exist.
---
---@param options table
function Jumper.jump(options)
local opts = options or {}
local bufname = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
if not vim.endswith(bufname, ".rb") then
vim.notify("[rspec.nvim] RSpecJump can only be run on `.rb` files", vim.log.levels.ERROR)
Expand All @@ -224,20 +248,47 @@ function Jumper.jump()
return
end

local is_file_found = false
for _, inferred_path in pairs(inferred_paths) do
if vim.fn.filereadable(inferred_path) == 1 then
vim.api.nvim_command(config.jump_command .. " " .. inferred_path)
is_file_found = true
break
if opts.force then
local selected_path

if vim.tbl_count(inferred_paths) > 1 then
local inputlist_items = { "Choose the path you want to jump." }
for i, inferred_path in pairs(inferred_paths) do
table.insert(inputlist_items, i .. ": " .. inferred_path)
end

local selected_num = vim.fn.inputlist(inputlist_items)
selected_path = inferred_paths[selected_num]

-- If the user presses `Escape` or `q`, 0 is passed.
if selected_num == 0 then
return
elseif not selected_path then
vim.notify("[rspec.nvim] Invalid number '" .. selected_num .. "' is selected", vim.log.levels.ERROR)
return
end
else
selected_path = inferred_paths[1]
end
end

if not is_file_found then
vim.notify(
"[rspec.nvim] Not found all of the following files:\n" .. table.concat(inferred_paths, "\n"),
vim.log.levels.ERROR
)
force_jump_to_file(selected_path)
else
local is_file_found = false

for _, inferred_path in pairs(inferred_paths) do
if vim.fn.filereadable(inferred_path) == 1 then
jump_to_file(inferred_path)
is_file_found = true
break
end
end

if not is_file_found then
vim.notify(
"[rspec.nvim] Not found all of the following files:\n" .. table.concat(inferred_paths, "\n"),
vim.log.levels.ERROR
)
end
end
end

Expand Down

0 comments on commit f2da341

Please sign in to comment.