feat: support devicons (#31)

* feat: support devicons

* fix: respect opts.disable_devicons

* chore: stylua format
This commit is contained in:
Senghan Bright 2021-08-04 20:25:49 +02:00 committed by GitHub
parent f7ee2172da
commit e5dd397504
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,18 +4,19 @@ local has_telescope, telescope = pcall(require, "telescope")
-- TODO: make filters handle mulitple directories -- TODO: make filters handle mulitple directories
if not has_telescope then if not has_telescope then
error("This plugin requires telescope.nvim (https://github.com/nvim-telescope/telescope.nvim)") error "This plugin requires telescope.nvim (https://github.com/nvim-telescope/telescope.nvim)"
end end
local actions = require('telescope.actions') local has_devicons, devicons = pcall(require, "nvim-web-devicons")
local conf = require('telescope.config').values local actions = require "telescope.actions"
local conf = require("telescope.config").values
local entry_display = require "telescope.pickers.entry_display" local entry_display = require "telescope.pickers.entry_display"
local finders = require "telescope.finders" local finders = require "telescope.finders"
local Path = require('plenary.path') local Path = require "plenary.path"
local pickers = require "telescope.pickers" local pickers = require "telescope.pickers"
local sorters = require "telescope.sorters" local sorters = require "telescope.sorters"
local utils = require('telescope.utils') local utils = require "telescope.utils"
local db_client = require("telescope._extensions.frecency.db_client") local db_client = require "telescope._extensions.frecency.db_client"
local os_home = vim.loop.os_homedir() local os_home = vim.loop.os_homedir()
local os_path_sep = utils.get_separator() local os_path_sep = utils.get_separator()
@ -30,7 +31,7 @@ local state = {
show_scores = false, show_scores = false,
user_workspaces = {}, user_workspaces = {},
lsp_workspaces = {}, lsp_workspaces = {},
picker = {} picker = {},
} }
local function format_filepath(filename, opts) local function format_filepath(filename, opts)
@ -58,7 +59,6 @@ local function format_filepath(filename, opts)
end end
local function get_workspace_tags() local function get_workspace_tags()
-- Add user config workspaces. TODO: validate that workspaces are existing directories -- Add user config workspaces. TODO: validate that workspaces are existing directories
local tags = {} local tags = {}
for k, _ in pairs(state.user_workspaces) do for k, _ in pairs(state.user_workspaces) do
@ -66,13 +66,13 @@ local function get_workspace_tags()
end end
-- Add CWD filter -- Add CWD filter
table.insert(tags, 'CWD') table.insert(tags, "CWD")
-- Add LSP workpace(s) -- Add LSP workpace(s)
local lsp_workspaces = vim.api.nvim_buf_call(state.previous_buffer, vim.lsp.buf.list_workspace_folders) local lsp_workspaces = vim.api.nvim_buf_call(state.previous_buffer, vim.lsp.buf.list_workspace_folders)
if not vim.tbl_isempty(lsp_workspaces) then if not vim.tbl_isempty(lsp_workspaces) then
state.lsp_workspaces = lsp_workspaces state.lsp_workspaces = lsp_workspaces
table.insert(tags, 'LSP') table.insert(tags, "LSP")
else else
state.lsp_workspaces = {} state.lsp_workspaces = {}
end end
@ -85,7 +85,7 @@ end
local frecency = function(opts) local frecency = function(opts)
opts = opts or {} opts = opts or {}
state.previous_buffer = vim.fn.bufnr('%') state.previous_buffer = vim.fn.bufnr "%"
state.cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) state.cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
local function get_display_cols() local function get_display_cols()
@ -103,6 +103,9 @@ local frecency = function(opts)
if state.show_filter_column then if state.show_filter_column then
table.insert(res, { width = directory_col_width }) table.insert(res, { width = directory_col_width })
end end
if has_devicons and not state.disable_devicons then
table.insert(res, { width = 2 }) -- icon column
end
table.insert(res, { remaining = true }) table.insert(res, { remaining = true })
return res return res
end end
@ -110,16 +113,16 @@ local frecency = function(opts)
local displayer = entry_display.create { local displayer = entry_display.create {
separator = "", separator = "",
hl_chars = { [os_path_sep] = "TelescopePathSeparator" }, hl_chars = { [os_path_sep] = "TelescopePathSeparator" },
items = get_display_cols() items = get_display_cols(),
} }
local bufnr, buf_is_loaded, filename, hl_filename, display_items local bufnr, buf_is_loaded, display_filename, hl_filename, display_items, icon, icon_highlight
local make_display = function(entry) local make_display = function(entry)
bufnr = vim.fn.bufnr bufnr = vim.fn.bufnr
buf_is_loaded = vim.api.nvim_buf_is_loaded buf_is_loaded = vim.api.nvim_buf_is_loaded
filename = entry.name display_filename = entry.name
hl_filename = buf_is_loaded(bufnr(filename)) and "TelescopeBufferLoaded" or "" hl_filename = buf_is_loaded(bufnr(display_filename)) and "TelescopeBufferLoaded" or ""
filename = format_filepath(filename, opts) display_filename = format_filepath(display_filename, opts)
display_items = state.show_scores and { { entry.score, "TelescopeFrecencyScores" } } or {} display_items = state.show_scores and { { entry.score, "TelescopeFrecencyScores" } } or {}
@ -134,7 +137,11 @@ local frecency = function(opts)
end end
end end
table.insert(display_items, { filter_path, "Directory" }) table.insert(display_items, { filter_path, "Directory" })
table.insert(display_items, {filename, hl_filename}) if has_devicons and not state.disable_devicons then
icon, icon_highlight = devicons.get_icon(entry.name, string.match(entry.name, "%a+$"), { default = true })
table.insert(display_items, { icon, icon_highlight })
end
table.insert(display_items, { display_filename, hl_filename })
return displayer(display_items) return displayer(display_items)
end end
@ -173,7 +180,7 @@ local frecency = function(opts)
display = make_display, display = make_display,
ordinal = entry.filename, ordinal = entry.filename,
name = entry.filename, name = entry.filename,
score = entry.score score = entry.score,
} }
end end
@ -195,13 +202,13 @@ local frecency = function(opts)
displayer = entry_display.create { displayer = entry_display.create {
separator = "", separator = "",
hl_chars = { [os_path_sep] = "TelescopePathSeparator" }, hl_chars = { [os_path_sep] = "TelescopePathSeparator" },
items = get_display_cols() items = get_display_cols(),
} }
state.last_filter = new_filter state.last_filter = new_filter
new_finder = finders.new_table { new_finder = finders.new_table {
results = state.results, results = state.results,
entry_maker = entry_maker entry_maker = entry_maker,
} }
end end
@ -222,7 +229,7 @@ local frecency = function(opts)
end, end,
finder = finders.new_table { finder = finders.new_table {
results = state.results, results = state.results,
entry_maker = entry_maker entry_maker = entry_maker,
}, },
previewer = conf.file_previewer(opts), previewer = conf.file_previewer(opts),
sorter = sorters.get_substr_matcher(opts), sorter = sorters.get_substr_matcher(opts),
@ -231,11 +238,22 @@ local frecency = function(opts)
vim.api.nvim_buf_set_option(state.picker.prompt_bufnr, "filetype", "frecency") vim.api.nvim_buf_set_option(state.picker.prompt_bufnr, "filetype", "frecency")
vim.api.nvim_buf_set_option(state.picker.prompt_bufnr, "completefunc", "frecency#FrecencyComplete") vim.api.nvim_buf_set_option(state.picker.prompt_bufnr, "completefunc", "frecency#FrecencyComplete")
vim.api.nvim_buf_set_keymap(state.picker.prompt_bufnr, "i", "<Tab>", "pumvisible() ? '<C-n>' : '<C-x><C-u>'", {expr = true, noremap = true}) vim.api.nvim_buf_set_keymap(
vim.api.nvim_buf_set_keymap(state.picker.prompt_bufnr, "i", "<S-Tab>", "pumvisible() ? '<C-p>' : ''", {expr = true, noremap = true}) state.picker.prompt_bufnr,
"i",
"<Tab>",
"pumvisible() ? '<C-n>' : '<C-x><C-u>'",
{ expr = true, noremap = true }
)
vim.api.nvim_buf_set_keymap(
state.picker.prompt_bufnr,
"i",
"<S-Tab>",
"pumvisible() ? '<C-p>' : ''",
{ expr = true, noremap = true }
)
end end
local function set_config_state(opt_name, value, default) local function set_config_state(opt_name, value, default)
state[opt_name] = value == nil and default or value state[opt_name] = value == nil and default or value
end end
@ -246,23 +264,29 @@ local health_error = vim.fn["health#report_error"]
local function checkhealth() local function checkhealth()
local has_sql, _ = pcall(require, "sql") local has_sql, _ = pcall(require, "sql")
if has_sql then if has_sql then
health_ok("sql.nvim installed.") health_ok "sql.nvim installed."
-- return "MOOP" -- return "MOOP"
else else
health_error("NOOO") health_error "NOOO"
end end
end end
return telescope.register_extension { return telescope.register_extension {
setup = function(ext_config) setup = function(ext_config)
set_config_state('db_root', ext_config.db_root, nil) set_config_state("db_root", ext_config.db_root, nil)
set_config_state('show_scores', ext_config.show_scores, false) set_config_state("show_scores", ext_config.show_scores, false)
set_config_state('show_unindexed', ext_config.show_unindexed, true) set_config_state("show_unindexed", ext_config.show_unindexed, true)
set_config_state('show_filter_column', ext_config.show_filter_column, true) set_config_state("show_filter_column", ext_config.show_filter_column, true)
set_config_state('user_workspaces', ext_config.workspaces, {}) set_config_state("user_workspaces", ext_config.workspaces, {})
set_config_state("disable_devicons", ext_config.disable_devicons, false)
-- start the database client -- start the database client
db_client.init(ext_config.db_root, ext_config.ignore_patterns, ext_config.db_safe_mode or true, ext_config.auto_validate or true) db_client.init(
ext_config.db_root,
ext_config.ignore_patterns,
ext_config.db_safe_mode or true,
ext_config.auto_validate or true
)
end, end,
exports = { exports = {
frecency = frecency, frecency = frecency,