diff --git a/lua/jquote/init.lua b/lua/jquote/init.lua index 0f11469..f29c246 100644 --- a/lua/jquote/init.lua +++ b/lua/jquote/init.lua @@ -1,7 +1,6 @@ --- JQuote: A Neovim plugin for cycling through quote characters --- @module jquote --- @author Jan ---- @version 1.0.0 --- @license MIT local M = {} @@ -24,7 +23,6 @@ M.options = vim.deepcopy(DEFAULT_OPTIONS) --- Plugin metadata --- @type table -M._version = "1.0.0" M._name = "jquote" --- Validates if a character is one of the configured quote characters @@ -35,7 +33,7 @@ local function is_defined_quote_char(char) if type(char) ~= "string" or #char ~= 1 then return false end - + for _, quote_char in ipairs(M.options.quote_chars) do if char == quote_char then return true @@ -58,14 +56,14 @@ local function find_quoted_string_at_cursor(line, cursor_col) --- @type table[] Array of quote position records local quote_positions = {} - + -- Scan line for quote characters and record their positions for i = 1, #line do local char = line:sub(i, i) if is_defined_quote_char(char) then - table.insert(quote_positions, { - char = char, - index = i - 1 -- Convert to 0-based indexing + table.insert(quote_positions, { + char = char, + index = i - 1 -- Convert to 0-based indexing }) end end @@ -75,13 +73,13 @@ local function find_quoted_string_at_cursor(line, cursor_col) local start_quote = quote_positions[i] for j = i + 1, #quote_positions do local end_quote = quote_positions[j] - + -- Match opening and closing quotes of same type if start_quote.char == end_quote.char then - if cursor_col >= start_quote.index and cursor_col <= end_quote.index then + if (cursor_col >= start_quote.index and cursor_col <= end_quote.index) or (cursor_col <= start_quote.index and cursor_col <= end_quote.index) then return start_quote.index, end_quote.index, start_quote.char end - + -- Skip to next pair to avoid nested matches i = j goto continue_outer_loop @@ -101,9 +99,9 @@ local function get_next_quote_char(current_char) if type(current_char) ~= "string" then return M.options.quote_chars[1] end - + local quote_count = #M.options.quote_chars - + for i = 1, quote_count do if M.options.quote_chars[i] == current_char then -- Cycle to next character (wrapping around to first if at end) @@ -111,7 +109,7 @@ local function get_next_quote_char(current_char) return M.options.quote_chars[next_index] end end - + -- Fallback to first quote character if current not found return M.options.quote_chars[1] end @@ -125,13 +123,13 @@ function M.toggle_quotes() vim.notify("JQuote: Failed to get current line", vim.log.levels.ERROR) return false end - + local success_cursor, cursor_pos = pcall(vim.api.nvim_win_get_cursor, 0) if not success_cursor or type(cursor_pos) ~= "table" or #cursor_pos < 2 then vim.notify("JQuote: Failed to get cursor position", vim.log.levels.ERROR) return false end - + local cursor_row, cursor_col = cursor_pos[1], cursor_pos[2] local start_idx, end_idx, current_char = find_quoted_string_at_cursor(current_line, cursor_col) @@ -160,7 +158,7 @@ function M.toggle_quotes() vim.notify("JQuote: Failed to update line", vim.log.levels.ERROR) return false end - + -- Restore cursor position pcall(vim.api.nvim_win_set_cursor, 0, { cursor_row, cursor_col }) return true @@ -175,27 +173,27 @@ local function validate_options(options) if type(options) ~= "table" then return false, "Options must be a table" end - + if options.hotkey and type(options.hotkey) ~= "string" then return false, "hotkey must be a string" end - + if options.quote_chars then if type(options.quote_chars) ~= "table" then return false, "quote_chars must be a table" end - + if #options.quote_chars == 0 then return false, "quote_chars cannot be empty" end - + for i, char in ipairs(options.quote_chars) do if type(char) ~= "string" or #char ~= 1 then return false, string.format("quote_chars[%d] must be a single character string", i) end end end - + return true, nil end @@ -204,23 +202,23 @@ end --- @return boolean success Whether setup completed successfully function M.setup(user_options) user_options = user_options or {} - + -- Validate user options local is_valid, error_msg = validate_options(user_options) if not is_valid then vim.notify(string.format("JQuote setup error: %s", error_msg), vim.log.levels.ERROR) return false end - + -- Merge with defaults using safe deep extend local success, merged_options = pcall(vim.tbl_deep_extend, "force", vim.deepcopy(DEFAULT_OPTIONS), user_options) if not success then vim.notify("JQuote: Failed to merge configuration options", vim.log.levels.ERROR) return false end - + M.options = merged_options - + -- Defensive check for quote_chars integrity if not M.options.quote_chars or #M.options.quote_chars == 0 then vim.notify("JQuote: quote_chars corrupted, restoring defaults", vim.log.levels.WARN) @@ -239,7 +237,7 @@ function M.setup(user_options) vim.notify(string.format("JQuote: Failed to set keymap for '%s'", M.options.hotkey), vim.log.levels.ERROR) return false end - + vim.notify(string.format("JQuote: Initialized with hotkey '%s'", M.options.hotkey), vim.log.levels.INFO) return true end @@ -248,12 +246,11 @@ end --- @return table health_info Plugin health information function M.health() local health_info = { - version = M._version, options = M.options, quote_chars_count = M.options.quote_chars and #M.options.quote_chars or 0, hotkey_configured = M.options.hotkey ~= nil, } - + return health_info end