From ecfa2efbf62ada14171689c07bbb715cab05bed9 Mon Sep 17 00:00:00 2001 From: Shawon Date: Wed, 5 Feb 2025 21:00:37 +0600 Subject: [PATCH] refactor(cmp): Moved checks before `require()` statements Ref: #281 --- lua/cmp-markview.lua | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lua/cmp-markview.lua b/lua/cmp-markview.lua index 7684814..4a342ca 100644 --- a/lua/cmp-markview.lua +++ b/lua/cmp-markview.lua @@ -4,7 +4,15 @@ local source = {}; --- Is this source available? ---@return boolean function source:is_available() - local fts = require("markview.spec").get({ "preview", "filetypes" }, { fallback = {}, ignore_enable = true }); + if not package.loaded["markview.spec"] then + --- Plugin not available. + return false; + end + + local fts = require("markview.spec").get({ "preview", "filetypes" }, { + fallback = {}, + ignore_enable = true + }); return vim.list_contains(fts, vim.bo.ft); end @@ -17,7 +25,7 @@ end --- Characters that trigger the completion. ---@return string[] function source:get_trigger_characters() - return { "[", "!" } + return { "[", "!" }; end --- Completion items. @@ -25,17 +33,18 @@ end ---@param params table ---@param callback function function source:complete(params, callback) - local spec = require("markview.spec"); + if not package.loaded["markview.spec"] then + --- Plugin not available. + return; + end + + local spec = package.loaded["markview.spec"]; local before = params.context.cursor_before_line or ""; local after = params.context.cursor_after_line or ""; local comp = {}; - if vim.bo.ft ~= "markdown" then - goto not_md; - end - if before:match("^[ %>]+%s*%[%!%a*$") then ---+${func, Callout completion} local items = spec.get({ "markdown", "block_quotes" }, { fallback = {} }); @@ -101,8 +110,6 @@ function source:complete(params, callback) return; end - ::not_md:: - callback(comp); end