From 0ee9c3e470ed8c0bb7a09eabcd592e149f5e2627 Mon Sep 17 00:00:00 2001 From: Sandeep Date: Mon, 7 May 2012 14:58:41 +0530 Subject: [PATCH] rename controllers - in progress --- autoload/common.vim | 58 +++++++++++++++++++++++- plugin/refactorings/renamecontroller.vim | 32 +++++++++++++ plugin/ruby-refactoring.vim | 24 +++++----- 3 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 plugin/refactorings/renamecontroller.vim diff --git a/autoload/common.vim b/autoload/common.vim index 47561f3..3bef488 100644 --- a/autoload/common.vim +++ b/autoload/common.vim @@ -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() @@ -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 diff --git a/plugin/refactorings/renamecontroller.vim b/plugin/refactorings/renamecontroller.vim new file mode 100644 index 0000000..7c93745 --- /dev/null +++ b/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 diff --git a/plugin/ruby-refactoring.vim b/plugin/ruby-refactoring.vim index 2a39d2e..33d2eed 100644 --- a/plugin/ruby-refactoring.vim +++ b/plugin/ruby-refactoring.vim @@ -2,17 +2,18 @@ " Author: Sandeep Ravichandran " " Load all refactoring recipes -exec 'runtime ' . expand(':p:h') . '/refactorings/general/*.vim' +exec 'runtime ' . expand(':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: @@ -22,9 +23,10 @@ endif if g:ruby_refactoring_map_keys vnoremap riv :RenameInstanceVariable - vnoremap rv :RenameLocalVariable - noremap ic :IndentFile - noremap mf :MoveCurrentFile - noremap cf :CopyCurrentFile - vnoremap em :ExtractMethod + vnoremap rv :RenameLocalVariable + noremap ic :IndentFile + noremap mf :MoveCurrentFile + noremap cf :CopyCurrentFile + vnoremap rv :RenameLocalVariable + noremap rc :RenameController endif