allow scores and ignore_patterns to be configurable.

This commit is contained in:
Senghan Bright 2021-01-17 19:48:19 +01:00
parent 1db3bb17aa
commit e8266b0b94
2 changed files with 34 additions and 22 deletions

View File

@ -10,14 +10,16 @@ local db_client = require("telescope._extensions.frecency.db_client")
db_client.init() db_client.init()
-- finder code -- finder code
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 pickers = require "telescope.pickers"
local conf = require('telescope.config').values
local sorters = require "telescope._extensions.frecency.sorter"
local path = require('telescope.path') local path = require('telescope.path')
local pickers = require "telescope.pickers"
local sorters = require "telescope._extensions.frecency.sorter"
local utils = require('telescope.utils') local utils = require('telescope.utils')
local os_path_sep = vim.loop.os_uname().sysname == "Windows" and "\\" or "/" local os_path_sep = vim.loop.os_uname().sysname == "Windows" and "\\" or "/"
local show_scores = false
local frecency = function(opts) local frecency = function(opts)
opts = opts or {} opts = opts or {}
@ -27,22 +29,25 @@ local frecency = function(opts)
-- TODO: decide on how to handle cwd or lsp_workspace for pathname shorten? -- TODO: decide on how to handle cwd or lsp_workspace for pathname shorten?
local results = db_client.get_file_scores(opts) -- TODO: pass `filter_workspace` option local results = db_client.get_file_scores(opts) -- TODO: pass `filter_workspace` option
local display_cols = {}
display_cols[1] = show_scores and {width = 8} or nil
table.insert(display_cols, {remaining = true})
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 = { items = display_cols
{ width = 8 },
{ remaining = true },
},
} }
-- TODO: look into why this gets called so much -- TODO: look into why this gets called so much
local make_display = function(entry) local bufnr, buf_is_loaded, filename, hl_filename, display_items
local bufnr = vim.fn.bufnr
local buf_is_loaded = vim.api.nvim_buf_is_loaded
local filename = entry.name local make_display = function(entry)
local hl_filename = buf_is_loaded(bufnr(filename)) and "TelescopeBufferLoaded" or "" bufnr = vim.fn.bufnr
buf_is_loaded = vim.api.nvim_buf_is_loaded
filename = entry.name
hl_filename = buf_is_loaded(bufnr(filename)) and "TelescopeBufferLoaded" or ""
if opts.tail_path then if opts.tail_path then
filename = utils.path_tail(filename) filename = utils.path_tail(filename)
@ -52,14 +57,14 @@ local frecency = function(opts)
filename = path.make_relative(filename, cwd) filename = path.make_relative(filename, cwd)
return displayer { display_items = show_scores and {{entry.score, "Directory"}} or {}
{entry.score, "Directory"}, table.insert(display_items, {filename, hl_filename})
{filename, hl_filename},
} return displayer(display_items)
end end
pickers.new(opts, { pickers.new(opts, {
prompt_title = "Frecency files", prompt_title = "Frecency",
finder = finders.new_table { finder = finders.new_table {
results = results, results = results,
entry_maker = function(entry) entry_maker = function(entry)
@ -78,6 +83,9 @@ local frecency = function(opts)
end end
return telescope.register_extension { return telescope.register_extension {
setup = function(ext_config)
show_scores = ext_config.show_scores or false
end,
exports = { exports = {
frecency = frecency, frecency = frecency,
}, },

View File

@ -1,5 +1,6 @@
local sqlwrap = require("telescope._extensions.frecency.sql_wrapper") local sqlwrap = require("telescope._extensions.frecency.sql_wrapper")
local util = require("telescope._extensions.frecency.util") local util = require("telescope._extensions.frecency.util")
local conf = require('telescope.config').values
local MAX_TIMESTAMPS = 10 local MAX_TIMESTAMPS = 10
local DB_REMOVE_SAFETY_THRESHOLD = 10 local DB_REMOVE_SAFETY_THRESHOLD = 10
@ -19,6 +20,7 @@ local default_ignore_patterns = {
} }
local sql_wrapper = nil local sql_wrapper = nil
local ignore_patterns = {}
local function import_oldfiles() local function import_oldfiles()
local oldfiles = vim.api.nvim_get_vvar("oldfiles") local oldfiles = vim.api.nvim_get_vvar("oldfiles")
@ -30,8 +32,8 @@ end
local function file_is_ignored(filepath) local function file_is_ignored(filepath)
local is_ignored = false local is_ignored = false
for _, ignore_pattern in pairs(default_ignore_patterns) do for _, pattern in pairs(ignore_patterns) do
if util.filename_match(filepath, ignore_pattern) then if util.filename_match(filepath, pattern) then
is_ignored = true is_ignored = true
goto continue goto continue
end end
@ -41,7 +43,7 @@ local function file_is_ignored(filepath)
return is_ignored return is_ignored
end end
local function validate() local function validate_db()
if not sql_wrapper then return {} end if not sql_wrapper then return {} end
local queries = sql_wrapper.queries local queries = sql_wrapper.queries
@ -81,13 +83,15 @@ local function init()
sql_wrapper = sqlwrap:new() sql_wrapper = sqlwrap:new()
local first_run = sql_wrapper:bootstrap() local first_run = sql_wrapper:bootstrap()
validate() validate_db()
if first_run then if first_run then
-- TODO: this needs to be scheduled for after shada load -- TODO: this needs to be scheduled for after shada load
vim.defer_fn(import_oldfiles, 100) vim.defer_fn(import_oldfiles, 100)
end end
ignore_patterns = conf.file_ignore_patterns or default_ignore_patterns
-- setup autocommands -- setup autocommands
vim.api.nvim_command("augroup TelescopeFrecency") vim.api.nvim_command("augroup TelescopeFrecency")
vim.api.nvim_command("autocmd!") vim.api.nvim_command("autocmd!")