-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: use OO & add tests (#100)
* I did an overhall for all codes and added typing by Lua-language-server and tests. It also works on CI. * Now it searches files on the workspace completely asynchronously. It does not block your text input. (Fix #106) Make count = 1 when you open a file you've never opened (Fix #107)
- Loading branch information
Showing
24 changed files
with
1,798 additions
and
653 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
name: CI | ||
on: | ||
- push | ||
- pull_request | ||
jobs: | ||
test: | ||
name: Run tests | ||
strategy: | ||
matrix: | ||
os: | ||
- ubuntu-latest | ||
# TODO: nix seems not to work with SIP | ||
# - macos-latest | ||
# TODO: PlenaryBustedDirectory seems not to run on Windows | ||
# - windows-latest | ||
version: | ||
- v0.9.0 | ||
- nightly | ||
runs-on: ${{ matrix.os }} | ||
timeout-minutes: 15 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Checkout plenary.nvim | ||
uses: actions/checkout@v3 | ||
with: | ||
repository: nvim-lua/plenary.nvim | ||
path: plenary.nvim | ||
- name: Checkout telescope.nvim | ||
uses: actions/checkout@v3 | ||
with: | ||
repository: nvim-telescope/telescope.nvim | ||
path: telescope.nvim | ||
- name: Checkout sqlite.lua | ||
uses: actions/checkout@v3 | ||
with: | ||
repository: kkharji/sqlite.lua | ||
path: sqlite.lua | ||
- name: Install Neovim | ||
uses: rhysd/action-setup-vim@v1 | ||
id: nvim | ||
with: | ||
neovim: true | ||
version: ${{ matrix.version }} | ||
- name: Run tests | ||
env: | ||
PLENARY_PATH: plenary.nvim | ||
TELESCOPE_PATH: telescope.nvim | ||
SQLITE_PATH: sqlite.lua | ||
DEBUG_PLENARY: 1 | ||
EXE: ${{ steps.nvim.outputs.executable }} | ||
run: |- | ||
TEST_DIR=lua/frecency/tests/ | ||
MINIMAL_LUA=${TEST_DIR}minimal.lua | ||
NVIM=$(perl -e '$_ = $ENV{EXE}; s,\\,/,g; print') | ||
$NVIM --headless --clean -u $MINIMAL_LUA -c "PlenaryBustedDirectory $TEST_DIR {minimal_init = '$MINIMAL_LUA'}" | ||
- name: Type Check Code Base | ||
uses: mrcjkb/[email protected] | ||
with: | ||
checkLevel: Hint | ||
configpath: .luarc.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"diagnostics": { | ||
"globals": [ | ||
"describe", | ||
"it", | ||
"vim" | ||
] | ||
}, | ||
"runtime.version": "LuaJIT", | ||
"runtime.path": [ | ||
"lua/?.lua", | ||
"lua/?/init.lua" | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
local async = require "plenary.async" | ||
|
||
---@class FrecencyAsyncFinder | ||
---@field closed boolean | ||
---@field entries FrecencyEntry[] | ||
---@field rx FrecencyRx | ||
---@overload fun(_: string, process_result: (fun(entry: FrecencyEntry): nil), process_complete: fun(): nil): nil | ||
local AsyncFinder = {} | ||
|
||
---@class FrecencyRx | ||
---@field recv fun(): FrecencyEntry? | ||
|
||
---@class FrecencyTx | ||
---@field send fun(entry: FrecencyEntry?): nil | ||
|
||
---@param fs FrecencyFS | ||
---@param path string | ||
---@param entry_maker fun(file: FrecencyFile): FrecencyEntry | ||
---@param initial_results FrecencyFile[] | ||
---@return FrecencyAsyncFinder | ||
AsyncFinder.new = function(fs, path, entry_maker, initial_results) | ||
local self = setmetatable({ closed = false, entries = {} }, { | ||
__index = AsyncFinder, | ||
---@param self FrecencyAsyncFinder | ||
__call = function(self, ...) | ||
return self:find(...) | ||
end, | ||
}) | ||
local seen = {} | ||
for i, file in ipairs(initial_results) do | ||
local entry = entry_maker(file) | ||
seen[entry.filename] = true | ||
entry.index = i | ||
table.insert(self.entries, entry) | ||
end | ||
---@type FrecencyTx, FrecencyRx | ||
local tx, rx = async.control.channel.mpsc() | ||
self.rx = rx | ||
async.run(function() | ||
local index = #initial_results | ||
local count = 0 | ||
for name in fs:scan_dir(path) do | ||
if self.closed then | ||
break | ||
end | ||
local fullpath = vim.fs.joinpath(path, name) | ||
if not seen[fullpath] then | ||
seen[fullpath] = true | ||
index = index + 1 | ||
count = count + 1 | ||
local entry = entry_maker { id = 0, count = 0, path = vim.fs.joinpath(path, name), score = 0 } | ||
if entry then | ||
entry.index = index | ||
table.insert(self.entries, entry) | ||
tx.send(entry) | ||
if count % 1000 == 0 then | ||
-- NOTE: This is needed not to lock text input. | ||
async.util.sleep(0) | ||
end | ||
end | ||
end | ||
end | ||
self:close() | ||
tx.send(nil) | ||
end) | ||
return self | ||
end | ||
|
||
---@param _ string | ||
---@param process_result fun(entry: FrecencyEntry): nil | ||
---@param process_complete fun(): nil | ||
---@return nil | ||
function AsyncFinder:find(_, process_result, process_complete) | ||
for _, entry in ipairs(self.entries) do | ||
if process_result(entry) then | ||
return | ||
end | ||
end | ||
local last_index = self.entries[#self.entries].index | ||
while true do | ||
if self.closed then | ||
break | ||
end | ||
local entry = self.rx.recv() | ||
if not entry then | ||
break | ||
elseif entry.index > last_index and process_result(entry) then | ||
return | ||
end | ||
end | ||
process_complete() | ||
end | ||
|
||
function AsyncFinder:close() | ||
self.closed = true | ||
end | ||
|
||
return AsyncFinder |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.