From b25fe21151adde382d3b87532fd9fe8b19f3cff8 Mon Sep 17 00:00:00 2001 From: Kassio Borges Date: Mon, 30 Nov 2020 00:29:42 +0000 Subject: [PATCH] Use the minimum available ID Fixes #309 --- CHANGELOG.md | 3 +++ autoload/neoterm.vim | 16 ++++++++++++++++ doc/neoterm.txt | 4 ++-- plugin/neoterm.vim | 14 +++----------- vmtest/neoterm.vim | 23 +++++++++++++++++++++++ 5 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 vmtest/neoterm.vim diff --git a/CHANGELOG.md b/CHANGELOG.md index eee9d51..ad61296 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/autoload/neoterm.vim b/autoload/neoterm.vim index 765f28b..c3a758f 100644 --- a/autoload/neoterm.vim +++ b/autoload/neoterm.vim @@ -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 diff --git a/doc/neoterm.txt b/doc/neoterm.txt index 0492da1..077a7fc 100644 --- a/doc/neoterm.txt +++ b/doc/neoterm.txt @@ -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* diff --git a/plugin/neoterm.vim b/plugin/neoterm.vim index 812e1de..1b86c13 100644 --- a/plugin/neoterm.vim +++ b/plugin/neoterm.vim @@ -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() diff --git a/vmtest/neoterm.vim b/vmtest/neoterm.vim new file mode 100644 index 0000000..394a7be --- /dev/null +++ b/vmtest/neoterm.vim @@ -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