8000 Merge pull request #1145 from lieryan/lieryan-operator-select-logical… · python-mode/python-mode@85d2cd1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 85d2cd1

Browse files
authored
Merge pull request #1145 from lieryan/lieryan-operator-select-logical-line
Text object/operator/motion mapping to select logical line
2 parents 6d81349 + 7c5731d commit 85d2cd1

File tree

6 files changed

+85
8000 -5
lines changed

6 files changed

+85
-5
lines changed

after/ftplugin/python.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ if g:pymode_motion
4242
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>
4343
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
45+
onoremap <buffer> V :<C-U>call pymode#rope#select_logical_line()<CR>
46+
4547
endif
4648

4749
if g:pymode_rope && g:pymode_rope_completion

autoload/pymode/rope.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,7 @@ fun! pymode#rope#generate_package() "{{{
194194
endif
195195
PymodePython rope.GenerateElementRefactoring('package').run()
196196
endfunction "}}}
197+
198+
fun! pymode#rope#select_logical_line() "{{{
199+
PymodePython rope.select_logical_line()
200+
endfunction "}}}

doc/pymode.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,11 @@ Key Command
209209
]] Jump to next class or function (normal, visual, operator modes)
210210
[M Jump to previous class or method (normal, visual, operator modes)
211211
]M Jump to next class or method (normal, visual, operator modes)
212-
aC Select a class. Ex: vaC, daC, yaC, caC (normal, operator modes)
213-
iC Select inner class. Ex: viC, diC, yiC, ciC (normal, operator modes)
214-
aM Select a function or method. Ex: vaM, daM, yaM, caM (normal, operator modes)
215-
iM Select inner function or method. Ex: viM, diM, yiM, ciM (normal, operator modes)
212+
aC Select a class. Ex: vaC, daC, yaC, caC (operator modes)
213+
iC Select inner class. Ex: viC, diC, yiC, ciC (operator modes)
214+
aM Select a function or method. Ex: vaM, daM, yaM, caM (operator modes)
215+
iM Select inner function or method. Ex: viM, diM, yiM, ciM (operator modes)
216+
V Select logical line. Ex: dV, yV, cV (operator modes), also works with count
216217
==== ============================
217218

218219
Enable pymode-motion *'g:pymode_motion'*

pymode/environment.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,8 @@ def goto_buffer(bufnr):
242242
if str(bufnr) != '-1':
243243
vim.command('buffer %s' % bufnr)
244244

245+
def select_line(self, start, end):
246+
vim.command('normal %sggV%sgg' % (start, end))
247+
245248

246249
env = VimPymodeEnviroment()

pymode/rope.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import site
66
import sys
77

8-
from rope.base import project, libutils, exceptions, change, worder, pycore
8+
from rope.base import project, libutils, exceptions, change, worder, pycore, codeanalyze
99
from rope.base.fscommands import FileSystemCommands # noqa
1010
from rope.base.taskhandle import TaskHandle # noqa
1111
from rope.contrib import autoimport as rope_autoimport, codeassist, findit, generate # noqa
@@ -921,6 +921,22 @@ def _insert_import(name, module, ctx):
921921
reload_changes(changes)
922922

923923

924+
@env.catch_exceptions
925+
def select_logical_line():
926+
source, offset = env.get_offset_params()
927+
count = int(env.var('v:count1'))
928+
929+
lines = codeanalyze.SourceLinesAdapter(source)
930+
start_line = lines.get_line_number(offset)
931+
line_finder = codeanalyze.LogicalLineFinder(lines)
932+
933+
start_lineno, end_lineno = line_finder.logical_line_in(start_line)
934+
for _, (_, end_lineno) in zip(range(count), line_finder.generate_regions(start_lineno)):
935+
pass
936+
937+
env.select_line(start_lineno, end_lineno)
938+
939+
924940
# Monkey patch Rope
925941
def find_source_folders(self, folder):
926942
"""Look only python files an packages."""

tests/test_procedures_vimscript/textobject.vim

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
set noautoindent
2+
let g:pymode_rope=1
3+
14
" Load sample python file.
25
" With 'def'.
36
execute "normal! idef func1():\<CR> a = 1\<CR>"
@@ -22,6 +25,57 @@ let content=getline('^', '$')
2225
call assert_true(content == ['class Class2():', ' b = 2', '', 'class Class1():', ' a = 1'])
2326

2427

28+
" Clean file.
29+
%delete
30+
31+
" With 'def'.
32+
execute "normal! iprint(\<CR> 1\<CR>)\<CR>"
33+
execute "normal! iprint(\<CR> 2\<CR>)\<CR>"
34+
execute "normal! iprint(\<CR> 3\<CR>)\<CR>"
35+
normal 4ggdV
36+
37+
let content=getline('^', '$')
38+
call assert_true(content == [
39+
\ "print(", " 1", ")",
40+
\ "print(", " 3", ")",
41+
\ ""
42+
\])
43+
44+
45+
" Clean file.
46+
%delete
47+
48+
" With 'def'.
49+
execute "normal! iprint(\<CR> 1\<CR>)\<CR>"
50+
execute "normal! iprint(\<CR> 2\<CR>)\<CR>"
51+
execute "normal! iprint(\<CR> 3\<CR>)\<CR>"
52+
execute "normal! iprint(\<CR> 4\<CR>)\<CR>"
53+
normal 5ggd2V
54+
55+
let content=getline('^', '$')
56+
call assert_true(content == [
57+
\ "print(", " 1", ")",
58+
\ "print(", " 4", ")",
59+
\ ""
60+
\])
61+
62+
" Clean file.
63+
%delete
64+
65+
" With 'def'.
66+
execute "normal! iprint(\<CR> 1\<CR>)\<CR>"
67+
execute "normal! iprint(\<CR> 2\<CR>)\<CR>"
68+
execute "normal! iprint(\<CR> 3\<CR>)\<CR>"
69+
execute "normal! iprint(\<CR> 4\<CR>)\<CR>"
70+
normal 5ggd2V
71+
72+
let content=getline('^', '$')
73+
call assert_true(content == [
74+
\ "print(", " 1", ")",
75+
\ "print(", " 4", ")",
76+
\ ""
77+
\])
78+
2579
if len(v:errors) > 0
2680
cquit!
2781
else

0 commit comments

Comments
 (0)
0