robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1---@class nvim.TutorMetadata 2---@field expect table<string, string|-1> 3 4---@alias nvim.TutorExtmarks table<string, string> 5 6---@type nvim.TutorExtmarks? 7vim . b . tutor_extmarks =vim . b . tutor_extmarks 8 9---@type nvim.TutorMetadata? 10vim . b . tutor_metadata =vim . b . tutor_metadata 11 12local sign_text_correct ='✓' 13local sign_text_incorrect ='✗' 14local tutor_mark_ns =vim . api . nvim_create_namespace ( 'nvim.tutor.mark' ) 15local tutor_hl_ns =vim . api . nvim_create_namespace ( 'nvim.tutor.hl' ) 16 17local M ={} 18 19-- Extract inline comments into metadata. 20-- Example: "This is wrong. [[This is right.]]" => { '1' = 'This is right.'} 21function M . load_metadata () 22---@type table<string, string|-1> 23local data ={} 24local lines =vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) 25 26for i , line in ipairs ( lines ) do 27local text , expected_text =line : match ( '^(.-)%s%[%[(.-)%]%]$' ) 28 29if text then 30if expected_text == '-1' then 31data [ tostring ( i )] =- 1 32else 33data [ tostring ( i )] =expected_text 34end 35lines [ i ] =text 36end 37end 38 39vim . b . tutor_metadata ={ expect =data } 40vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , lines ) 41end 42 43---@param line integer 1-based 44local function check_line ( line ) 45if vim . b . tutor_metadata and vim . b . tutor_metadata . expect and vim . b . tutor_extmarks then 46local ctext =vim . fn . getline ( line ) 47 48---@type vim.api.keyset.get_extmark_item[] 49local extmarks =vim 50. iter ( vim . api . nvim_buf_get_extmarks ( 510 , 52tutor_mark_ns , 53{ line - 1 , 0 } , 54{ line - 1 , - 1 } , -- the extmark can move to col > 0 if users insert text there 55{ details =true } 56)) 57: filter ( function ( extmark ) 58return not assert ( extmark [ 4 ]). invalid 59end ) 60: totable () 61 62for _ , extmark in ipairs ( extmarks ) do 63local mark_id =extmark [ 1 ] 64local expct =vim . b . tutor_extmarks [ tostring ( mark_id )] 65local expect =vim . b . tutor_metadata . expect [ expct ] 66local is_correct =expect == - 1 or ctext == expect 67 68vim . api . nvim_buf_set_extmark ( 0 , tutor_mark_ns , line - 1 , 0 , { 69id =mark_id , 70sign_text =is_correct and sign_text_correct or sign_text_incorrect , 71sign_hl_group =is_correct and 'tutorOK' or 'tutorX' , 72invalidate =true , 73} ) 74end 75end 76end 77 78function M . apply_marks () 79vim . cmd [[hi! link tutorExpect Special]] 80if vim . b . tutor_metadata and vim . b . tutor_metadata . expect then 81vim . b . tutor_extmarks ={} 82for expct , _ in pairs ( vim . b . tutor_metadata . expect ) do 83---@diagnostic disable-next-line: assign-type-mismatch 84local lnum =vim . _assert_integer ( expct ) 85vim . api . nvim_buf_set_extmark ( 0 , tutor_hl_ns , lnum - 1 , 0 , { 86line_hl_group ='tutorExpect' , 87invalidate =true , 88} ) 89 90local mark_id =vim . api . nvim_buf_set_extmark ( 0 , tutor_mark_ns , lnum - 1 , 0 ) 91 92-- Cannot edit field of a Vimscript dictionary from Lua directly, see `:h lua-vim-variables` 93---@type nvim.TutorExtmarks 94local tutor_extmarks =vim . b . tutor_extmarks 95tutor_extmarks [ tostring ( mark_id )] =expct 96vim . b . tutor_extmarks =tutor_extmarks 97 98check_line ( lnum ) 99end 100end 101end 102 103function M . apply_marks_on_changed () 104if vim . b . tutor_metadata and vim . b . tutor_metadata . expect and vim . b . tutor_extmarks then 105local lnum =vim . fn . line ( '.' ) 106check_line ( lnum ) 107end 108end 109 110return M