Created
August 5, 2018 22:17
-
-
Save deris/7f2068a262ae79846cfdf5ac4f7c65d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command -nargs=1 CppCreateClass call s:cpp_create_class(<q-args>) | |
function! s:cpp_create_class(class_name) | |
if a:class_name !~ '^\a\w*$' | |
echom printf('[error] invalid parameter(%s)', a:class_name) | |
return | |
endif | |
let class_name = substitute(a:class_name, '^.', '\u&', '') | |
let file_name = substitute(a:class_name, '^.\+$', '\L&\E', '') | |
let file_name_cpp = file_name . '.cpp' | |
let file_name_h = file_name . '.h' | |
let inc_guard = substitute(a:class_name, '^.\+$', '\U&\E_H', '') | |
if filereadable(file_name_h) | |
echom printf('[error] file already exists(%s)', file_name_h) | |
return | |
endif | |
if filereadable(file_name_cpp) | |
echom printf('[error] file already exists(%s)', file_name_cpp) | |
return | |
endif | |
let h_content = [ | |
\ '#ifndef ' . inc_guard, | |
\ '#define ' . inc_guard, | |
\ '', | |
\ '/**', | |
\ ' * \brief ' . class_name . 'クラス', | |
\ ' */', | |
\ 'class ' . class_name . ' {', | |
\ 'public:', | |
\ "\t/**", | |
\ "\t * \\brief コンストラクタ", | |
\ "\t */", | |
\ "\t" . class_name . '();', | |
\ "\t/**", | |
\ "\t * \\brief デストラクタ", | |
\ "\t */", | |
\ "\tvirtual ~" . class_name . '();', | |
\ '};', | |
\ '', | |
\ '#endif // ' . inc_guard, | |
\ ] | |
let cpp_content = [ | |
\ '#include "' . file_name_h . '"', | |
\ '', | |
\ class_name . '::' . class_name . '()', | |
\ '{', | |
\ '}', | |
\ '', | |
\ class_name . '::~' . class_name . '()', | |
\ '{', | |
\ '}', | |
\ ] | |
call writefile(h_content, file_name_h, 'a') | |
call writefile(cpp_content, file_name_cpp, 'a') | |
endfunction | |
command -nargs=0 -range CppAddMethod call s:cpp_add_method_impl() | |
function! s:cpp_add_method_impl() | |
let cur_file = expand('%') | |
if cur_file =~ '\.h\%(pp\)$' | |
echom printf('[error] current buffer is not header') | |
return | |
endif | |
let lnum = search('^\%(class\|struct\)\s\+\u\w\+', 'bcnW') | |
if lnum == 0 | |
echom printf('[error] class not found') | |
return | |
endif | |
let lstr = getline(lnum) | |
let class_name = matchstr(lstr, '^\%(class\|struct\)\s\+\zs\w\+') | |
let method_content = [] | |
for lnum in range(line("'<"), line("'>")) | |
call add(method_content, getline(lnum)) | |
endfor | |
if method_content[0] !~ '\ze\%(operator\s*\)\?\S\+\s*(' | |
echom '[error] select method in first line' | |
return | |
endif | |
let method_content[0] = substitute(method_content[0], '^\s\+', '', '') | |
let method_content[0] = substitute(method_content[0], '\%(explicit\|virtual\)\s\+', '', 'g') | |
let method_content[0] = substitute(method_content[0], '\ze\%(operator\s*\)\?\S\+\s*(', class_name.'::', '') | |
let method_content[-1] = substitute(method_content[-1], ';.*', '', '') | |
let file_name_cpp = substitute(cur_file, '\.h\%(pp\)\?$', '.cpp', '') | |
if !filereadable(file_name_cpp) | |
echom printf('[error] cpp file not found(%s)', file_name_cpp) | |
return | |
endif | |
call writefile(extend(['', '{', '}'], method_content, 1), file_name_cpp, 'a') | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment