From e547c1cd321846f1ec52c896506030822d223fd9 Mon Sep 17 00:00:00 2001 From: Senghan Bright Date: Sat, 16 Jan 2021 18:47:17 +0100 Subject: [PATCH] import from v:oldfiles on first run --- .../_extensions/frecency/db_client.lua | 15 +++++++- .../_extensions/frecency/sql_wrapper.lua | 35 ++++++++++--------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/lua/telescope/_extensions/frecency/db_client.lua b/lua/telescope/_extensions/frecency/db_client.lua index ae1e0a1..0e7c3a0 100644 --- a/lua/telescope/_extensions/frecency/db_client.lua +++ b/lua/telescope/_extensions/frecency/db_client.lua @@ -15,11 +15,24 @@ local recency_modifier = { local sql_wrapper = nil +local function import_oldfiles() + local oldfiles = vim.api.nvim_get_vvar("oldfiles") + for _, filepath in pairs(oldfiles) do + -- TODO: don't touch existing entries + sql_wrapper:update(filepath) + end + print(("Telescope-Frecency: Imported %d entries from oldfiles."):format(#oldfiles)) +end + local function init() if sql_wrapper then return end sql_wrapper = sqlwrap:new() - sql_wrapper:bootstrap() + local first_run = sql_wrapper:bootstrap() + if first_run then + -- TODO: this needs to be scheduled for after shada load + vim.defer_fn(import_oldfiles, 100) + end -- setup autocommands vim.api.nvim_command("augroup TelescopeFrecency") diff --git a/lua/telescope/_extensions/frecency/sql_wrapper.lua b/lua/telescope/_extensions/frecency/sql_wrapper.lua index f397e72..8b28762 100644 --- a/lua/telescope/_extensions/frecency/sql_wrapper.lua +++ b/lua/telescope/_extensions/frecency/sql_wrapper.lua @@ -46,25 +46,28 @@ function M:bootstrap(opts) return end - -- create tables if they don't exist - self.db:create("files", { - ensure = true, - id = {"INTEGER", "PRIMARY", "KEY"}, - count = "INTEGER", - path = "TEXT" - }) - self.db:create("timestamps", { - ensure = true, - id = {"INTEGER", "PRIMARY", "KEY"}, - file_id = "INTEGER", - timestamp = "REAL" - -- FOREIGN KEY(file_id) REFERENCES files(id) - }) - self.db:close() + local first_run = false + if not self.db:exists("files") then + first_run = true + -- create tables if they don't exist + self.db:create("files", { + id = {"INTEGER", "PRIMARY", "KEY"}, + count = "INTEGER", + path = "TEXT" + }) + self.db:create("timestamps", { + id = {"INTEGER", "PRIMARY", "KEY"}, + file_id = "INTEGER", + timestamp = "REAL" + -- FOREIGN KEY(file_id) REFERENCES files(id) + }) + end + self.db:close() + return first_run end -------------------------------------------- +-- function M:do_transaction(t, params) return self.db:with_open(function(db)