wip: add file pattern ignores

This commit is contained in:
Senghan Bright 2021-01-17 10:37:25 +01:00
parent 2f7636ba0f
commit 81faa642ed
3 changed files with 69 additions and 25 deletions

View File

@ -13,6 +13,10 @@ local recency_modifier = {
[6] = { age = 129600, value = 10 } -- past 90 days
}
local default_ignore_patterns = {
"*.git/*", "*/tmp/*"
}
local sql_wrapper = nil
local function import_oldfiles()
@ -23,6 +27,19 @@ local function import_oldfiles()
print(("Telescope-Frecency: Imported %d entries from oldfiles."):format(#oldfiles))
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
is_ignored = true
goto continue
end
end
::continue::
return is_ignored
end
local function init()
if sql_wrapper then return end
@ -83,8 +100,6 @@ local function get_file_scores(opts)
local files = sql_wrapper:do_transaction(queries.file_get_entries, {})
local timestamp_ages = sql_wrapper:do_transaction(queries.timestamp_get_all_entry_ages, {})
-- print(vim.inspect(files))
-- print(vim.inspect(timestamp_ages))
if vim.tbl_isempty(files) then return scores end
-- filter to LSP workspace directory
@ -113,13 +128,12 @@ local function autocmd_handler(filepath)
-- check if file is registered as loaded
if not vim.b.frecency_registered then
-- allow noname files to go unregistered until BufWritePost
-- allow [noname] files to go unregistered until BufWritePost
if not util.fs_stat(filepath).exists then return end
-- TODO: only register buffer if update did something?
-- TODO: apply filetype_ignore here?
if file_is_ignored(filepath) then return end
vim.b.frecency_registered = 1
-- print("registered buffer")
sql_wrapper:update(filepath)
end
end
@ -128,13 +142,15 @@ local function validate()
if not sql_wrapper then return {} end
local queries = sql_wrapper.queries
local files = sql_wrapper:do_transaction(queries.file_get_entry, {})
local files = sql_wrapper:do_transaction(queries.file_get_entries, {})
for _, entry in pairs(files) do
if not util.fs_stat(entry.path).exists then
if not util.fs_stat(entry.path).exists
or file_is_ignored(entry.path) then -- cleanup entries that match the _current_ ignore list (DANGEROUS! bad pattern could wipe the database)
-- remove entries from file and timestamp tables
print("removing entry: " .. entry.path .. "[" .. entry.id .."]")
sql_wrapper:do_eval(queries.file_delete_entry, { id = entry.id })
sql_wrapper:do_eval(queries.timestamp_delete_with_file_id, { file_id = entry.id })
sql_wrapper:do_transaction(queries.file_delete_entry , {where = {id = entry.id }})
sql_wrapper:do_transaction(queries.timestamp_delete_entry, {where = {file_id = entry.id}})
end
end
end

View File

@ -9,11 +9,6 @@ end
-- TODO: pass in max_timestamps from db.lua
local MAX_TIMESTAMPS = 10
-- local ignore_patterns = {
-- }
--
local M = {}
@ -70,11 +65,14 @@ end
--
function M:do_transaction(t, params)
-- print(vim.inspect(t))
-- print(vim.inspect(params))
return self.db:with_open(function(db)
local case = {
[1] = function() return db:select(t.cmd_data, params) end,
[2] = function() return db:insert(t.cmd_data, params) end,
[3] = function() return db:eval(t.cmd_data, params) end,
[3] = function() return db:delete(t.cmd_data, params) end,
[4] = function() return db:eval(t.cmd_data, params) end,
}
return case[t.cmd]()
end)
@ -83,7 +81,8 @@ end
local cmd = {
select = 1,
insert = 2,
eval = 3,
delete = 3,
eval = 4,
}
local queries = {
@ -91,6 +90,10 @@ local queries = {
cmd = cmd.insert,
cmd_data = "files"
},
file_delete_entry = {
cmd = cmd.delete,
cmd_data = "files"
},
file_get_entries = {
cmd = cmd.select,
cmd_data = "files"
@ -103,6 +106,10 @@ local queries = {
cmd = cmd.eval,
cmd_data = "INSERT INTO timestamps (file_id, timestamp) values(:file_id, julianday('now'));"
},
timestamp_delete_entry = {
cmd = cmd.delete,
cmd_data = "timestamps"
},
timestamp_get_all_entries = {
cmd = cmd.select,
cmd_data = "timestamps",
@ -115,7 +122,6 @@ local queries = {
cmd = cmd.eval,
cmd_data = "DELETE FROM timestamps WHERE id < :id and file_id == :file_id;"
},
-- timestamp_delete_with_file_id = "DELETE FROM timestamps WHERE file_id == :file_id;",
}
M.queries = queries

View File

@ -2,23 +2,45 @@ local uv = vim.loop
local util = {}
util.string_isempty = function(s)
return s == nil or s == ''
-- stolen from penlight
-- escape any Lua 'magic' characters in a string
util.escape = function(str)
return (str:gsub('[%-%.%+%[%]%(%)%$%^%%%?%*]','%%%1'))
end
util.string_starts = function(str, start)
return string.sub(str, 1, str.len(start)) == start
util.filemask = function(mask)
mask = util.escape(mask)
return '^'..mask:gsub('%%%*','.*'):gsub('%%%?','.')..'$'
end
util.split = function(s, delimiter)
util.filename_match = function(filename, pattern)
return filename:find(util.filemask(pattern)) ~= nil
end
--
util.string_isempty = function(str)
return str == nil or str == ''
end
util.string_starts = function(str, token)
return str:sub(1, str:len(token)) == token
end
util.string_ends = function(str, token)
return str:sub(str:len() - token:len() + 1, -1) == token
end
util.split = function(str, delimiter)
local result = {}
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
for match in (str .. delimiter):gmatch("(.-)" .. delimiter) do
table.insert(result, match)
end
return result
end
util.fs_stat = function(path) -- TODO: move this to new file with M
util.fs_stat = function(path)
local stat = uv.fs_stat(path)
local res = {}
res.exists = stat and true or false -- TODO: this is silly