martanne / vis

A vi-like editor based on Plan 9's structural regular expressions
Other
4.19k stars 259 forks source link

Complete file name and file path swapped in doc #1146

Closed lobre closed 8 months ago

lobre commented 8 months ago

Doing :help gives me this:

<C-x><C-f>            Complete file path                                                                                                                                                                                             
<C-x><C-o>            Complete file name

Trying myself, it seems it is the opposite in fact:

<C-x><C-f>            Complete file name                                                                                                                                                                                             
<C-x><C-o>            Complete file path

I don't know if it is the implementation or doc that is wrong.

mcepl commented 8 months ago

It is all in the script lua/plugins/complete-filename.lua (which badly cries for the refactoring love), and I wonder what’s the actual difference between two commands. Documentation has a lot to be desired.

rnpnr commented 8 months ago

The difference isn't that big. Assuming $PWD = /usr:

<C-x><C-f>: bin/ --> bin/awk
<C-x><C-o>: bin/ --> /usr/bin/awk

Here is my first attempt at cleaning it up, but it could probably be better:

local complete_filename = function(expand)
    local win = vis.win
    local file = win.file
    local pos = win.selection.pos
    if not pos then return end

    -- TODO do something clever here
    local range = file:text_object_longword(pos > 0 and pos-1 or pos);
    if not range then return end
    if range.finish > pos then range.finish = pos end
    if not expand and range.start == range.finish then return end

    local prefix = file:content(range)
    if not prefix then return end

    -- Strip leading delimiters for some progamming languages
    local _, j = prefix:find("[[(<'\"]+")
    if not expand and j then prefix = prefix:sub(j + 1) end
    if expand and prefix:match("^%s*$") then
        prefix = ""
        range.start = pos
        range.finish = pos
    end

    local cmdfmt = "vis-complete --file '%s'"
    if expand then cmdfmt = "vis-open -- '%s'*" end
    local status, out, err = vis:pipe(cmdfmt:format(prefix:gsub("'", "'\\''")))
    if status ~= 0 or not out then
        if err then vis:info(err) end
        return
    end
    out = out:gsub("\n$", "")

    if expand then
        file:delete(range)
        file:insert(range.start, out)
        win.selection.pos = range.start + #out
    else
        file:insert(pos, out)
        win.selection.pos = pos + #out
    end
end

-- complete file path at primary selection location using vis-complete(1)
vis:map(vis.modes.INSERT, "<C-x><C-f>", function()
    complete_filename(false);
end, "Complete file name")

-- complete file path at primary selection location using vis-open(1)
vis:map(vis.modes.INSERT, "<C-x><C-o>", function()
    complete_filename(true);
end, "Complete file name (expands path)")
mcepl commented 8 months ago

Perhaps PR would be better? Something like https://git.sr.ht/~mcepl/vis/commit/620d37d121f76b0fe98d75040d3337fe5d73f5f4 ?

rnpnr commented 8 months ago

Perhaps PR would be better?

Done: #1148. I didn't use yours because your repo has some commit adding expansion of ~ to $HOME. Its probably fine but should be added as a separate commit.