Skip to content

Commit

Permalink
Allow users to disable looping
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Jul 29, 2024
1 parent 21bf127 commit bde47cd
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 13 deletions.
47 changes: 34 additions & 13 deletions autoload/sideways.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function! sideways#Parse()
function! sideways#Parse() abort
let defined = {}
let definitions = g:sideways_definitions

Expand All @@ -17,7 +17,9 @@ function! sideways#Parse()
return sideways#parsing#Parse(definitions)
endfunction

function! sideways#MoveLeft()
function! sideways#MoveLeft(...) abort
let opts = a:0 > 0 ? opts : {'loop': g:sideways_loop_move}

let [definition, items] = sideways#Parse()
if empty(items)
return 0
Expand All @@ -30,6 +32,10 @@ function! sideways#MoveLeft()
endif

if active_index == 0
if !opts.loop
return 0
endif

let first = items[active_index]
let second = items[last_index]
let new_active_index = last_index
Expand All @@ -47,7 +53,9 @@ function! sideways#MoveLeft()
return 1
endfunction

function! sideways#MoveRight()
function! sideways#MoveRight(...) abort
let opts = a:0 > 0 ? opts : {'loop': g:sideways_loop_move}

let [definition, items] = sideways#Parse()
if empty(items)
return 0
Expand All @@ -60,6 +68,10 @@ function! sideways#MoveRight()
endif

if active_index == last_index
if !opts.loop
return 0
endif

let first = items[0]
let second = items[last_index]
let new_active_index = 0
Expand All @@ -77,7 +89,9 @@ function! sideways#MoveRight()
return 1
endfunction

function! sideways#JumpLeft()
function! sideways#JumpLeft(...) abort
let opts = a:0 > 0 ? opts : {'loop': g:sideways_loop_jump}

let [_, items] = sideways#Parse()
if empty(items)
return 0
Expand All @@ -90,6 +104,10 @@ function! sideways#JumpLeft()
endif

if active_index == 0
if !opts.loop
return 0
endif

call s:JumpToItem(items, last_index)
else
call s:JumpToItem(items, active_index - 1)
Expand All @@ -98,9 +116,10 @@ function! sideways#JumpLeft()
return 1
endfunction

function! sideways#JumpRight()
let [_, items] = sideways#Parse()
function! sideways#JumpRight(...) abort
let opts = a:0 > 0 ? opts : {'loop': g:sideways_loop_jump}

let [_, items] = sideways#Parse()
if empty(items)
return 0
end
Expand All @@ -111,9 +130,11 @@ function! sideways#JumpRight()
return 0
endif

let position = getpos('.')

if active_index == last_index
if !opts.loop
return 0
endif

call s:JumpToItem(items, 0)
else
call s:JumpToItem(items, active_index + 1)
Expand All @@ -122,7 +143,7 @@ function! sideways#JumpRight()
return 1
endfunction

function! sideways#AroundCursor(parsed_items)
function! sideways#AroundCursor(parsed_items) abort
let items = a:parsed_items
if empty(items)
return []
Expand All @@ -146,7 +167,7 @@ function! sideways#AroundCursor(parsed_items)
return [previous, current, next]
endfunction

function s:JumpToItem(items, index)
function s:JumpToItem(items, index) abort
let position = getpos('.')
let position[1] = a:items[a:index].start_line
let position[2] = a:items[a:index].start_col
Expand All @@ -161,7 +182,7 @@ endfunction
" a:first is expected to be positioned before a:second. Assuming that, the
" function first places the second item and then the first one, ensuring that
" the column number remain consistent until it's done.
function! s:Swap(first, second)
function! s:Swap(first, second) abort
let first_body = sideways#util#GetItem(a:first)
let second_body = sideways#util#GetItem(a:second)

Expand All @@ -178,7 +199,7 @@ endfunction
" item.
"
" Returns the index of the found item, or -1 if it's not found.
function! s:FindActiveItem(items)
function! s:FindActiveItem(items) abort
if len(a:items) == 0
return -1
endif
Expand Down Expand Up @@ -208,7 +229,7 @@ endfunction
" This means that patterns defined in a:source will take priority over
" a:extension.
"
function! s:ExtendDefinitions(source, extension)
function! s:ExtendDefinitions(source, extension) abort
let defined = {}
let result = []

Expand Down
13 changes: 13 additions & 0 deletions doc/sideways.txt
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ This variable controls whether the plugin will activate vim-repeat for the
mappings to add a new item. The setup for them is a bit more complicated,
which is why this flag exists to disable it if it causes problems.

*g:sideways_loop_jump*
*g:sideways_loop_move*
>
let g:sideways_loop_jump = 0
let g:sideways_loop_move = 0
<
Default value: 1 for both

These two variables control whether moving or jumping will loop around from
the last item to the first one (going left) and from the first to the last
(going right). By default, the plugin loops, but if you want to, for instance,
mash a key to get to the first/last item, it could be useful to avoid it.


==============================================================================
ISSUES *sideways-issues*
Expand Down
8 changes: 8 additions & 0 deletions plugin/sideways.vim
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ if !exists('g:sideways_add_item_repeat')
let g:sideways_add_item_repeat = 1
endif

if !exists('g:sideways_loop_move')
let g:sideways_loop_move = 1
endif

if !exists('g:sideways_loop_jump')
let g:sideways_loop_jump = 1
endif

command! SidewaysLeft call sideways#MoveLeft() | silent! call repeat#set("\<Plug>SidewaysLeft")
command! SidewaysRight call sideways#MoveRight() | silent! call repeat#set("\<Plug>SidewaysRight")

Expand Down
110 changes: 110 additions & 0 deletions spec/plugin/looping_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require 'spec_helper'

describe "looping" do
let(:filename) { 'test.py' }

describe "jumping" do
before :each do
set_file_contents <<-EOF
def function(one, two, three):
pass
EOF
end

after :each do
vim.command('let g:sideways_loop_jump = 1')
end

specify "active by default" do
vim.search('one')
vim.jump_left.normal('~').write
assert_file_contents <<-EOF
def function(one, two, Three):
pass
EOF

vim.jump_right.normal('~').write
assert_file_contents <<-EOF
def function(One, two, Three):
pass
EOF
end

specify "can be disabled" do
vim.command('let g:sideways_loop_jump = 0')

vim.search('one')
vim.jump_left.normal('~').write
assert_file_contents <<-EOF
def function(One, two, three):
pass
EOF

vim.search('three')
vim.jump_right.normal('~').write
assert_file_contents <<-EOF
def function(One, two, Three):
pass
EOF

vim.jump_left.normal('~').write
assert_file_contents <<-EOF
def function(One, Two, Three):
pass
EOF
end
end

describe "moving" do
before :each do
set_file_contents <<-EOF
def function(one, two, three):
pass
EOF
end

after :each do
vim.command('let g:sideways_loop_move = 1')
end

specify "active by default" do
vim.search('one')
vim.left.write
assert_file_contents <<-EOF
def function(three, two, one):
pass
EOF

vim.right.write
assert_file_contents <<-EOF
def function(one, two, three):
pass
EOF
end

specify "can be disabled" do
vim.command('let g:sideways_loop_move = 0')

vim.search('one')
vim.left.write
assert_file_contents <<-EOF
def function(one, two, three):
pass
EOF

vim.search('three')
vim.right.write
assert_file_contents <<-EOF
def function(one, two, three):
pass
EOF

vim.search('two')
vim.right.write
assert_file_contents <<-EOF
def function(one, three, two):
pass
EOF
end
end
end

0 comments on commit bde47cd

Please sign in to comment.