mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
vim-patch:9.0.1007: there is no way to get a list of swap file names
Problem: There is no way to get a list of swap file names. Solution: Add the swapfilelist() function. Use it in the test script to clean up. Remove deleting individual swap files.c216a7a21a
vim-patch:9.0.1005: a failed test may leave a swap file behind Problem: A failed test may leave a swap file behind. Solution: Delete the swap file to avoid another test to fail. Use another file name.d0f8d39d20
Cherry-pick test_window_cmd.vim changes from patch 8.2.1593. Remove FUNC_ATTR_UNUSED from eval functions as fptr is always unused. Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
parent
d82a6ca72a
commit
85c61d6716
@ -506,6 +506,7 @@ submatch({nr} [, {list}]) String or List
|
||||
specific match in ":s" or substitute()
|
||||
substitute({expr}, {pat}, {sub}, {flags})
|
||||
String all {pat} in {expr} replaced with {sub}
|
||||
swapfilelist() List swap files found in 'directory'
|
||||
swapinfo({fname}) Dict information about swap file {fname}
|
||||
swapname({buf}) String swap file of buffer {buf}
|
||||
synID({lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col}
|
||||
@ -8417,6 +8418,17 @@ substitute({string}, {pat}, {sub}, {flags}) *substitute()*
|
||||
Can also be used as a |method|: >
|
||||
GetString()->substitute(pat, sub, flags)
|
||||
|
||||
swapfilelist() *swapfilelist()*
|
||||
Returns a list of swap file names, like what "vim -r" shows.
|
||||
See the |-r| command argument. The 'directory' option is used
|
||||
for the directories to inspect. If you only want to get a
|
||||
list of swap files in the current directory then temporarily
|
||||
set 'directory' to a dot: >
|
||||
let save_dir = &directory
|
||||
let &directory = '.'
|
||||
let swapfiles = swapfilelist()
|
||||
let &directory = save_dir
|
||||
|
||||
swapinfo({fname}) *swapinfo()*
|
||||
The result is a dictionary, which holds information about the
|
||||
swapfile {fname}. The available fields are:
|
||||
|
@ -883,6 +883,7 @@ Buffers, windows and the argument list:
|
||||
getwininfo() get a list with window information
|
||||
getchangelist() get a list of change list entries
|
||||
getjumplist() get a list of jump list entries
|
||||
swapfilelist() list of existing swap files in 'directory'
|
||||
swapinfo() information about a swap file
|
||||
swapname() get the swap file path of a buffer
|
||||
|
||||
|
@ -400,6 +400,7 @@ return {
|
||||
strwidth={args=1, base=1, fast=true},
|
||||
submatch={args={1, 2}, base=1},
|
||||
substitute={args=4, base=1},
|
||||
swapfilelist={},
|
||||
swapinfo={args=1, base=1},
|
||||
swapname={args=1, base=1},
|
||||
synID={args=3},
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include "nvim/eval/executor.h"
|
||||
#include "nvim/eval/funcs.h"
|
||||
#include "nvim/eval/typval.h"
|
||||
#include "nvim/eval/typval_defs.h"
|
||||
#include "nvim/eval/userfunc.h"
|
||||
#include "nvim/eval/vars.h"
|
||||
#include "nvim/eval/window.h"
|
||||
@ -3850,8 +3851,7 @@ static void f_insert(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
}
|
||||
|
||||
/// "interrupt()" function
|
||||
static void f_interrupt(typval_T *argvars FUNC_ATTR_UNUSED, typval_T *rettv FUNC_ATTR_UNUSED,
|
||||
EvalFuncData fptr FUNC_ATTR_UNUSED)
|
||||
static void f_interrupt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
{
|
||||
got_int = true;
|
||||
}
|
||||
@ -8523,6 +8523,13 @@ static void f_substitute(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
}
|
||||
}
|
||||
|
||||
/// "swapfilelist()" function
|
||||
static void f_swapfilelist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
{
|
||||
tv_list_alloc_ret(rettv, kListLenUnknown);
|
||||
recover_names(NULL, false, rettv->vval.v_list, 0, NULL);
|
||||
}
|
||||
|
||||
/// "swapinfo(swap_filename)" function
|
||||
static void f_swapinfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
{
|
||||
|
@ -466,7 +466,7 @@ int main(int argc, char **argv)
|
||||
// Recovery mode without a file name: List swap files.
|
||||
// Uses the 'dir' option, therefore it must be after the initializations.
|
||||
if (recoverymode && fname == NULL) {
|
||||
recover_names(NULL, true, 0, NULL);
|
||||
recover_names(NULL, true, NULL, 0, NULL);
|
||||
os_exit(0);
|
||||
}
|
||||
|
||||
|
@ -762,7 +762,7 @@ void ml_recover(bool checkext)
|
||||
directly = false;
|
||||
|
||||
// count the number of matching swap files
|
||||
len = recover_names(fname, false, 0, NULL);
|
||||
len = recover_names(fname, false, NULL, 0, NULL);
|
||||
if (len == 0) { // no swap files found
|
||||
semsg(_("E305: No swap file found for %s"), fname);
|
||||
goto theend;
|
||||
@ -772,7 +772,7 @@ void ml_recover(bool checkext)
|
||||
i = 1;
|
||||
} else { // several swap files found, choose
|
||||
// list the names of the swap files
|
||||
(void)recover_names(fname, true, 0, NULL);
|
||||
(void)recover_names(fname, true, NULL, 0, NULL);
|
||||
msg_putchar('\n');
|
||||
msg_puts(_("Enter number of swap file to use (0 to quit): "));
|
||||
i = get_number(false, NULL);
|
||||
@ -781,7 +781,7 @@ void ml_recover(bool checkext)
|
||||
}
|
||||
}
|
||||
// get the swap file name that will be used
|
||||
(void)recover_names(fname, false, i, &fname_used);
|
||||
(void)recover_names(fname, false, NULL, i, &fname_used);
|
||||
}
|
||||
if (fname_used == NULL) {
|
||||
goto theend; // user chose invalid number.
|
||||
@ -1200,13 +1200,15 @@ theend:
|
||||
/// - list the swap files for "vim -r"
|
||||
/// - count the number of swap files when recovering
|
||||
/// - list the swap files when recovering
|
||||
/// - list the swap files for swapfilelist()
|
||||
/// - find the name of the n'th swap file when recovering
|
||||
///
|
||||
/// @param fname base for swap file name
|
||||
/// @param list when true, list the swap file names
|
||||
/// @param do_list when true, list the swap file names
|
||||
/// @param ret_list when not NULL add file names to it
|
||||
/// @param nr when non-zero, return nr'th swap file name
|
||||
/// @param fname_out result when "nr" > 0
|
||||
int recover_names(char *fname, int list, int nr, char **fname_out)
|
||||
int recover_names(char *fname, bool do_list, list_T *ret_list, int nr, char **fname_out)
|
||||
{
|
||||
int num_names;
|
||||
char *(names[6]);
|
||||
@ -1230,7 +1232,7 @@ int recover_names(char *fname, int list, int nr, char **fname_out)
|
||||
fname_res = fname;
|
||||
}
|
||||
|
||||
if (list) {
|
||||
if (do_list) {
|
||||
// use msg() to start the scrolling properly
|
||||
msg(_("Swap files found:"));
|
||||
msg_putchar('\n');
|
||||
@ -1306,9 +1308,11 @@ int recover_names(char *fname, int list, int nr, char **fname_out)
|
||||
}
|
||||
}
|
||||
|
||||
// remove swapfile name of the current buffer, it must be ignored
|
||||
// Remove swapfile name of the current buffer, it must be ignored.
|
||||
// But keep it for swapfilelist().
|
||||
if (curbuf->b_ml.ml_mfp != NULL
|
||||
&& (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL) {
|
||||
&& (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL
|
||||
&& ret_list == NULL) {
|
||||
for (int i = 0; i < num_files; i++) {
|
||||
// Do not expand wildcards, on Windows would try to expand
|
||||
// "%tmp%" in "%tmp%file"
|
||||
@ -1333,7 +1337,7 @@ int recover_names(char *fname, int list, int nr, char **fname_out)
|
||||
*fname_out = xstrdup(files[nr - 1 + num_files - file_count]);
|
||||
dirp = ""; // stop searching
|
||||
}
|
||||
} else if (list) {
|
||||
} else if (do_list) {
|
||||
if (dir_name[0] == '.' && dir_name[1] == NUL) {
|
||||
if (fname == NULL) {
|
||||
msg_puts(_(" In current directory:\n"));
|
||||
@ -1359,6 +1363,14 @@ int recover_names(char *fname, int list, int nr, char **fname_out)
|
||||
msg_puts(_(" -- none --\n"));
|
||||
}
|
||||
ui_flush();
|
||||
} else if (ret_list != NULL) {
|
||||
for (int i = 0; i < num_files; i++) {
|
||||
char *name = concat_fnames(dir_name, files[i], true);
|
||||
if (name != NULL) {
|
||||
tv_list_append_string(ret_list, name, -1);
|
||||
xfree(name);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
file_count += num_files;
|
||||
}
|
||||
|
@ -130,6 +130,14 @@ if has('mac')
|
||||
let $BASH_SILENCE_DEPRECATION_WARNING = 1
|
||||
endif
|
||||
|
||||
|
||||
" A previous (failed) test run may have left swap files behind. Delete them
|
||||
" before running tests again, they might interfere.
|
||||
for name in s:GetSwapFileList()
|
||||
call delete(name)
|
||||
endfor
|
||||
|
||||
|
||||
" Prepare for calling test_garbagecollect_now().
|
||||
let v:testing = 1
|
||||
|
||||
@ -152,6 +160,22 @@ if has('reltime')
|
||||
let g:func_start = reltime()
|
||||
endif
|
||||
|
||||
" Get the list of swap files in the current directory.
|
||||
func s:GetSwapFileList()
|
||||
let save_dir = &directory
|
||||
let &directory = '.'
|
||||
let files = swapfilelist()
|
||||
let &directory = save_dir
|
||||
|
||||
" remove a match with runtest.vim
|
||||
let idx = indexof(files, 'v:val =~ "runtest.vim."')
|
||||
if idx >= 0
|
||||
call remove(files, idx)
|
||||
endif
|
||||
|
||||
return files
|
||||
endfunc
|
||||
|
||||
" Invoked when a test takes too much time.
|
||||
func TestTimeout(id)
|
||||
split test.log
|
||||
@ -294,6 +318,16 @@ func RunTheTest(test)
|
||||
endif
|
||||
call add(s:messages, message)
|
||||
let s:done += 1
|
||||
|
||||
" Check if the test has left any swap files behind. Delete them before
|
||||
" running tests again, they might interfere.
|
||||
let swapfiles = s:GetSwapFileList()
|
||||
if len(swapfiles) > 0
|
||||
call add(s:messages, "Found swap files: " .. string(swapfiles))
|
||||
for name in swapfiles
|
||||
call delete(name)
|
||||
endfor
|
||||
endif
|
||||
endfunc
|
||||
|
||||
func AfterTheTest(func_name)
|
||||
|
@ -115,6 +115,15 @@ func Test_swapinfo()
|
||||
w
|
||||
let fname = s:swapname()
|
||||
call assert_match('Xswapinfo', fname)
|
||||
|
||||
let nr = 0
|
||||
for name in swapfilelist()
|
||||
if name =~ '[\\/]' .. fname .. '$'
|
||||
let nr += 1
|
||||
endif
|
||||
endfor
|
||||
call assert_equal(1, nr)
|
||||
|
||||
let info = fname->swapinfo()
|
||||
|
||||
let ver = printf('VIM %d.%d', v:version / 100, v:version % 100)
|
||||
|
@ -460,7 +460,7 @@ func Test_invalid_name()
|
||||
endfunc
|
||||
|
||||
func Test_ownsyntax()
|
||||
new Xfoo
|
||||
new XfooOwnSyntax
|
||||
call setline(1, '#define FOO')
|
||||
syntax on
|
||||
set filetype=c
|
||||
|
@ -119,10 +119,9 @@ endfunc
|
||||
|
||||
" Test the ":wincmd ^" and "<C-W>^" commands.
|
||||
func Test_window_split_edit_alternate()
|
||||
|
||||
" Test for failure when the alternate buffer/file no longer exists.
|
||||
edit Xfoo | %bw
|
||||
call assert_fails(':wincmd ^', 'E23')
|
||||
call assert_fails(':wincmd ^', 'E23:')
|
||||
|
||||
" Test for the expected behavior when we have two named buffers.
|
||||
edit Xfoo | edit Xbar
|
||||
@ -152,12 +151,11 @@ endfunc
|
||||
|
||||
" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
|
||||
func Test_window_split_edit_bufnr()
|
||||
|
||||
%bwipeout
|
||||
let l:nr = bufnr('%') + 1
|
||||
call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92')
|
||||
call assert_fails(':' . l:nr . 'wincmd ^', 'E16')
|
||||
call assert_fails(':0wincmd ^', 'E16')
|
||||
call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:')
|
||||
call assert_fails(':' . l:nr . 'wincmd ^', 'E16:')
|
||||
call assert_fails(':0wincmd ^', 'E16:')
|
||||
|
||||
edit Xfoo | edit Xbar | edit Xbaz
|
||||
let l:foo_nr = bufnr('Xfoo')
|
||||
|
Loading…
Reference in New Issue
Block a user