diff --git a/lua/img-clip/config.lua b/lua/img-clip/config.lua index 7c53a62..88b09da 100644 --- a/lua/img-clip/config.lua +++ b/lua/img-clip/config.lua @@ -4,6 +4,7 @@ M.config_file = "Default" M.sorted_files = {} M.sorted_dirs = {} M.configs = {} +M.api_opts = {} M.opts = {} local defaults = { @@ -205,7 +206,7 @@ local function get_custom_opt(key, opts, args) for _, config_opts in ipairs(opts["custom"]) do if config_opts["trigger"] and get_val(config_opts["trigger"]) then - return M.get_opt(key, {}, args, config_opts) + return M.get_opt(key, args, config_opts) end end end @@ -225,7 +226,7 @@ local function get_file_opt(key, opts, args, file) for _, config_file in ipairs(M.sorted_files) do if file_matches(file, config_file) or file_matches(file, vim.fn.resolve(vim.fn.expand(config_file))) then - return M.get_opt(key, {}, args, opts["files"][config_file]) + return M.get_opt(key, args, opts["files"][config_file]) end end @@ -247,7 +248,7 @@ local function get_dir_opt(key, opts, args, dir) for _, config_dir in ipairs(M.sorted_dirs) do if dir_matches(dir, config_dir) or dir_matches(dir, vim.fn.resolve(vim.fn.expand(config_dir))) then - return M.get_opt(key, {}, args, opts["dirs"][config_dir]) + return M.get_opt(key, args, opts["dirs"][config_dir]) end end @@ -276,22 +277,21 @@ local function get_unscoped_opt(key, opts) end ---@param key string: The key, may be nested (e.g. "default.debug") ----@param api_opts? table: The opts passed to pasteImage function ---@param args? table: Args that should be passed to the option function ----@param opts? table: The opts table to use instead of the config +---@param opts? table: Opts passed explicitly to the function ---@return string | nil -M.get_opt = function(key, api_opts, args, opts) - if api_opts then - local val = M.get_opt(key, nil, args, api_opts) - if val then - return get_val(val, args) +M.get_opt = function(key, args, opts) + -- use explicit opts if provided + -- otherwise, try to get the value from the api_opts + -- and then from the config file + if not opts then + local val = M.get_opt(key, args, M.api_opts) + if val ~= nil then + return val end + return M.get_opt(key, args, M.get_config()) end - -- if options are passed explicitly, use those instead of the config - -- otherwise use the config (either from file or neovim config) - opts = opts or M.get_config() - local val = get_custom_opt(key, opts, args) if val == nil then val = get_file_opt(key, opts, args, vim.fn.expand("%:p")) diff --git a/lua/img-clip/fs.lua b/lua/img-clip/fs.lua index 24978df..6e6b821 100644 --- a/lua/img-clip/fs.lua +++ b/lua/img-clip/fs.lua @@ -71,29 +71,28 @@ M.add_file_ext = function(str, ext) end ---@param ext string ----@param opts? table ---@return string -M.get_file_path = function(ext, opts) - local config_dir_path = config.get_opt("dir_path", opts) - local config_file_name = os.date(config.get_opt("file_name", opts)) +M.get_file_path = function(ext) + local config_dir_path = config.get_opt("dir_path") + local config_file_name = os.date(config.get_opt("file_name")) local dir_path = config_dir_path - if config.get_opt("relative_to_current_file", opts) then + if config.get_opt("relative_to_current_file") then local current_file_path = vim.fn.expand("%:.:h") if current_file_path ~= "." and current_file_path ~= "" then dir_path = current_file_path .. M.sep .. config_dir_path end end - if config.get_opt("use_absolute_path", opts) then + if config.get_opt("use_absolute_path") then dir_path = vim.fn.fnamemodify(dir_path, ":p") end dir_path = M.normalize_path(dir_path) local file_path - if config.get_opt("prompt_for_file_name", opts) then - if config.get_opt("show_dir_path_in_prompt", opts) then + if config.get_opt("prompt_for_file_name") then + if config.get_opt("show_dir_path_in_prompt") then local input_file_path = util.input({ prompt = "File path: ", default = dir_path, diff --git a/lua/img-clip/init.lua b/lua/img-clip/init.lua index 9871b54..979ed77 100644 --- a/lua/img-clip/init.lua +++ b/lua/img-clip/init.lua @@ -3,21 +3,23 @@ local paste = require("img-clip.paste") local M = {} ----@param opts? table -M.setup = function(opts) - config.setup(opts) +---@param config_opts? table +M.setup = function(config_opts) + config.setup(config_opts) end ----@param opts? table +---@param api_opts? table ---@param input? string -M.paste_image = function(opts, input) - return paste.paste_image(opts, input) +M.paste_image = function(api_opts, input) + config.api_opts = api_opts or {} + return paste.paste_image(input) end ----@param opts? table +---@param api_opts? table ---@param input? string -M.pasteImage = function(opts, input) - return paste.paste_image(opts, input) +M.pasteImage = function(api_opts, input) + config.api_opts = api_opts or {} + return paste.paste_image(input) end return M diff --git a/lua/img-clip/markup.lua b/lua/img-clip/markup.lua index d072fa5..39de124 100644 --- a/lua/img-clip/markup.lua +++ b/lua/img-clip/markup.lua @@ -58,9 +58,8 @@ M.url_encode = function(str) end ---@param file_path string the file path or base64 string ----@param opts? table ---@return boolean -function M.insert_markup(file_path, opts) +function M.insert_markup(file_path) local file_name = vim.fn.fnamemodify(file_path, ":t") local file_name_no_ext = vim.fn.fnamemodify(file_path, ":t:r") local label = file_name_no_ext:gsub("%s+", "-"):lower() @@ -83,13 +82,13 @@ function M.insert_markup(file_path, opts) cursor = "$CURSOR", label = label, } - local template = config.get_opt("template", opts, template_args) + local template = config.get_opt("template", template_args) if not template then return false end -- url encode path - if config.get_opt("url_encode_path", opts) then + if config.get_opt("url_encode_path") then file_path = M.url_encode(file_path) file_path = file_path:gsub("%%", "%%%%") -- escape % so we can call gsub again end @@ -99,7 +98,7 @@ function M.insert_markup(file_path, opts) template = template:gsub("$FILE_PATH", file_path) template = template:gsub("$LABEL", label) - if not config.get_opt("use_cursor_in_template", opts) then + if not config.get_opt("use_cursor_in_template") then template = template:gsub("$CURSOR", "") end @@ -117,7 +116,7 @@ function M.insert_markup(file_path, opts) vim.api.nvim_win_set_cursor(0, { new_row, new_col }) - if config.get_opt("insert_mode_after_paste", opts) and vim.api.nvim_get_mode().mode ~= "i" then + if config.get_opt("insert_mode_after_paste") and vim.api.nvim_get_mode().mode ~= "i" then if new_col == string.len(line) - 1 then vim.api.nvim_input("a") else @@ -129,9 +128,8 @@ function M.insert_markup(file_path, opts) end ---@param base64 string the file path or base64 string ----@param opts? table ---@return boolean -function M.insert_base64_markup(base64, opts) +function M.insert_base64_markup(base64) -- pass args to template local template_args = { file_path = "", @@ -140,7 +138,7 @@ function M.insert_base64_markup(base64, opts) cursor = "$CURSOR", label = "", } - local template = config.get_opt("template", opts, template_args) + local template = config.get_opt("template", template_args) if not template then return false end @@ -150,7 +148,7 @@ function M.insert_base64_markup(base64, opts) template = template:gsub("$FILE_PATH", base64) template = template:gsub("$LABEL", "") - if not config.get_opt("use_cursor_in_template", opts) then + if not config.get_opt("use_cursor_in_template") then template = template:gsub("$CURSOR", "") end @@ -168,7 +166,7 @@ function M.insert_base64_markup(base64, opts) vim.api.nvim_win_set_cursor(0, { new_row, new_col }) - if config.get_opt("insert_mode_after_paste", opts) and vim.api.nvim_get_mode().mode ~= "i" then + if config.get_opt("insert_mode_after_paste") and vim.api.nvim_get_mode().mode ~= "i" then if new_col == string.len(line) - 1 then vim.api.nvim_input("a") else diff --git a/lua/img-clip/paste.lua b/lua/img-clip/paste.lua index d9931b6..9f968dd 100644 --- a/lua/img-clip/paste.lua +++ b/lua/img-clip/paste.lua @@ -6,18 +6,17 @@ local fs = require("img-clip.fs") local M = {} ----@param opts? table ---@param input? string file path or url ---@return boolean -M.paste_image = function(opts, input) +M.paste_image = function(input) -- check if input (file path or url) is provided if input then input = util.sanitize_input(input) if util.is_image_url(input) then - return M.paste_image_from_url(input, opts) + return M.paste_image_from_url(input) elseif util.is_image_path(input) then - return M.paste_image_from_path(input, opts) + return M.paste_image_from_path(input) end util.warn("Content is not an image.") @@ -32,14 +31,14 @@ M.paste_image = function(opts, input) -- if no input is provided, check clipboard content if clipboard.content_is_image() then - return M.paste_image_from_clipboard(opts) + return M.paste_image_from_clipboard() end -- if clipboard does not contain an image, then get the -- clipboard content as text and check attempt to paste it local clipboard_content = clipboard.get_content() if clipboard_content then - return M.paste_image(opts, clipboard_content) + return M.paste_image(clipboard_content) end util.warn("Content is not an image.") @@ -47,8 +46,7 @@ M.paste_image = function(opts, input) end ---@param url string ----@param opts? table -M.paste_image_from_url = function(url, opts) +M.paste_image_from_url = function(url) if not config.get_opt("drag_and_drop.download_images") then if not markup.insert_markup(url) then util.error("Could not insert markup code.") @@ -58,7 +56,7 @@ M.paste_image_from_url = function(url, opts) return true end - local file_path = fs.get_file_path("png", opts) + local file_path = fs.get_file_path("png") if not file_path then util.error("Could not determine file path.") return false @@ -97,10 +95,9 @@ M.paste_image_from_url = function(url, opts) end ---@param src_path string ----@param opts? table -M.paste_image_from_path = function(src_path, opts) - if config.get_opt("embed_image_as_base64", opts) then - if M.embed_image_as_base64(src_path, opts) then +M.paste_image_from_path = function(src_path) + if config.get_opt("embed_image_as_base64") then + if M.embed_image_as_base64(src_path) then return true end end @@ -114,7 +111,7 @@ M.paste_image_from_path = function(src_path, opts) return true end - local file_path = fs.get_file_path("png", opts) + local file_path = fs.get_file_path("png") if not file_path then util.error("Could not determine file path.") return false @@ -145,15 +142,14 @@ M.paste_image_from_path = function(src_path, opts) return true end ----@param opts? table -M.paste_image_from_clipboard = function(opts) - if config.get_opt("embed_image_as_base64", opts) then - if M.embed_image_as_base64(nil, opts) then +M.paste_image_from_clipboard = function() + if config.get_opt("embed_image_as_base64") then + if M.embed_image_as_base64(nil) then return true end end - local file_path = fs.get_file_path("png", opts) + local file_path = fs.get_file_path("png") if not file_path then util.error("Could not determine file path.") return false @@ -185,8 +181,7 @@ M.paste_image_from_clipboard = function(opts) end ---@param file_path? string ----@param opts? table -M.embed_image_as_base64 = function(file_path, opts) +M.embed_image_as_base64 = function(file_path) local ft = vim.bo.filetype if not M.lang_supports_base64(ft) then util.warn("Filetype " .. ft .. " does not support base64 encoding.") @@ -207,14 +202,14 @@ M.embed_image_as_base64 = function(file_path, opts) -- check if base64 string is too long (max_base64_size is in KB) local base64_size_kb = math.floor((string.len(base64) * 6) / (8 * 1024)) - local max_size_kb = config.get_opt("max_base64_size", opts) + local max_size_kb = config.get_opt("max_base64_size") if base64_size_kb > max_size_kb then util.warn("Base64 string is too large (" .. base64_size_kb .. " KB). Max allowed size is " .. max_size_kb .. " KB.") return false end local prefix = M.get_base64_prefix() - if not markup.insert_base64_markup(prefix .. base64, opts) then + if not markup.insert_base64_markup(prefix .. base64) then util.error("Could not insert markup code.") return false end diff --git a/tests/config_spec.lua b/tests/config_spec.lua index 2986efe..3a5d583 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -56,27 +56,27 @@ describe("config", function() end) it("should prioritize API options over config values", function() - assert.equals("custom-filename", config.get_opt("file_name", { file_name = "custom-filename" })) - assert.equals("some-dir-path", config.get_opt("dir_path", { dir_path = "some-dir-path" })) + config.api_opts = { dir_path = "some-dir-path", file_name = "custom-filename" } + assert.equals("custom-filename", config.get_opt("file_name")) + assert.equals("some-dir-path", config.get_opt("dir_path")) + config.api_opts = {} end) it("should execute functions that are passed in the API", function() - assert.equals( - 42, - config.get_opt("life", { - life = function() - return 40 + 2 - end, - }) - ) + config.api_opts = { + life = function() + return 42 + end, + } + assert.equals(42, config.get_opt("life")) + config.api_opts = {} end) it("should allow nested API options", function() vim.bo.filetype = "markdown" - assert.equals( - "markdown-template", - config.get_opt("template", { filetypes = { markdown = { template = "markdown-template" } } }) - ) + config.api_opts = { filetypes = { markdown = { template = "markdown-template" } } } + assert.equals("markdown-template", config.get_opt("template")) + config.api_opts = {} end) end) diff --git a/tests/paste_spec.lua b/tests/paste_spec.lua index 7d0b273..0af1ce7 100644 --- a/tests/paste_spec.lua +++ b/tests/paste_spec.lua @@ -1,5 +1,6 @@ local clipboard = require("img-clip.clipboard") local paste = require("img-clip.paste") +local plugin = require("img-clip") local config = require("img-clip.config") local util = require("img-clip.util") local spy = require("luassert.spy") @@ -44,7 +45,7 @@ describe("paste", function() spy.on(paste, "paste_image_from_path") - paste.paste_image({}, "/home/user/Pictures/image.png") + plugin.paste_image({}, "/home/user/Pictures/image.png") assert.spy(paste.paste_image_from_path).was_called() paste.paste_image_from_url = function() @@ -53,7 +54,7 @@ describe("paste", function() spy.on(paste, "paste_image_from_url") - paste.paste_image({}, "https://example.com/image.png") + plugin.paste_image({}, "https://example.com/image.png") assert.spy(paste.paste_image_from_url).was_called() end)