diff --git a/README.md b/README.md index 8f3f1065..7ccdbbd2 100644 --- a/README.md +++ b/README.md @@ -58,22 +58,22 @@ Define your keymaps, commands, and autocommands as simple Lua tables, building a This project uses git tags to adhere to [Semantic Versioning](https://semver.org/). To check the latest version, see the [git tag list](https://github.com/mrjones2014/legendary.nvim/tags). -With `packer.nvim`: +With `lazy.nvim`: ```lua -- to use a version -use({ +{ 'mrjones2014/legendary.nvim', - tag = 'v2.1.0', + version = 'v2.1.0', -- sqlite is only needed if you want to use frecency sorting - -- requires = 'kkharji/sqlite.lua' -}) + -- dependencies = { 'kkharji/sqlite.lua' } +} -- or, to get rolling updates -use({ +{ 'mrjones2014/legendary.nvim', -- sqlite is only needed if you want to use frecency sorting - -- requires = 'kkharji/sqlite.lua' -}) + -- dependencies = { 'kkharji/sqlite.lua' } +} ``` With `vim-plug`: @@ -346,7 +346,7 @@ require('legendary').setup({ -- settings for frecency sorting. -- https://en.wikipedia.org/wiki/Frecency -- Set `frecency = false` to disable. - -- this feature requires sqlite.lua (https://github.com/tami5/sqlite.lua) + -- this feature requires sqlite.lua (https://github.com/kkharji/sqlite.lua) -- and will be automatically disabled if sqlite is not available. -- NOTE: THIS TAKES PRECEDENCE OVER OTHER SORT OPTIONS! frecency = { diff --git a/lua/legendary/api/db/init.lua b/lua/legendary/api/db/init.lua new file mode 100644 index 00000000..64ed9fc2 --- /dev/null +++ b/lua/legendary/api/db/init.lua @@ -0,0 +1,18 @@ +local M = {} + +M.disabled_due_to_error = false + +function M.is_supported() + if M.disabled_due_to_error then + return false + end + + local has_sqlite, _ = pcall(require, 'sqlite') + if not has_sqlite then + return false + end + + return true +end + +return M diff --git a/lua/legendary/api/db/wrapper.lua b/lua/legendary/api/db/wrapper.lua index c18592ef..93befd65 100644 --- a/lua/legendary/api/db/wrapper.lua +++ b/lua/legendary/api/db/wrapper.lua @@ -3,7 +3,7 @@ local ok, sqlite = pcall(require, 'sqlite') if not ok then - error('Frecency sorting requires sqlite.lua (https://github.com/tami5/sqlite.lua) ' .. tostring(sqlite)) + error('Frecency sorting requires sqlite.lua (https://github.com/kkharji/sqlite.lua) ' .. tostring(sqlite)) return end @@ -64,7 +64,8 @@ function M:bootstrap() return sqlite:open(db_path) end) if not db_ok then - Log.error('Failed to open database at %s: %s', db_path, db_opened) + Log.error('Disabling frecency sort, failed to open database at %s: %s', db_path, db_opened) + require('legendary.api.db').disabled_due_to_error = true return else self.db = db_opened diff --git a/lua/legendary/data/itemlist.lua b/lua/legendary/data/itemlist.lua index c82138ab..df8856cb 100644 --- a/lua/legendary/data/itemlist.lua +++ b/lua/legendary/data/itemlist.lua @@ -116,10 +116,17 @@ function ItemList:sort_inplace() local items = self.items if Config.sort.frecency ~= false then - local has_sqlite, _ = pcall(require, 'sqlite') - if has_sqlite then + if require('legendary.api.db').is_supported() then -- inline require because this module requires sqlite and is a bit heavier local DbClient = require('legendary.api.db.client').init() + -- if bootstrapping fails, bail + if not require('legendary.api.db').is_supported() then + Log.debug( + 'Config.sort.frecency is enabled, but sqlite is not available or database could not be opened, ' + .. 'frecency is automatically disabled.' + ) + return + end local frecency_scores = DbClient.get_item_scores() Log.debug('Computed item scores: %s', vim.inspect(frecency_scores)) @@ -146,7 +153,10 @@ function ItemList:sort_inplace() self.items = items return else - Log.debug('Config.sort.frecency is enabled, but sqlite is not availabe, so frecency is automatically disabled.') + Log.debug( + 'Config.sort.frecency is enabled, but sqlite is not available or database could not be opened, ' + .. 'frecency is automatically disabled.' + ) end end diff --git a/lua/legendary/ui/init.lua b/lua/legendary/ui/init.lua index 4152e38d..3b9d154e 100644 --- a/lua/legendary/ui/init.lua +++ b/lua/legendary/ui/init.lua @@ -9,13 +9,23 @@ local Log = require('legendary.log') local function update_item_frecency_score(item) if Config.sort.frecency ~= false then - local has_sqlite, _ = pcall(require, 'sqlite') - if has_sqlite then + if require('legendary.api.db').is_supported() then Log.trace('Updating scoring data for selected item.') local DbClient = require('legendary.api.db.client').init() + -- if bootstrapping fails, bail + if not require('legendary.api.db').is_supported() then + Log.debug( + 'Config.sort.frecency is enabled, but sqlite is not available or database could not be opened, ' + .. 'frecency is automatically disabled.' + ) + return + end DbClient.update_item_score(item) else - Log.debug('Config.sort.frecency is enabled, but sqlite is not availabe, so frecency is automatically disabled.') + Log.debug( + 'Config.sort.frecency is enabled, but sqlite is not available or database could not be opened, ' + .. 'frecency is automatically disabled.' + ) end end end