Skip to content

Commit

Permalink
Fixed #57; slashes not normalized in some cases.
Browse files Browse the repository at this point in the history
The `clink.filematches` and `clink.dirmatches` functions and the
`exec.match` setting didn't normalize the root path.
So inputting `../../s` and completing wouldn't convert the early slashes
to backslashes.
  • Loading branch information
chrisant996 committed Jan 19, 2021
1 parent de76485 commit 1f44ad7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 30 deletions.
25 changes: 3 additions & 22 deletions clink/app/scripts/dir.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
-- Copyright (c) 2012 Martin Ridgers
-- License: http://opensource.org/licenses/MIT

--------------------------------------------------------------------------------
function clink.dir_matches(match_word, word_index, line_state)
local word = line_state:getword(word_index)
local expanded
word, expanded = rl.expandtilde(word)

local root = path.getdirectory(word) or ""
if expanded then
root = rl.collapsetilde(root)
end

local matches = {}
for _, d in ipairs(os.globdirs(word.."*", true)) do
local dir = path.join(root, d.name)
table.insert(matches, { match = dir, type = d.type })
end
return matches
end

--------------------------------------------------------------------------------
clink.argmatcher("cd", "chdir")
:addflags("/d")
:addarg(clink.dir_matches)
:addarg(clink.dirmatches)
:nofiles()

--------------------------------------------------------------------------------
clink.argmatcher("pushd", "md", "mkdir")
:addarg(clink.dir_matches)
:addarg(clink.dirmatches)
:nofiles()

--------------------------------------------------------------------------------
clink.argmatcher("rd", "rmdir")
:addflags("/s", "/q")
:addarg(clink.dir_matches)
:addarg(clink.dirmatches)
:nofiles()
8 changes: 4 additions & 4 deletions clink/app/scripts/exec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local function get_environment_paths()

-- Append slashes.
for i = 1, #paths, 1 do
paths[i] = paths[i].."/"
paths[i] = paths[i].."\\"
end

return paths
Expand Down Expand Up @@ -79,7 +79,7 @@ function exec_generator:generate(line_state, match_builder)
local text = line_state:getword(1)
local expanded
text, expanded = rl.expandtilde(text)
local text_dir = path.getdirectory(text) or ""
local text_dir = path.normalise(path.getdirectory(text) or "")
if #text_dir == 0 then
-- Add console aliases as matches.
local aliases = os.getaliases()
Expand All @@ -104,7 +104,7 @@ function exec_generator:generate(line_state, match_builder)
local any_added = false
local root = nil
if rooted then
root = path.getdirectory(pattern) or ""
root = path.normalise(path.getdirectory(pattern) or "")
if expanded then
root = rl.collapsetilde(root)
end
Expand Down Expand Up @@ -133,7 +133,7 @@ function exec_generator:generate(line_state, match_builder)

-- Lastly we may wish to consider directories too.
if match_dirs or not added then
local root = path.getdirectory(text) or ""
local root = path.normalise(path.getdirectory(text) or "")
if expanded then
root = rl.collapsetilde(root)
end
Expand Down
1 change: 1 addition & 0 deletions clink/core/src/globber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ globber::globber(const char* pattern)
m_handle = nullptr;

path::get_directory(pattern, m_root);
path::normalise(m_root.data());
}

//------------------------------------------------------------------------------
Expand Down
7 changes: 3 additions & 4 deletions clink/lua/scripts/arguments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,8 @@ end
--- You can use this function in an argmatcher to supply directory matches.
--- This automatically handles Readline tilde completion.
function clink.dirmatches(match_word)
local word = rl.expandtilde(match_word)

local root = path.getdirectory(word) or ""
local word, expanded = rl.expandtilde(match_word)
local root = path.normalise(path.getdirectory(word) or "")
if expanded then
root = rl.collapsetilde(root)
end
Expand Down Expand Up @@ -650,7 +649,7 @@ end
function clink.filematches(match_word)
local word = rl.expandtilde(match_word)

local root = path.getdirectory(word) or ""
local root = path.normalise(path.getdirectory(word) or "")
if expanded then
root = rl.collapsetilde(root)
end
Expand Down

0 comments on commit 1f44ad7

Please sign in to comment.
-