Permalink
Browse files

Version 0.0.4

- Fix doc: operator-replace => operator-camelize (thanks thinca for reporting)
- Support camelCase. not only PascalCase. (thanks thinca for reporting)
- Add operator#camelize#load().
  • Loading branch information...
1 parent d564190 commit c50378c37ce3f45340329d958e9b46eaa9cd7c2b @tyru tyru committed with Apr 16, 2011
Showing with 91 additions and 64 deletions.
  1. +73 −60 autoload/operator/camelize.vim
  2. +16 −2 doc/operator-camelize.txt
  3. +2 −2 plugin/operator/camelize.vim
@@ -8,34 +8,37 @@ set cpo&vim
-function! s:map_text_with_regex(text, funcname, regex, ...) "{{{
- let give_context = a:0 ? a:1 : 0
- let converted_text = ''
+function! operator#camelize#load() "{{{
+ runtime! plugin/operator/camelize.vim
+endfunction "}}}
+
+
+
+" Utilities
+function! s:map_text_with_regex(text, funcname, regex) "{{{
let text = a:text
- let whole_offset = 0
+ let context = {
+ \ 'converted': '',
+ \ 'match': '',
+ \}
while text != ''
let offset = match(text, a:regex)
if offset ==# -1
break
endif
- let context = {'offset': offset, 'whole_offset': whole_offset}
- let len = strlen(matchstr(text, a:regex))
- let whole_offset += len
-
- let left = offset == 0 ? '' : text[: offset - 1]
- let middle = text[offset : offset + len - 1]
- let right = text[offset + len :]
-
- let converted_text .= left . call(
- \ a:funcname,
- \ [middle] + (give_context ? [context] : [])
- \)
+ let len = matchend(text, a:regex)
+
+ let left = offset == 0 ? '' : text[: offset - 1]
+ let context.match = text[offset : offset + len - 1]
+ let right = text[offset + len :]
+
+ let context.converted .= left . {a:funcname}(context)
let text = right
endwhile
- return converted_text . text
+ return context.converted . text
endfunction "}}}
-" For operator.
+" Utilities for operator
function! s:yank_range(motion_wiseness) "{{{
" Select previously-selected range in visual mode.
" NOTE: `normal! gv` does not work
@@ -96,22 +99,31 @@ endfunction "}}}
-" operator#camelize#camelize_word('snake_case')
-" " => 'SnakeCase'
-function! operator#camelize#camelize_word(word) "{{{
+" For a atom
+" e.g.: 'snake'
+function! s:camelize_atom(context) "{{{
+ let word = a:context.match[0] == '_' ? a:context.match[1:] : a:context.match
+ return toupper(word[0]) . tolower(word[1:])
+endfunction "}}}
+
+" For a word
+" e.g.: 'snake_case'
+function! s:camelize_word(context) "{{{
" NOTE: Nested sub-replace-expression can't work...omg
" (:help sub-replace-expression)
"
- " return substitute(tolower(a:word), '^[a-z]\|_\zs[a-z]'.'\C', '\=toupper(submatch(0))', 'g')
+ " return substitute(tolower(a:context.match), '^[a-z]\|_\zs[a-z]'.'\C', '\=toupper(submatch(0))', 'g')
+
+ let word = a:context.match
- if a:word =~# '^[A-Z]\+$'
+ if word =~# '^[A-Z]\+$'
let action = g:operator_camelize_all_uppercase_action
if action ==# 'nop'
- return a:word
+ return word
elseif action ==# 'lowercase'
- return tolower(a:word)
+ return tolower(word)
elseif action ==# 'camelize'
- return toupper(a:word[0]) . tolower(a:word[1:])
+ return toupper(word[0]) . tolower(word[1:])
else
echohl WarningMsg
echomsg "g:operator_camelize_all_uppercase_action is invalid value '"
@@ -121,42 +133,48 @@ function! operator#camelize#camelize_word(word) "{{{
endif
return s:map_text_with_regex(
- \ a:word,
- \ 's:camelize',
+ \ word,
+ \ 's:camelize_atom',
\ '\<[a-zA-Z0-9]\+\|_[a-zA-Z0-9]\+'.'\C'
\)
endfunction "}}}
-function! s:camelize(word) "{{{
- let word = a:word[0] == '_' ? a:word[1:] : a:word
- return toupper(word[0]) . tolower(word[1:])
-endfunction "}}}
-" operator#camelize#camelize_text('snake_case other_text')
-" " => 'SnakeCase OtherText'
-function! operator#camelize#camelize_text(text) "{{{
- return s:map_text_with_regex(a:text, 'operator#camelize#camelize_word', '\w\+')
+" For a text
+" e.g.: 'snake_case other_text'
+function! s:camelize_text(text) "{{{
+ return s:map_text_with_regex(a:text, 's:camelize_word', '\w\+')
endfunction "}}}
-function! operator#camelize#op_camelize(motion_wiseness) "{{{
- call s:replace_range('operator#camelize#camelize_text', a:motion_wiseness)
+" For <Plug>(operator-camelize)
+function! operator#camelize#camelize(motion_wiseness) "{{{
+ call s:replace_range('s:camelize_text', a:motion_wiseness)
endfunction "}}}
-" operator#camelize#decamelize_word('CamelCase')
-" " => 'camel_case'
-function! operator#camelize#decamelize_word(word) "{{{
+" For a atom
+" e.g.: 'Snake'
+function! s:decamelize_atom(context) "{{{
+ return (a:context.converted ==# '' ? '' : '_')
+ \ . tolower(a:context.match)
+endfunction "}}}
+
+" For a word
+" e.g.: 'SnakeCase'
+function! s:decamelize_word(context) "{{{
" NOTE: Nested sub-replace-expression can't work...omg
" (:help sub-replace-expression)
"
- " return substitute(a:word, '^[A-Z]\|[a-z]\zs[A-Z]'.'\C', '\='_' . tolower(submatch(0))', 'g')
+ " return substitute(a:context.match, '^[A-Z]\|[a-z]\zs[A-Z]'.'\C', '\='_' . tolower(submatch(0))', 'g')
- if a:word =~# '^[A-Z]\+$'
+ let word = a:context.match
+
+ if word =~# '^[A-Z]\+$'
let action = g:operator_decamelize_all_uppercase_action
if action ==# 'nop'
- return a:word
+ return word
elseif action ==# 'lowercase'
- return tolower(a:word)
+ return word
elseif action ==# 'decamelize'
" Fall through
else
@@ -168,26 +186,21 @@ function! operator#camelize#decamelize_word(word) "{{{
endif
return s:map_text_with_regex(
- \ a:word,
- \ 's:decamelize',
- \ '[A-Z][a-z0-9]*'.'\C',
- \ 1
+ \ word,
+ \ 's:decamelize_atom',
+ \ '^[a-z0-9]\+\ze[A-Z]\|^[A-Z][a-z0-9]*'.'\C',
\)
endfunction "}}}
-function! s:decamelize(word, context) "{{{
- return
- \ (a:context.whole_offset == 0 ? '' : '_')
- \ . tolower(a:word)
-endfunction "}}}
-" operator#camelize#decamelize_text('CamelCase OtherText')
-" " => 'camel_case other_text'
-function! operator#camelize#decamelize_text(text) "{{{
- return s:map_text_with_regex(a:text, 'operator#camelize#decamelize_word', '\w\+')
+" For a text
+" e.g.: 'SnakeCase OtherText'
+function! s:decamelize_text(text) "{{{
+ return s:map_text_with_regex(a:text, 's:decamelize_word', '\w\+')
endfunction "}}}
-function! operator#camelize#op_decamelize(motion_wiseness) "{{{
- call s:replace_range('operator#camelize#decamelize_text', a:motion_wiseness)
+" For <Plug>(operator-decamelize)
+function! operator#camelize#decamelize(motion_wiseness) "{{{
+ call s:replace_range('s:decamelize_text', a:motion_wiseness)
endfunction "}}}
View
@@ -2,7 +2,7 @@
Author:
tyru <tyru.exe@gmail.com>
-Version: 0.0.3
+Version: 0.0.4
License: Under the same license as Vim itself (see :help license)
==============================================================================
@@ -12,13 +12,14 @@ Introduction |operator-camelize-introduction|
Interface |operator-camelize-interface|
Keymappings |operator-camelize-keymappings|
Variables |operator-camelize-variables|
+ Functions |operator-camelize-functions|
Changelog |operator-camelize-changelog|
==============================================================================
INTRODUCTION *operator-camelize-introduction* {{{
-*operator-replace* is a Vim plugin to provide an operator
+|operator-camelize| is a Vim plugin to provide an operator
to camelize/decamelize a specified text.
Requirements:
@@ -67,6 +68,15 @@ g:operator_decamelize_all_uppercase_action *g:operator_decamelize_all_uppercase
"lowercase" "word"
"decamelize" "w_o_r_d"
}}}
+------------------------------------------------------------------------------
+FUNCTIONS *operator-camelize-functions* {{{
+
+ *operator#camelize#load()*
+operator#camelize#load()
+ Load autoload/operator/camelize.vim
+ and plugin/operator/camelize.vim .
+
+}}}
}}}
==============================================================================
CHANGELOG *operator-camelize-changelog* {{{
@@ -84,6 +94,10 @@ CHANGELOG *operator-camelize-changelog* {{{
- Fix bug: If there are two more than words in selected range,
head character is not camelized.
- A lot of internal changes.
+0.0.4:
+- Fix doc: operator-replace => operator-camelize (thanks thinca for reporting)
+- Support camelCase. not only PascalCase. (thanks thinca for reporting)
+- Add operator#camelize#load().
}}}
==============================================================================
vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl:noet:fdm=marker:fen
@@ -19,8 +19,8 @@ if !exists('g:operator_camelize_all_uppercase_action')
let g:operator_camelize_all_uppercase_action = 'nop'
endif
-call operator#user#define('camelize', 'operator#camelize#op_camelize')
-call operator#user#define('decamelize', 'operator#camelize#op_decamelize')
+call operator#user#define('camelize', 'operator#camelize#camelize')
+call operator#user#define('decamelize', 'operator#camelize#decamelize')
" Restore 'cpoptions' {{{

0 comments on commit c50378c

Please sign in to comment.