Skip to content

Commit

Permalink
Use the minimum available ID
Browse files Browse the repository at this point in the history
Fixes #309
  • Loading branch information
kassio committed Nov 30, 2020
1 parent 7846193 commit b25fe21
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 30/11/2020
- Change the next id calculation to use the minimum available value.
([\#309](https://github.com/kassio/neoterm/issues/309))
### 05/11/2020
- Better exception handling when toggling neoterm and it's the last open
window.
Expand Down
16 changes: 16 additions & 0 deletions autoload/neoterm.vim
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,19 @@ function! s:resize_instance(instance) abort
exec printf('%s resize %s', a:instance.mod, size)
endif
endfunction

" Calculates the next neoterm's ID.
" Returns the minimum next available id, an id not related
" to an neoterm instance.
function! neoterm#next_id(instances, last_id)
let l:instance_ids = map(keys(a:instances), {_, v -> str2nr(v) })

for i in range(1, max(l:instance_ids) + 1)
if index(l:instance_ids, i) < 0
let l:last_id = i
return l:last_id
end
endfor

return a:last_id + 1
endfunction
4 changes: 2 additions & 2 deletions doc/neoterm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Commands lacking a prefix will always be executed in the last active neoterm.

1.4 ID calculation *neoterm-id-calculation*

The ID is a sequential number starting from 1. To avoid big ID numbers, the ID
is reseted to 1 when there is no instance of a neoterm windows open.
The ID is a numeric value. It's calculates the minimum available value,
starting from 1.

1.4 Last Active *neoterm-last-active*

Expand Down
14 changes: 3 additions & 11 deletions plugin/neoterm.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@ let g:neoterm = {
\ 'managed': []
\ }

" Calculates the next neoterm's ID.
" The ID is a sequential number starting from 1.
" To avoid big ID numbers, the ID is reseted to 1
" when there is no instance of a neoterm windows open
function! g:neoterm.next_id()
if len(keys(l:self.instances)) == 0
let l:self.last_id = 1
return l:self.last_id
else
let l:self.last_id += 1
return l:self.last_id
end
let l:self.last_id = neoterm#next_id(l:self.instances, l:self.last_id)

return l:self.last_id
endfunction

function! g:neoterm.has_any()
Expand Down
23 changes: 23 additions & 0 deletions vmtest/neoterm.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
call vmtest#plugin('neoterm')
let g:vmtests.neoterm.neoterm = { '_name': 'g:neoterm tests' }

function! g:vmtests.neoterm.neoterm.test_when_theres_no_instances()
call assert_equal(
\ 1,
\ neoterm#next_id({}, 0)
\ )
endfunction

function! g:vmtests.neoterm.neoterm.test_when_theres_sequential_instances()
call assert_equal(
\ 4,
\ neoterm#next_id({'1': v:null, '2': v:null, '3': v:null}, 3)
\ )
endfunction

function! g:vmtests.neoterm.neoterm.test_when_instances_are_not_sequential()
call assert_equal(
\ 2,
\ neoterm#next_id({'1': v:null, '3': v:null}, 3)
\ )
endfunction

0 comments on commit b25fe21

Please sign in to comment.