From d25f5840e648ee64302e407935d4548f22aed129 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Mon, 6 May 2024 02:06:21 +0300 Subject: [PATCH] refactor: file structure --- doc/tags | 40 ++--- doc/text-transform.txt | 159 ++++++++++-------- lua/text-transform/commands.lua | 33 +++- lua/text-transform/config.lua | 49 +++--- lua/text-transform/init.lua | 2 +- lua/text-transform/main.lua | 2 +- lua/text-transform/popup.lua | 14 -- .../{popup_common.lua => popup/common.lua} | 2 +- lua/text-transform/popup/init.lua | 20 +++ lua/text-transform/{ => popup}/select.lua | 8 +- lua/text-transform/{ => popup}/telescope.lua | 4 +- lua/text-transform/replacers.lua | 56 +++--- lua/text-transform/state.lua | 60 ++----- lua/text-transform/transformers.lua | 46 ++--- lua/text-transform/{util => utils}/debug.lua | 0 lua/text-transform/{util => utils}/init.lua | 18 -- tests/helpers.lua | 1 + tests/test_config.lua | 5 - tests/test_popups.lua | 4 +- 19 files changed, 259 insertions(+), 264 deletions(-) delete mode 100644 lua/text-transform/popup.lua rename lua/text-transform/{popup_common.lua => popup/common.lua} (97%) create mode 100644 lua/text-transform/popup/init.lua rename lua/text-transform/{ => popup}/select.lua (52%) rename lua/text-transform/{ => popup}/telescope.lua (97%) rename lua/text-transform/{util => utils}/debug.lua (100%) rename lua/text-transform/{util => utils}/init.lua (50%) diff --git a/doc/tags b/doc/tags index f74aace..a528cf9 100644 --- a/doc/tags +++ b/doc/tags @@ -1,25 +1,25 @@ -TextTransform.config text-transform.txt /*TextTransform.config* -TextTransform.get_visual_selection_details() text-transform.txt /*TextTransform.get_visual_selection_details()* -TextTransform.init_commands() text-transform.txt /*TextTransform.init_commands()* -TextTransform.replace_columns() text-transform.txt /*TextTransform.replace_columns()* -TextTransform.replace_selection() text-transform.txt /*TextTransform.replace_selection()* -TextTransform.replace_word() text-transform.txt /*TextTransform.replace_word()* -TextTransform.setup() text-transform.txt /*TextTransform.setup()* -TextTransform.to_camel_case() text-transform.txt /*TextTransform.to_camel_case()* -TextTransform.to_const_case() text-transform.txt /*TextTransform.to_const_case()* -TextTransform.to_dot_case() text-transform.txt /*TextTransform.to_dot_case()* -TextTransform.to_kebab_case() text-transform.txt /*TextTransform.to_kebab_case()* -TextTransform.to_pascal_case() text-transform.txt /*TextTransform.to_pascal_case()* -TextTransform.to_snake_case() text-transform.txt /*TextTransform.to_snake_case()* -TextTransform.to_title_case() text-transform.txt /*TextTransform.to_title_case()* -TextTransform.to_words() text-transform.txt /*TextTransform.to_words()* -TextTransform.transform_words() text-transform.txt /*TextTransform.transform_words()* +commands.init_commands() text-transform.txt /*commands.init_commands()* +commands.init_keymaps() text-transform.txt /*commands.init_keymaps()* +config.config text-transform.txt /*config.config* +config.setup() text-transform.txt /*config.setup()* find_word_boundaries() text-transform.txt /*find_word_boundaries()* -init() text-transform.txt /*init()* -state.enable() text-transform.txt /*state.enable()* +popup.show_popup() text-transform.txt /*popup.show_popup()* +replacers.get_visual_selection_details() text-transform.txt /*replacers.get_visual_selection_details()* +replacers.replace_columns() text-transform.txt /*replacers.replace_columns()* +replacers.replace_range() text-transform.txt /*replacers.replace_range()* +replacers.replace_selection() text-transform.txt /*replacers.replace_selection()* +replacers.replace_word() text-transform.txt /*replacers.replace_word()* +select.select_popup() text-transform.txt /*select.select_popup()* state.restore_positions() text-transform.txt /*state.restore_positions()* state.save_positions() text-transform.txt /*state.save_positions()* -state.toggle() text-transform.txt /*state.toggle()* telescope.telescope_popup() text-transform.txt /*telescope.telescope_popup()* -utils.dump() text-transform.txt /*utils.dump()* +transformers.to_camel_case() text-transform.txt /*transformers.to_camel_case()* +transformers.to_const_case() text-transform.txt /*transformers.to_const_case()* +transformers.to_dot_case() text-transform.txt /*transformers.to_dot_case()* +transformers.to_kebab_case() text-transform.txt /*transformers.to_kebab_case()* +transformers.to_pascal_case() text-transform.txt /*transformers.to_pascal_case()* +transformers.to_snake_case() text-transform.txt /*transformers.to_snake_case()* +transformers.to_title_case() text-transform.txt /*transformers.to_title_case()* +transformers.to_words() text-transform.txt /*transformers.to_words()* +transformers.transform_words() text-transform.txt /*transformers.transform_words()* utils.merge() text-transform.txt /*utils.merge()* diff --git a/doc/text-transform.txt b/doc/text-transform.txt index dd3d19c..ace3c81 100644 --- a/doc/text-transform.txt +++ b/doc/text-transform.txt @@ -1,20 +1,26 @@ ============================================================================== ------------------------------------------------------------------------------ - *TextTransform.init_commands()* - `TextTransform.init_commands`() + *commands.init_commands()* + `commands.init_commands`() Initializes user commands @private +------------------------------------------------------------------------------ + *commands.init_keymaps()* + `commands.init_keymaps`() +Initializes user keymaps +@private + ============================================================================== ------------------------------------------------------------------------------ - *TextTransform.config* - `TextTransform.config` + *config.config* + `config.config` Your plugin configuration with its default values. Default values: > - TextTransform.config = { + config.config = { --- Prints information about internals of the plugin. Very verbose, only useful for debugging. debug = false, --- Keymap configurations @@ -55,13 +61,8 @@ Default values: < ------------------------------------------------------------------------------ - *init()* - `init`() -@internal - ------------------------------------------------------------------------------- - *TextTransform.setup()* - `TextTransform.setup`({options}) + *config.setup()* + `config.setup`({options}) Define your text-transform setup. Parameters ~ @@ -81,8 +82,20 @@ Finds the boundaries of the surrounding word around `start_col` within `line`. @return number start_col, number end_col ------------------------------------------------------------------------------ - *TextTransform.replace_word()* - `TextTransform.replace_word`({transform_name}, {position}) + *replacers.replace_range()* +`replacers.replace_range`({start_line}, {start_col}, {end_line}, {end_col}, {transform_name}) +Replace the range between the given positions with the given transform. +Acts on the lines between the given positions, replacing the text between the given columns. + +@param start_line number The starting line +@param start_col number The starting column +@param end_line number The ending line +@param end_col number The ending column +@param transform_name string The transformer name + +------------------------------------------------------------------------------ + *replacers.replace_word()* + `replacers.replace_word`({transform_name}, {position}) Replace the word under the cursor with the given transform. If `position` is provided, replace the word under the given position. Otherwise, attempts to find the word under the cursor. @@ -91,14 +104,16 @@ Otherwise, attempts to find the word under the cursor. @param position table|nil A table containing the position of the word to replace ------------------------------------------------------------------------------ - *TextTransform.replace_columns()* - `TextTransform.replace_columns`({transform_name}) + *replacers.replace_columns()* + `replacers.replace_columns`({transform_name}) Replaces each column in visual block mode selection with the given transform. Assumes that the each selection is 1 character and operates on the whole word under each cursor. +@param transform_name string The transformer name + ------------------------------------------------------------------------------ - *TextTransform.replace_selection()* - `TextTransform.replace_selection`({transform_name}) + *replacers.replace_selection()* + `replacers.replace_selection`({transform_name}) Replaces a selection with the given transform. This function attempts to infer the replacement type based on the cursor positiono and visual selections, and passes information to relevant range replacement functions. @@ -106,8 +121,8 @@ range replacement functions. @param transform_name string The transformer name ------------------------------------------------------------------------------ - *TextTransform.get_visual_selection_details()* - `TextTransform.get_visual_selection_details`() + *replacers.get_visual_selection_details()* + `replacers.get_visual_selection_details`() Takes the saved positions and translates them into individual visual ranges, regardless of how the original selection was performed. @@ -116,23 +131,10 @@ the full information around the selection logic. ============================================================================== ------------------------------------------------------------------------------- - *state.toggle()* - `state.toggle`() -Toggle the plugin by calling the `enable`/`disable` methods respectively. -@private - ------------------------------------------------------------------------------- - *state.enable()* - `state.enable`() -Enables the plugin -@private - ------------------------------------------------------------------------------ *state.save_positions()* `state.save_positions`() Save the current cursor position, mode, and visual selection ranges -@private ------------------------------------------------------------------------------ *state.restore_positions()* @@ -143,27 +145,15 @@ or a given modified state, if passed as the first argument ============================================================================== ------------------------------------------------------------------------------ - *telescope.telescope_popup()* - `telescope.telescope_popup`() -Pops up a telescope menu, containing the available case transformers. -When a transformer is selected, the cursor position/range/columns will be used to replace the -words around the cursor or inside the selection. - -The cursor positions/ranges are saved before opening the menu and restored once a selection is -made. - - -============================================================================== ------------------------------------------------------------------------------- - *TextTransform.to_words()* - `TextTransform.to_words`({string}) + *transformers.to_words()* + `transformers.to_words`({string}) Splits a string into words. @param string string @return table ------------------------------------------------------------------------------ - *TextTransform.transform_words()* - `TextTransform.transform_words`({words}, {with_word_cb}, {separator}) + *transformers.transform_words()* + `transformers.transform_words`({words}, {with_word_cb}, {separator}) Transforms a table of strings into a string using a callback and separator. The callback is called with the word, the index, and the table of words. The separator is added between each word. @@ -174,55 +164,91 @@ The separator is added between each word. @return string ------------------------------------------------------------------------------ - *TextTransform.to_camel_case()* - `TextTransform.to_camel_case`({string}) + *transformers.to_camel_case()* + `transformers.to_camel_case`({string}) Transforms a string into camelCase. @param string string @return string ------------------------------------------------------------------------------ - *TextTransform.to_snake_case()* - `TextTransform.to_snake_case`({string}) + *transformers.to_snake_case()* + `transformers.to_snake_case`({string}) Transfroms a string into snake_case. @param string any @return string ------------------------------------------------------------------------------ - *TextTransform.to_pascal_case()* - `TextTransform.to_pascal_case`({string}) + *transformers.to_pascal_case()* + `transformers.to_pascal_case`({string}) Transforms a string into PascalCase. @param string string @return string ------------------------------------------------------------------------------ - *TextTransform.to_title_case()* - `TextTransform.to_title_case`({string}) + *transformers.to_title_case()* + `transformers.to_title_case`({string}) Transforms a string into Title Case. @param string string @return string ------------------------------------------------------------------------------ - *TextTransform.to_kebab_case()* - `TextTransform.to_kebab_case`({string}) + *transformers.to_kebab_case()* + `transformers.to_kebab_case`({string}) Transforms a string into kebab-case. @param string string @return string ------------------------------------------------------------------------------ - *TextTransform.to_dot_case()* - `TextTransform.to_dot_case`({string}) + *transformers.to_dot_case()* + `transformers.to_dot_case`({string}) Transforms a string into dot.case. @param string string @return string ------------------------------------------------------------------------------ - *TextTransform.to_const_case()* - `TextTransform.to_const_case`({string}) + *transformers.to_const_case()* + `transformers.to_const_case`({string}) Transforms a string into CONSTANT_CASE. @param string string @return string +============================================================================== +------------------------------------------------------------------------------ + *popup.show_popup()* + `popup.show_popup`() +Pops up a selection menu, containing the available case transformers. +When a transformer is selected, the cursor position/range/columns will be used to replace the +words around the cursor or inside the selection. + +The cursor positions/ranges are saved before opening the menu and restored once a selection is +made. + + +============================================================================== +------------------------------------------------------------------------------ + *select.select_popup()* + `select.select_popup`() +Pops up a selection menu, containing the available case transformers. +When a transformer is selected, the cursor position/range/columns will be used to replace the +words around the cursor or inside the selection. + +The cursor positions/ranges are saved before opening the menu and restored once a selection is +made. + + +============================================================================== +------------------------------------------------------------------------------ + *telescope.telescope_popup()* + `telescope.telescope_popup`() +Pops up a telescope menu, containing the available case transformers. +When a transformer is selected, the cursor position/range/columns will be used to replace the +words around the cursor or inside the selection. + +The cursor positions/ranges are saved before opening the menu and restored once a selection is +made. + + ============================================================================== ------------------------------------------------------------------------------ *utils.merge()* @@ -236,12 +262,5 @@ TODO accept multiple tables to merge @param t2 table @return table ------------------------------------------------------------------------------- - *utils.dump()* - `utils.dump`({obj}) -Dumps the object into a string. -@param obj any -@return string - vim:tw=78:ts=8:noet:ft=help:norl: \ No newline at end of file diff --git a/lua/text-transform/commands.lua b/lua/text-transform/commands.lua index ec188eb..a98e701 100644 --- a/lua/text-transform/commands.lua +++ b/lua/text-transform/commands.lua @@ -1,14 +1,15 @@ --- local D = require("text-transform.util.debug") -local util = require("text-transform.util") +local D = require("text-transform.utils.debug") +local util = require("text-transform.utils") local state = require("text-transform.state") local replacers = require("text-transform.replacers") local popup = require("text-transform.popup") -local common = require("text-transform.popup_common") -local TextTransform = {} +local common = require("text-transform.popup.common") + +local commands = {} --- Initializes user commands --- @private -function TextTransform.init_commands() +function commands.init_commands() local map = { TtCamel = "camel_case", TtConst = "const_case", @@ -40,11 +41,11 @@ function TextTransform.init_commands() -- specific popups vim.api.nvim_create_user_command("TtTelescope", function() - local telescope = require("text-transform.telescope") + local telescope = require("text-transform.popup.telescope") telescope.telescope_popup() end, opts("Change Case with Telescope")) vim.api.nvim_create_user_command("TtSelect", function() - local select = require("text-transform.select") + local select = require("text-transform.popup.select") select.select_popup() end, opts("Change Case with Select")) @@ -52,4 +53,20 @@ function TextTransform.init_commands() vim.api.nvim_create_user_command("TextTransform", popup.show_popup, opts("Change Case")) end -return TextTransform +--- Initializes user keymaps +--- @private +function commands.init_keymaps() + local keymaps = _G.TextTransform.config.keymap + D.log("init_keymaps", "Initializing keymaps, config %s", vim.inspect(_G.TextTransform)) + if keymaps.telescope_popup then + local keys = keymaps.telescope_popup + if keys.n then + vim.keymap.set("n", keys.n, popup.show_popup, { silent = true, desc = "Change Case" }) + end + if keys.v then + vim.keymap.set("v", keys.v, popup.show_popup, { silent = true, desc = "Change Case" }) + end + end +end + +return commands diff --git a/lua/text-transform/config.lua b/lua/text-transform/config.lua index 3cfc1a5..6813c79 100644 --- a/lua/text-transform/config.lua +++ b/lua/text-transform/config.lua @@ -1,14 +1,28 @@ -local popup = require("text-transform.popup") local commands = require("text-transform.commands") -local D = require("text-transform.util.debug") -local utils = require("text-transform.util") -local TextTransform = {} +local D = require("text-transform.utils.debug") +local utils = require("text-transform.utils") +local config = {} + +local function ensure_config() + -- when the config is not set to the global object, we set it + if _G.TextTransform.config == nil then + _G.TextTransform.config = config.config + end +end + +local function init() + ensure_config() + local o = config.config + D.log("config", "Initializing TextTransform with %s", vim.inspect(o)) + commands.init_commands() + commands.init_keymaps() +end --- Your plugin configuration with its default values. --- --- Default values: ---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section) -TextTransform.config = { +config.config = { --- Prints information about internals of the plugin. Very verbose, only useful for debugging. debug = false, --- Keymap configurations @@ -46,32 +60,15 @@ TextTransform.config = { popup_type = "telescope", } ---- @internal -local function init() - local o = TextTransform.config - D.log("config", "Initializing TextTransform with %s", utils.dump(o)) - commands.init_commands() - - if o.keymap.telescope_popup then - local keys = o.keymap.telescope_popup - if keys.n then - vim.keymap.set("n", keys.n, popup.show_popup, { silent = true, desc = "Change Case" }) - end - if keys.v then - vim.keymap.set("v", keys.v, popup.show_popup, { silent = true, desc = "Change Case" }) - end - end -end - --- Define your text-transform setup. --- ---@param options table Module config table. See |TextTransform.options|. --- ---@usage `require("text-transform").setup()` (add `{}` with your |TextTransform.options| table) -function TextTransform.setup(options) +function config.setup(options) options = options or {} - TextTransform.config = utils.merge(TextTransform.config, options) + config.config = utils.merge(config.config, options) if vim.api.nvim_get_vvar("vim_did_enter") == 0 then vim.defer_fn(function() @@ -81,7 +78,7 @@ function TextTransform.setup(options) init() end - return TextTransform.config + return config.config end -return TextTransform +return config diff --git a/lua/text-transform/init.lua b/lua/text-transform/init.lua index d5be7a7..67ebe88 100644 --- a/lua/text-transform/init.lua +++ b/lua/text-transform/init.lua @@ -1,5 +1,5 @@ local M = require("text-transform.main") -local utils = require("text-transform.util") +local utils = require("text-transform.utils") local TextTransform = {} diff --git a/lua/text-transform/main.lua b/lua/text-transform/main.lua index 034c183..f97d0e2 100644 --- a/lua/text-transform/main.lua +++ b/lua/text-transform/main.lua @@ -1,4 +1,4 @@ -local utils = require("text-transform.util") +local utils = require("text-transform.utils") local tt = require("text-transform.transformers") local replacers = require("text-transform.replacers") local state = require("text-transform.state") diff --git a/lua/text-transform/popup.lua b/lua/text-transform/popup.lua deleted file mode 100644 index b23b025..0000000 --- a/lua/text-transform/popup.lua +++ /dev/null @@ -1,14 +0,0 @@ -local popup = {} - -function popup.show_popup() - local config = _G.TextTransform.config - if config.popup_type == "telescope" then - local telescope = require("text-transform.telescope") - telescope.telescope_popup() - else - local select = require("text-transform.select") - select.select_popup() - end -end - -return popup diff --git a/lua/text-transform/popup_common.lua b/lua/text-transform/popup/common.lua similarity index 97% rename from lua/text-transform/popup_common.lua rename to lua/text-transform/popup/common.lua index 8693cde..9964976 100644 --- a/lua/text-transform/popup_common.lua +++ b/lua/text-transform/popup/common.lua @@ -1,4 +1,4 @@ -local D = require("text-transform.util.debug") +local D = require("text-transform.utils.debug") local state = require("text-transform.state") local replacers = require("text-transform.replacers") diff --git a/lua/text-transform/popup/init.lua b/lua/text-transform/popup/init.lua new file mode 100644 index 0000000..0a5f61a --- /dev/null +++ b/lua/text-transform/popup/init.lua @@ -0,0 +1,20 @@ +local popup = {} + +--- Pops up a selection menu, containing the available case transformers. +--- When a transformer is selected, the cursor position/range/columns will be used to replace the +--- words around the cursor or inside the selection. +--- +--- The cursor positions/ranges are saved before opening the menu and restored once a selection is +--- made. +function popup.show_popup() + local config = _G.TextTransform.config + if config.popup_type == "telescope" then + local telescope = require("text-transform.popup.telescope") + telescope.telescope_popup() + else + local select = require("text-transform.popup.select") + select.select_popup() + end +end + +return popup diff --git a/lua/text-transform/select.lua b/lua/text-transform/popup/select.lua similarity index 52% rename from lua/text-transform/select.lua rename to lua/text-transform/popup/select.lua index 56c813b..6ec72fd 100644 --- a/lua/text-transform/select.lua +++ b/lua/text-transform/popup/select.lua @@ -1,8 +1,14 @@ -local common = require("text-transform.popup_common") +local common = require("text-transform.popup.common") local state = require("text-transform.state") local select = {} +--- Pops up a selection menu, containing the available case transformers. +--- When a transformer is selected, the cursor position/range/columns will be used to replace the +--- words around the cursor or inside the selection. +--- +--- The cursor positions/ranges are saved before opening the menu and restored once a selection is +--- made. function select.select_popup() common.load_frequency() state.save_positions() diff --git a/lua/text-transform/telescope.lua b/lua/text-transform/popup/telescope.lua similarity index 97% rename from lua/text-transform/telescope.lua rename to lua/text-transform/popup/telescope.lua index 503c8a9..359a92a 100644 --- a/lua/text-transform/telescope.lua +++ b/lua/text-transform/popup/telescope.lua @@ -1,5 +1,5 @@ -local common = require("text-transform.popup_common") -local D = require("text-transform.util.debug") +local common = require("text-transform.popup.common") +local D = require("text-transform.utils.debug") local state = require("text-transform.state") local pickers = require("telescope.pickers") diff --git a/lua/text-transform/replacers.lua b/lua/text-transform/replacers.lua index a9d8578..620a8a7 100644 --- a/lua/text-transform/replacers.lua +++ b/lua/text-transform/replacers.lua @@ -1,9 +1,9 @@ -local D = require("text-transform.util.debug") +local D = require("text-transform.utils.debug") local state = require("text-transform.state") -local utils = require("text-transform.util") +local utils = require("text-transform.utils") local t = require("text-transform.transformers") -local TextTransform = {} +local replacers = {} --- Finds the boundaries of the surrounding word around `start_col` within `line`. --- @param line number @@ -29,7 +29,15 @@ local function find_word_boundaries(line, start_col) return word_start_col, word_end_col end -function TextTransform.replace_range(start_line, start_col, end_line, end_col, transform_name) +--- Replace the range between the given positions with the given transform. +--- Acts on the lines between the given positions, replacing the text between the given columns. +--- +--- @param start_line number The starting line +--- @param start_col number The starting column +--- @param end_line number The ending line +--- @param end_col number The ending column +--- @param transform_name string The transformer name +function replacers.replace_range(start_line, start_col, end_line, end_col, transform_name) D.log("replace_range", "Replacing range with %s", transform_name) local transform = t["to_" .. transform_name] local lines = vim.fn.getline(start_line, end_line) --- @type any @@ -61,7 +69,7 @@ end --- --- @param transform_name string The transformer name --- @param position table|nil A table containing the position of the word to replace -function TextTransform.replace_word(transform_name, position) +function replacers.replace_word(transform_name, position) D.log("replace_word", "Replacing word with %s", transform_name) local word, line, col, start_col, end_col if not position then @@ -79,17 +87,19 @@ function TextTransform.replace_word(transform_name, position) if not position then vim.cmd("normal ciw" .. transformed) else - TextTransform.replace_range(line, start_col, line, end_col, transform_name) + replacers.replace_range(line, start_col, line, end_col, transform_name) end end --- Replaces each column in visual block mode selection with the given transform. --- Assumes that the each selection is 1 character and operates on the whole word under each cursor. -function TextTransform.replace_columns(transform_name) - local selections = TextTransform.get_visual_selection_details() +--- +--- @param transform_name string The transformer name +function replacers.replace_columns(transform_name) + local selections = replacers.get_visual_selection_details() D.log("replace_columns", "Replacing columns with %s", transform_name) for _, sel in ipairs(selections) do - TextTransform.replace_word(transform_name, { 0, sel.start_line, sel.start_col, 0 }) + replacers.replace_word(transform_name, { 0, sel.start_line, sel.start_col, 0 }) end end @@ -98,13 +108,13 @@ end --- range replacement functions. --- --- @param transform_name string The transformer name -function TextTransform.replace_selection(transform_name) +function replacers.replace_selection(transform_name) D.log("replace_selection", "Replacing selection with %s", transform_name) -- determine if cursor is a 1-width column across multiple lines or a normal selection -- local start_line, start_col, end_line, end_col = unpack(vim.fn.getpos("'<")) - local selections = TextTransform.get_visual_selection_details() + local selections = replacers.get_visual_selection_details() - D.log("replace_selection", "Selections: %s", utils.dump(selections)) + D.log("replace_selection", "Selections: %s", vim.inspect(selections)) local is_multiline = #selections > 1 local is_column = is_multiline and selections[1].start_col == selections[#selections].end_col local is_single_cursor = not is_multiline @@ -122,12 +132,12 @@ function TextTransform.replace_selection(transform_name) ) if is_single_cursor then - TextTransform.replace_word(transform_name) + replacers.replace_word(transform_name) elseif is_column then - TextTransform.replace_columns(transform_name) + replacers.replace_columns(transform_name) else for _, sel in pairs(selections) do - TextTransform.replace_range( + replacers.replace_range( sel.start_line, sel.start_col, sel.end_line, @@ -143,7 +153,7 @@ end --- --- This allows to treat all ranges equally and allows to work on each selection without knowing --- the full information around the selection logic. -function TextTransform.get_visual_selection_details() +function replacers.get_visual_selection_details() if not state.positions then D.log("get_visual_selection_details", "No positions saved") return {} @@ -152,8 +162,8 @@ function TextTransform.get_visual_selection_details() "get_visual_selection_details", "Getting visual selection details - mode: %s, is_visual: %s, is_block: %s", state.positions.mode, - utils.is_visual_mode(), - utils.is_block_visual_mode() + state.is_visual_mode(), + state.is_block_visual_mode() ) -- Get the start and end positions of the selection @@ -164,8 +174,8 @@ function TextTransform.get_visual_selection_details() -- Check if currently in visual mode; if not, return the cursor position if - not utils.is_visual_mode() - and not utils.is_block_visual_mode() + not state.is_visual_mode() + and not state.is_block_visual_mode() and not state.has_range(start_pos, end_pos) then local pos = state.positions.pos @@ -187,7 +197,7 @@ function TextTransform.get_visual_selection_details() end -- If it's block visual mode, return table for each row - if utils.is_block_visual_mode() or state.has_range(start_pos, end_pos) then + if state.is_block_visual_mode() or state.has_range(start_pos, end_pos) then local block_selection = {} for line = start_line, end_line do if start_col == end_col then @@ -204,7 +214,7 @@ function TextTransform.get_visual_selection_details() D.log( "get_visual_selection_details", "Returning block selection: %s", - utils.dump(block_selection) + vim.inspect(block_selection) ) return block_selection else @@ -221,4 +231,4 @@ function TextTransform.get_visual_selection_details() end end -return TextTransform +return replacers diff --git a/lua/text-transform/state.lua b/lua/text-transform/state.lua index dfeb3b7..4c036b8 100644 --- a/lua/text-transform/state.lua +++ b/lua/text-transform/state.lua @@ -1,59 +1,12 @@ -local D = require("text-transform.util.debug") - -local function ensure_config() - -- when the config is not set to the global object, we set it - if _G.TextTransform.config == nil then - _G.TextTransform.config = require("text-transform.config").config - end -end +local D = require("text-transform.utils.debug") -- methods local state = { - -- Boolean determining if the plugin is enabled or not. - enabled = false, -- A table containing cursor position and visual selection details, -- saved using `save_position()` and can be restored using `restore_positions()` positions = nil, } ---- Toggle the plugin by calling the `enable`/`disable` methods respectively. ---- @private -function state.toggle() - if state.enabled then - return state.disable() - end - - return state.enable() -end - ---- Enables the plugin ---- @private -function state.enable() - ensure_config() - - if state.enabled then - return state - end - - state.enabled = true - return state -end - ----Disables the plugin and reset the internal state. ----@private -function state.disable() - ensure_config() - - if not state.enabled then - return state - end - - -- reset the state - state.enabled = false - state.positions = nil - return state -end - local function get_mode_type(mode) -- classify mode as either visual, line, block or normal local mode_map = { @@ -80,8 +33,17 @@ local function capture_part(start_sel, end_sel, return_type) return { sel[1], l, sel[3], sel[4] } end +function state.is_block_visual_mode() + return state.positions.mode == "block" + -- return vim.fn.mode() == "V" or vim.fn.mode() == "\22" +end + +function state.is_visual_mode() + return state.positions.mode == "visual" + -- return vim.fn.mode() == 'v' +end + --- Save the current cursor position, mode, and visual selection ranges ---- @private function state.save_positions() local buf = vim.api.nvim_get_current_buf() local mode_info = vim.api.nvim_get_mode() diff --git a/lua/text-transform/transformers.lua b/lua/text-transform/transformers.lua index 87991cc..6d4f47f 100644 --- a/lua/text-transform/transformers.lua +++ b/lua/text-transform/transformers.lua @@ -1,21 +1,21 @@ -local D = require("text-transform.util.debug") -local utils = require("text-transform.util") +local D = require("text-transform.utils.debug") +-- local utils = require("text-transform.utils") -local TextTransform = {} +local transformers = {} -TextTransform.WORD_BOUNDRY = "[%_%-%s%.]" +transformers.WORD_BOUNDRY = "[%_%-%s%.]" --- Splits a string into words. --- @param string string --- @return table -function TextTransform.to_words(string) +function transformers.to_words(string) local words = {} local word = "" local last_is_upper = false local last_is_digit = false for i = 1, #string do local char = string:sub(i, i) - if char:match(TextTransform.WORD_BOUNDRY) then + if char:match(transformers.WORD_BOUNDRY) then if word ~= "" then table.insert(words, word:lower()) end @@ -64,9 +64,9 @@ end --- @param with_word_cb function (word: string, index: number, words: table) -> string --- @param separator string|nil (optional) --- @return string -function TextTransform.transform_words(words, with_word_cb, separator) +function transformers.transform_words(words, with_word_cb, separator) if type(words) ~= "table" then - words = TextTransform.to_words(words) + words = transformers.to_words(words) end local out = "" for i, word in ipairs(words) do @@ -83,8 +83,8 @@ end --- Transforms a string into camelCase. --- @param string string --- @return string -function TextTransform.to_camel_case(string) - return TextTransform.transform_words(string, function(word, i) +function transformers.to_camel_case(string) + return transformers.transform_words(string, function(word, i) if i == 1 then return word:lower() end @@ -95,8 +95,8 @@ end --- Transfroms a string into snake_case. --- @param string any --- @return string -function TextTransform.to_snake_case(string) - return TextTransform.transform_words(string, function(word, i) +function transformers.to_snake_case(string) + return transformers.transform_words(string, function(word, i) if i == 1 then return word:lower() end @@ -107,16 +107,16 @@ end --- Transforms a string into PascalCase. --- @param string string --- @return string -function TextTransform.to_pascal_case(string) - local cc = TextTransform.to_camel_case(string) +function transformers.to_pascal_case(string) + local cc = transformers.to_camel_case(string) return cc:sub(1, 1):upper() .. cc:sub(2) end --- Transforms a string into Title Case. --- @param string string --- @return string -function TextTransform.to_title_case(string) - return TextTransform.transform_words(string, function(word) +function transformers.to_title_case(string) + return transformers.transform_words(string, function(word) return word:sub(1, 1):upper() .. word:sub(2):lower() end, " ") end @@ -124,8 +124,8 @@ end --- Transforms a string into kebab-case. --- @param string string --- @return string -function TextTransform.to_kebab_case(string) - return TextTransform.transform_words(string, function(word) +function transformers.to_kebab_case(string) + return transformers.transform_words(string, function(word) return word:lower() end, "-") end @@ -133,8 +133,8 @@ end --- Transforms a string into dot.case. --- @param string string --- @return string -function TextTransform.to_dot_case(string) - return TextTransform.transform_words(string, function(word) +function transformers.to_dot_case(string) + return transformers.transform_words(string, function(word) return word:lower() end, ".") end @@ -142,10 +142,10 @@ end --- Transforms a string into CONSTANT_CASE. --- @param string string --- @return string -function TextTransform.to_const_case(string) - return TextTransform.transform_words(string, function(word) +function transformers.to_const_case(string) + return transformers.transform_words(string, function(word) return word:upper() end, "_") end -return TextTransform +return transformers diff --git a/lua/text-transform/util/debug.lua b/lua/text-transform/utils/debug.lua similarity index 100% rename from lua/text-transform/util/debug.lua rename to lua/text-transform/utils/debug.lua diff --git a/lua/text-transform/util/init.lua b/lua/text-transform/utils/init.lua similarity index 50% rename from lua/text-transform/util/init.lua rename to lua/text-transform/utils/init.lua index 4fe4d5d..ea7e036 100644 --- a/lua/text-transform/util/init.lua +++ b/lua/text-transform/utils/init.lua @@ -1,4 +1,3 @@ -local state = require("text-transform.state") local utils = {} --- Merges two tables into one. Same as `vim.tbl_extend("keep", t1, t2)`. @@ -13,23 +12,6 @@ function utils.merge(t1, t2) return vim.tbl_extend("force", t1, t2) end ---- Dumps the object into a string. ---- @param obj any ---- @return string -function utils.dump(obj) - return vim.inspect(obj) -end - -function utils.is_block_visual_mode() - return state.positions.mode == "block" - -- return vim.fn.mode() == "V" or vim.fn.mode() == "\22" -end - -function utils.is_visual_mode() - return state.positions.mode == "visual" - -- return vim.fn.mode() == 'v' -end - function utils.has_range(visual_start, visual_end) return visual_start and visual_end and visual_start[2] ~= visual_end[2] end diff --git a/tests/helpers.lua b/tests/helpers.lua index 89742f9..c3da730 100644 --- a/tests/helpers.lua +++ b/tests/helpers.lua @@ -1,3 +1,4 @@ +local MiniTest = require("mini.test") -- partially imported from https://github.com/echasnovski/mini.nvim local Helpers = {} diff --git a/tests/test_config.lua b/tests/test_config.lua index 0a10a3c..85f51c6 100644 --- a/tests/test_config.lua +++ b/tests/test_config.lua @@ -32,11 +32,6 @@ T["setup()"]["sets exposed methods and default options value"] = function() -- global object that holds your plugin information eq_type_global(child, "_G.TextTransform", "table") - -- public methods - eq_type_global(child, "_G.TextTransform.toggle", "function") - eq_type_global(child, "_G.TextTransform.disable", "function") - eq_type_global(child, "_G.TextTransform.enable", "function") - -- config eq_type_global(child, "_G.TextTransform.config", "table") diff --git a/tests/test_popups.lua b/tests/test_popups.lua index 5d5d0c2..018aa50 100644 --- a/tests/test_popups.lua +++ b/tests/test_popups.lua @@ -43,7 +43,7 @@ T["popups"]["telescope exposes telescope_popup"] = function() eq_type_global(child, "_G.TextTransform.telescope_popup", "nil") - child.lua([[Telescope = require('text-transform.telescope')]]) + child.lua([[Telescope = require('text-transform.popup.telescope')]]) eq_type_global(child, "Telescope.telescope_popup", "function") end @@ -55,7 +55,7 @@ T["popups"]["select exposes select_popup"] = function() eq_type_global(child, "_G.TextTransform.select_popup", "nil") - child.lua([[Select = require('text-transform.select')]]) + child.lua([[Select = require('text-transform.popup.select')]]) eq_type_global(child, "Select.select_popup", "function") end