Skip to content

Commit

Permalink
docs: update doc file
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed May 15, 2024
1 parent 8fd1cb3 commit d78f246
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 102 deletions.
2 changes: 0 additions & 2 deletions doc/tags
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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()*
Expand Down
110 changes: 61 additions & 49 deletions doc/text-transform.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
==============================================================================
------------------------------------------------------------------------------
*TextTransform.init_commands()*
`TextTransform.init_commands`()
Initializes user commands
@private

------------------------------------------------------------------------------
*TextTransform.init_keymaps()*
`TextTransform.init_keymaps`()
Initializes user keymaps
@private


==============================================================================
------------------------------------------------------------------------------
*TextTransform.config*
Expand Down Expand Up @@ -77,21 +63,24 @@ Usage ~
*find_word_boundaries()*
`find_word_boundaries`({line}, {start_col})
Finds the boundaries of the surrounding word around `start_col` within `line`.
@param line number
@param start_col number
@return number start_col, number end_col
Parameters ~
{line} `(number)`
{start_col} `(number)`
Return ~
`(number)` start_col, number end_col

------------------------------------------------------------------------------
*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.

@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
Parameters ~
{start_line} `(number)` The starting line
{start_col} `(number)` The starting column
{end_line} `(number)` The ending line
{end_col} `(number)` The ending column
{transform_name} `(string)` The transformer name

------------------------------------------------------------------------------
*TextTransform.replace_word()*
Expand All @@ -100,16 +89,18 @@ 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.

@param transform_name string The transformer name
@param position table|nil A table containing the position of the word to replace
Parameters ~
{transform_name} `(string)` The transformer name
{position} `(table|nil)` A table containing the position of the word to replace

------------------------------------------------------------------------------
*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
Parameters ~
{transform_name} `(string)` The transformer name

------------------------------------------------------------------------------
*TextTransform.replace_selection()*
Expand All @@ -118,7 +109,8 @@ Replaces a selection with the given transform. This function attempts to infer t
type based on the cursor positiono and visual selections, and passes information to relevant
range replacement functions.

@param transform_name string The transformer name
Parameters ~
{transform_name} `(string)` The transformer name

------------------------------------------------------------------------------
*TextTransform.get_visual_selection_details()*
Expand Down Expand Up @@ -148,8 +140,10 @@ or a given modified state, if passed as the first argument
*TextTransform.to_words()*
`TextTransform.to_words`({string})
Splits a string into words.
@param string string
@return table
Parameters ~
{string} `(string)`
Return ~
`(table)`

------------------------------------------------------------------------------
*TextTransform.transform_words()*
Expand All @@ -158,59 +152,75 @@ 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
Parameters ~
{words} `(string|table)` string or table of strings
{with_word_cb} `(function)` (word: string, index: number, words: table) -> string
{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
Parameters ~
{string} `(string)`
Return ~
`(string)`

------------------------------------------------------------------------------
*TextTransform.to_snake_case()*
`TextTransform.to_snake_case`({string})
Transfroms a string into snake_case.
@param string any
@return string
Parameters ~
{string} `(any)`
Return ~
`(string)`

------------------------------------------------------------------------------
*TextTransform.to_pascal_case()*
`TextTransform.to_pascal_case`({string})
Transforms a string into PascalCase.
@param string string
@return string
Parameters ~
{string} `(string)`
Return ~
`(string)`

------------------------------------------------------------------------------
*TextTransform.to_title_case()*
`TextTransform.to_title_case`({string})
Transforms a string into Title Case.
@param string string
@return string
Parameters ~
{string} `(string)`
Return ~
`(string)`

------------------------------------------------------------------------------
*TextTransform.to_kebab_case()*
`TextTransform.to_kebab_case`({string})
Transforms a string into kebab-case.
@param string string
@return string
Parameters ~
{string} `(string)`
Return ~
`(string)`

------------------------------------------------------------------------------
*TextTransform.to_dot_case()*
`TextTransform.to_dot_case`({string})
Transforms a string into dot.case.
@param string string
@return string
Parameters ~
{string} `(string)`
Return ~
`(string)`

------------------------------------------------------------------------------
*TextTransform.to_const_case()*
`TextTransform.to_const_case`({string})
Transforms a string into CONSTANT_CASE.
@param string string
@return string
Parameters ~
{string} `(string)`
Return ~
`(string)`


==============================================================================
Expand Down Expand Up @@ -258,9 +268,11 @@ Mutates the first table.

TODO accept multiple tables to merge

@param t1 table
@param t2 table
@return table
Parameters ~
{t1} `(table)`
{t2} `(table)`
Return ~
`(table)`


vim:tw=78:ts=8:noet:ft=help:norl:
4 changes: 0 additions & 4 deletions lua/text-transform/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ local common = require("text-transform.popup.common")

local TextTransform = {}

--- Initializes user commands
--- @private
function TextTransform.init_commands()
local map = {
TtCamel = "camel_case",
Expand Down Expand Up @@ -56,8 +54,6 @@ function TextTransform.init_commands()
vim.api.nvim_create_user_command("TextTransform", popup.show_popup, opts("Change Case"))
end

--- Initializes user keymaps
--- @private
function TextTransform.init_keymaps()
local keymaps = _G.TextTransform.config.keymap
D.log("init_keymaps", "Initializing keymaps, config %s", vim.inspect(_G.TextTransform))
Expand Down
26 changes: 13 additions & 13 deletions lua/text-transform/replacers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ local t = require("text-transform.transformers")
local TextTransform = {}

--- Finds the boundaries of the surrounding word around `start_col` within `line`.
--- @param line number
--- @param start_col number
--- @return number start_col, number end_col
---@param line number
---@param start_col number
---@return number start_col, number end_col
local function find_word_boundaries(line, start_col)
local line_text = vim.fn.getline(line)
-- dashes, underscores, and periods are considered part of a word
Expand All @@ -31,15 +31,15 @@ end
--- 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
---@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 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
local lines = vim.fn.getline(start_line, end_line) ---@type any
local transformed = {}
if #lines == 1 then
local line = lines[1]
Expand All @@ -66,8 +66,8 @@ end
--- If `position` is provided, replace the word under the given position.
--- Otherwise, attempts to find the word under the cursor.
---
--- @param transform_name string The transformer name
--- @param position table|nil A table containing the position of the word to replace
---@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)
D.log("replace_word", "Replacing word with %s", transform_name)
local word, line, col, start_col, end_col
Expand All @@ -93,7 +93,7 @@ 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.
---
--- @param transform_name string The transformer name
---@param transform_name string The transformer name
function TextTransform.replace_columns(transform_name)
local selections = TextTransform.get_visual_selection_details()
D.log("replace_columns", "Replacing columns with %s", transform_name)
Expand All @@ -106,7 +106,7 @@ end
--- type based on the cursor positiono and visual selections, and passes information to relevant
--- range replacement functions.
---
--- @param transform_name string The transformer name
---@param transform_name string The transformer 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
Expand Down
40 changes: 20 additions & 20 deletions lua/text-transform/transformers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ local TextTransform = {}
TextTransform.WORD_BOUNDRY = "[%_%-%s%.]"

--- Splits a string into words.
--- @param string string
--- @return table
---@param string string
---@return table
function TextTransform.to_words(string)
local words = {}
local word = ""
Expand Down Expand Up @@ -60,10 +60,10 @@ end
--- 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
---@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
function TextTransform.transform_words(words, with_word_cb, separator)
if type(words) ~= "table" then
words = TextTransform.to_words(words)
Expand All @@ -81,8 +81,8 @@ function TextTransform.transform_words(words, with_word_cb, separator)
end

--- Transforms a string into camelCase.
--- @param string string
--- @return string
---@param string string
---@return string
function TextTransform.to_camel_case(string)
return TextTransform.transform_words(string, function(word, i)
if i == 1 then
Expand All @@ -93,8 +93,8 @@ function TextTransform.to_camel_case(string)
end

--- Transfroms a string into snake_case.
--- @param string any
--- @return string
---@param string any
---@return string
function TextTransform.to_snake_case(string)
return TextTransform.transform_words(string, function(word, i)
if i == 1 then
Expand All @@ -105,43 +105,43 @@ function TextTransform.to_snake_case(string)
end

--- Transforms a string into PascalCase.
--- @param string string
--- @return string
---@param string string
---@return 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
---@param string string
---@return string
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

--- Transforms a string into kebab-case.
--- @param string string
--- @return string
---@param string string
---@return string
function TextTransform.to_kebab_case(string)
return TextTransform.transform_words(string, function(word)
return word:lower()
end, "-")
end

--- Transforms a string into dot.case.
--- @param string string
--- @return string
---@param string string
---@return string
function TextTransform.to_dot_case(string)
return TextTransform.transform_words(string, function(word)
return word:lower()
end, ".")
end

--- Transforms a string into CONSTANT_CASE.
--- @param string string
--- @return string
---@param string string
---@return string
function TextTransform.to_const_case(string)
return TextTransform.transform_words(string, function(word)
return word:upper()
Expand Down
Loading

0 comments on commit d78f246

Please sign in to comment.