diff --git a/lua/cmp_tmux/source.lua b/lua/cmp_tmux/source.lua index ba99dd3..5a7eb1a 100644 --- a/lua/cmp_tmux/source.lua +++ b/lua/cmp_tmux/source.lua @@ -5,7 +5,6 @@ -- license: MIT -- -local config = require('cmp.config') local Tmux = require('cmp_tmux.tmux') local source = {} @@ -20,14 +19,13 @@ local default_config = { } local function create_config() + local config = require('cmp.config') local source_config = config.get_source_config('tmux') or {} return vim.tbl_extend('force', default_config, source_config.option or {}) end source.new = function() local self = setmetatable({}, { __index = source }) - self.config = create_config() - self.tmux = Tmux.new(self.config) return self end @@ -35,24 +33,38 @@ source.get_debug_name = function() return 'tmux' end +function source:config() + if self.config_impl == nil then + self.config_impl = create_config() + end + return self.config_impl +end + +function source:tmux() + if self.tmux_impl == nil then + self.tmux_impl = Tmux.new(self:config()) + end + return self.tmux_impl +end + function source:is_available() - return self.tmux:is_enabled() + return self:tmux():is_enabled() end function source:get_keyword_pattern() - return self.config.keyword_pattern + return self:config().keyword_pattern end function source:get_trigger_characters() local ft = vim.bo.filetype - local tcft = self.config.trigger_characters_ft[ft] - return tcft or self.config.trigger_characters + local tcft = self:config().trigger_characters_ft[ft] + return tcft or self:config().trigger_characters end function source:complete(request, callback) local word = string.sub(request.context.cursor_before_line, request.offset) - self.tmux:complete(word, function(words) + self:tmux():complete(word, function(words) if words == nil then return callback() end @@ -62,7 +74,7 @@ function source:complete(request, callback) word = w, label = w, labelDetails = { - detail = self.config.label, + detail = self:config().label, }, } end, words)