Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: folke/snacks.nvim picker support #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/leetcode/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

---@alias lc.storage table<"cache"|"home", string>

---@alias lc.picker { provider?: "fzf-lua" | "telescope" }
---@alias lc.picker { provider?: "fzf-lua" | "telescope" | "snacks-picker" }

---@class lc.UserConfig
local M = {
Expand Down
13 changes: 11 additions & 2 deletions lua/leetcode/picker/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local log = require("leetcode.logger")
local config = require("leetcode.config")

---@return "fzf" | "telescope"
---@return "fzf" | "telescope" | "snacks"
local function resolve_provider()
---@type string
local provider = config.user.picker.provider
Expand All @@ -15,9 +15,18 @@ local function resolve_provider()
if telescope_ok then
return "telescope"
end
local snacks_ok = pcall(require, "snacks.picker")
if snacks_ok then
return "snacks"
end
error("no supported picker provider found")
else
local provider_ok = pcall(require, provider)
local mod = provider
if provider == "snacks-picker" then
provider = "snacks"
mod = "snacks.picker"
end
local provider_ok = pcall(require, mod)
assert(provider_ok, ("specified picker provider not found: `%s`"):format(provider))
return provider == "fzf-lua" and "fzf" or provider
end
Expand Down
63 changes: 63 additions & 0 deletions lua/leetcode/picker/language/snacks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local log = require("leetcode.logger")
local t = require("leetcode.translator")
local picker = require("snacks.picker")

local language_picker = require("leetcode.picker.language")

---@param question lc.ui.Question
return function(question, cb)
local items = language_picker.items(question.q.code_snippets)
local finder_items = {}
local completed = false

for _, item in ipairs(items) do
local text = language_picker.ordinal(item.value)
table.insert(finder_items, {
text = text,
item = item,
})
end

picker.pick({
source = "select",
items = finder_items,
format = function(item)
local ret = {}
vim.tbl_map(function(col)
if type(col) == "table" then
ret[#ret + 1] = col
else
ret[#ret + 1] = { col }
end
ret[#ret + 1] = { " " }
end, item.item.entry)
return ret
end,
title = t("Available Languages"),
layout = {
preview = false,
layout = {
height = math.floor(math.min(vim.o.lines * 0.8 - 10, #items + 2) + 0.5),
},
},
actions = {
confirm = function(p, item)
if completed then
return
end
completed = true
p:close()
vim.schedule(function()
language_picker.select(item.item.value.t, question, cb)
end)
end,
},
on_close = function()
if completed then
return
end
completed = true
log.warn("No selection")
end,
})
end
63 changes: 63 additions & 0 deletions lua/leetcode/picker/question/snacks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local log = require("leetcode.logger")
local t = require("leetcode.translator")
local question_picker = require("leetcode.picker.question")

local picker = require("snacks.picker")

---@param questions lc.cache.Question[]
return function(questions, opts)
local items = question_picker.items(questions, opts)
local finder_items = {}
local completed = false

for _, item in ipairs(items) do
local text = question_picker.ordinal(item.value)
table.insert(finder_items, {
text = text,
item = item,
})
end

picker.pick({
source = "select",
items = finder_items,
format = function(item)
local ret = {}
vim.tbl_map(function(col)
if type(col) == "table" then
ret[#ret + 1] = col
else
ret[#ret + 1] = { col }
end
ret[#ret + 1] = { " " }
end, item.item.entry)
return ret
end,
title = t("Select a Question"),
layout = {
preview = false,
layout = {
height = math.floor(math.min(vim.o.lines * 0.8 - 10, #items + 2) + 0.5),
},
},
actions = {
confirm = function(p, item)
if completed then
return
end
completed = true
p:close()
vim.schedule(function()
question_picker.select(item.item.value)
end)
end,
},
on_close = function()
if completed then
return
end
completed = true
log.warn("No selection")
end,
})
end
65 changes: 65 additions & 0 deletions lua/leetcode/picker/tabs/snacks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local log = require("leetcode.logger")
local t = require("leetcode.translator")
local tabs_picker = require("leetcode.picker.tabs")

local picker = require("snacks.picker")

return function(tabs)
local items = tabs_picker.items(tabs)
local finder_items = {}
local completed = false
local items_reflect = {}

for _, item in ipairs(items) do
items_reflect[item.value.question.q.frontend_id] = item.value
local text = tabs_picker.ordinal(item.value.question.q)
table.insert(finder_items, {
entry = item.entry,
item = item.value.question.q.frontend_id,
text = text,
})
end

picker.pick({
source = "select",
items = finder_items,
format = function(item)
local ret = {}
vim.tbl_map(function(col)
if type(col) == "table" then
ret[#ret + 1] = col
else
ret[#ret + 1] = { col }
end
ret[#ret + 1] = { " " }
end, item.entry)
return ret
end,
title = t("Select a Question"),
layout = {
preview = false,
layout = {
height = math.floor(math.min(vim.o.lines * 0.8 - 10, #items + 2) + 0.5),
},
},
actions = {
confirm = function(p, item)
if completed then
return
end
completed = true
p:close()
vim.schedule(function()
tabs_picker.select(items_reflect[item.item])
end)
end,
},
on_close = function()
if completed then
return
end
completed = true
log.warn("No selection")
end,
})
end