|
| 1 | +" ============================================================== |
| 2 | + |
| 3 | +" Repository: https://github.com/chxuan/cppmode |
| 4 | +" Create Date: 2018-05-31 |
| 5 | +" License: MIT |
| 6 | +" ============================================================== |
| 7 | + |
| 8 | +" 转到定义 |
| 9 | +function! cppmode#go_to_fun_impl#go_to_fun_impl() |
| 10 | + if <sid>go_to_impl_in_impl_file() == -1 |
| 11 | + if <sid>go_to_impl_in_head_file() == -1 |
| 12 | + echo "Not found function implement" |
| 13 | + endif |
| 14 | + endif |
| 15 | +endfunction |
| 16 | + |
| 17 | +" 在实现文件查找函数实现 |
| 18 | +function! s:go_to_impl_in_impl_file() |
| 19 | + let file_path = <sid>get_impl_file_path() |
| 20 | + |
| 21 | + if cppmode#util#is_exist(file_path) |
| 22 | + return <sid>go_to_impl_with_class(file_path) |
| 23 | + endif |
| 24 | + |
| 25 | + return -1 |
| 26 | +endfunction |
| 27 | + |
| 28 | +" 在头文件查找函数实现 |
| 29 | +function! s:go_to_impl_in_head_file() |
| 30 | + let file_path = <sid>get_head_file_path() |
| 31 | + |
| 32 | + if cppmode#util#is_exist(file_path) && <sid>go_to_impl_with_class(file_path) == -1 |
| 33 | + return <sid>go_to_impl_not_with_class(file_path) |
| 34 | + endif |
| 35 | + |
| 36 | + return -1 |
| 37 | +endfunction |
| 38 | + |
| 39 | +" 获得实现文件路径 |
| 40 | +function! s:get_impl_file_path() |
| 41 | + let file_path = cppmode#util#get_current_file_path() |
| 42 | + let suffix = cppmode#util#get_current_file_suffix() |
| 43 | + |
| 44 | + if suffix == "cpp" |
| 45 | + return file_path |
| 46 | + elseif suffix == "h" |
| 47 | + return cppmode#util#substr(file_path, 0, len(file_path) - 1) . "cpp" |
| 48 | + else |
| 49 | + return "" |
| 50 | + endif |
| 51 | + |
| 52 | +endfunction |
| 53 | + |
| 54 | +" 获得头文件路径 |
| 55 | +function! s:get_head_file_path() |
| 56 | + let file_path = cppmode#util#get_current_file_path() |
| 57 | + let suffix = cppmode#util#get_current_file_suffix() |
| 58 | + |
| 59 | + if suffix == "h" |
| 60 | + return file_path |
| 61 | + elseif suffix == "cpp" |
| 62 | + return cppmode#util#substr(file_path, 0, len(file_path) - 3) . "h" |
| 63 | + else |
| 64 | + return "" |
| 65 | + endif |
| 66 | + |
| 67 | +endfunction |
| 68 | + |
| 69 | +" 转到函数实现 |
| 70 | +function! s:go_to_impl_with_class(file_path) |
| 71 | + let fun_name = cppmode#util#get_current_cursor_word() |
| 72 | + let lines = cppmode#util#read_file(a:file_path) |
| 73 | + let row_num = <sid>get_row_num_with_class(lines, fun_name) |
| 74 | + |
| 75 | + if row_num != -1 |
| 76 | + if a:file_path != cppmode#util#get_current_file_path() |
| 77 | + call cppmode#util#open_tab(a:file_path) |
| 78 | + endif |
| 79 | + |
| 80 | + let text = cppmode#util#get_row_text(row_num) |
| 81 | + let col_num = cppmode#util#find(text, "::" . fun_name . "(") + 3 |
| 82 | + call cppmode#util#set_cursor_position(row_num, col_num) |
| 83 | + |
| 84 | + return 1 |
| 85 | + endif |
| 86 | + |
| 87 | + return -1 |
| 88 | +endfunction |
| 89 | + |
| 90 | +" 转到函数实现 |
| 91 | +function! s:go_to_impl_not_with_class(file_path) |
| 92 | + let fun_name = cppmode#util#get_current_cursor_word() |
| 93 | + let lines = cppmode#util#read_file(a:file_path) |
| 94 | + let row_num = <sid>get_row_num_not_with_class(lines, fun_name) |
| 95 | + |
| 96 | + if row_num != -1 |
| 97 | + if a:file_path != cppmode#util#get_current_file_path() |
| 98 | + call cppmode#util#open_tab(a:file_path) |
| 99 | + endif |
| 100 | + |
| 101 | + let text = cppmode#util#get_row_text(row_num) |
| 102 | + let col_num = cppmode#util#find(text, fun_name) + 1 |
| 103 | + call cppmode#util#set_cursor_position(row_num, col_num) |
| 104 | + |
| 105 | + return 1 |
| 106 | + endif |
| 107 | + |
| 108 | + return -1 |
| 109 | +endfunction |
| 110 | + |
| 111 | +" 获得行号 |
| 112 | +function! s:get_row_num_with_class(lines, fun_name) |
| 113 | + let target = "::" . a:fun_name . "(" |
| 114 | + |
| 115 | + for i in range(0, len(a:lines) - 1) |
| 116 | + if cppmode#util#is_contains(a:lines[i], target) |
| 117 | + return i + 1 |
| 118 | + endif |
| 119 | + endfor |
| 120 | + |
| 121 | + return -1 |
| 122 | +endfunction |
| 123 | + |
| 124 | +" 获得行号 |
| 125 | +function! s:get_row_num_not_with_class(lines, fun_name) |
| 126 | + let target = a:fun_name . "(" |
| 127 | + |
| 128 | + for i in range(0, len(a:lines) - 1) |
| 129 | + if cppmode#util#is_contains(a:lines[i], target) && (cppmode#util#is_contains(a:lines[i], "{") && cppmode#util#is_contains(a:lines[i], "}") || cppmode#util#is_contains(a:lines[i + 1], "{")) |
| 130 | + return i + 1 |
| 131 | + endif |
| 132 | + endfor |
| 133 | + |
| 134 | + return -1 |
| 135 | +endfunction |
| 136 | + |