10000 Merge pull request #1015 from KamranMaharov/method_motion_include_dec… · python-mode/python-mode@f61b74a · GitHub
[go: up one dir, main page]

Skip to content

Commit f61b74a

Browse files
authored
Merge pull request #1015 from KamranMaharov/method_motion_include_decorator
method motions to include decorators
2 parents ac11db2 + 0a90e0f commit f61b74a

File tree

3 files changed

+77
-32
lines changed

3 files changed

+77
-32
lines changed

after/ftplugin/python.vim

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ if g:pymode_motion
3030
vnoremap <buffer> ]M :call pymode#motion#vmove('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', '')<CR>
3131
vnoremap <buffer> [M :call pymode#motion#vmove('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 'b')<CR>
3232
33-
onoremap <buffer> C :<C-U>call pymode#motion#select('^<Bslash>s*class<Bslash>s', 0)<CR>
34-
onoremap <buffer> aC :<C-U>call pymode#motion#select('^<Bslash>s*class<Bslash>s', 0)<CR>
35-
onoremap <buffer> iC :<C-U>call pymode#motion#select('^<Bslash>s*class<Bslash>s', 1)<CR>
36-
vnoremap <buffer> aC :<C-U>call pymode#motion#select('^<Bslash>s*class<Bslash>s', 0)<CR>
37-
vnoremap <buffer> iC :<C-U>call pymode#motion#select('^<Bslash>s*class<Bslash>s', 1)<CR>
38-
39-
onoremap <buffer> M :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 0)<CR>
40-
onoremap <buffer> aM :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 0)<CR>
41-
onoremap <buffer> iM :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 1)<CR>
42-
vnoremap <buffer> aM :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 0)<CR>
43-
vnoremap <buffer> iM :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 1)<CR>
33+
onoremap <buffer> C :<C-U>call pymode#motion#select_c('^<Bslash>s*class<Bslash>s', 0)<CR>
34+
onoremap <buffer> aC :<C-U>call pymode#motion#select_c('^<Bslash>s*class<Bslash>s', 0)<CR>
35+
onoremap <buffer> iC :<C-U>call pymode#motion#select_c('^<Bslash>s*class<Bslash>s', 1)<CR>
36+
vnoremap <buffer> aC :<C-U>call pymode#motion#select_c('^<Bslash>s*class<Bslash>s', 0)<CR>
37+
vnoremap <buffer> iC :<C-U>call pymode#motion#select_c('^<Bslash>s*class<Bslash>s', 1)<CR>
38+
39+
onoremap <buffer> M :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=@', '^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 0)<CR>
40+< 10000 div class="diff-text-inner"> onoremap <buffer> aM :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=@', '^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 0)<CR>
41+
onoremap <buffer> iM :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=@', '^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 1)<CR>
42+
vnoremap <buffer> aM :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=@', '^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 0)<CR>
43+
vnoremap <buffer> iM :<C-U>call pymode#motion#select('^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=@', '^<Bslash>s*<Bslash>(async<Bslash>s<Bslash>+<Bslash>)<Bslash>=def<Bslash>s', 1)<CR>
4444
4545
endif
4646

autoload/pymode/motion.vim

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,26 @@ fun! pymode#motion#pos_le(pos1, pos2) "{{{
2828
return ((a:pos1[0] < a:pos2[0]) || (a:pos1[0] == a:pos2[0] && a:pos1[1] <= a:pos2[1]))
2929
endfunction "}}}
3030

31-
32-
fun! pymode#motion#select(pattern, inner) "{{{
31+
fun! pymode#motion#select(first_pattern, second_pattern, inner) "{{{
3332
let cnt = v:count1 - 1
3433
let orig = getpos('.')[1:2]
35-
let snum = s:BlockStart(orig[0], a:pattern)
36-
if getline(snum) !~ a:pattern
34+
let posns = s:BlockStart(orig[0], a:first_pattern, a:second_pattern)
35+
if getline(posns[0]) !~ a:first_pattern && getline(posns[0]) !~ a:second_pattern
3736
return 0
3837
endif
39-
let enum = s:BlockEnd(snum, indent(snum))
38+
let snum = posns[0]
39+
let enum = s:BlockEnd(posns[1], indent(posns[1]))
4040
while cnt
41-
let lnum = search(a:pattern, 'nW')
41+
let lnum = search(a:second_pattern, 'nW')
4242
if lnum
4343
let enum = s:BlockEnd(lnum, indent(lnum))
4444
call cursor(enum, 1)
4545
endif
4646
let cnt = cnt - 1
4747
endwhile
48-
if pymode#motion#pos_le([snum, 0], orig) && pymode#motion#pos_le(orig, [enum, 1])
48+
if pymode#motion#pos_le([snum, 0], orig) && pymode#motion#pos_le(orig, [enum+1, 0])
4949
if a:inner
50-
let snum = snum + 1
51-
let enum = prevnonblank(enum)
50+
let snum = posns[1] + 1
5251
endif
5352

5453
call cursor(snum, 1)
@@ -57,28 +56,37 @@ fun! pymode#motion#select(pattern, inner) "{{{
5756
endif
5857
endfunction "}}}
5958

59+
fun! pymode#motion#select_c(pattern, inner) "{{{
60+
call pymode#motion#select(a:pattern, a:pattern, a:inner)
61+
endfunction "}}}
6062

61-
fun! s:BlockStart(lnum, ...) "{{{
62-
let pattern = a:0 ? a:1 : '^\s*\(@\|class\s.*:\|def\s\)'
63+
fun! s:BlockStart(lnum, first_pattern, second_pattern) "{{{
6364
let lnum = a:lnum + 1
6465
let indent = 100
6566
while lnum
6667
let lnum = prevnonblank(lnum - 1)
6768
let test = indent(lnum)
6869
let line = getline(lnum)
69-
if line =~ '^\s*#' " Skip comments
70-
continue
71-
elseif !test " Zero-level regular line
72-
return lnum
73-
elseif test >= indent " Skip deeper or equal lines
70+
" Skip comments, deeper or equal lines
71+
if line =~ '^\s*#' || test >= indent
7472
continue
75-
" Indent is strictly less at this point: check for def/class
76-
elseif line < F438 span class="pl-k">=~ pattern && line !~ '^\s*@'
77-
return lnum
7873
endif
7974
let indent = indent(lnum)
75+
76+
" Indent is strictly less at this point: check for def/class/@
77+
if line =~ a:first_pattern || line =~ a:second_pattern
78+
while getline(lnum-1) =~ a:first_pattern
79+
let lnum = lnum - 1
80+
endwhile
81+
let first_pos = lnum
82+
while getline(lnum) !~ a:second_pattern
83+
let lnum = lnum + 1
84+
endwhile
85+
let second_pos = lnum
86+
return [first_pos, second_pos]
87+
endif
8088
endwhile
81-
return 0
89+
return [0, 0]
8290
endfunction "}}}
8391

8492

@@ -89,7 +97,7 @@ fun! s:BlockEnd(lnum, ...) "{{{
8997
let lnum = nextnonblank(lnum + 1)
9098
if getline(lnum) =~ '^\s*#' | continue
9199
elseif lnum && indent(lnum) <= indent
92-
return lnum - 1
100+
return prevnonblank(lnum - 1)
93101
endif
94102
endwhile
95103
return line('$')

tests/motion_decorator.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!coding=utf-8
2+
"""
3+
to test pymode motions, please put cursor on each of the lines
4+
and press "vaM" for selecting methods or
5+
"vaC" for selection class.
6+
"""
7+
8+
def a_decorator(func):
9+
print("chamando func")
10+
def wrapped(*args, **kw):
11+
return func(*args, **kw)
12+
print("Pós func")
13+
return wrapped
14+
15+
def b_decorator(func):
16+
print("second chamando func")
17+
def wrapped(*args, **kw):
18+
return func(*args, **kw)
19+
print("second Pós func")
20+
return wrapped
21+
22+
@b_decorator
23+
@a_decorator
24+
def teste():
25+
print("Not Selecting Decorator")
26+
27+
class Teste:
28+
@a_decorator
29+
@b_decorator
30+
def metodo(self):
31+
print("Meu método")
32+
33+
34+
teste()
35+
36+
testinho = Teste()
37+
testinho.metodo()

0 commit comments

Comments
 (0)
0