Skip to content

Commit

Permalink
feat: add ExercismLanguages command
Browse files Browse the repository at this point in the history
  • Loading branch information
2KAbhishek committed Nov 21, 2024
1 parent d6f59b3 commit ca5c1de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ exercism.nvim is a neovim plugin that integrates exercism.io into your neovim wo
{
'2kabhishek/exercism.nvim',
cmd = {
'ExercismLanguages',
'ExercismList',
'ExercismSubmit',
'ExercismTest',
},
keys = {
'<leader>exa',
'<leader>exl',
'<leader>exs',
'<leader>ext',
Expand Down
5 changes: 5 additions & 0 deletions lua/exercism/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ local config = require('exercism.config').config
local M = {}

M.setup = function()
vim.api.nvim_create_user_command('ExercismLanguages', function(_)
main.list_languages()
end, {})

vim.api.nvim_create_user_command('ExercismList', function(opts)
local language = vim.split(opts.args, ' ')[1]
main.list_exercises(language)
Expand All @@ -23,6 +27,7 @@ M.setup = function()
end

if config.add_default_keybindings then
add_keymap('<leader>exa', ':ExercismLanguages<CR>', 'All Exercism Languages')
add_keymap('<leader>exl', ':ExercismList<CR>', 'Exercism List')
add_keymap('<leader>ext', ':ExercismTest<CR>', 'Exercism Test')
add_keymap('<leader>exs', ':ExercismSubmit<CR>', 'Exercism Submit')
Expand Down
24 changes: 22 additions & 2 deletions lua/exercism/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ local type_icons_map = {
['practice'] = config.icons.practice .. ' ',
}

local function get_plugin_path()
local script_path = debug.getinfo(1, 'S').source:sub(2)
return vim.fn.fnamemodify(script_path, ':h:h:h')
end

---@param language string
---@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 plugin_path = get_plugin_path()
local exercise_file = plugin_path .. '/data/' .. language .. '.json'
local exercise_data = vim.fn.json_decode(vim.fn.readfile(exercise_file))
return exercise_data
Expand Down Expand Up @@ -51,6 +55,22 @@ local function handle_selection(exercise_name, language)
end
end

M.list_languages = function()
local plugin_path = get_plugin_path()
local language_files = vim.fn.glob(plugin_path .. '/data/*.json', false, true)
local languages = vim.tbl_map(function(file)
return vim.fn.fnamemodify(file, ':t:r')
end, language_files)

vim.ui.select(languages, {
prompt = 'Select Language',
}, function(selected_language)
if selected_language then
M.list_exercises(selected_language)
end
end)
end

---@param language string
M.list_exercises = function(language)
if language == '' or language == nil then
Expand Down

0 comments on commit ca5c1de

Please sign in to comment.