From bd983c7e8164817cc88e9e71d4f13d58a97f81e0 Mon Sep 17 00:00:00 2001 From: Graham Herceg Date: Sun, 12 Nov 2023 23:25:26 -0500 Subject: [PATCH 1/2] Add support for nose test format When targeting specific tests in nose, there are two path options: 1) some.package.path:SomeClass.some_function 2) some/package/path.py:SomeClass.some_function This adds support for option 1. I found that for either option, an additional separator needs to be introduced, whether it is used to separate the module from the attributes, or the attributes themselves. --- autoload/python_copy_reference.vim | 33 ++++++++++++++++++++++-------- plugin/python-copy-reference.vim | 1 + test/nose.vader | 30 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 test/nose.vader diff --git a/autoload/python_copy_reference.vim b/autoload/python_copy_reference.vim index 89b7394..710be32 100644 --- a/autoload/python_copy_reference.vim +++ b/autoload/python_copy_reference.vim @@ -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('') let pattern = '\v^(class|def|async)' if match(current_word, pattern) == -1 - return [] + return '' endif let current_column = col('.') @@ -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 @@ -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' @@ -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 diff --git a/plugin/python-copy-reference.vim b/plugin/python-copy-reference.vim index 07f7174..ba44871 100644 --- a/plugin/python-copy-reference.vim +++ b/plugin/python-copy-reference.vim @@ -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() diff --git a/test/nose.vader b/test/nose.vader new file mode 100644 index 0000000..1f274ca --- /dev/null +++ b/test/nose.vader @@ -0,0 +1,30 @@ +Do (copy reference): + :e! some/package/path.py\ + :let g:python_copy_reference = {}\ + :call python_copy_reference#nose()\ + +Do (paste reference): + "+p + +Expect: + some.package.path + +Do (clean up): + let @+ = '' + :%bd!\ + +Do (copy reference with attributes): + :e! some/package/path.py\ + idef SomeClass():\ def some_function():\ pass\ + k + :let g:python_copy_reference = {}\ + :call python_copy_reference#nose()\ + +Do (paste reference): + "+p + +Expect: + some.package.path:SomeClass.some_function + +Do (clean up): + :%bd!\ From f889905d3f8855413499034a9b5ebe9f5d669e54 Mon Sep 17 00:00:00 2001 From: Graham Herceg Date: Mon, 13 Nov 2023 20:56:59 -0500 Subject: [PATCH 2/2] Add attribute test for pytest --- test/pytest.vader | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/pytest.vader b/test/pytest.vader index c0603c9..e76b146 100644 --- a/test/pytest.vader +++ b/test/pytest.vader @@ -11,3 +11,19 @@ Expect: Do (clean up): :%bd!\ + +Do (copy reference with attributes): + :e! some/package/path.py\ + idef SomeClass():\ def some_function():\ pass\ + k + :let g:python_copy_reference = {}\ + :call python_copy_reference#pytest()\ + +Do (paste reference): + "+p + +Expect: + some/package/path.py::SomeClass::some_function + +Do (clean up): + :%bd!\