Skip to content

Commit

Permalink
feat: add exercism list functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
2KAbhishek committed Nov 15, 2024
1 parent 1ed0125 commit 155be27
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
13 changes: 7 additions & 6 deletions lua/exercism/commands.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
local exercism_module = require('exercism.module')
local config = require('exercism.config')
local config = require('exercism.config').config

local M = {}

M.setup = function()
-- Add all user commands here
vim.api.nvim_create_user_command('ExercismHello', function(opts)
exercism_module.greet(opts.args)
vim.api.nvim_create_user_command('ExercismList', function(opts)
local language = vim.split(opts.args, ' ')[1]
exercism_module.list_exercises(language)
end, { nargs = '?' })

if config.add_default_keybindings then
local function add_keymap(keys, cmd, desc)
vim.api.nvim_set_keymap('n', keys, cmd, { noremap = true, silent = true, desc = desc })
end

-- Add all keybindings here
add_keymap('<leader>Th', ':ExercismHello Neovim (btw!)<CR>', 'Exercism says hi')
if config.add_default_keybindings then
add_keymap('<leader>goe', ':ExercismList lua<CR>', 'Exercism says hi')
end
end
end

Expand Down
1 change: 1 addition & 0 deletions lua/exercism/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local M = {}

---@class ExercismConfig
---@field name string
---@field exercism_workspace string
---@field add_default_keybindings boolean
local config = {
name = 'World!',
Expand Down
59 changes: 47 additions & 12 deletions lua/exercism/module.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
---@class CustomModule
local utils = require('utils')
local Path = require('plenary.path')

local M = {}
local config = require('exercism.config').config

---@return string
M.greet = function(name)
name = name and #name > 0 and name or config.name

M.show_notification('Hello ' .. name)
return 'Hello ' .. name
---@param language any
---@return any
local function get_exercise_data(language)
local script_path = debug.getinfo(1, 'S').source:sub(2)
local plugin_path = vim.fn.fnamemodify(script_path, ':h:h:h')
local exercise_file = plugin_path .. '/data/' .. language .. '.json'
local exercise_data = vim.fn.json_decode(vim.fn.readfile(exercise_file))
return exercise_data
end

M.show_notification = function(message)
vim.notify(message, vim.log.levels.INFO, {
title = 'Exercism',
timeout = 5000,
})
---@param language string
M.list_exercises = function(language)
local exercise_data = get_exercise_data(language)

if #exercise_data > 0 then
vim.ui.select(exercise_data, {
prompt = 'Select Exercise',
format_item = function(exercise)
return exercise.name .. ' : ' .. exercise.type
end,
}, function(selected_exercise, _)
if not selected_exercise then
return
end
local exercise_dir = config.exercism_workspace .. '/' .. language .. '/' .. selected_exercise.name

if not Path:new(exercise_dir):exists() then
local exercism_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(exercism_cmd, function(result)
if result then
utils.open_dir(exercise_dir)
end
end)
else
utils.open_dir(exercise_dir)
end
end)
end
end

return M

0 comments on commit 155be27

Please sign in to comment.