-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_copy_reference.vim
More file actions
168 lines (129 loc) · 4.98 KB
/
python_copy_reference.vim
File metadata and controls
168 lines (129 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
let g:python_copy_reference = get(g:, 'python_copy_reference', {})
function! python_copy_reference#_jump_to_parent()
" Specialized version of `Python_jump`:
" https://github.com/vim/vim/blob/v8.2.5172/runtime/ftplugin/python.vim#L88
" Move to the start of line so that inner `def`/`class` will not be included in search.
normal! 0
" `v` to avoid escaping `()` and `|`.
let pattern = '\v^(class|def|async def)'
" `b` flag to search backward.
let flags = 'Wb'
" Search the nearest `def`/`class` backward.
call search(pattern, flags)
endfunction
function! python_copy_reference#_get_nearest_name()
" Move to next word if it's not `def`/`class` yet.
if expand('<cword>') == 'async'
execute 'normal! w'
endif
execute 'normal! w'
return expand('<cword>')
endfunction
function! python_copy_reference#_get_module(path_format, separator)
let file_path = expand(a:path_format)
" Truncate files opened with full paths using CWD.
let file_path = fnamemodify(file_path, ":.")
let module = python_copy_reference#_remove_prefixes(file_path)
if a:separator == '.'
let module = substitute(module, '\/', a:separator, 'g')
endif
return module
endfunction
function! python_copy_reference#_get_attributes(allow_nested, separator)
let current_word = expand('<cword>')
let pattern = '\v^(class|def|async)'
if match(current_word, pattern) == -1
return ''
endif
let current_column = col('.')
let name = python_copy_reference#_get_nearest_name()
" `def`/`class` is already at the gutter/edge.
if current_column == 1
" Return the file path + function/class.
return name
elseif !a:allow_nested
return ''
else
call python_copy_reference#_jump_to_parent()
let parent_name = python_copy_reference#_get_nearest_name()
" Return the file path + function/class + inner function/class/method.
return parent_name . a:separator . name
endif
endfunction
function! python_copy_reference#_remove_prefixes(file_path)
if !has_key(g:python_copy_reference, 'remove_prefixes')
return a:file_path
endif
for path in g:python_copy_reference['remove_prefixes']
let pattern = '^' . path . '/'
if match(a:file_path, pattern) == 0
return substitute(a:file_path, pattern, '', 'g')
endif
endfor
return a:file_path
endfunction
function! python_copy_reference#_get_reference(format)
" Mark the current cursor/column location.
" Use `X` instead of `x` to minimize collision with user's marks.
execute 'normal! mX'
" The cursor could be in an indented/nested function/class/method.
" So, move the cursor to the start of line to check if it has `def`/`class` keyword.
execute 'normal! ^'
if a:format == 'dotted'
" Path format (without file extension): foo/bar/baz
let path_format = '%:r'
let separator = '.'
let allow_nested = 1
elseif a:format == 'pytest'
" Path format (with file extension): foo/bar/baz.py
let path_format = '%:p:.'
let separator = '::'
let allow_nested = 1
elseif a:format == 'import'
" Path format (without file extension): foo/bar/baz
let path_format = '%:r'
let separator = '.'
let allow_nested = 0
elseif a:format == 'nose'
" Path format (without file extension): foo/bar/baz
let path_format = '%:r'
let separator = '.'
let module_attr_separator = ':'
let allow_nested = 1
endif
let module = python_copy_reference#_get_module(path_format, separator)
let attributes = python_copy_reference#_get_attributes(allow_nested, separator)
if empty(attributes)
let reference = module
else
let module_attr_separator = exists('module_attr_separator') ? module_attr_separator : separator
let reference = module . module_attr_separator . attributes
endif
" Go back to marked/initial cursor for better UX.
execute 'normal! `X'
return reference
endfunction
function! python_copy_reference#dotted()
let reference = python_copy_reference#_get_reference('dotted')
echomsg 'Copied reference: ' . reference
let @+ = reference
endfunction
function! python_copy_reference#pytest()
let reference = python_copy_reference#_get_reference('pytest')
echomsg 'Copied reference: ' . reference
let @+ = reference
endfunction
function! python_copy_reference#import()
let reference = python_copy_reference#_get_reference('import')
" Convert 'x.y.z' into 'x.y import z'
let reference = substitute(reference, '^\(.*\)\.\([^.]*\)$', '\1 import \2', 'g')
let reference = 'from ' . reference
" Copy to system clipboard.
echomsg 'Copied reference: ' . reference
let @+ = reference
endfunction
function! python_copy_reference#nose()
let reference = python_copy_reference#_get_reference('nose')
echomsg 'Copied reference: ' . reference
let @+ = reference
endfunction