refactor: file structure

This commit is contained in:
2024-05-06 02:06:21 +03:00
committed by Chen Asraf
parent 1c39528e7c
commit 1c8fa21578
19 changed files with 299 additions and 304 deletions

View File

@@ -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()*

View File

@@ -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()*
@@ -141,6 +143,100 @@ Restore the cursor position, mode, and visual selection ranges saved using `save
or a given modified state, if passed as the first argument
==============================================================================
------------------------------------------------------------------------------
*transformers.to_words()*
`transformers.to_words`({string})
Splits a string into words.
@param string string
@return table
------------------------------------------------------------------------------
*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.
@param words string|table string or table of strings
@param with_word_cb function (word: string, index: number, words: table) -> string
@param separator string|nil (optional)
@return string
------------------------------------------------------------------------------
*transformers.to_camel_case()*
`transformers.to_camel_case`({string})
Transforms a string into camelCase.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_snake_case()*
`transformers.to_snake_case`({string})
Transfroms a string into snake_case.
@param string any
@return string
------------------------------------------------------------------------------
*transformers.to_pascal_case()*
`transformers.to_pascal_case`({string})
Transforms a string into PascalCase.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_title_case()*
`transformers.to_title_case`({string})
Transforms a string into Title Case.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_kebab_case()*
`transformers.to_kebab_case`({string})
Transforms a string into kebab-case.
@param string string
@return string
------------------------------------------------------------------------------
*transformers.to_dot_case()*
`transformers.to_dot_case`({string})
Transforms a string into dot.case.
@param string string
@return 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()*
@@ -153,76 +249,6 @@ The cursor positions/ranges are saved before opening the menu and restored once
made.
==============================================================================
------------------------------------------------------------------------------
*TextTransform.to_words()*
`TextTransform.to_words`({string})
Splits a string into words.
@param string string
@return table
------------------------------------------------------------------------------
*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.
@param words string|table string or table of strings
@param with_word_cb function (word: string, index: number, words: table) -> string
@param separator string|nil (optional)
@return string
------------------------------------------------------------------------------
*TextTransform.to_camel_case()*
`TextTransform.to_camel_case`({string})
Transforms a string into camelCase.
@param string string
@return string
------------------------------------------------------------------------------
*TextTransform.to_snake_case()*
`TextTransform.to_snake_case`({string})
Transfroms a string into snake_case.
@param string any
@return string
------------------------------------------------------------------------------
*TextTransform.to_pascal_case()*
`TextTransform.to_pascal_case`({string})
Transforms a string into PascalCase.
@param string string
@return string
------------------------------------------------------------------------------
*TextTransform.to_title_case()*
`TextTransform.to_title_case`({string})
Transforms a string into Title Case.
@param string string
@return string
------------------------------------------------------------------------------
*TextTransform.to_kebab_case()*
`TextTransform.to_kebab_case`({string})
Transforms a string into kebab-case.
@param string string
@return string
------------------------------------------------------------------------------
*TextTransform.to_dot_case()*
`TextTransform.to_dot_case`({string})
Transforms a string into dot.case.
@param string string
@return string
------------------------------------------------------------------------------
*TextTransform.to_const_case()*
`TextTransform.to_const_case`({string})
Transforms a string into CONSTANT_CASE.
@param string string
@return string
==============================================================================
------------------------------------------------------------------------------
*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:

View File

@@ -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

View File

@@ -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

View File

@@ -1,5 +1,5 @@
local M = require("text-transform.main")
local utils = require("text-transform.util")
local utils = require("text-transform.utils")
local TextTransform = {}

View File

@@ -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")

View File

@@ -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

View File

@@ -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")

View File

@@ -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

View File

@@ -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()

View File

@@ -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")

View File

@@ -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

View File

@@ -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()

View File

@@ -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

View File

@@ -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

View File

@@ -1,3 +1,4 @@
local MiniTest = require("mini.test")
-- partially imported from https://github.com/echasnovski/mini.nvim
local Helpers = {}

View File

@@ -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")

View File

@@ -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