Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #97 from mrjones2014/matjones/25-fzf-lua-support
Browse files Browse the repository at this point in the history
Add support for fzf-lua
  • Loading branch information
mrjones2014 authored Oct 31, 2021
2 parents f2d76f1 + b552fa1 commit 5318ad2
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 67 deletions.
97 changes: 60 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Dash.nvim

Query [Dash.app](https://kapeli.com/dash) within Neovim with a Telescope picker!
Query [Dash.app](https://kapeli.com/dash) within Neovim with Telescope or fzf-lua!

<!-- panvimdoc-ignore-start -->

Expand All @@ -20,32 +20,33 @@ Note: Dash is a Mac-only app, so you'll only find this plugin useful on Mac.

## Install

This plugin must be loaded *after* your fuzzy-finder plugin of choice. Currently supported fuzzy-finder plugins are:

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

After installing Dash.nvim, you must run `make install`. This can be done through a post-install hook with most plugin managers.

Packer:

```lua
use({
'mrjones2014/dash.nvim',
requires = { 'nvim-telescope/telescope.nvim' },
run = 'make install',
disable = not vim.fn.has('macunix'),
})
```

Paq:

```lua
require("paq")({
'nvim-telescope/telescope.nvim';
{'mrjones2014/dash.nvim', run = 'make install'};
{ 'mrjones2014/dash.nvim', run = 'make install' };
})
```

Vim-Plug:

```VimL
Plug 'nvim-telescope/telescope.nvim'
Plug 'mrjones2014/dash.nvim', { 'do': 'make install' }
```

Expand All @@ -63,50 +64,70 @@ This plugin has two editor commands, `:Dash` and `:DashWord`, each of which acce
search Dash.app with keywords based on config (see `file_type_keywords` in [configuration](#configuration)). The bang (`!`)
will search without this keyword filtering.

`:Dash [query]` will open the Telescope picker, and if `[query]` is passed, it will pre-fill the prompt with `[query]`.
`:Dash [query]` will open the fuzzy-finder, and if `[query]` is passed, it will pre-fill the prompt with `[query]`.

`:DashWord` will open the Telescope picker and pre-fill the prompt with the word under the cursor.
`:DashWord` will open the fuzzy-finder and pre-fill the prompt with the word under the cursor.

If using Telescope, these can also be accessed via `:Telescope dash search` and `:Telescope dash search_no_filter`.
If using fzf-lua, you can use `:FzfLua dash`, or `:lua require('fzf-lua').dash({ bang = false, initial_text = '' })`.

## Configuration

`dash.nvim` can be configured in your Telescope config. Options and defaults are described below:
### With Telescope

```lua
require('telescope').setup({
extensions = {
dash = {
-- configure path to Dash.app if installed somewhere other than /Applications/Dash.app
dash_app_path = '/Applications/Dash.app',
-- search engine to fall back to when Dash has no results, must be one of: 'ddg', 'duckduckgo', 'startpage', 'google'
search_engine = 'ddg',
-- debounce while typing, in milliseconds
debounce = 0,
-- map filetype strings to the keywords you've configured for docsets in Dash
-- setting to false will disable filtering by filetype for that filetype
-- filetypes not included in this table will not filter the query by filetype
-- check src/config.rs to see all defaults
-- the values you pass for file_type_keywords are merged with the defaults
-- to disable filtering for all filetypes,
-- set file_type_keywords = false
file_type_keywords = {
dashboard = false,
NvimTree = false,
TelescopePrompt = false,
terminal = false,
packer = false,
-- a table of strings will search on multiple keywords
javascript = { 'javascript', 'nodejs' },
typescript = { 'typescript', 'javascript', 'nodejs' },
typescriptreact = { 'typescript', 'javascript', 'react' },
javascriptreact = { 'javascript', 'react' },
-- you can also do a string, for example,
-- sh = 'bash'
},
-- your config here
}
}
})
```

### With fzf-lua

```lua
require('dash').setup({
-- your config here
})
```

Options and defaults are described below:

```lua
{
-- configure path to Dash.app if installed somewhere other than /Applications/Dash.app
dash_app_path = '/Applications/Dash.app',
-- search engine to fall back to when Dash has no results, must be one of: 'ddg', 'duckduckgo', 'startpage', 'google'
search_engine = 'ddg',
-- debounce while typing, in milliseconds
debounce = 0,
-- map filetype strings to the keywords you've configured for docsets in Dash
-- setting to false will disable filtering by filetype for that filetype
-- filetypes not included in this table will not filter the query by filetype
-- check src/config.rs to see all defaults
-- the values you pass for file_type_keywords are merged with the defaults
-- to disable filtering for all filetypes,
-- set file_type_keywords = false
file_type_keywords = {
dashboard = false,
NvimTree = false,
TelescopePrompt = false,
terminal = false,
packer = false,
fzf = false,
-- a table of strings will search on multiple keywords
javascript = { 'javascript', 'nodejs' },
typescript = { 'typescript', 'javascript', 'nodejs' },
typescriptreact = { 'typescript', 'javascript', 'react' },
javascriptreact = { 'javascript', 'react' },
-- you can also do a string, for example,
-- sh = 'bash'
},
}
```

If you notice an issue with the default `file_type_keywords` or would like a new filetype added, please file an issue or submit a PR!

### Lua API
Expand All @@ -121,8 +142,10 @@ require('dash').setup(config)
```

```lua
--- This will bind to the first fuzzy-finder it finds to be available,
--- checked in order: telescope, fzf-lua
---@param bang boolean @bang searches without any filtering
---@param initial_text string @pre-fill text into the telescope picker
---@param initial_text string @pre-fill text into the finder prompt
require('dash').search(bang, initial_text)
```

Expand Down
Binary file modified bin/arm/libdash_nvim.so
Binary file not shown.
Binary file modified bin/x86/libdash_nvim.so
Binary file not shown.
12 changes: 11 additions & 1 deletion lua/dash/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ local M = {}
---@param bang boolean @bang searches without any filtering
---@param initial_text string @pre-fill text into the telescope picker
function M.search(bang, initial_text)
require('dash.providers.telescope').build_picker(bang == true, initial_text):find()
local telescope_installed, _ = pcall(require, 'telescope')
if telescope_installed then
return require('dash.providers.telescope').dash(bang == true, initial_text)
end

local fzf_lua_installed, _ = pcall(require, 'fzf-lua')
if fzf_lua_installed then
return require('dash.providers.fzf-lua').dash({ bang = bang or false, initial_text = initial_text or '' })
end

vim.api.nvim_err_writeln('Dash.nvim: no supported fuzzy-finder plugins found.')
end

--- see src/config.rs for all config keys
Expand Down
72 changes: 72 additions & 0 deletions lua/dash/providers/fzf-lua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
local M = {}

local cached_results = {}

local function handle_selected(selected)
if not selected or #selected ~= 1 or not cached_results or #cached_results < 1 then
return
end

local matching_items = vim.tbl_filter(function(item)
return item.display == selected[1]
end, cached_results)
if not matching_items or #matching_items < 1 then
return
end
local selected_item = matching_items[1]
local libdash = require('libdash_nvim')
if selected_item.is_fallback then
libdash.open_search_engine(selected_item.value)
else
libdash.query(selected_item.query, '', true)
libdash.open_item(selected_item.value)
end
end

local default_opts = {
exec_empty_query = true,
bang = false,
actions = {
default = handle_selected,
},
}

M.dash = function(opts)
local fzf_lua = require('fzf-lua')
opts = fzf_lua.config.normalize_opts(opts, default_opts)
if not opts then
return
end

opts.query = opts.initial_text or ''
opts.prompt = 'Dash> '
opts.fzf_opts = {
['--header'] = vim.fn.shellescape(require('dash.providers').build_picker_title(opts.bang or false)),
}

-- This gets called whenever input is changed
-- Also gets called first run if `opts.exec_empty_query == true`
local current_file_type = vim.bo.filetype
opts._reload_action = function(query)
if not query or #query == 0 then
return {}
end

cached_results = require('libdash_nvim').query(query, current_file_type, opts.bang or false)
local items = {}
for _, item in pairs(cached_results) do
table.insert(items, item.display)
end
return items
end

-- This sets all the required fzf arguments for `change:reload` callbacks
opts = fzf_lua.core.set_fzf_interactive_cb(opts)

coroutine.wrap(function()
local selected = fzf_lua.core.fzf_files(opts)
fzf_lua.actions.act(opts.actions, selected, opts)
end)()
end

return M
26 changes: 26 additions & 0 deletions lua/dash/providers/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local M = {}

function M.build_picker_title(bang)
local config = require('libdash_nvim').config
local filetype = vim.bo.filetype
local file_type_keywords = config.file_type_keywords[filetype]
if bang or not file_type_keywords then
return 'Dash'
end

if file_type_keywords == true then
return 'Dash - filtering by: ' .. filetype
end

if type(file_type_keywords) == 'string' then
return 'Dash - filtering by: ' .. file_type_keywords
end

if type(file_type_keywords) == 'table' then
return 'Dash - filtering by: ' .. (vim.inspect(file_type_keywords)):gsub('\n', '')
end

return 'Dash'
end

return M
30 changes: 3 additions & 27 deletions lua/dash/providers/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,10 @@ local function attach_mappings(_, map)
return true
end

local function build_picker_title(bang)
local config = require('libdash_nvim').config
local filetype = vim.bo.filetype
local file_type_keywords = config.file_type_keywords[filetype]
if bang or not file_type_keywords then
return 'Dash'
end

if file_type_keywords == true then
return 'Dash - filtering by: ' .. filetype
end

if type(file_type_keywords) == 'string' then
return 'Dash - filtering by: ' .. file_type_keywords
end

if type(file_type_keywords) == 'table' then
return 'Dash - filtering by: ' .. (vim.inspect(file_type_keywords)):gsub('\n', '')
end

return 'Dash'
end

--- Build a Telescope picker for Dash.app and return it
---@param bang boolean @bang disables filtering by filetype
---@param initial_text string @pre-fill text into the telescope prompt
---@return table @Telescope Picker, has :find() method
function M.build_picker(bang, initial_text)
function M.dash(bang, initial_text)
local Picker = require('telescope.pickers')
local Finder = require('telescope.finders')
local Sorter = require('telescope.sorters')
Expand All @@ -69,15 +45,15 @@ function M.build_picker(bang, initial_text)
})

local picker = Picker:new({
prompt_title = build_picker_title(bang),
prompt_title = require('dash.providers').build_picker_title(bang),
finder = finder,
sorter = Sorter.get_generic_fuzzy_sorter(),
debounce = require('libdash_nvim').config.debounce,
attach_mappings = attach_mappings,
default_text = initial_text,
})

return picker
picker:find()
end

return M
18 changes: 16 additions & 2 deletions lua/dash/startup.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
local M = {}

local function load_telescope_extension()
local ok, telescope = pcall(require, 'telescope')
if ok then
telescope.load_extension('dash')
end
end

local function load_fzf_lua_extension()
local ok, fzf_lua = pcall(require, 'fzf-lua')
if ok then
fzf_lua.dash = require('dash.providers.fzf-lua').dash
end
end

function M.init()
-- check if `make install` was run
local ok, libdash = pcall(require, 'libdash_nvim')
Expand All @@ -11,8 +25,8 @@ function M.init()
return
end

require('telescope._extensions.dash')
require('telescope').load_extension('dash')
load_telescope_extension()
load_fzf_lua_extension()

vim.g.loaded_dash_vim = true
end
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn init_config(lua: &Lua) -> LuaTable {
TelescopePrompt = false,
terminal = false,
packer = false,
fzf = false,
-- filetypes, keep these ones alphabetical
actionscript = true,
Expand Down

0 comments on commit 5318ad2

Please sign in to comment.