* refactor(db): use new sql.nvim api * refactor(*): create const, algo and add more util * refactor(picker): try to cleanup and make it easy to read FIXME: it seems that I didn't refactor the entry maker right. * refactor(*): move util to lua/frecency * misc(db): flag possible bug * refactor(db): reflect changes introduced in https://github.com/tami5/sql.nvim/pull/96 * refactor(db): move set_config to frecency.lua * new(db): ignore term and octo paths This may possibly fix the issue with entry.count being nil. * misc(picker): move health out of export * refactor(util) * refactor(db): general * misc(*): nothing major * refactor(db): abbreviate table namespace access * refactor(picker): working-ish * fix(*): general * refactor(picker): move fd function to the end of the file * refactor(*): remove the need for db.init * new(db): use foreign keys * sync with sqlite.lua@#105 * feat: add settings for StyLua * fix: detect the valid module in healthcheck See #65 * style: fix by StyLua * fix: detect CWD tag to cut paths See #66 * fix: show icon before directory See #67 * fix: deal with show_filter_column option validly * feat: support opts.workspace (#68) * doc(readme): update config example (#33) remove comma causing error. * doc(readme): fix packer install instructions (#34) Co-authored-by: tami5 <65782666+tami5@users.noreply.github.com> * doc: follow sqlite new release (#40) * refactor(sql_wrapper): follow sqlite new release * update readme * refactor: follow telescope's interface changes See #46 * feat: add default_workspace tag See #43 * fix: fetch workspaces in addition to completing See #72 * Update url for sqlite dependency (#64) The old repository on github redirects to this one. * fix: use vim.notify not to block outputs See #75 * feat: opts.path_display to customize See #76 * Enable to specify tags to show the tails (#77) * Enable to specify tags to show the tails * Add doc for show_filter_column * feat: use more reasonable matcher to sort See #73 * Fix broken Frecency Algorithm link in README.md (#82) MDN appear to have removed the Frecency Algorithm page linked in the README document. Updating link to use archived version. * fix: set the telescope default filetype in prompt See #81 * feat: use the newer API to define autocmds See #79 * refactor: use new API and add check for devicons * feat: detect entries when it has added new ones See #87 * fix: use valid widths to show entries * fix: use substr matcher to use sorting by scores See #94 * Revert "fix: set the telescope default filetype in prompt" This reverts commit 4937f7045438412e31a77374f91230bdbcbeb34d. * fix: enable to filter by valid paths * refactor: do nothing until calling setup() See #80 * fix: fix typo for `workspaces` * fix: show `0` score for unindexed instead of `nil` * style: fix by StyLua * fix: return a valid entry with get() It have used where() to get the entry and where() uses get() to fetch from DB. But this table has been customized by overwriting get(), so it has a bug to return the same entry every time it calls. This fixes it. * refactor: get the buffer name explicitly * fix: clean up logic to validate DB * feat: enable to work db_root option * fix: show no msg when no need to validate in auto --------- Co-authored-by: Tami <65782666+tami5@users.noreply.github.com> Co-authored-by: Munif Tanjim <hello@muniftanjim.dev> Co-authored-by: premell <65544203+premell@users.noreply.github.com> Co-authored-by: Anshuman Medhi <amedhi@connect.ust.hk> Co-authored-by: Lucas Hoffmann <lucc@users.noreply.github.com> Co-authored-by: Rohan Orton <rohan.orton@gmail.com> |
||
|---|---|---|
| lua | ||
| syntax | ||
| .stylua.toml | ||
| LICENSE | ||
| README.md | ||
telescope-frecency.nvim
A telescope.nvim extension that offers intelligent prioritization when selecting files from your editing history.
Using an implementation of Mozilla's Frecency algorithm (used in Firefox's address bar), files edited frecently are given higher precedence in the list index.
As the extension learns your editing habits over time, the sorting of the list is dynamically altered to prioritize the files you're likely to need.
- Scores shown in finder for demonstration purposes - disabled by default
Frecency: Sorting by 'frequency' and 'recency'
'Frecency' is a score given to each unique file indexed in a file history database.
A timestamp is recorded once per session when a file is first loaded into a buffer.
The score is calculated using the age of the 10 most recent timestamps and the total amount of times that the file has been loaded:
Recency values (per timestamp)
| Timestamp age | Value |
|---|---|
| 4 hours | 100 |
| 1 day | 80 |
| 3 days | 60 |
| 1 week | 40 |
| 1 month | 20 |
| 90 days | 10 |
Score calculation
score = frequency * recency_score / max_number_of_timestamps
What about files that are neither 'frequent' or 'recent' ?
Frecency naturally works best for indexed files that have been given a reasonably high score.
New projects or rarely used files with generic names either don't get listed at all or can be buried under results with a higher score.
Frecency tackles this with Workspace Filters:
The workspace filter feature enables you to select from user defined filter tags that map to a directory or collection of directories.
Filters are applied by entering :workspace_tag: anywhere in the query.
Filter name completion is available by pressing <Tab> after the first : character.
When a filter is applied, results are reduced to entries whose path is a descendant of the workspace directory. The indexed results are optionally augmented with a listing of all files found in a recurssive search of the workspace directory. Non-indexed files are given a score of zero and appear below the 'frecent' entries. When a non-indexed file is opened, it gains a score value and is available in future 'frecent' search results.
If the active buffer (prior to the finder being launched) is attached to an LSP server, an automatic LSP tag is available, which maps to the workspace directories provided by the language server.
Requirements
- telescope.nvim (required)
- sqlite.lua (required)
- nvim-web-devicons (optional)
Timestamps and file records are stored in an SQLite3 database for persistence and speed.
This plugin uses sqlite.lua to perform the database transactions.
Installation
Packer.nvim
use {
"nvim-telescope/telescope-frecency.nvim",
config = function()
require"telescope".load_extension("frecency")
end,
requires = {"kkharji/sqlite.lua"}
}
TODO: add installation instructions for other package managers
If no database is found when running Neovim with the plugin installed, a new one is created and entries from shada v:oldfiles are automatically imported.
Usage
:Telescope frecency
or to map to a key:
vim.api.nvim_set_keymap("n", "<leader><leader>", "<Cmd>lua require('telescope').extensions.frecency.frecency()<CR>", {noremap = true, silent = true})
Use a specific workspace tag:
:Telescope frecency workspace=CWD
or
vim.api.nvim_set_keymap("n", "<leader><leader>", "<Cmd>lua require('telescope').extensions.frecency.frecency({ workspace = 'CWD' })<CR>", {noremap = true, silent = true})
Filter tags are applied by typing the :tag: name (adding surrounding colons) in the finder query.
Entering :<Tab> will trigger omnicompletion for available tags.
Configuration
See default configuration for full details on configuring Telescope.
-
db_root(default:nil)Path to parent directory of custom database location. Defaults to
$XDG_DATA_HOME/nvimif unset. -
default_workspace(default:nil)Default workspace tag to filter by e.g.
'CWD'to filter by default to the current directory. Can be overridden at query time by specifying another filter like':*:'. -
ignore_patterns(default:{"*.git/*", "*/tmp/*"})Patterns in this table control which files are indexed (and subsequently which you'll see in the finder results).
-
show_scores(default :false)To see the scores generated by the algorithm in the results, set this to
true. -
workspaces(default: {})This table contains mappings of
workspace_tag->workspace_directoryThe key corresponds to the:tag_nameused to select the filter in queries. The value corresponds to the top level directory by which results will be filtered. -
show_unindexed(default:true)Determines if non-indexed files are included in workspace filter results.
-
devicons_disabled(default:false)Disable devicons (if available)
-
show_filter_column(default:true)Show the path of the active filter before file paths. In default, it uses the tail of paths for
'LSP'and'CWD'tags. You can configure this by setting a table for this option.-- show the tail for "LSP", "CWD" and "FOO" show_filter_column = { "LSP", "CWD", "FOO" }
Example Configuration:
telescope.setup {
extensions = {
frecency = {
db_root = "home/my_username/path/to/db_root",
show_scores = false,
show_unindexed = true,
ignore_patterns = {"*.git/*", "*/tmp/*"},
disable_devicons = false,
workspaces = {
["conf"] = "/home/my_username/.config",
["data"] = "/home/my_username/.local/share",
["project"] = "/home/my_username/projects",
["wiki"] = "/home/my_username/wiki"
}
}
},
}
SQL database location
The default location for the sqlite3 database is $XDG_DATA_HOME/nvim (eg ~/.local/share/nvim/ on linux).
This can be configured with the db_root config option.
SQL database maintainance
By default, frecency will prune files that no longer exist from the database. In certain workflows, switching branches in a repository, that behaviour might not be desired. The following configuration control this behaviour:
db_safe_mode - When this is enabled, the user will be prompted before any entries are removed from the database.
auto_validate - When this to false, stale entries will never be automatically removed.
The command FrecencyValidate can be used to clean the database when auto_validate is disabled.
" clean DB
:FrecencyValidate
" clean DB without prompts to confirm
:FrecencyValidate!
Highlight Groups
TelescopeBufferLoaded
TelescopePathSeparator
TelescopeFrecencyScores
TelescopeQueryFilter
TODO: describe highlight groups