Skip to content

Commit 6af1cdc

Browse files
committed
Fix python import regex
1 parent 9e64404 commit 6af1cdc

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

autoload/sj/python.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function! sj#python#JoinTuple()
9292
endfunction
9393

9494
function! sj#python#SplitImport()
95-
let import_pattern = '^from \%(.*\) import \zs.*$'
95+
let import_pattern = '^\s*from \%(.*\) import \zs.*$'
9696

9797
normal! 0
9898
if search(import_pattern, 'Wc', line('.')) <= 0
@@ -141,7 +141,7 @@ function! sj#python#SplitImport()
141141
endfunction
142142

143143
function! sj#python#JoinImportWithNewlineEscape()
144-
let import_pattern = '^from \%(.*\) import .*\\\s*$'
144+
let import_pattern = '^\s*from \%(.*\) import .*\\\s*$'
145145
if getline('.') !~ import_pattern
146146
return 0
147147
endif
@@ -160,7 +160,7 @@ function! sj#python#JoinImportWithNewlineEscape()
160160
endfunction
161161

162162
function! sj#python#JoinImportWithRoundBrackets()
163-
let import_pattern = '^from \%(.*\) import \zs('
163+
let import_pattern = '^\s*from \%(.*\) import \zs('
164164
if search(import_pattern, 'Wc') <= 0
165165
return 0
166166
endif

spec/plugin/python_spec.rb

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,50 @@
6262
end
6363

6464
specify "imports" do
65-
set_file_contents 'from foo import bar, baz'
65+
set_file_contents <<~EOF
66+
def surrounding_function():
67+
from foo import bar, baz
68+
EOF
6669

70+
vim.search('from foo')
6771
split
6872

6973
assert_file_contents <<~EOF
70-
from foo import bar,\\
71-
baz
74+
def surrounding_function():
75+
from foo import bar,\\
76+
baz
7277
EOF
7378

7479
join
75-
assert_file_contents 'from foo import bar, baz'
80+
assert_file_contents <<~EOF
81+
def surrounding_function():
82+
from foo import bar, baz
83+
EOF
7684

7785
vim.command 'let b:splitjoin_python_import_style = "round_brackets"'
7886
split
7987

8088
assert_file_contents <<~EOF
81-
from foo import (bar,
82-
baz)
89+
def surrounding_function():
90+
from foo import (bar,
91+
baz)
8392
EOF
8493

8594
join
86-
assert_file_contents 'from foo import bar, baz'
95+
assert_file_contents <<~EOF
96+
def surrounding_function():
97+
from foo import bar, baz
98+
EOF
8799

88100
vim.command 'let b:splitjoin_python_brackets_on_separate_lines = 1'
89101
split
90102

91103
assert_file_contents <<~EOF
92-
from foo import (
93-
bar,
94-
baz
95-
)
104+
def surrounding_function():
105+
from foo import (
106+
bar,
107+
baz
108+
)
96109
EOF
97110

98111
join

0 commit comments

Comments
 (0)