Skip to content

Commit 1b5ec1f

Browse files
committed
Replace :ScratchBufferCleanAllOf {ext} by :ScratchBufferClean
1 parent ec55edd commit 1b5ec1f

File tree

5 files changed

+20
-28
lines changed

5 files changed

+20
-28
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ Of course, you can open other file types too!
5555
## :gear: Other Features
5656

5757
```vim
58-
" Delete all scratch files and Markdown buffers that is opened by :ScratchBufferOpen
59-
:ScratchBufferCleanAllOf md
58+
" Delete all scratch files and buffers
59+
:ScratchBufferClean
6060
```

autoload/scratch_buffer.vim

+9-15
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,20 @@ function! s:find_fresh_tmp_file(pattern) abort
4444
return v:null
4545
endfunction
4646

47-
" Params:
48-
" - file_ext {string} same as scratch_buffer#open's file_ext
49-
function! scratch_buffer#clean_all_of(file_ext) abort
47+
" Clean up all scratch buffers and files
48+
function! scratch_buffer#clean() abort
5049
const all_buffer_names = scratch_buffer#helper#get_all_buffer_names()
51-
const pattern = $'{g:scratch_buffer_tmp_file_pattern}.{a:file_ext}'
52-
53-
for i in range(0, 100000)
54-
let scratch = expand(printf(pattern, i))
55-
let file_exists = filereadable(scratch)
56-
let buffer_exists = all_buffer_names->scratch_buffer#helper#contains(scratch)
57-
58-
if !file_exists && !buffer_exists
59-
return
60-
endif
6150

62-
if file_exists
51+
const base_pattern = printf(g:scratch_buffer_tmp_file_pattern, '*')
52+
const files = glob(base_pattern .. '*', 0, 1)
53+
for scratch in files
54+
" Delete file if exists
55+
if filereadable(scratch)
6356
call delete(scratch)
6457
endif
6558

66-
if buffer_exists
59+
" Delete buffer if exists
60+
if all_buffer_names->scratch_buffer#helper#contains(scratch)
6761
execute ':bwipe' bufnr(scratch)
6862
endif
6963
endfor

doc/vim-scratch-buffer.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ COMMANDS *scratch-buffer-commands*
116116
or doesn't resize if this is omitted.
117117

118118

119-
*:ScratchBufferCleanAllOf*
120-
:ScratchBufferCleanAllOf [file-extension]
119+
*:ScratchBufferClean*
120+
:ScratchBufferClean
121121
Delete all scratch files and buffers that
122122
were opened by `:ScratchBufferOpen`.
123123

@@ -134,9 +134,9 @@ scratch_buffer#open([file-extension | --no-filetype][, open-method][, buffer-siz
134134
- Otherwise:
135135
Uses the specified extension as filetype
136136

137-
*scratch_buffer#clean_all_of()*
138-
scratch_buffer#clean_all_of({file-extension})
139-
Same as `:ScratchBufferCleanAllOf`.
137+
*scratch_buffer#clean()*
138+
scratch_buffer#clean()
139+
Same as `:ScratchBufferClean`.
140140

141141

142142
==============================================================================

plugin/scratch_buffer.vim

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ let g:loaded_scratch_buffer = v:true
1313
" `:ScratchBufferOpen md sp 5`
1414
command! -bar -nargs=* ScratchBufferOpen call scratch_buffer#open(<f-args>)
1515

16-
" Example:
17-
" `:ScratchBufferCleanAllOf md`
18-
command! -bar -nargs=1 ScratchBufferCleanAllOf call scratch_buffer#clean_all_of(<q-args>)
16+
command! -bar -nargs=0 ScratchBufferClean call scratch_buffer#clean()
1917

2018
let g:scratch_buffer_tmp_file_pattern = '/tmp/vim-scratch-buffer-%d'

test/scratch_buffer.vim

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ endfunction
77

88
function! s:suite.before_each() abort
99
" Clean all created scratch files and buffers
10-
ScratchBufferCleanAllOf md
10+
ScratchBufferClean
1111
endfunction
1212

1313
function! s:suite.can_make_buffer() abort
@@ -57,8 +57,8 @@ function! s:suite.wipes_opened_files_and_buffer() abort
5757
\ all_buffer_names->scratch_buffer#helper#contains(second_file),
5858
\ ).not.to_equal(-1)
5959

60-
" Wipe all
61-
ScratchBufferCleanAllOf md
60+
" Wipe all scratch buffers and files
61+
ScratchBufferClean
6262
const new_all_buffer_names = scratch_buffer#helper#get_all_buffer_names()
6363

6464
call s:expect(filereadable(first_file)).not.to_equal(1)

0 commit comments

Comments
 (0)