-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
14 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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
|
||
return function() | ||
vim.api.nvim_create_user_command('W', 'write', {}) | ||
vim.api.nvim_create_user_command('Wq', 'wq', {}) | ||
vim.api.nvim_create_user_command('Q', 'quit', {}) | ||
vim.api.nvim_create_user_command('Qa', 'qall', {}) | ||
vim.api.nvim_create_user_command('Wqa', 'wqall', {}) | ||
vim.api.nvim_create_user_command('FO', 'Fo', {}) | ||
vim.api.nvim_create_user_command('W', 'write', {}) | ||
vim.api.nvim_create_user_command('Wq', 'wq', {}) | ||
vim.api.nvim_create_user_command('Q', 'quit', {}) | ||
vim.api.nvim_create_user_command('Qa', 'qall', {}) | ||
vim.api.nvim_create_user_command('Wqa', 'wqall', {}) | ||
vim.api.nvim_create_user_command('FO', 'Fo', {}) | ||
vim.api.nvim_create_user_command('Ranker', function (opts) | ||
require('handdara.util.ranker').ranker(opts.line1, opts.line2) | ||
end , { range = true }) | ||
end |
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,72 @@ | ||
local M = {} | ||
|
||
local function user_cmp_sync(prompt, x, y) | ||
-- Create a placeholder for the input result | ||
local result = nil | ||
|
||
-- Create a coroutine | ||
local co = coroutine.running() | ||
if not co then | ||
error("This function must be called from within a coroutine") | ||
end | ||
vim.ui.select({ x, y }, { | ||
prompt = prompt, | ||
format_item = function(item) | ||
return '- ' .. item | ||
end, | ||
}, function(choice) | ||
result = choice | ||
coroutine.resume(co) | ||
end) | ||
-- Yield control until the coroutine is resumed | ||
coroutine.yield() | ||
return result | ||
end | ||
|
||
local function sort(xs, cmp) | ||
if #xs <= 1 then | ||
return | ||
else | ||
local left, right = {}, {} | ||
local pivot = xs[1] | ||
for i = 2, #xs do | ||
-- cmp ~ < | ||
if cmp(pivot, xs[i]) then | ||
table.insert(right, xs[i]) | ||
else | ||
table.insert(left, xs[i]) | ||
end | ||
end | ||
sort(left, cmp) | ||
sort(right, cmp) | ||
for j, v in ipairs(left) do | ||
xs[j] = v | ||
end | ||
xs[#left + 1] = pivot | ||
for j, v in ipairs(right) do | ||
xs[j + 1 + #left] = v | ||
end | ||
end | ||
end | ||
|
||
-- Example usage | ||
local function rank(items, line1, line2) | ||
local cmp = function(x, y) | ||
local result = user_cmp_sync("Pick better:", x, y) | ||
if result then | ||
return x == result | ||
else | ||
vim.notify("cancelled ranker") | ||
error("cancelled sort") | ||
end | ||
end | ||
sort(items, cmp) | ||
vim.api.nvim_buf_set_lines(0, line1 - 1, line2, false, items) | ||
end | ||
|
||
function M.ranker(line1, line2) | ||
local items = vim.api.nvim_buf_get_lines(0, line1 - 1, line2, false) | ||
coroutine.wrap(rank)(items, line1, line2) | ||
end | ||
|
||
return M |