Skip to content

Commit

Permalink
refactor: reset code on open by default
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Feb 29, 2024
1 parent 144c5b6 commit 7cec0d3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 31 deletions.
12 changes: 3 additions & 9 deletions lua/leetcode-ui/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ function Question:set_lines(code)
end

function Question:reset_lines()
if not self.reset then
return
end

local new_lines = self:snippet(true) or ""

vim.schedule(function() --
log.info("Previous code found and reseted\nTo undo, simply press `u`")
log.info("Previous code found and reset\nTo undo, simply press `u`")
end)

self:set_lines(new_lines)
Expand Down Expand Up @@ -315,14 +311,12 @@ Question.change_lang = vim.schedule_wrap(function(self, lang)
end)

---@param problem lc.cache.Question
---@param reset boolean
function Question:init(problem, reset)
function Question:init(problem)
self.cache = problem
self.lang = config.lang
self.reset = reset and true or false
end

---@type fun(question: lc.cache.Question, reset?: boolean): lc.ui.Question
---@type fun(question: lc.cache.Question): lc.ui.Question
local LeetQuestion = Question

return LeetQuestion
8 changes: 4 additions & 4 deletions lua/leetcode-ui/renderer/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ function Menu:unmount()

require("leetcode.command").q_close_all()

if self.winid and vim.api.nvim_win_is_valid(self.winid) then
vim.api.nvim_win_close(self.winid, true)
end

vim.schedule(function()
if self.winid and vim.api.nvim_win_is_valid(self.winid) then
vim.api.nvim_win_close(self.winid, true)
end

if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then
vim.api.nvim_buf_delete(self.bufnr, { force = true })
end
Expand Down
4 changes: 2 additions & 2 deletions lua/leetcode/command/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function cmd.qot()
return log.err(err)
end
local problemlist = require("leetcode.cache.problemlist")
Question(problemlist.get_by_title_slug(qot.title_slug), true):mount()
Question(problemlist.get_by_title_slug(qot.title_slug)):mount()
end)
end

Expand Down Expand Up @@ -168,7 +168,7 @@ function cmd.random_question(opts)

local item = problems.get_by_title_slug(q.title_slug) or {}
local Question = require("leetcode-ui.question")
Question(item, true):mount()
Question(item):mount()
end

function cmd.start_with_cmd()
Expand Down
13 changes: 6 additions & 7 deletions lua/leetcode/config/hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
local hooks = {}

hooks["question_enter"] = {
vim.schedule_wrap(function(q)
function(q)
-- https://github.com/kawre/leetcode.nvim/issues/14
if q.lang ~= "rust" then
return
if q.lang == "rust" then
pcall(function()
require("rust-tools.standalone").start_standalone_client()
end)
end
pcall(function()
require("rust-tools.standalone").start_standalone_client()
end)
end),
end,
}

return hooks
11 changes: 2 additions & 9 deletions lua/leetcode/pickers/question.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ return {
}),
sorter = conf.generic_sorter(theme),
attach_mappings = function(prompt_bufnr, map)
local function mount_question(reset)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
if not selection then
return
Expand All @@ -140,14 +140,7 @@ return {
end

actions.close(prompt_bufnr)
Question(q, reset):mount()
end

actions.select_default:replace(function()
mount_question()
end)
map({ "n", "i" }, "<C-Enter>", function()
mount_question(true)
Question(q):mount()
end)

return true
Expand Down

0 comments on commit 7cec0d3

Please sign in to comment.