Skip to content
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

fzf integration as an alternative to telescope - closes #701 #702

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ Please read the docs over there if you are every working on debugging related
stuff with as they are pretty thorough and insightful. Mathias does a great job
at making sure his stuff is stable and documented.

The metals commands are also available as a
[telescope](https://github.com/nvim-telescope/telescope.nvim) extension. The
code for this is pretty self contained and can be found in
`https://github.com/scalameta/nvim-metals/tree/main/lua/telescope/_extensions`.

The metals commands are also available as
[extensions](https://github.com/scalameta/nvim-metals/tree/main/lua/telescope/_extensions)
for [telescope](https://github.com/nvim-telescope/telescope.nvim)
and [fzf-lua](https://github.com/ibhagwan/fzf-lua).

## Logging

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ To see the full details on the available configurations, checkout out `:help
metals-integrations`. The currently available integrations are:
- [nvim-dap](https://github.com/mfussenegger/nvim-dap)
- [Telescope](https://github.com/nvim-telescope/telescope.nvim)

- [fzf-lua](https://github.com/ibhagwan/fzf-lua)

[^no-lspconfig]: If you're familiar with nvim and LSP you'll probably know of
[`nvim-lspconfig`](https://github.com/neovim/nvim-lspconfig) which also has a
Expand Down
23 changes: 16 additions & 7 deletions doc/metals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,11 @@ commands() *commands()
utilizing |vim.ui.select()|. If you're a telescope
user you can choose to use this with something like
https://github.com/stevearc/dressing.nvim or feel
free to use the built-in telescope extension
|metals-telescope| which just displays things a bit
better since we have more control over using
|vim.ui.select()|. If you're not a telescope user,
then you'll want to use this.
free to use the built-in extensions
|metals-telescope| or |metals-fzf-lua| which just displays
things a bit better since we have more control over using
|vim.ui.select()|. If you're not a telescope or
fzf-lua user, then you'll want to use this.

*compile_cancel()*
compile_cancel() Use to execute a |metals.compile-cancel| command.
Expand Down Expand Up @@ -1370,7 +1370,7 @@ https://github.com/nvim-telescope/telescope.nvim which allows you to easily
choose any of the |metals-commands|. You can trigger the picker by using the
following function in a mapping: >

lua require("telescope").extensions.metals.commands()
:lua require("telescope").extensions.metals.commands()
<

If this is the only way you'll trigger the picker, then there is no need to
Expand All @@ -1387,8 +1387,17 @@ longer be lazy loaded. >
require("telescope").extensions.metals.commands()

<
*metals-fzf-lua*

NOTE: If you'd rather utilize |vim.ui.select| for this a more generate
Metals provides a custom picker from
https://github.com/ibhagwan/fzf-lua which allows you to easily
choose any of the |metals-commands|. You can trigger the picker by using the
following function in a mapping: >

:lua require("metals").fzf_menu()
<

NOTE: If you'd rather utilize |vim.ui.select| for this, a more generic
Copy link
Member

Choose a reason for hiding this comment

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

Ahhh I see what I wrote before you probably know since you touched this.

|commands()| is available in the |metals-lua-api|.

vim:tw=80:ts=2:ft=help:
45 changes: 45 additions & 0 deletions lua/fzf-lua/_extensions/metals.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local log = require("metals.log")
local commands_table = require("metals.commands").commands_table

local has_fzf, fzf = pcall(require, "fzf-lua")

if not has_fzf then
local msg = "fzf-lua must be installed to use this functionality (https://github.com/ibhagwan/fzf-lua)"
log.error_and_show(msg)
end

local function run_entry(entry)
local command = require("metals")[entry.id]
local success, msg = pcall(command)
if not success then
vim.api.nvim_notify(msg, 2, {})
end
end

local function run_selected(label)
for _, entry in ipairs(commands_table) do
if entry.label == label then
run_entry(entry)
return
end
end
end

return function(opts)
opts = opts or {}
Copy link
Member

Choose a reason for hiding this comment

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

I don't see one looking through the docs, but I wish there was a way to also include the description that would be shown but not considered when you search. Either way, maybe an improvement for later on if someone knows how to add that.

Copy link
Author

Choose a reason for hiding this comment

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

The only way I see is through a preview, as the fzf-lua menu entries are simple strings.

opts.prompt = "Metals Commands> "
opts.previewer = false
opts.actions = {
["default"] = function(sel)
run_selected(sel[1])
end,
}
opts.winopts = { width = 40, height = 12, fullscreen = false }

local labels = {}
for _, entry in ipairs(commands_table) do
table.insert(labels, entry.label)
end

return fzf.fzf_exec(labels, opts)
end
2 changes: 2 additions & 0 deletions lua/metals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,6 @@ M.commands = function()
end)
end

M.fzf_menu = require("fzf-lua._extensions.metals")

return M
Loading