|
| 1 | +local log = require("leetcode.logger") |
| 2 | +local t = require("leetcode.translator") |
| 3 | +local tabs_picker = require("leetcode.picker.tabs") |
| 4 | + |
| 5 | +local picker = require("snacks.picker") |
| 6 | + |
| 7 | +return function(tabs) |
| 8 | + local items = tabs_picker.items(tabs) |
| 9 | + local finder_items = {} |
| 10 | + local completed = false |
| 11 | + local items_reflect = {} |
| 12 | + |
| 13 | + for _, item in ipairs(items) do |
| 14 | + items_reflect[item.value.question.q.frontend_id] = item.value |
| 15 | + local text = tabs_picker.ordinal(item.value.question.q) |
| 16 | + table.insert(finder_items, { |
| 17 | + entry = item.entry, |
| 18 | + item = item.value.question.q.frontend_id, |
| 19 | + text = text, |
| 20 | + }) |
| 21 | + end |
| 22 | + |
| 23 | + picker.pick({ |
| 24 | + source = "select", |
| 25 | + items = finder_items, |
| 26 | + format = function(item) |
| 27 | + local ret = {} |
| 28 | + vim.tbl_map(function(col) |
| 29 | + if type(col) == "table" then |
| 30 | + ret[#ret + 1] = col |
| 31 | + else |
| 32 | + ret[#ret + 1] = { col } |
| 33 | + end |
| 34 | + ret[#ret + 1] = { " " } |
| 35 | + end, item.entry) |
| 36 | + return ret |
| 37 | + end, |
| 38 | + title = t("Select a Question"), |
| 39 | + layout = { |
| 40 | + preview = false, |
| 41 | + layout = { |
| 42 | + height = math.floor(math.min(vim.o.lines * 0.8 - 10, #items + 2) + 0.5), |
| 43 | + }, |
| 44 | + }, |
| 45 | + actions = { |
| 46 | + confirm = function(p, item) |
| 47 | + if completed then |
| 48 | + return |
| 49 | + end |
| 50 | + completed = true |
| 51 | + p:close() |
| 52 | + vim.schedule(function() |
| 53 | + tabs_picker.select(items_reflect[item.item]) |
| 54 | + end) |
| 55 | + end, |
| 56 | + }, |
| 57 | + on_close = function() |
| 58 | + if completed then |
| 59 | + return |
| 60 | + end |
| 61 | + completed = true |
| 62 | + log.warn("No selection") |
| 63 | + end, |
| 64 | + }) |
| 65 | +end |
0 commit comments