fix: words split

This commit is contained in:
Chen Asraf
2023-05-18 02:41:33 +03:00
parent eb3a7ae843
commit 43cc07cde4
2 changed files with 15 additions and 12 deletions

View File

@@ -46,15 +46,10 @@ function TextTransform.disable()
return S
end
local function trim(s)
return s:match("^%s*(.-)%s*$")
end
--- Splits a string into words.
function TextTransform.into_words(str)
local words = {}
local word = ""
str = trim(str)
local previous_is_split_token = false
for i = 1, #str do
@@ -66,15 +61,15 @@ function TextTransform.into_words(str)
-- split on uppercase letters or numbers
if is_split_token and not previous_is_split_token then
if word ~= "" then
table.insert(words, trim(str):lower())
table.insert(words, word:lower())
end
previous_is_split_token = is_split_token
previous_is_split_token = true
word = char
-- split on underscores, hyphens, and spaces
elseif is_separator then
if word ~= "" then
table.insert(words, trim(str):lower())
previous_is_split_token = is_split_token
table.insert(words, word:lower())
previous_is_split_token = false
end
word = ""
else
@@ -83,7 +78,7 @@ function TextTransform.into_words(str)
end
end
if word ~= "" then
table.insert(words, trim(str):lower())
table.insert(words, word:lower())
previous_is_split_token = false
end
return words
@@ -231,9 +226,9 @@ function TextTransform.replace_columns(transform)
-- get the line of this cursor
local line = vim.fn.getline(line_num)
-- match the surrounding word using start_col
local word = line:match("[%w%_%-%s%.]+", start_col)
local word = line:match("[%w%_%-%.]+", start_col)
-- replace the word with the transformed word
TextTransform.replace_cursor_range(line_num, start_col, start_col + #word - 1, transform)
TextTransform.replace_cursor_range(line_num, start_col, start_col + #word, transform)
end
end

View File

@@ -57,4 +57,12 @@ T["into_words()"]["should split two words with a number inside"] = function()
eq_global(child, "result[3]", "123")
end
T["into_words()"]["should split two words and ignore trailing/leading spaces"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').into_words(" hello world ")]])
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "world")
end
return T