Skip to content

Commit

Permalink
refactor: extract out handle selection
Browse files Browse the repository at this point in the history
  • Loading branch information
2KAbhishek committed Nov 21, 2024
1 parent 1736e8e commit 89db02b
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions lua/exercism/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ local function get_exercise_dir(exercise_name, language)
return vim.fn.expand(config.exercism_workspace .. '/' .. language .. '/' .. exercise_name)
end

---@param exercise_name string
---@param language string
local function handle_selection(exercise_name, language)
local exercise_dir = get_exercise_dir(exercise_name, language)

if not Path:new(exercise_dir):exists() then
local download_cmd =
string.format('exercism download --track=%s --exercise=%s', language, exercise_name)

utils.show_notification(
'Setting up exercise: ' .. exercise_name .. ' in ' .. language,
vim.log.levels.INFO,
'Exercism'
)

utils.async_shell_execute(download_cmd, function(result)
if result then
utils.open_dir(exercise_dir)
end
end)
else
utils.open_dir(exercise_dir)
end
end

---@param language string
M.list_exercises = function(language)
local exercise_data = get_exercise_data(language)
Expand All @@ -41,26 +66,7 @@ M.list_exercises = function(language)
if not selected_exercise then
return
end
local exercise_dir = get_exercise_dir(selected_exercise.name, language)

if not Path:new(exercise_dir):exists() then
local download_cmd =
string.format('exercism download --track=%s --exercise=%s', language, selected_exercise.name)

utils.show_notification(
'Setting up exercise: ' .. selected_exercise.name .. ' in ' .. language,
vim.log.levels.INFO,
'Exercism'
)

utils.async_shell_execute(download_cmd, function(result)
if result then
utils.open_dir(exercise_dir)
end
end)
else
utils.open_dir(exercise_dir)
end
handle_selection(selected_exercise.name, language)
end)
end
end
Expand Down

0 comments on commit 89db02b

Please sign in to comment.