Skip to content

Commit

Permalink
Merge pull request #369 from mrjones2014/mrj/364/frecency-fail-gracef…
Browse files Browse the repository at this point in the history
…ully

fix(frecency): Fail gracefully if sqlite not available or cannot open DB
  • Loading branch information
mrjones2014 authored May 8, 2023
2 parents 2f59e16 + e3e046d commit 93ad2ef
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down Expand Up @@ -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 = {
Expand Down
18 changes: 18 additions & 0 deletions lua/legendary/api/db/init.lua
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions lua/legendary/api/db/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions lua/legendary/data/itemlist.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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

Expand Down
16 changes: 13 additions & 3 deletions lua/legendary/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 93ad2ef

Please sign in to comment.