Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for any case, and Title Case #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
This fork add support for: `aNy-CasE` or `Any cAse` - always converted to snake_case
Also I added support for new: `Title Case`

<video controls="true" allowfullscreen="true" poster="path/to/poster_image.png">
<source src="https://user-images.githubusercontent.com/13521338/113520457-8213d100-9593-11eb-974b-b8172eddd950.mp4" type="video/mp4">
</video>

Comment on lines +1 to +7
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! thanks for your contribution!

Could you please make this look like it is part of the same repo? 🙇

# casechange.vim

![demo](https://user-images.githubusercontent.com/4542735/39995493-b4d45c28-57bf-11e8-9246-22b30e142bc0.gif)
Expand Down
5 changes: 5 additions & 0 deletions doc/tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
casechange casechange.txt /*casechange*
casechange-about casechange.txt /*casechange-about*
casechange-keymaps casechange.txt /*casechange-keymaps*
casechange-usage casechange.txt /*casechange-usage*
casechange.txt casechange.txt /*casechange.txt*
92 changes: 92 additions & 0 deletions lua/casechange.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
-- casechange.vim - Lets role the cases
-- Maintainer: Ignacio Catalina
-- Version: 1.0
--
-- License:
-- Copyright (c) Ignacio Catalina. Distributed under the same terms as Vim itself.
-- See :help license
--

local my_plugin = {}

local function with_defaults(options)
return {
name = options.name or "John Doe"
}
end


function my_plugin.setup(options)
my_plugin.options = with_defaults(options)
end

function my_plugin.is_configured()
return my_plugin.options ~= nil
end

-- escape terminal control characters
local function t(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end

-- \C - case sensitive
-- \v - magic mode (no need for \)
local dash = vim.regex [==[\v^[a-z0-9]+(-+[a-z0-9]+)+$]==] -- [==['^[a-z0-9]+(-+[a-z0-9]+)+$]==] -- dash-case
local camel = vim.regex [==[\v\C^[a-z][a-z0-9]*([A-Z][a-z0-9]*)*$]==] -- camelCase
local snake = vim.regex [==[\v\C^[a-z0-9]+(_+[a-z0-9]+)*$]==] -- snake_case
local upper = vim.regex [==[\v\C^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$]==] -- UPPER_CASE
local pascal = vim.regex [==[\v\C^[A-Z][a-z0-9]*([A-Z0-9][a-z0-9]*)*$]==] -- PascalCase
local title = vim.regex [==[\v\C^[A-Z][a-z0-9]*( [A-Z][a-z0-9]+)*$]==] -- Title Case
local any = vim.regex [==[\v\C^[a-zA-Z][a-zA-Z0-9]*((\W)[a-zA-Z][a-zA-Z0-9]+)*$]==] -- -case etc.

local substitute = function(args)
return vim.api.nvim_call_function("substitute", args)
end

local toupper = function(args)
return vim.api.nvim_call_function("toupper", args)
end

local tolower = function(args)
return vim.api.nvim_call_function("tolower", args)
end


function _G.casechange()
vim.cmd [[noau normal! "zd]]
-- vim.pretty_print(vim.api.nvim_get_mode())
local str = vim.fn.getreg("z")

-- local start = vim.api.nvim_buf_get_mark(0, '<')
-- local finish = vim.api.nvim_buf_get_mark(0, '>')

local sub
if dash:match_str(str) then
sub = substitute({ str, [[\v-+([a-z])]], [[\U\1]], 'g' }) -- camelCase
elseif camel:match_str(str) then
sub = substitute({ str, [[^.*$]], [[\u\0]], '' }) -- PascalCase
elseif upper:match_str(str) then
local tit_under = substitute({ str, [[\v([A-Z])([A-Z]*)]], [[\1\L\2]], 'g' })
sub = substitute({ tit_under, '_', ' ', 'g' }) -- Title Case
elseif pascal:match_str(str) then
local res = substitute({ str, [==[\C^\@<![A-Z]]==], [[_\0]], 'g' })
sub = toupper({ res }) -- UPPER_CASE
elseif title:match_str(str) then
local res = substitute({ str, ' ', '_', 'g' })
sub = tolower({ res }) -- snake_case
elseif snake:match_str(str) then
sub = substitute({ str, [[\v_+]], '-', 'g' }) -- dash-case
elseif any:match_str(str) then -- wurst case scenario
local res = substitute({ str, [[\v(\W)([a-z])]], [[_\U\2]], 'g' }) -- snake_case
sub = tolower({ res })
end

vim.fn.setreg("z", sub)
vim.cmd('normal! ' .. [["zPv`[]]) -- FIXED: by using: d - rather than c at fun beg
end

vim.keymap.set('v', '~', function() casechange() end, { noremap = true, silent = true, desc = 'CaseChange Plug' })
-- vnoremap ~ "<C-R>=casechange(@z)<CR><Esc>v`[

my_plugin.options = nil
return my_plugin
36 changes: 5 additions & 31 deletions plugin/casechange.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,9 @@
" Copyright (c) Ignacio Catalina. Distributed under the same terms as Vim itself.
" See :help license
"
let s:casechange = luaeval('require("casechange")')

if exists("g:loaded_casechange") || &cp || v:version < 700
finish
endif
let g:loaded_casechange = 1


function! casechange#next(str)
let l:snake = '^[a-z0-9]\+\(-\+[a-z0-9]\+\)\+$'
let l:camel = '\C^[a-z][a-z0-9]*\([A-Z][a-z0-9]*\)*$'
let l:under = '\C^[a-z0-9]\+\(_\+[a-z0-9]\+\)*$'
let l:constant = '\C^[A-Z][A-Z0-9]*\(_[A-Z0-9]\+\)*$'
let l:pascal = '\C^[A-Z][a-z0-9]*\([A-Z0-9][a-z0-9]*\)*$'

if (a:str =~ l:snake)
return substitute(a:str, '-\+\([a-z]\)', '\U\1', 'g')
elseif (a:str =~ l:camel)
return substitute(a:str, '^.*$', '\u\0', 'g')
elseif (a:str =~ l:constant)
return tolower(a:str)
elseif (a:str =~ l:pascal)
return toupper(substitute(a:str, '\C^\@<![A-Z]', '_\0', 'g'))
else
return substitute(a:str, '_\+', '-', 'g')
endif
endfunction

if !exists("g:casechange_nomap")
vnoremap ~ "zc<C-R>=casechange#next(@z)<CR><Esc>v`[
endif

" vim:set ft=vim et sw=4 sts=4:
" if s:casechange.is_configured()
" Neovim does not expose a lua API to create commands yet
" command! MyPluginGreet call s:casechange.greet()
" endif