Skip to content

Commit

Permalink
Add support for variables in python for-loops
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Nov 18, 2023
1 parent f66a98c commit 6babbee
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 136 deletions.
7 changes: 7 additions & 0 deletions doc/sideways.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions ftplugin/python/sideways.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ let b:sideways_definitions = [
\ 'delimiter': ',\s*',
\ 'brackets': ['', ''],
\ },
\ {
\ 'start': '\<for ',
\ 'end': ' in\>',
\ 'delimiter': ',\s*',
\ 'brackets': ['([{''"', ')]}''"'],
\ },
\ ]
95 changes: 0 additions & 95 deletions spec/plugin/python_functions_spec.rb

This file was deleted.

41 changes: 0 additions & 41 deletions spec/plugin/python_imports_spec.rb

This file was deleted.

158 changes: 158 additions & 0 deletions spec/plugin/python_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6babbee

Please sign in to comment.