refactor: use plugin name

This commit is contained in:
2024-05-06 02:30:52 +03:00
committed by Chen Asraf
parent 701d373e7a
commit 33eecfffab
11 changed files with 170 additions and 164 deletions

View File

@@ -1,25 +1,25 @@
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()*
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.init_keymaps() text-transform.txt /*TextTransform.init_keymaps()*
TextTransform.merge() text-transform.txt /*TextTransform.merge()*
TextTransform.replace_columns() text-transform.txt /*TextTransform.replace_columns()*
TextTransform.replace_range() text-transform.txt /*TextTransform.replace_range()*
TextTransform.replace_selection() text-transform.txt /*TextTransform.replace_selection()*
TextTransform.replace_word() text-transform.txt /*TextTransform.replace_word()*
TextTransform.restore_positions() text-transform.txt /*TextTransform.restore_positions()*
TextTransform.save_positions() text-transform.txt /*TextTransform.save_positions()*
TextTransform.select_popup() text-transform.txt /*TextTransform.select_popup()*
TextTransform.setup() text-transform.txt /*TextTransform.setup()*
TextTransform.show_popup() text-transform.txt /*TextTransform.show_popup()*
TextTransform.telescope_popup() text-transform.txt /*TextTransform.telescope_popup()*
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()*
find_word_boundaries() text-transform.txt /*find_word_boundaries()*
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()*
telescope.telescope_popup() text-transform.txt /*telescope.telescope_popup()*
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()*

View File

@@ -1,26 +1,26 @@
==============================================================================
------------------------------------------------------------------------------
*commands.init_commands()*
`commands.init_commands`()
*TextTransform.init_commands()*
`TextTransform.init_commands`()
Initializes user commands
@private
------------------------------------------------------------------------------
*commands.init_keymaps()*
`commands.init_keymaps`()
*TextTransform.init_keymaps()*
`TextTransform.init_keymaps`()
Initializes user keymaps
@private
==============================================================================
------------------------------------------------------------------------------
*config.config*
`config.config`
*TextTransform.config*
`TextTransform.config`
Your plugin configuration with its default values.
Default values:
>
config.config = {
TextTransform.config = {
--- Prints information about internals of the plugin. Very verbose, only useful for debugging.
debug = false,
--- Keymap configurations
@@ -61,8 +61,8 @@ Default values:
<
------------------------------------------------------------------------------
*config.setup()*
`config.setup`({options})
*TextTransform.setup()*
`TextTransform.setup`({options})
Define your text-transform setup.
Parameters ~
@@ -82,8 +82,8 @@ Finds the boundaries of the surrounding word around `start_col` within `line`.
@return number start_col, number end_col
------------------------------------------------------------------------------
*replacers.replace_range()*
`replacers.replace_range`({start_line}, {start_col}, {end_line}, {end_col}, {transform_name})
*TextTransform.replace_range()*
`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.
@@ -94,8 +94,8 @@ Acts on the lines between the given positions, replacing the text between the gi
@param transform_name string The transformer name
------------------------------------------------------------------------------
*replacers.replace_word()*
`replacers.replace_word`({transform_name}, {position})
*TextTransform.replace_word()*
`TextTransform.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.
@@ -104,16 +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
------------------------------------------------------------------------------
*replacers.replace_columns()*
`replacers.replace_columns`({transform_name})
*TextTransform.replace_columns()*
`TextTransform.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
------------------------------------------------------------------------------
*replacers.replace_selection()*
`replacers.replace_selection`({transform_name})
*TextTransform.replace_selection()*
`TextTransform.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.
@@ -121,8 +121,8 @@ range replacement functions.
@param transform_name string The transformer name
------------------------------------------------------------------------------
*replacers.get_visual_selection_details()*
`replacers.get_visual_selection_details`()
*TextTransform.get_visual_selection_details()*
`TextTransform.get_visual_selection_details`()
Takes the saved positions and translates them into individual visual ranges, regardless of how
the original selection was performed.
@@ -132,28 +132,28 @@ the full information around the selection logic.
==============================================================================
------------------------------------------------------------------------------
*state.save_positions()*
`state.save_positions`()
*TextTransform.save_positions()*
`TextTransform.save_positions`()
Save the current cursor position, mode, and visual selection ranges
------------------------------------------------------------------------------
*state.restore_positions()*
`state.restore_positions`({new_state})
*TextTransform.restore_positions()*
`TextTransform.restore_positions`({positions})
Restore the cursor position, mode, and visual selection ranges saved using `save_position()`,
or a given modified state, if passed as the first argument
==============================================================================
------------------------------------------------------------------------------
*transformers.to_words()*
`transformers.to_words`({string})
*TextTransform.to_words()*
`TextTransform.to_words`({string})
Splits a string into words.
@param string string
@return table
------------------------------------------------------------------------------
*transformers.transform_words()*
`transformers.transform_words`({words}, {with_word_cb}, {separator})
*TextTransform.transform_words()*
`TextTransform.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.
@@ -164,50 +164,50 @@ The separator is added between each word.
@return string
------------------------------------------------------------------------------
*transformers.to_camel_case()*
`transformers.to_camel_case`({string})
*TextTransform.to_camel_case()*
`TextTransform.to_camel_case`({string})
Transforms a string into camelCase.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_snake_case()*
`transformers.to_snake_case`({string})
*TextTransform.to_snake_case()*
`TextTransform.to_snake_case`({string})
Transfroms a string into snake_case.
@param string any
@return string
------------------------------------------------------------------------------
*transformers.to_pascal_case()*
`transformers.to_pascal_case`({string})
*TextTransform.to_pascal_case()*
`TextTransform.to_pascal_case`({string})
Transforms a string into PascalCase.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_title_case()*
`transformers.to_title_case`({string})
*TextTransform.to_title_case()*
`TextTransform.to_title_case`({string})
Transforms a string into Title Case.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_kebab_case()*
`transformers.to_kebab_case`({string})
*TextTransform.to_kebab_case()*
`TextTransform.to_kebab_case`({string})
Transforms a string into kebab-case.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_dot_case()*
`transformers.to_dot_case`({string})
*TextTransform.to_dot_case()*
`TextTransform.to_dot_case`({string})
Transforms a string into dot.case.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_const_case()*
`transformers.to_const_case`({string})
*TextTransform.to_const_case()*
`TextTransform.to_const_case`({string})
Transforms a string into CONSTANT_CASE.
@param string string
@return string
@@ -215,8 +215,8 @@ Transforms a string into CONSTANT_CASE.
==============================================================================
------------------------------------------------------------------------------
*popup.show_popup()*
`popup.show_popup`()
*TextTransform.show_popup()*
`TextTransform.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.
@@ -227,8 +227,8 @@ made.
==============================================================================
------------------------------------------------------------------------------
*select.select_popup()*
`select.select_popup`()
*TextTransform.select_popup()*
`TextTransform.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.
@@ -239,8 +239,8 @@ made.
==============================================================================
------------------------------------------------------------------------------
*telescope.telescope_popup()*
`telescope.telescope_popup`()
*TextTransform.telescope_popup()*
`TextTransform.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.
@@ -251,8 +251,8 @@ made.
==============================================================================
------------------------------------------------------------------------------
*utils.merge()*
`utils.merge`({t1}, {t2})
*TextTransform.merge()*
`TextTransform.merge`({t1}, {t2})
Merges two tables into one. Same as `vim.tbl_extend("keep", t1, t2)`.
Mutates the first table.

View File

@@ -5,11 +5,11 @@ local replacers = require("text-transform.replacers")
local popup = require("text-transform.popup")
local common = require("text-transform.popup.common")
local commands = {}
local TextTransform = {}
--- Initializes user commands
--- @private
function commands.init_commands()
function TextTransform.init_commands()
local map = {
TtCamel = "camel_case",
TtConst = "const_case",
@@ -36,6 +36,9 @@ function commands.init_commands()
vim.api.nvim_create_user_command(cmd, function()
state.save_positions()
replacers.replace_selection(transformer_name)
vim.schedule(function()
state.restore_positions()
end)
end, opts("Change to " .. item))
end
@@ -55,7 +58,7 @@ end
--- Initializes user keymaps
--- @private
function commands.init_keymaps()
function TextTransform.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
@@ -69,4 +72,4 @@ function commands.init_keymaps()
end
end
return commands
return TextTransform

View File

@@ -1,18 +1,18 @@
local commands = require("text-transform.commands")
local D = require("text-transform.utils.debug")
local utils = require("text-transform.utils")
local config = {}
local TextTransform = {}
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
_G.TextTransform.config = TextTransform.config
end
end
local function init()
ensure_config()
local o = config.config
local o = TextTransform.config
D.log("config", "Initializing TextTransform with %s", vim.inspect(o))
commands.init_commands()
commands.init_keymaps()
@@ -22,7 +22,7 @@ end
---
--- Default values:
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
config.config = {
TextTransform.config = {
--- Prints information about internals of the plugin. Very verbose, only useful for debugging.
debug = false,
--- Keymap configurations
@@ -65,10 +65,10 @@ config.config = {
---@param options table Module config table. See |TextTransform.options|.
---
---@usage `require("text-transform").setup()` (add `{}` with your |TextTransform.options| table)
function config.setup(options)
function TextTransform.setup(options)
options = options or {}
config.config = utils.merge(config.config, options)
TextTransform.config = utils.merge(TextTransform.config, options)
if vim.api.nvim_get_vvar("vim_did_enter") == 0 then
vim.defer_fn(function()
@@ -78,7 +78,7 @@ function config.setup(options)
init()
end
return config.config
return TextTransform.config
end
return config
return TextTransform

View File

@@ -1,4 +1,4 @@
local popup = {}
local TextTransform = {}
--- 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
@@ -6,7 +6,7 @@ local popup = {}
---
--- The cursor positions/ranges are saved before opening the menu and restored once a selection is
--- made.
function popup.show_popup()
function TextTransform.show_popup()
local config = _G.TextTransform.config
if config.popup_type == "telescope" then
local telescope = require("text-transform.popup.telescope")
@@ -17,4 +17,4 @@ function popup.show_popup()
end
end
return popup
return TextTransform

View File

@@ -1,7 +1,7 @@
local common = require("text-transform.popup.common")
local state = require("text-transform.state")
local select = {}
local TextTransform = {}
--- 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
@@ -9,7 +9,7 @@ local select = {}
---
--- The cursor positions/ranges are saved before opening the menu and restored once a selection is
--- made.
function select.select_popup()
function TextTransform.select_popup()
common.load_frequency()
state.save_positions()
@@ -27,4 +27,4 @@ function select.select_popup()
end)
end
return select
return TextTransform

View File

@@ -10,7 +10,7 @@ local action_state = require("telescope.actions.state")
local dropdown = require("telescope.themes").get_dropdown({})
local Sorter = require("telescope.sorters").Sorter
local telescope = {}
local TextTransform = {}
local frequency_sorter = Sorter:new({
---@diagnostic disable-next-line: unused-local
@@ -60,7 +60,7 @@ local sorter_map = {
---
--- The cursor positions/ranges are saved before opening the menu and restored once a selection is
--- made.
function telescope.telescope_popup()
function TextTransform.telescope_popup()
state.save_positions()
local filtered = {}
@@ -101,4 +101,4 @@ function telescope.telescope_popup()
end)
end
return telescope
return TextTransform

View File

@@ -1,9 +1,8 @@
local D = require("text-transform.utils.debug")
local state = require("text-transform.state")
local utils = require("text-transform.utils")
local t = require("text-transform.transformers")
local replacers = {}
local TextTransform = {}
--- Finds the boundaries of the surrounding word around `start_col` within `line`.
--- @param line number
@@ -37,7 +36,7 @@ end
--- @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)
function TextTransform.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
@@ -69,7 +68,7 @@ end
---
--- @param transform_name string The transformer name
--- @param position table|nil A table containing the position of the word to replace
function replacers.replace_word(transform_name, position)
function TextTransform.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
@@ -87,7 +86,7 @@ function replacers.replace_word(transform_name, position)
if not position then
vim.cmd("normal ciw" .. transformed)
else
replacers.replace_range(line, start_col, line, end_col, transform_name)
TextTransform.replace_range(line, start_col, line, end_col, transform_name)
end
end
@@ -95,11 +94,11 @@ end
--- Assumes that the each selection is 1 character and operates on the whole word under each cursor.
---
--- @param transform_name string The transformer name
function replacers.replace_columns(transform_name)
local selections = replacers.get_visual_selection_details()
function TextTransform.replace_columns(transform_name)
local selections = TextTransform.get_visual_selection_details()
D.log("replace_columns", "Replacing columns with %s", transform_name)
for _, sel in ipairs(selections) do
replacers.replace_word(transform_name, { 0, sel.start_line, sel.start_col, 0 })
TextTransform.replace_word(transform_name, { 0, sel.start_line, sel.start_col, 0 })
end
end
@@ -108,11 +107,11 @@ end
--- range replacement functions.
---
--- @param transform_name string The transformer name
function replacers.replace_selection(transform_name)
function TextTransform.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 = replacers.get_visual_selection_details()
local selections = TextTransform.get_visual_selection_details()
D.log("replace_selection", "Selections: %s", vim.inspect(selections))
local is_multiline = #selections > 1
@@ -132,12 +131,12 @@ function replacers.replace_selection(transform_name)
)
if is_single_cursor then
replacers.replace_word(transform_name)
TextTransform.replace_word(transform_name)
elseif is_column then
replacers.replace_columns(transform_name)
TextTransform.replace_columns(transform_name)
else
for _, sel in pairs(selections) do
replacers.replace_range(
TextTransform.replace_range(
sel.start_line,
sel.start_col,
sel.end_line,
@@ -153,22 +152,22 @@ 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 replacers.get_visual_selection_details()
if not state.positions then
function TextTransform.get_visual_selection_details()
if not state.state.positions then
D.log("get_visual_selection_details", "No positions saved")
return {}
end
D.log(
"get_visual_selection_details",
"Getting visual selection details - mode: %s, is_visual: %s, is_block: %s",
state.positions.mode,
state.state.positions.mode,
state.is_visual_mode(),
state.is_block_visual_mode()
)
-- Get the start and end positions of the selection
local start_pos = state.positions.visual_start
local end_pos = state.positions.visual_end
local start_pos = state.state.positions.visual_start
local end_pos = state.state.positions.visual_end
local start_line, start_col = start_pos[2], start_pos[3]
local end_line, end_col = end_pos[2], end_pos[3]
@@ -231,4 +230,4 @@ function replacers.get_visual_selection_details()
end
end
return replacers
return TextTransform

View File

@@ -1,10 +1,13 @@
local D = require("text-transform.utils.debug")
-- methods
local state = {
-- A table containing cursor position and visual selection details,
-- saved using `save_position()` and can be restored using `restore_positions()`
positions = nil,
local TextTransform = {
-- The current state of the plugin
state = {
-- A table containing cursor position and visual selection details,
-- saved using `save_position()` and can be restored using `restore_positions()`
positions = nil,
},
}
local function get_mode_type(mode)
@@ -17,7 +20,7 @@ local function get_mode_type(mode)
return mode_map[mode] or "normal"
end
function state.has_range(visual_start, visual_end)
function TextTransform.has_range(visual_start, visual_end)
return visual_start and visual_end and visual_start[2] ~= visual_end[2]
end
@@ -33,18 +36,18 @@ 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"
function TextTransform.is_block_visual_mode()
return TextTransform.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"
function TextTransform.is_visual_mode()
return TextTransform.state.positions.mode == "visual"
-- return vim.fn.mode() == 'v'
end
--- Save the current cursor position, mode, and visual selection ranges
function state.save_positions()
function TextTransform.save_positions()
local buf = vim.api.nvim_get_current_buf()
local mode_info = vim.api.nvim_get_mode()
local mode = get_mode_type(mode_info.mode)
@@ -58,7 +61,7 @@ function state.save_positions()
D.log("save_positions", "Saved mode %s, cursor %s", mode, vim.inspect(pos))
if mode == "visual" or mode == "line" or mode == "block" then
if state.has_range(visual_start, visual_end) then -- for ranges
if TextTransform.has_range(visual_start, visual_end) then -- for ranges
D.log(
"save_positions",
"Visual range, mode is %s, %s",
@@ -86,36 +89,37 @@ function state.save_positions()
}
D.log("save_positions", "State: %s", vim.inspect(positions))
state.positions = positions
TextTransform.state.positions = positions
return positions
end
--- Restore the cursor position, mode, and visual selection ranges saved using `save_position()`,
--- or a given modified state, if passed as the first argument
function state.restore_positions(new_state)
new_state = new_state or new_state.positions
vim.api.nvim_set_current_buf(new_state.buf)
vim.fn.setpos(".", new_state.pos)
function TextTransform.restore_positions(positions)
positions = positions or TextTransform.state.positions
vim.api.nvim_set_current_buf(positions.buf)
vim.fn.setpos(".", positions.pos)
D.log(
"restore_positions",
"Restored mode %s, cursor %s",
new_state.mode,
vim.inspect(new_state.pos)
positions.mode,
vim.inspect(positions.pos)
)
-- Attempt to restore visual mode accurately
if
(new_state.mode == "visual" or new_state.mode == "block")
and new_state.visual_start
and new_state.visual_end
(positions.mode == "visual" or positions.mode == "block")
and positions.visual_start
and positions.visual_end
then
vim.fn.setpos("'<", new_state.visual_start)
vim.fn.setpos("'>", new_state.visual_end)
vim.fn.setpos("'<", positions.visual_start)
vim.fn.setpos("'>", positions.visual_end)
local command = "normal! gv"
vim.cmd(command)
D.log("restore_positions", [[Restored visual mode %s using "%s"]], new_state.mode, command)
D.log("restore_positions", [[Restored visual mode %s using "%s"]], positions.mode, command)
end
new_state.positions = nil
TextTransform.state.positions = nil
end
return state
return TextTransform

View File

@@ -1,21 +1,21 @@
local D = require("text-transform.utils.debug")
-- local utils = require("text-transform.utils")
local transformers = {}
local TextTransform = {}
transformers.WORD_BOUNDRY = "[%_%-%s%.]"
TextTransform.WORD_BOUNDRY = "[%_%-%s%.]"
--- Splits a string into words.
--- @param string string
--- @return table
function transformers.to_words(string)
function TextTransform.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(transformers.WORD_BOUNDRY) then
if char:match(TextTransform.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 transformers.transform_words(words, with_word_cb, separator)
function TextTransform.transform_words(words, with_word_cb, separator)
if type(words) ~= "table" then
words = transformers.to_words(words)
words = TextTransform.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 transformers.to_camel_case(string)
return transformers.transform_words(string, function(word, i)
function TextTransform.to_camel_case(string)
return TextTransform.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 transformers.to_snake_case(string)
return transformers.transform_words(string, function(word, i)
function TextTransform.to_snake_case(string)
return TextTransform.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 transformers.to_pascal_case(string)
local cc = transformers.to_camel_case(string)
function TextTransform.to_pascal_case(string)
local cc = TextTransform.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 transformers.to_title_case(string)
return transformers.transform_words(string, function(word)
function TextTransform.to_title_case(string)
return TextTransform.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 transformers.to_kebab_case(string)
return transformers.transform_words(string, function(word)
function TextTransform.to_kebab_case(string)
return TextTransform.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 transformers.to_dot_case(string)
return transformers.transform_words(string, function(word)
function TextTransform.to_dot_case(string)
return TextTransform.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 transformers.to_const_case(string)
return transformers.transform_words(string, function(word)
function TextTransform.to_const_case(string)
return TextTransform.transform_words(string, function(word)
return word:upper()
end, "_")
end
return transformers
return TextTransform

View File

@@ -1,4 +1,4 @@
local utils = {}
local TextTransform = {}
--- Merges two tables into one. Same as `vim.tbl_extend("keep", t1, t2)`.
--- Mutates the first table.
@@ -8,12 +8,12 @@ local utils = {}
--- @param t1 table
--- @param t2 table
--- @return table
function utils.merge(t1, t2)
function TextTransform.merge(t1, t2)
return vim.tbl_extend("force", t1, t2)
end
function utils.has_range(visual_start, visual_end)
function TextTransform.has_range(visual_start, visual_end)
return visual_start and visual_end and visual_start[2] ~= visual_end[2]
end
return utils
return TextTransform