|
1 | 1 | # Phoenix |
2 | 2 |
|
3 | | -A blazing-fast in-process server providing word and path completion. |
| 3 | +A blazing-fast in-process server providing word (based on frequency score and) path completion. |
4 | 4 |
|
5 | | -## Demo |
| 5 | +## How fast it is ? |
6 | 6 |
|
7 | | -Smooth word completion from nearly 10,000 lines of text and instant path completion in project with thousands of files. |
| 7 | +It enables smooth word completion in nearly 10,000 lines of text and provides |
| 8 | +instant completion in projects with thousands of files. |
8 | 9 |
|
9 | 10 |  |
10 | 11 |
|
11 | | -Words are stored in a [Trie](https://wikipedia.org/wiki/Trie), additionally, they are weighted in frequency and last time used. Low frequency words are cleaned up periodically to speed the process up even more. |
| 12 | +[Completion Config](#completion) |
12 | 13 |
|
13 | | -Path completion is done in a similar fashion (with LRU). |
| 14 | +In the Phoenix, a Trie tree is used to store words, ensuring that the |
| 15 | +completion results can be obtained in O(L) time (L is the length of the word). |
| 16 | +Additionally, the weight of each word is calculated based on its usage frequency |
| 17 | +and last usage time, and low-frequency words are asynchronously cleaned up periodically |
| 18 | +to ensure that the desired results can be obtained quickly with each input. |
| 19 | + |
| 20 | +For path completion, Phoenix uses an LRU (Least Recently Used) cache to handle |
| 21 | +the results, and the completion results can be obtained in O(1) time. Meanwhile, |
| 22 | +the cache is cleaned up based on a set time period to ensure that the directory |
| 23 | +status is kept synchronized. |
14 | 24 |
|
15 | 25 | ## Usage |
16 | 26 |
|
17 | 27 | Install with any plugin manager or builtin `:help packages`. |
| 28 | +Phoenix can work with any completion plugin which support lsp source or neovim |
| 29 | +nightly `vim.lsp.completion` module. |
18 | 30 |
|
19 | | -You have to setup your completion to use the fake `phoenix` language server. ([Example using vim.lsp.completion](https://github.com/glepnir/nvim/blob/main/lua/internal/completion.lua)). |
| 31 | +## Config |
20 | 32 |
|
21 | | -Options are configured via `vim.g.phoenix`. |
| 33 | +Used `vim.g.phoenix` option table and modified the field what you need before |
| 34 | +plugin loaded. |
22 | 35 |
|
23 | 36 | ```lua |
24 | | -{ |
25 | | - -- Enabled filetypes |
| 37 | +---Default configuration values for Phoenix |
| 38 | +---@type PhoenixConfig |
| 39 | +vim.g.phoenix = { |
| 40 | + -- Enable for all filetypes by default |
26 | 41 | filetypes = { '*' }, |
27 | 42 |
|
28 | | - -- Dictionary settings: controls cache and scoring algorithm |
| 43 | + -- Dictionary settings control word storage |
29 | 44 | dict = { |
30 | | - capacity = 50000, -- Store up to 50k words |
31 | | - min_word_length = 2, -- Ignore single-letter words |
| 45 | + capacity = 50000, -- Store up to 50k words |
| 46 | + min_word_length = 2, -- Ignore single-letter words |
| 47 | + word_pattern = '[^%s%.%_:%p%d]+', -- Word pattern |
| 48 | + }, |
| 49 | + |
| 50 | + -- Completion control the scoring |
| 51 | + completion = { |
| 52 | + max_items = 1000, -- Max result items |
| 53 | + decay_minutes = 30, -- Time period for decay calculation |
32 | 54 | weights = { |
33 | | - recency = 0.3, -- 30% weight to recent usage |
34 | | - frequency = 0.7, -- 70% weight to frequency |
| 55 | + recency = 0.3, -- 30% weight to recent usage |
| 56 | + frequency = 0.7, -- 70% weight to frequency |
| 57 | + }, |
| 58 | + priority = { |
| 59 | + base = 100, -- Base priority score (0-999) |
| 60 | + position = 'after', -- Position relative to other LSP results: 'before' or 'after' |
35 | 61 | }, |
36 | 62 | }, |
37 | 63 |
|
38 | | - -- Cleanup settings: controls "garbage collection" |
| 64 | + -- Cleanup settings control dictionary maintenance |
39 | 65 | cleanup = { |
40 | | - cleanup_batch_size = 1000, -- Process 1000 words per batch |
41 | | - frequency_threshold = 0.1, -- Keep words used >10% of max frequency |
42 | | - collection_batch_size = 100, -- Collect 100 words before yielding |
43 | | - rebuild_batch_size = 100, -- Rebuild 100 words before yielding |
44 | | - idle_timeout_ms = 1000, -- Wait 1s before cleanup |
45 | | - cleanup_ratio = 0.9, -- Cleanup at 90% capacity |
| 66 | + cleanup_batch_size = 1000, -- Process 1000 words per batch |
| 67 | + frequency_threshold = 0.1, -- Keep words used >10% of max frequency |
| 68 | + collection_batch_size = 100, -- Collect 100 words before yielding |
| 69 | + rebuild_batch_size = 100, -- Rebuild 100 words before yielding |
| 70 | + idle_timeout_ms = 1000, -- Wait 1s before cleanup |
| 71 | + cleanup_ratio = 0.9, -- Cleanup at 90% capacity |
| 72 | + enable_notify = false, -- Enable notify when cleanup dictionary |
46 | 73 | }, |
47 | 74 |
|
48 | | - -- Scanner settings: controls filesystem interaction |
| 75 | + -- Scanner settings control filesystem interaction |
49 | 76 | scanner = { |
50 | | - scan_batch_size = 1000, -- Scan 1000 items per batch |
51 | | - cache_duration_ms = 5000, -- Cache results for 5s |
52 | | - throttle_delay_ms = 100, -- Wait 100ms between updates |
53 | | - ignore_patterns = {}, -- No ignore patterns by default |
| 77 | + scan_batch_size = 1000, -- Scan 1000 items per batch |
| 78 | + cache_duration_ms = 5000, -- Cache results for 5s |
| 79 | + throttle_delay_ms = 150, -- Wait 150ms between updates |
| 80 | + ignore_patterns = {}, -- Dictionary or file ignored when path completion |
54 | 81 | }, |
55 | 82 | } |
56 | 83 | ``` |
57 | 84 |
|
| 85 | +## Completion |
| 86 | + |
| 87 | +1. Phoenix should works with any completion plugin which support lsp source. |
| 88 | + |
| 89 | +2. I have a simple wrapper around `vim.lsp.completion` for works on character which |
| 90 | + does not exist in server triggerCharacters since `vim.lsp.completion` used |
| 91 | + `InsertcharPre` for autotrigger. Notice this script does not works when |
| 92 | + deleted a character if you want need use `TextChangedI`. |
| 93 | + |
| 94 | +```lua |
| 95 | +vim.opt.cot = 'menu,menuone,noinsert,fuzzy,popup' |
| 96 | +vim.opt.cia = 'kind,abbr,menu' |
| 97 | + |
| 98 | +api.nvim_create_autocmd('LspAttach', { |
| 99 | + group = g, |
| 100 | + callback = function(args) |
| 101 | + local bufnr = args.buf |
| 102 | + local client = lsp.get_client_by_id(args.data.client_id) |
| 103 | + if not client or not client:supports_method(ms.textDocument_completion) then |
| 104 | + return |
| 105 | + end |
| 106 | + |
| 107 | + completion.enable(true, client.id, bufnr, { |
| 108 | + autotrigger = true, |
| 109 | + convert = function(item) |
| 110 | + return { |
| 111 | + abbr = item.label:gsub('%b()', ''), |
| 112 | + kind = item.kind:gsub('^.', string.lower) |
| 113 | + } |
| 114 | + end, |
| 115 | + }) |
| 116 | + if #api.nvim_get_autocmds({ buffer = bufnr, event = 'InsertCharPre', group = g }) ~= 0 then |
| 117 | + return |
| 118 | + end |
| 119 | + api.nvim_create_autocmd(InsertCharPre, { |
| 120 | + buffer = bufnr, |
| 121 | + group = g, |
| 122 | + callback = function() |
| 123 | + if tonumber(pumvisible()) == 1 then |
| 124 | + return |
| 125 | + end |
| 126 | + local triggerchars = vim.tbl_get( |
| 127 | + client, |
| 128 | + 'server_capabilities', |
| 129 | + 'completionProvider', |
| 130 | + 'triggerCharacters' |
| 131 | + ) or {} |
| 132 | + if vim.v.char:match('[%w_]') and not vim.list_contains(triggerchars, vim.v.char) then |
| 133 | + vim.schedule(function() |
| 134 | + completion.trigger() |
| 135 | + end) |
| 136 | + end |
| 137 | + end, |
| 138 | + desc = 'glepnir: completion on character which not exist in lsp client triggerCharacters', |
| 139 | + }) |
| 140 | + end, |
| 141 | +}) |
| 142 | +``` |
| 143 | + |
58 | 144 | ## License MIT |
0 commit comments