Skip to content

Custom command to run test in terminal #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

adam12
Copy link
Owner

@adam12 adam12 commented Apr 24, 2025

:lua vim.lsp.codelens.refresh()

Then on a test

:lua vim.lsp.codelens.run()

@adam12 adam12 force-pushed the code-lens-run-in-terminal branch from b66074a to da9d990 Compare April 24, 2025 19:12
@@ -180,6 +180,16 @@ ruby_lsp.setup = function(config)

local server_started = false

-- TODO: Do I like this here?
vim.api.nvim_create_autocmd('LspAttach', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example given in the nvim docs triggers a refresh on BufEnter,CursorHold,InsertLeave. I'm not sure how much of a performance hit this would be. If you go this route, it would make sense to create buffer-local autocmds, or use a pattern filter.

Copy link
Contributor

@christopher-b christopher-b Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somthing like

vim.api.nvim_create_autocmd({ 'BufEnter', 'CursorHold', 'InsertLeave' }, {
  pattern = { '*.rb' },
  callback = function(args)
    vim.lsp.codelens.refresh({ bufnr = args.buf })
  end,
})

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely more appropriate. Let's go with it!

end

vim.lsp.commands['rubyLsp.debugTest'] = function(command)
vim.cmd(':terminal ' .. command.arguments[3])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about, for now:

vim.notify('"Debug" is not yet implemented', vim.log.levels.INFO)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternately, we could filter out unsupported commands. I think having "run" and "run in terminal" as two different commands doesn't make sense in nvim the way it does in VS Code. This seems to be working for me, only displaying the "run" virtual text, and running the command when I do vim.lsp.codelens.run(), without asking me to choose a command:

local original_codelens_handler = vim.lsp.codelens.on_codelens
local supported_commands = {
  ["rubyLsp.runTest"] = true,
}

vim.lsp.codelens.on_codelens = function(err, result, ctx)
  local client = vim.lsp.get_client_by_id(ctx.client_id)
  if client.name ~= "ruby_lsp" then return original_codelens_handler(err, result, ctx) end

  local filtered_result = vim.tbl_filter(function(lens)
    return lens.command and supported_commands[lens.command.command]
  end, result or {})

  return original_codelens_handler(err, filtered_result, ctx)
end

What do you think?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering supported commands is a great idea.

@christopher-b
Copy link
Contributor

We can also support the migration task:

vim.lsp.commands["rubyLsp.runTask"] = function(command)
  vim.cmd(":split | terminal " .. command.arguments[1])
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants