From b192f9f84043537e4b0c85a54849314376236a6c Mon Sep 17 00:00:00 2001 From: Chris Antos Date: Sun, 26 Nov 2023 18:25:41 -0800 Subject: [PATCH] Improve syntax handling in DOSKEY argmatcher. --- completions/doskey.lua | 70 +++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/completions/doskey.lua b/completions/doskey.lua index ff73b6d..f6f3f0d 100644 --- a/completions/doskey.lua +++ b/completions/doskey.lua @@ -1,4 +1,5 @@ require('arghelper') +local clink_version = require('clink_version') local function exe_matches_all(word, word_index, line_state, match_builder) -- luacheck: no unused args match_builder:addmatch({ match="all", display="\x1b[1mALL" }) @@ -11,32 +12,35 @@ local function exe_matches(word, word_index, line_state, match_builder) -- luach match_builder:addmatches(clink.filematches("")) end -local function require_equal_sign(arg_index, word, word_index, line_state, classifications) -- luacheck: no unused +local function has_equal_sign(arg_index, word_index, line_state) if arg_index == 1 then local x = line_state:getwordinfo(word_index) local y = line_state:getwordinfo(word_index + 1) if x and y then local line = line_state:getline() local s = line:sub(x.offset + x.length, y.offset - 1) - if not s:find("=") then - local color = settings.get("color.unexpected") or "" - local delta = s:find("[ \t]") - delta = delta and (delta - 1) or #s - local lastinfo = line_state:getwordinfo(line_state:getwordcount()) - local endoffset = lastinfo.offset + lastinfo.length - local tailoffset = x.offset + x.length + delta - if endoffset > tailoffset then - local tail = line:sub(endoffset):match("^([^&|]+)[&|]?.*$") or "" - endoffset = endoffset + #tail - end - classifications:applycolor(tailoffset, endoffset - tailoffset, color, true) - end + return s:find("=") and true end end end +local onlink_parsers = {} +local function chain_if_equal_sign(_, arg_index, _, word_index, line_state) + if has_equal_sign(arg_index, word_index, line_state) then + if not onlink_parsers.chain then + onlink_parsers.chain = clink.argmatcher():chaincommand() + end + return onlink_parsers.chain + else + if not onlink_parsers.nofiles then + onlink_parsers.nofiles = clink.argmatcher():nofiles() + end + return onlink_parsers.nofiles + end +end + -- luacheck: no max line length -clink.argmatcher("doskey") +local doskey = clink.argmatcher("doskey") :_addexflags({ {"/reinstall", "Installs a new copy of Doskey"}, {"/macros", "Display all Doskey macros for the current executable"}, @@ -44,6 +48,36 @@ clink.argmatcher("doskey") {"/exename="..clink.argmatcher():addarg(exe_matches), "Specifies the executable"}, {"/macrofile=", "Specifies a file of macros to install"}, }) -:addarg() -:chaincommand() -:setclassifier(require_equal_sign) +:addarg({onlink=chain_if_equal_sign}) + +if not clink_version.supports_argmatcher_onlink then + + local function require_equal_sign(arg_index, _, word_index, line_state, classifications) + if arg_index == 1 then + local x = line_state:getwordinfo(word_index) + local y = line_state:getwordinfo(word_index + 1) + if x and y then + local line = line_state:getline() + local s = line:sub(x.offset + x.length, y.offset - 1) + if not s:find("=") then + local color = settings.get("color.unexpected") or "" + local delta = s:find("[ \t]") + delta = delta and (delta - 1) or #s + local lastinfo = line_state:getwordinfo(line_state:getwordcount()) + local endoffset = lastinfo.offset + lastinfo.length + local tailoffset = x.offset + x.length + delta + if endoffset > tailoffset then + local tail = line:sub(endoffset):match("^([^&|]+)[&|]?.*$") or "" + endoffset = endoffset + #tail + end + classifications:applycolor(tailoffset, endoffset - tailoffset, color, true) + end + end + end + end + + doskey:chaincommand() + doskey:setclassifier(require_equal_sign) + +end +