1
change_case.nvim[github]
1
2
A small Neovim plugin that converts the word under the cursor
3
between cases: camel, upper camel, snake, kebab, screaming snake,
4
train, dot, lowercase and uppercase.
5
6
Pure Lua, no dependencies, with a test suite.
7
8
9
11
12
return {
13
camel_case = {
14
separator = "",
15
word_transformer = function(word, index)
16
if index ~= 1 then
17
return capitalize(word:lower())
18
end
19
return word:lower()
20
end,
21
},
22
upper_camel_case = {
23
separator = "",
24
word_transformer = function(word)
25
return capitalize(word)
26
end,
27
},
28
snake_case = {
29
separator = "_",
30
word_transformer = function(word)
31
return word:lower()
32
end,
33
},
34
35
36
return {
camel_case = {
separator = "",
word_transformer = function(word, index)
if index ~= 1 then
return capitalize(word:lower())
end
return word:lower()
end,
},
upper_camel_case = {
separator = "",
word_transformer = function(word)
return capitalize(word)
end,
},
snake_case = {
separator = "_",
word_transformer = function(word)
return word:lower()
end,
},
--- @param text string
--- @return string[]
local function split_into_words(text)
local sub_words = {}
local word_pointer = 1
local index = 1
while index <= #text do
-- Extract the i-th character
local curr_char = string.sub(text, index, index)
local peek_char = string.sub(text, index + 1, index + 1)
if isLetter(curr_char) then
sub_words[word_pointer] = (sub_words[word_pointer] or "") .. curr_char
if isNewWordFromPeek(curr_char, peek_char) then
word_pointer = word_pointer + 1
end
end
index = index + 1
end
return sub_words
end
--- @param case Case
M.coherse_keyword = function(case)
local keyword = vim.fn.expand("<cword>")
local words = util.split_into_words(keyword)
local updated_keyword = util.words_to_case(words, case)
if keyword == updated_keyword then
vim.notify("[change_case] No change required", vim.log.levels.INFO)
return
end
local rename, rename_type = util.get_rename_fun()
local ok, result = pcall(rename, updated_keyword)
if not ok then
vim.notify("[change_case] Failed to rename: " .. result, vim.log.levels.ERROR)
return
end
vim.notify(
string.format("[change_case] Converted '%s' to '%s' using '%s'", keyword, updated_keyword, rename_type),
vim.log.levels.INFO
)
end
--- @return fun(), string
get_rename_fun = function()
local lsp_rename, lsp_type = get_lsp_rename()
if lsp_rename ~= nil then
return lsp_rename, lsp_type
end
local treesitter_rename, treesitter_type = get_treesitter_rename()
if treesitter_rename ~= nil then
return treesitter_rename, treesitter_type
end
return get_norm_rename()
end,