From 6babbee58fbe84ef92bb8372c42d8fabb6abb2d3 Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Sat, 18 Nov 2023 21:14:37 +0100 Subject: [PATCH] Add support for variables in python for-loops --- doc/sideways.txt | 7 ++ ftplugin/python/sideways.vim | 6 + spec/plugin/python_functions_spec.rb | 95 ---------------- spec/plugin/python_imports_spec.rb | 41 ------- spec/plugin/python_spec.rb | 158 +++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 136 deletions(-) delete mode 100644 spec/plugin/python_functions_spec.rb delete mode 100644 spec/plugin/python_imports_spec.rb create mode 100644 spec/plugin/python_spec.rb diff --git a/doc/sideways.txt b/doc/sideways.txt index 17ee1b1..61b3295 100644 --- a/doc/sideways.txt +++ b/doc/sideways.txt @@ -182,6 +182,13 @@ Typescript enum values code: 200 | 404 | 500; } < +Python imports and for loops: +> + from some_package import Foo, Bar + + for value, index in enumerate(items): + pass +< See |sideways-customization| for instructions on how to add more definitions or override existing ones. diff --git a/ftplugin/python/sideways.vim b/ftplugin/python/sideways.vim index 6aaa4df..c3ffcf5 100644 --- a/ftplugin/python/sideways.vim +++ b/ftplugin/python/sideways.vim @@ -9,4 +9,10 @@ let b:sideways_definitions = [ \ 'delimiter': ',\s*', \ 'brackets': ['', ''], \ }, + \ { + \ 'start': '\', + \ 'delimiter': ',\s*', + \ 'brackets': ['([{''"', ')]}''"'], + \ }, \ ] diff --git a/spec/plugin/python_functions_spec.rb b/spec/plugin/python_functions_spec.rb deleted file mode 100644 index 865c91b..0000000 --- a/spec/plugin/python_functions_spec.rb +++ /dev/null @@ -1,95 +0,0 @@ -require 'spec_helper' - -describe "python functions" do - let(:filename) { 'test.py' } - - before :each do - set_file_contents <<-EOF - def function(one, two, three): - pass - EOF - - vim.search('one') - end - - specify "to the left" do - vim.left - assert_file_contents <<-EOF - def function(three, two, one): - pass - EOF - - vim.left - assert_file_contents <<-EOF - def function(three, one, two): - pass - EOF - - vim.left - assert_file_contents <<-EOF - def function(one, three, two): - pass - EOF - end - - specify "to the right" do - vim.right - assert_file_contents <<-EOF - def function(two, one, three): - pass - EOF - - vim.right - assert_file_contents <<-EOF - def function(two, three, one): - pass - EOF - - vim.right - assert_file_contents <<-EOF - def function(one, three, two): - pass - EOF - end - - specify "incomplete function call" do - set_file_contents <<-EOF - def function(one, two, three - pass - EOF - - vim.search('one') - vim.right - assert_file_contents <<-EOF - def function(two, one, three - pass - EOF - end - - specify "extra whitespace" do - set_file_contents <<-EOF - def function( one, two, three ): - pass - EOF - - vim.search('one') - vim.right - assert_file_contents <<-EOF - def function( two, one, three ): - pass - EOF - end - - specify "complicated function call" do - set_file_contents <<-EOF - foo(bar, baz(foobar(), foobaz)) - EOF - - vim.search('bar') - vim.right - - assert_file_contents <<-EOF - foo(baz(foobar(), foobaz), bar) - EOF - end -end diff --git a/spec/plugin/python_imports_spec.rb b/spec/plugin/python_imports_spec.rb deleted file mode 100644 index 4afc50e..0000000 --- a/spec/plugin/python_imports_spec.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'spec_helper' - -describe "python imports" do - let(:filename) { 'test.py' } - - specify "basic import" do - set_file_contents <<-EOF - import foo, bar as b, baz - EOF - - vim.search('foo') - - vim.left - assert_file_contents <<-EOF - import baz, bar as b, foo - EOF - - vim.right - assert_file_contents <<-EOF - import foo, bar as b, baz - EOF - end - - specify "import with from" do - set_file_contents <<-EOF - from some_package import Foo, Bar - EOF - - vim.search('Foo') - - vim.left - assert_file_contents <<-EOF - from some_package import Bar, Foo - EOF - - vim.right - assert_file_contents <<-EOF - from some_package import Foo, Bar - EOF - end -end diff --git a/spec/plugin/python_spec.rb b/spec/plugin/python_spec.rb new file mode 100644 index 0000000..f2611ec --- /dev/null +++ b/spec/plugin/python_spec.rb @@ -0,0 +1,158 @@ +require 'spec_helper' + +describe "python" do + let(:filename) { 'test.py' } + + describe "functions" do + before :each do + set_file_contents <<-EOF + def function(one, two, three): + pass + EOF + + vim.search('one') + end + + specify "to the left" do + vim.left + assert_file_contents <<-EOF + def function(three, two, one): + pass + EOF + + vim.left + assert_file_contents <<-EOF + def function(three, one, two): + pass + EOF + + vim.left + assert_file_contents <<-EOF + def function(one, three, two): + pass + EOF + end + + specify "to the right" do + vim.right + assert_file_contents <<-EOF + def function(two, one, three): + pass + EOF + + vim.right + assert_file_contents <<-EOF + def function(two, three, one): + pass + EOF + + vim.right + assert_file_contents <<-EOF + def function(one, three, two): + pass + EOF + end + + specify "incomplete function call" do + set_file_contents <<-EOF + def function(one, two, three + pass + EOF + + vim.search('one') + vim.right + assert_file_contents <<-EOF + def function(two, one, three + pass + EOF + end + + specify "extra whitespace" do + set_file_contents <<-EOF + def function( one, two, three ): + pass + EOF + + vim.search('one') + vim.right + assert_file_contents <<-EOF + def function( two, one, three ): + pass + EOF + end + + specify "complicated function call" do + set_file_contents <<-EOF + foo(bar, baz(foobar(), foobaz)) + EOF + + vim.search('bar') + vim.right + + assert_file_contents <<-EOF + foo(baz(foobar(), foobaz), bar) + EOF + end + end + + describe "imports" do + specify "basic import" do + set_file_contents <<-EOF + import foo, bar as b, baz + EOF + + vim.search('foo') + + vim.left + assert_file_contents <<-EOF + import baz, bar as b, foo + EOF + + vim.right + assert_file_contents <<-EOF + import foo, bar as b, baz + EOF + end + + specify "import with from" do + set_file_contents <<-EOF + from some_package import Foo, Bar + EOF + + vim.search('Foo') + + vim.left + assert_file_contents <<-EOF + from some_package import Bar, Foo + EOF + + vim.right + assert_file_contents <<-EOF + from some_package import Foo, Bar + EOF + end + end + + describe "for loops" do + specify "basic for loop" do + set_file_contents <<-EOF + for index, value in enumerate(items): + pass + EOF + + vim.search('index') + + vim.left + assert_file_contents <<-EOF + for value, index in enumerate(items): + pass + EOF + + vim.right + assert_file_contents <<-EOF + for index, value in enumerate(items): + pass + EOF + end + end +end