From 030149f10fd62e005738d4eda9eff93a6593b0ac Mon Sep 17 00:00:00 2001 From: luozhiya Date: Wed, 30 Oct 2024 17:22:38 +0800 Subject: [PATCH] Complete commands --- lua/fittencode/chat.lua | 2 +- lua/fittencode/command.lua | 47 +++++++++++++++++++++++++++++++++----- lua/fittencode/init.lua | 2 +- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lua/fittencode/chat.lua b/lua/fittencode/chat.lua index c767e78..05b11fa 100644 --- a/lua/fittencode/chat.lua +++ b/lua/fittencode/chat.lua @@ -666,7 +666,7 @@ local function reload_workspace_templates() end end -reload_builtin_templates() +-- reload_builtin_templates() local function reload_conversation_types() reload_builtin_templates() diff --git a/lua/fittencode/command.lua b/lua/fittencode/command.lua index d435289..24a81c2 100644 --- a/lua/fittencode/command.lua +++ b/lua/fittencode/command.lua @@ -1,25 +1,60 @@ local Client = require('fittencode.client') - --- reload_templates --- delete_all_chats +local Chat = require('fittencode.chat') +local Log = require('fittencode.log') local commands = { login = { - function() + execute = function() local username = vim.fn.input('Username/Email/Phone(+CountryCode): ') local password = vim.fn.inputsecret('Password: ') Client.login(username, password) - end }, + end + }, login3rd = { - function(source) Client.login3rd(source) end, + execute = function(source) Client.login3rd(source) end, complete = Client.login_providers }, + start_chat = { + execute = function() + Chat.start_chat() + end + }, + reload_templates = { + execute = function() + Chat.reload_templates() + end + }, + delete_all_chats = { + execute = function() + Chat.delete_all_chats() + end + }, } local function execute(input) end local function complete(arg_lead, cmd_line, cursor_pos) + local eles = vim.split(vim.trim(cmd_line), '%s+') + if cmd_line:sub(-1) == ' ' then + eles[#eles + 1] = '' + end + -- 1: FittenCode + table.remove(eles, 1) + -- action or nil + local prefix = table.remove(eles, 1) or '' + if #eles > 0 then + if commands[prefix] and commands[prefix].complete then + local next = table.remove(eles, 1) or '' + return vim.tbl_filter(function(key) + return key:find(next, 1, true) == 1 + end, commands[prefix].complete) + end + else + return vim.tbl_filter(function(key) + return key:find(prefix, 1, true) == 1 + end, vim.tbl_keys(commands)) + end end vim.api.nvim_create_user_command('FittenCode', function(input) diff --git a/lua/fittencode/init.lua b/lua/fittencode/init.lua index deaec61..79d953a 100644 --- a/lua/fittencode/init.lua +++ b/lua/fittencode/init.lua @@ -5,7 +5,7 @@ local M = {} function M.setup(opts) require('fittencode.config').setup(opts) require('fittencode.client').load_last_session() - require('fittencode.chat') + require('fittencode.command') end return setmetatable(M, {