From 9bec4cebf3f554b6f1befa00ff4234fffaff7e17 Mon Sep 17 00:00:00 2001
From: brianhuster <phambinhanctb2004@gmail.com>
Date: Fri, 8 Nov 2024 22:25:07 +0700
Subject: [PATCH] update

---
 lua/autosave.lua    | 18 ++++++++++++++++++
 plugin/autosave.vim | 12 +-----------
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/lua/autosave.lua b/lua/autosave.lua
index 9822cc7..2a618f8 100644
--- a/lua/autosave.lua
+++ b/lua/autosave.lua
@@ -44,6 +44,24 @@ function M.execute(args)
 	end
 end
 
+function M.cmdlineComplete(_A)
+	local ArgLead = _A[1]
+	local CmdLine = _A[2]
+	local subcommands = { "on", "off", "toggle", "status" }
+	local subcommand = vim.fn.split(CmdLine, " ")[2]
+	local completion = {}
+	if not subcommand then
+		completion = subcommands
+	elseif subcommand == ArgLead then
+		for _, value in ipairs(subcommands) do
+			if value:sub(1, #ArgLead) == ArgLead then
+				table.insert(completion, value)
+			end
+		end
+	end
+	return bool(vim.fn.has('nvim')) and completion or vim.list(completion)
+end
+
 --- Check if a buffer can be autosaved
 --- @return boolean
 function M.check_buffer()
diff --git a/plugin/autosave.vim b/plugin/autosave.vim
index 80b6d0c..41ebf85 100644
--- a/plugin/autosave.vim
+++ b/plugin/autosave.vim
@@ -26,17 +26,7 @@ augroup END
 command! -nargs=1 -complete=customlist,AutosaveComplete Autosave call luaeval("require'autosave'.execute(_A)", [<f-args>])
 
 function! AutosaveComplete(ArgLead, CmdLine, CursorPos) abort
-	let subcommands = ['on', 'off', 'toggle', 'status']
-	let cmd_parts = split(a:CmdLine, ' ')
-	if len(cmd_parts) < 2
-		return subcommands
-	endif
-	let subcommand = cmd_parts[1]
-	if empty(subcommand)
-		return subcommands
-	elseif subcommand == a:ArgLead
-		return filter(subcommands, 'v:val =~ "^' . a:ArgLead . '"')
-	endif
+	return luaeval("require'autosave'.cmdlineComplete(_A)", [a:ArgLead, a:CmdLine, a:CursorPos])
 endfunction
 
 set autowriteall