Skip to content

Commit

Permalink
Add SCREAMING_SNAKE_CASE naming policy
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHermitte committed Jul 26, 2019
1 parent 1aac8b1 commit c3308e9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
12 changes: 10 additions & 2 deletions autoload/lh/naming.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
" Version: 1.0.0
let s:k_version = 100
" Created: 05th Oct 2009
" Last Update: 17th Oct 2017
" Last Update: 26th Jul 2019
"------------------------------------------------------------------------
" Description:
" - Naming policies for programming styles
" TODO:
" - Change all regex to use '\v'
" }}}1
"=============================================================================

Expand Down Expand Up @@ -71,7 +72,14 @@ function! lh#naming#to_underscore(identifier)
"todo: handle constant-like identifiers
"test with lh#foo#FooBar ...
let identifier = substitute(a:identifier, '\%(^\|[^A-Za-z0-9]\)\zs\(\u\)', '\l\1', '')
let identifier = substitute(identifier, '\l\zs\(\u\)', '_\l\1', 'g')
let identifier = substitute(identifier, '\l\zs_*\(\u\)', '_\l\1', 'g')
return identifier
endfunction

" Function:lh#naming#to_shout_underscore(identifier) {{{3
function! lh#naming#to_shout_underscore(identifier)
let identifier = lh#naming#to_underscore(a:identifier)
let identifier = substitute(identifier, '.*', '\U&\E', 'g')
return identifier
endfunction

Expand Down
7 changes: 4 additions & 3 deletions doc/lh-style.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*lh-style.txt* Vim Script library providing stylistic related functions (v1.0.0.)
For Vim version 7+. Last change: 17th Oct 2017
For Vim version 7+. Last change: 26th Jul 2019

By Luc Hermitte
hermitte {at} free {dot} fr
Expand Down Expand Up @@ -205,9 +205,10 @@ Transforms {id} into a:
*lh#naming#to_lower_camel_case()*
*lh#naming#to_upper_camel_case()*
*lh#naming#to_underscore()*
*lh#naming#to_shout_underscore()*

Options~
Format: (bg):{ft_}naming_{option}
Format: `(bg):{ft_}naming_{option}`
Examples:
- `g:vim_naming_param_re`: global specialization used to extract name to build
a parameter name, when working on vim-scripts.
Expand All @@ -233,6 +234,6 @@ Mappings~


------------------------------------------------------------------------------
© Luc Hermitte, 2011-2017, http://github.com/LucHermitte/lh-style }}}1
© Luc Hermitte, 2011-2019, http://github.com/LucHermitte/lh-style }}}1
VIM: let b:VS_language = 'american'
vim:ts=8:sw=4:tw=80:fo=tcq2:isk=!-~,^*,^\|,^\":ft=help:fdm=marker:
4 changes: 2 additions & 2 deletions doc/naming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ NB: both commands support command-line auto-completion on naming policy names.
:samp:`:NameConvert` converts the identifier under the cursor to one of the following naming policies:


* naming styles: ``upper_camel_case``, ``lower_camel_case``, ``underscore``/``snake``, ``variable``,
* naming styles: ``upper_camel_case``/``UpperCamelCase``, ``lower_camel_case``/``lowerCamelCase``, ``underscore``/``snake``, ``UPPER_CASE``/``SCREAMING_SNAKE_CASE``, ``variable``,
* identifier kinds: ``getter``, ``setter``, ``local``, ``global``, ``member``, ``static``, ``constant``, ``param`` (the exact conversion process can be tuned thanks to the `following options <#options-to-tune-the-naming-policy>`_).

.. |ConvertNames| replace:: :samp:`:ConvertNames`
Expand Down Expand Up @@ -52,7 +52,7 @@ Naming conventions can be defined to:
* getters and setters
* types

* Control the case policy (``snake_case``, ``UpperCamelCase``, ``lowerCamelCase``) on functions (and thus on setters and
* Control the case policy (``snake_case``, ``UpperCamelCase``, ``lowerCamelCase``, ``SCREAMING_SNAKE_CASE``) on functions (and thus on setters and
getters too) and types.

It is done, respectively, with the following options:
Expand Down
36 changes: 20 additions & 16 deletions plugin/lh-style.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
" Version: 1.0.0
let s:k_version = 100
" Created: 31st May 2010
" Last Update: 16th Jan 2019
" Last Update: 26th Jul 2019
"------------------------------------------------------------------------
" Description:
" Global commands and definitions of lh-style
Expand Down Expand Up @@ -52,21 +52,25 @@ endif
" like functions that help building a vim-menu for this plugin.
" Name transformations {{{2
let s:k_convertions = [
\ ['upper_camel_case', 'lh#naming#to_upper_camel_case'],
\ ['lower_camel_case', 'lh#naming#to_lower_camel_case'],
\ ['underscore', 'lh#naming#to_underscore'],
\ ['snake', 'lh#naming#to_underscore'],
\ ['variable', 'lh#naming#variable'],
\ ['getter', 'lh#naming#getter'],
\ ['setter', 'lh#naming#setter'],
\ ['global', 'lh#naming#global'],
\ ['local', 'lh#naming#local'],
\ ['member', 'lh#naming#member'],
\ ['static', 'lh#naming#static'],
\ ['constant', 'lh#naming#constant'],
\ ['param', 'lh#naming#param'],
\ ['type', 'lh#naming#type'],
\ ['function', 'lh#naming#function']
\ ['UpperCamelCase', 'lh#naming#to_upper_camel_case'],
\ ['upper_camel_case', 'lh#naming#to_upper_camel_case'],
\ ['lowerCamelCase', 'lh#naming#to_lower_camel_case'],
\ ['lower_camel_case', 'lh#naming#to_lower_camel_case'],
\ ['underscore', 'lh#naming#to_underscore'],
\ ['snake', 'lh#naming#to_underscore'],
\ ['UPPPER_CASE', 'lh#naming#to_shout_underscore'],
\ ['SCREAMING_SNAKE_CASE', 'lh#naming#to_shout_underscore'],
\ ['variable', 'lh#naming#variable'],
\ ['getter', 'lh#naming#getter'],
\ ['setter', 'lh#naming#setter'],
\ ['global', 'lh#naming#global'],
\ ['local', 'lh#naming#local'],
\ ['member', 'lh#naming#member'],
\ ['static', 'lh#naming#static'],
\ ['constant', 'lh#naming#constant'],
\ ['param', 'lh#naming#param'],
\ ['type', 'lh#naming#type'],
\ ['function', 'lh#naming#function']
\ ]

" from plugin/vim-tip-swap-word.vim
Expand Down
19 changes: 18 additions & 1 deletion tests/lh/naming.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
" <URL:http://github.com/LucHermitte/lh-style/License.md>
" Version: 1.0.0
" Created: 05th Oct 2009
" Last Update: 17th Oct 2017
" Last Update: 26th Jul 2019
"------------------------------------------------------------------------
" Description:
" Unit Test for lh#naming functions
Expand Down Expand Up @@ -36,6 +36,23 @@ function! s:Test_2_name()
AssertEqual('name', lh#naming#variable('m_name'))
endfunction

function! s:Test_2_underscore() abort
AssertEqual('foobar', lh#naming#to_underscore('Foobar'))
AssertEqual('foobar', lh#naming#to_underscore('foobar'))
AssertEqual('foobar', lh#naming#to_underscore('Foobar'))
AssertEqual('foo_bar', lh#naming#to_underscore('FooBar'))
AssertEqual('foo_bar_bat', lh#naming#to_underscore('FooBar_bat'))
AssertEqual('foo_bar_bat', lh#naming#to_underscore('FooBar_Bat'))
endfunction

function! s:Test_2_shout_underscore() abort
AssertEqual('FOOBAR', lh#naming#to_shout_underscore('foobar'))
AssertEqual('FOOBAR', lh#naming#to_shout_underscore('Foobar'))
AssertEqual('FOO_BAR', lh#naming#to_shout_underscore('FooBar'))
AssertEqual('FOO_BAR_BAT', lh#naming#to_shout_underscore('FooBar_bat'))
AssertEqual('FOO_BAR_BAT', lh#naming#to_shout_underscore('FooBar_Bat'))
endfunction

function! s:Test_2_getter()
AssertEqual('getName', lh#naming#getter('name', ''))
" let b:FT_naming_get_subst = 'get_&'
Expand Down

0 comments on commit c3308e9

Please sign in to comment.