From 7ec83d863be10ee7f70cc948a50c60d7cfc29dfa Mon Sep 17 00:00:00 2001 From: luozhiya Date: Wed, 12 Jun 2024 18:03:27 +0800 Subject: [PATCH] Adding `enable_completions` --- .editorconfig | 3 +- lua/fittencode/api.lua | 3 ++ lua/fittencode/engines/inline/init.lua | 57 ++++++++++++++++++++++++++ lua/fittencode/status.lua | 11 ++--- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/.editorconfig b/.editorconfig index 8b42503d..86b6b1e0 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,4 +1,4 @@ -# The language server has a built-in code formatter, courtesy of CppCXY/EmmyLuaCodeStyle +# The language server has a built-in code formatter, courtesy of CppCXY/EmmyLuaCodeStyle # https://luals.github.io/wiki/formatter/ # https://github.com/CppCXY/EmmyLuaCodeStyle/blob/master/lua.template.editorconfig @@ -10,3 +10,4 @@ insert_final_newline = true indent_style = space indent_size = 2 quote_style = single +max_line_length = 200 diff --git a/lua/fittencode/api.lua b/lua/fittencode/api.lua index 21f97242..e6dc86d5 100644 --- a/lua/fittencode/api.lua +++ b/lua/fittencode/api.lua @@ -32,6 +32,9 @@ M.api = { triggering_completion = function() InlineEngine.triggering_completion() end, + enable_completions = function(opts) + InlineEngine.enable_completions(opts) + end, ---@return boolean has_suggestions = function() return InlineEngine.has_suggestions() diff --git a/lua/fittencode/engines/inline/init.lua b/lua/fittencode/engines/inline/init.lua index dbff62cc..85803296 100644 --- a/lua/fittencode/engines/inline/init.lua +++ b/lua/fittencode/engines/inline/init.lua @@ -418,6 +418,63 @@ function M.is_inline_enabled() return true end +---@class EnableCompletionsOptions +---@field enable? boolean +---@field mode? 'inline' | 'source' | 'all' +---@field global? boolean +---@field suffixes? string[] +function M.enable_completions(opts) + if not opts then + return + end + local enable = opts.enable + local mode = opts.mode or 'inline' + local global = opts.global + local suffixes = opts.suffixes + global = global == nil and true or global + enable = enable == nil and true or enable + local _inline = function() + return mode == 'inline' or mode == 'all' + end + local _source = function() + return mode == 'source' or mode == 'all' + end + local _global = function() + if _inline() then + Config.options.inline_completion.enable = enable + end + if _source() then + Config.options.source_completion.enable = enable + end + end + local _suffixes = function(tbl, filters) + if enable then + tbl = vim.tbl_extend('force', tbl, filters) + else + tbl = vim.tbl_filter(function(ft) + return not vim.tbl_contains(filters, ft) + end, tbl) + end + end + local _local = function() + if _inline() then + _suffixes(Config.options.disable_specific_inline_completion.suffixes, suffixes) + else + _suffixes(Config.options.source_completion.disable_specific_source_completion.suffixes, suffixes) + end + end + if global then + _global() + else + _local() + end + if enable then + status:update(SC.IDLE) + else + status:update(SC.DISABLED) + end +end + ---@return integer function M.get_status() return status:get_current() diff --git a/lua/fittencode/status.lua b/lua/fittencode/status.lua index 95314009..436afdea 100644 --- a/lua/fittencode/status.lua +++ b/lua/fittencode/status.lua @@ -17,11 +17,12 @@ local M = {} ---@type StatusCodes local C = { - IDLE = 0, - GENERATING = 1, - ERROR = 2, - NO_MORE_SUGGESTIONS = 3, - SUGGESTIONS_READY = 4, + DISABLED = 1, + IDLE = 2, + GENERATING = 3, + ERROR = 4, + NO_MORE_SUGGESTIONS = 5, + SUGGESTIONS_READY = 6, } M.C = C