From e8266b0b94fb10d2ee2c461562d0bd153193e9eb Mon Sep 17 00:00:00 2001 From: Senghan Bright Date: Sun, 17 Jan 2021 19:48:19 +0100 Subject: [PATCH] allow scores and ignore_patterns to be configurable. --- lua/telescope/_extensions/frecency.lua | 44 +++++++++++-------- .../_extensions/frecency/db_client.lua | 12 +++-- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/lua/telescope/_extensions/frecency.lua b/lua/telescope/_extensions/frecency.lua index c64c1c3..8f35be0 100644 --- a/lua/telescope/_extensions/frecency.lua +++ b/lua/telescope/_extensions/frecency.lua @@ -10,14 +10,16 @@ local db_client = require("telescope._extensions.frecency.db_client") db_client.init() -- finder code +local conf = require('telescope.config').values local entry_display = require "telescope.pickers.entry_display" local finders = require "telescope.finders" +local path = require('telescope.path') local pickers = require "telescope.pickers" -local conf = require('telescope.config').values local sorters = require "telescope._extensions.frecency.sorter" -local path = require('telescope.path') -local utils = require('telescope.utils') -local os_path_sep = vim.loop.os_uname().sysname == "Windows" and "\\" or "/" +local utils = require('telescope.utils') + +local os_path_sep = vim.loop.os_uname().sysname == "Windows" and "\\" or "/" +local show_scores = false local frecency = function(opts) opts = opts or {} @@ -27,22 +29,25 @@ local frecency = function(opts) -- 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 display_cols = {} + display_cols[1] = show_scores and {width = 8} or nil + table.insert(display_cols, {remaining = true}) + local displayer = entry_display.create { separator = "", hl_chars = {[os_path_sep] = "TelescopePathSeparator"}, - items = { - { width = 8 }, - { remaining = true }, - }, + items = display_cols } -- TODO: look into why this gets called so much - local make_display = function(entry) - local bufnr = vim.fn.bufnr - local buf_is_loaded = vim.api.nvim_buf_is_loaded + local bufnr, buf_is_loaded, filename, hl_filename, display_items - local filename = entry.name - local hl_filename = buf_is_loaded(bufnr(filename)) and "TelescopeBufferLoaded" or "" + local make_display = function(entry) + 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 filename = utils.path_tail(filename) @@ -52,14 +57,14 @@ local frecency = function(opts) filename = path.make_relative(filename, cwd) - return displayer { - {entry.score, "Directory"}, - {filename, hl_filename}, - } + display_items = show_scores and {{entry.score, "Directory"}} or {} + table.insert(display_items, {filename, hl_filename}) + + return displayer(display_items) end pickers.new(opts, { - prompt_title = "Frecency files", + prompt_title = "Frecency", finder = finders.new_table { results = results, entry_maker = function(entry) @@ -78,6 +83,9 @@ local frecency = function(opts) end return telescope.register_extension { + setup = function(ext_config) + show_scores = ext_config.show_scores or false + end, exports = { frecency = frecency, }, diff --git a/lua/telescope/_extensions/frecency/db_client.lua b/lua/telescope/_extensions/frecency/db_client.lua index 9e2acbf..aace9af 100644 --- a/lua/telescope/_extensions/frecency/db_client.lua +++ b/lua/telescope/_extensions/frecency/db_client.lua @@ -1,5 +1,6 @@ local sqlwrap = require("telescope._extensions.frecency.sql_wrapper") local util = require("telescope._extensions.frecency.util") +local conf = require('telescope.config').values local MAX_TIMESTAMPS = 10 local DB_REMOVE_SAFETY_THRESHOLD = 10 @@ -19,6 +20,7 @@ local default_ignore_patterns = { } local sql_wrapper = nil +local ignore_patterns = {} local function import_oldfiles() local oldfiles = vim.api.nvim_get_vvar("oldfiles") @@ -30,8 +32,8 @@ end local function file_is_ignored(filepath) local is_ignored = false - for _, ignore_pattern in pairs(default_ignore_patterns) do - if util.filename_match(filepath, ignore_pattern) then + for _, pattern in pairs(ignore_patterns) do + if util.filename_match(filepath, pattern) then is_ignored = true goto continue end @@ -41,7 +43,7 @@ local function file_is_ignored(filepath) return is_ignored end -local function validate() +local function validate_db() if not sql_wrapper then return {} end local queries = sql_wrapper.queries @@ -81,13 +83,15 @@ local function init() sql_wrapper = sqlwrap:new() local first_run = sql_wrapper:bootstrap() - validate() + validate_db() if first_run then -- TODO: this needs to be scheduled for after shada load vim.defer_fn(import_oldfiles, 100) end + ignore_patterns = conf.file_ignore_patterns or default_ignore_patterns + -- setup autocommands vim.api.nvim_command("augroup TelescopeFrecency") vim.api.nvim_command("autocmd!")