Skip to content

Commit

Permalink
rename controllers - in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepr-sourcebits committed May 7, 2012
1 parent e97b51f commit 0ee9c3e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 12 deletions.
58 changes: 57 additions & 1 deletion autoload/common.vim
Expand Up @@ -106,6 +106,18 @@ function! common#get_file_path()
return expand('%:p')
endfunction

" Synopsis:
" Return controller path for the parameter
function! common#get_controller_path()
return "app/controllers/"
endfunction

" Synopsis:
" Return helper path for the parameter
function! common#get_helper_path()
return "app/helpers/"
endfunction

" Synopsis:
" Return the current method name
function! common#get_method_name()
Expand Down Expand Up @@ -152,7 +164,51 @@ endfunction

" Synopsis:
" Copies a file from one location to another
function! common#move(source, destination)
function! common#copy(source, destination)
call system("cp ".a:source." ".a:destination)
return a:destination
endfunction

" Synopsis:
" Convert word to mixedcase
function! common#mixedcase(word)
return substitute(s:camelcase(a:word),'^.','\u&','')
endfunction

" Synopsis:
" Convert word to camelcase
function! common#camelcase(word)
let word = substitute(a:word, '-', '_', 'g')
if word !~# '_' && word =~# '\l'
return substitute(word,'^.','\l&','')
else
return substitute(word,'\C\(_\)\=\(.\)','\=submatch(1)==""?tolower(submatch(2)) : toupper(submatch(2))','g')
endif
endfunction

" Synopsis:
" Convert word to snakecase
function! common#snakecase(word)
let word = substitute(a:word,'::','/','g')
let word = substitute(word,'\(\u\+\)\(\u\l\)','\1_\2','g')
let word = substitute(word,'\(\l\|\d\)\(\u\)','\1_\2','g')
let word = substitute(word,'-','_','g')
let word = tolower(word)
return word
endfunction

" Synopsis:
" Convert snakecase to controller_name
function! common#controller(word)
let word = common#snakecase(a:word)
let word = word."_controller.rb"
return word
endfunction

" Synopsis:
" Convert snakecase to helper_name
function! common#helper(word)
let word = common#snakecase(a:word)
let word = word."_helper.rb"
return word
endfunction
32 changes: 32 additions & 0 deletions plugin/refactorings/renamecontroller.vim
@@ -0,0 +1,32 @@
" Synopsis:
" Rename current controller along with corresponding files
function! RenameController()
try

let newcontroller = common#get_input("Controller name: ", "ERROR:: Enter a controller name." )
catch
echo v:exception
return
endtry

" Rename Rails Controller

" Rename controller filename
let path = common#get_controller_path()
let newcontrollerpath = path.common#controller(newcontroller)
let oldcontrollerpath = path.common#controller(split(common#get_file_name(), ".rb")[0])
call common#move(oldcontrollerpath, newcontrollerpath)

" Rename helper filename
let path = common#get_helper_path()
let newhelperpath = path.common#helper(newcontroller)
let oldhelperpath = path.common#helper(split(common#get_file_name(), ".rb")[0])
call common#move(oldcontrollerpath, newcontrollerpath)
"
" Rename views folder
"
" Rename helper Classname
"
" Rename Controller Classname

endfunction
24 changes: 13 additions & 11 deletions plugin/ruby-refactoring.vim
Expand Up @@ -2,17 +2,18 @@
" Author: Sandeep Ravichandran
"
" Load all refactoring recipes
exec 'runtime ' . expand('<sfile>:p:h') . '/refactorings/general/*.vim'
exec 'runtime ' . expand('<sfile>:p:h') . '/refactorings/*.vim'

" Commands:
"

command! -range RenameInstanceVariable call RenameInstanceVariable()
command! -range RenameLocalVariable call RenameLocalVariable()
command! -range IndentFile call IndentProject()
command! -range MoveCurrentFile call MoveCurrentFile()
command! -range CopyCurrentFile call CopyCurrentFile()
command! -range ExtractMethod call ExtractMethod()
command! -range RenameLocalVariable call RenameLocalVariable()
command! -range IndentFile call IndentProject()
command! -range MoveCurrentFile call MoveCurrentFile()
command! -range CopyCurrentFile call CopyCurrentFile()
command! -range ExtractMethod call ExtractMethod()
command! -range RenameController call RenameController()

" Mappings:

Expand All @@ -22,9 +23,10 @@ endif

if g:ruby_refactoring_map_keys
vnoremap <leader>riv :RenameInstanceVariable<cr>
vnoremap <leader>rv :RenameLocalVariable<cr>
noremap <leader>ic :IndentFile<cr>
noremap <leader>mf :MoveCurrentFile<cr>
noremap <leader>cf :CopyCurrentFile<cr>
vnoremap <leader>em :ExtractMethod<cr>
vnoremap <leader>rv :RenameLocalVariable<cr>
noremap <leader>ic :IndentFile<cr>
noremap <leader>mf :MoveCurrentFile<cr>
noremap <leader>cf :CopyCurrentFile<cr>
vnoremap <leader>rv :RenameLocalVariable<cr>
noremap <leader>rc :RenameController<cr>
endif

0 comments on commit 0ee9c3e

Please sign in to comment.