Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cbfa385

Browse files
committed
feat: question-tabs picker
1 parent 440e729 commit cbfa385

File tree

9 files changed

+136
-35
lines changed

9 files changed

+136
-35
lines changed

lua/leetcode-ui/component/button.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ setmetatable(button, component)
1414
--- @param sc string|nil
1515
--- @param on_press function? optional
1616
--- @param expandable boolean? optional
17-
--- @param keybind string? optional
18-
--- @param keybind_opts table? optional
19-
function button:init(text, sc, on_press, expandable, keybind, keybind_opts)
17+
function button:init(text, sc, on_press, expandable)
2018
local opts = {
2119
position = "center",
2220
on_press = on_press or function() end,
2321
sc = sc,
2422
}
2523
sc = sc and "" or ""
26-
-- log.debug(opts)
2724

2825
local width = 50
2926
local expand = ""

lua/leetcode/command.lua

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,7 @@ function cmd.menu() vim.api.nvim_set_current_tabpage(_Lc_MenuTabPage) end
105105
---@param layout layouts
106106
function cmd.menu_layout(layout) _Lc_Menu:set_layout(layout) end
107107

108-
function cmd.list_questions()
109-
local utils = require("leetcode.utils")
110-
local questions = utils.get_current_questions()
111-
112-
if not vim.tbl_isempty(questions) then
113-
local ui = require("leetcode.ui")
114-
local curr_tabp = vim.api.nvim_get_current_tabpage()
115-
116-
ui.pick_one(questions, "Select a Question", function(item)
117-
---@type question_response
118-
local question = item.question.q
119-
local text = question.frontend_id .. ". " .. question.title
120-
121-
return (curr_tabp == item.tabpage and "(C) " or " ") .. text
122-
end, function(res) pcall(vim.cmd.tabnext, res.tabpage) end)
123-
else
124-
log.warn("No questions opened")
125-
end
126-
end
108+
function cmd.question_tabs() require("leetcode.pickers.question-tabs").pick() end
127109

128110
function cmd.change_lang() require("leetcode.pickers.language").pick() end
129111

lua/leetcode/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ local function setup_cmds()
4343

4444
vim.api.nvim_create_user_command("LcConsole", function() cmd.console() end, {})
4545
vim.api.nvim_create_user_command("LcMenu", function() cmd.menu() end, {})
46-
vim.api.nvim_create_user_command("LcQuestionTabs", function() cmd.list_questions() end, {})
46+
vim.api.nvim_create_user_command("LcQuestionTabs", function() cmd.question_tabs() end, {})
4747
vim.api.nvim_create_user_command("LcLanguage", function() cmd.change_lang() end, {})
4848
vim.api.nvim_create_user_command("LcDescriptionToggle", function() cmd.desc_toggle() end, {})
4949
end

lua/leetcode/pickers/init.lua

Lines changed: 0 additions & 5 deletions
This file was deleted.

lua/leetcode/pickers/language.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ return {
9898
local selection = action_state.get_selected_entry()
9999

100100
if not selection then return end
101-
config.lang = selection.slug
101+
config.lang = selection.value.slug
102102
log.info("Language set to " .. selection.lang)
103103
end)
104104
return true
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
local log = require("leetcode.logger")
2+
local utils = require("leetcode.utils")
3+
4+
local Question = require("leetcode.ui.question")
5+
6+
local pickers = require("telescope.pickers")
7+
local finders = require("telescope.finders")
8+
local conf = require("telescope.config").values
9+
local config = require("leetcode.config")
10+
11+
local entry_display = require("telescope.pickers.entry_display")
12+
local actions = require("telescope.actions")
13+
local action_state = require("telescope.actions.state")
14+
15+
---@param question lc.Cache.Question
16+
---
17+
---@return string
18+
local function question_formatter(question)
19+
return string.format("%d. %s", question.frontend_id, question.title)
20+
end
21+
22+
local function display_current(entry)
23+
local tabp = vim.api.nvim_get_current_tabpage()
24+
if tabp ~= entry.tabpage then return unpack({ "", "" }) end
25+
26+
return { "", "" }
27+
end
28+
29+
local function display_difficulty(q)
30+
local filetypes = {
31+
cpp = "cpp",
32+
java = "java",
33+
python = "py",
34+
python3 = "py",
35+
c = "c",
36+
csharp = "cs",
37+
javascript = "js",
38+
typescript = "ts",
39+
php = "php",
40+
swift = "swift",
41+
kotlin = "kt",
42+
dart = "dart",
43+
golang = "go",
44+
ruby = "rb",
45+
scala = "scala",
46+
rust = "rs",
47+
racket = "rkt",
48+
erlang = "erl",
49+
elixir = "ex",
50+
}
51+
52+
return { filetypes[q.lang], "LeetCode" .. q.q.difficulty }
53+
end
54+
55+
---@param question lc.Cache.Question
56+
local function display_question(question)
57+
local index = { question.frontend_id .. ".", "LeetCodeNormal" }
58+
local title = { question.title }
59+
60+
return unpack({ index, title })
61+
end
62+
63+
local displayer = entry_display.create({
64+
separator = " ",
65+
items = {
66+
{ width = 1 },
67+
{ width = 5 },
68+
{ width = 5 },
69+
{ remaining = true },
70+
},
71+
})
72+
73+
local function make_display(entry)
74+
---@type lc.Cache.Question
75+
local q = entry.value.question.q
76+
77+
return displayer({
78+
display_current(entry.value),
79+
display_difficulty(entry.value.question),
80+
display_question(q),
81+
})
82+
end
83+
84+
local function entry_maker(entry)
85+
return {
86+
value = entry,
87+
display = make_display,
88+
ordinal = question_formatter(entry.question.q),
89+
}
90+
end
91+
92+
local opts = require("telescope.themes").get_dropdown()
93+
94+
return {
95+
pick = function()
96+
local tabs = utils.get_current_question_tabs()
97+
if vim.tbl_isempty(tabs) then
98+
log.warn("No questions opened")
99+
return
100+
end
101+
102+
pickers
103+
.new(opts, {
104+
prompt_title = "Select a Question",
105+
finder = finders.new_table({
106+
results = tabs,
107+
entry_maker = entry_maker,
108+
}),
109+
sorter = conf.generic_sorter(opts),
110+
attach_mappings = function(prompt_bufnr, map)
111+
actions.select_default:replace(function()
112+
actions.close(prompt_bufnr)
113+
local selection = action_state.get_selected_entry()
114+
115+
if not selection then return end
116+
pcall(vim.cmd.tabnext, selection.value.tabpage)
117+
end)
118+
return true
119+
end,
120+
})
121+
:find()
122+
end,
123+
}

lua/leetcode/pickers/question.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ end
4545

4646
---@param question lc.Cache.Question
4747
local function display_question(question)
48-
-- local title = question_formatter(question)
49-
local index = { question.frontend_id .. "." }
48+
local index = { question.frontend_id .. ".", "LeetCodeNormal" }
5049
local title = { question.title }
5150

5251
return unpack({ index, title })

lua/leetcode/ui/question.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function question:autocmds()
6161
vim.api.nvim_create_autocmd("TabEnter", {
6262
group = group_id,
6363
callback = function()
64-
local questions = utils.get_current_questions()
64+
local questions = utils.get_current_question_tabs()
6565

6666
local tabpage = vim.api.nvim_get_current_tabpage()
6767
for _, q in ipairs(questions) do

lua/leetcode/utils.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ function utils.map(mode, lhs, rhs, opts)
4444
vim.api.nvim_set_keymap(mode, lhs, rhs, options)
4545
end
4646

47-
function utils.get_current_questions()
47+
function utils.get_current_question_tabs()
48+
---@class lc.Question.Tab
49+
---@field tabpage integer
50+
---@field question lc.Question
51+
52+
---@type lc.Question.Tab[]
4853
local questions = {}
4954

5055
for _, tabp in ipairs(vim.api.nvim_list_tabpages()) do
@@ -91,7 +96,7 @@ function utils.filetype(lang)
9196
}
9297

9398
local ft = filetypes[lang]
94-
assert(ft)
99+
assert(ft, "Unknown language: " .. lang)
95100
return ft
96101
end
97102

0 commit comments

Comments
 (0)