chrisant996 / clink-gizmos

A library of Lua scripts for use with Clink https://github.com/chrisant996/clink.
MIT License
72 stars 6 forks source link

Exception in cwdhistory when pressing Ctrl + \ #9

Closed eblis closed 6 months ago

eblis commented 6 months ago

I get an exception when I press Ctrl + \ combination without any text entered in the command prompt.

match generator failed:
...\<user>\AppData\Local\clink\clink-gizmos\cwdhistory.lua:377: attempt to index a nil value
stack traceback:
        ~clink~/lib/generator.lua:239: in function <~clink~/lib/generator.lua:227>
        [C]: in function 'xpcall'
        ~clink~/lib/generator.lua:261: in function <~clink~/lib/generator.lua:226>
        [C]: in function 'invokecommand'
        ...\F4GWHKR\AppData\Local\clink\clink-gizmos\cwdhistory.lua:421: in function <...\F4GWHKR\AppData\Local\clink\clink-gizmos\cwdhistory.lua:419>
eblis commented 6 months ago

I attempted to return from function dir_generator:generate(line_state, builder) if nothing is entered, but it's still filling out the command prompt with something (i was expecting it to do nothing), so the change is more complex it seems.

    local word = get_cursor_word(line_state)

    if not word or word == "" then
        --rl_buffer:ding()
        return
    end
chrisant996 commented 6 months ago

Thanks for reporting this.

word wasn't nil, word was an empty string (see get_cursor_word, which always returns a string).

The issue was path.getdirectory(word) can return nil if there's no directory (an empty string can't be a directory), and so the :sub() call was being used against nil instead of against a string.