Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for copying references to use with nose #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions autoload/python_copy_reference.vim
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ function! python_copy_reference#_get_module(path_format, separator)
endfunction


function! python_copy_reference#_get_attribute_parts(allow_nested)
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 []
return ''
endif

let current_column = col('.')
Expand All @@ -57,15 +57,15 @@ function! python_copy_reference#_get_attribute_parts(allow_nested)
" `def`/`class` is already at the gutter/edge.
if current_column == 1
" Return the file path + function/class.
return [name]
return name
elseif !a:allow_nested
return []
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, name]
return parent_name . a:separator . name
endif
endfunction

Expand Down Expand Up @@ -111,11 +111,21 @@ function! python_copy_reference#_get_reference(format)
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 attribute_parts = python_copy_reference#_get_attribute_parts(allow_nested)
let reference = join([module] + attribute_parts, 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'
Expand Down Expand Up @@ -149,3 +159,10 @@ function! python_copy_reference#import()
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
1 change: 1 addition & 0 deletions plugin/python-copy-reference.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ let g:loaded_python_copy_reference = 1
command! -nargs=0 PythonCopyReferenceDotted call python_copy_reference#dotted()
command! -nargs=0 PythonCopyReferencePytest call python_copy_reference#pytest()
command! -nargs=0 PythonCopyReferenceImport call python_copy_reference#import()
command! -nargs=0 PythonCopyReferenceNose call python_copy_reference#nose()
30 changes: 30 additions & 0 deletions test/nose.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Do (copy reference):
:e! some/package/path.py\<cr>
:let g:python_copy_reference = {}\<cr>
:call python_copy_reference#nose()\<cr>

Do (paste reference):
"+p

Expect:
some.package.path

Do (clean up):
let @+ = ''
:%bd!\<cr>

Do (copy reference with attributes):
:e! some/package/path.py\<cr>
idef SomeClass():\<cr> def some_function():\<cr> pass\<esc>
k
:let g:python_copy_reference = {}\<cr>
:call python_copy_reference#nose()\<cr>

Do (paste reference):
"+p

Expect:
some.package.path:SomeClass.some_function

Do (clean up):
:%bd!\<cr>
16 changes: 16 additions & 0 deletions test/pytest.vader
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ Expect:

Do (clean up):
:%bd!\<cr>

Do (copy reference with attributes):
:e! some/package/path.py\<cr>
idef SomeClass():\<cr> def some_function():\<cr> pass\<esc>
k
:let g:python_copy_reference = {}\<cr>
:call python_copy_reference#pytest()\<cr>

Do (paste reference):
"+p

Expect:
some/package/path.py::SomeClass::some_function

Do (clean up):
:%bd!\<cr>