Commit 42efbfd2 authored by John's avatar John Committed by Thiago de Arruda

Test and refactor `shorten_fname` and `shorten_fname1`

Rename `shorten_fname` -> `path_shorten_fname`
Rename `shorten_fname1` -> `path_shorten_fname_if_possible`
parent c57002a7
......@@ -4255,7 +4255,7 @@ int read_viminfo_bufferlist(vir_T *virp, int writing)
/* Expand "~/" in the file name at "line + 1" to a full path.
* Then try shortening it by comparing with the current directory */
expand_env(xline, NameBuff, MAXPATHL);
sfname = shorten_fname1(NameBuff);
sfname = path_shorten_fname_if_possible(NameBuff);
buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
if (buf != NULL) { /* just in case... */
......
......@@ -19553,7 +19553,7 @@ repeat:
if (p != NULL) {
if (c == '.') {
os_dirname(dirname, MAXPATHL);
s = shorten_fname(p, dirname);
s = path_shorten_fname(p, dirname);
if (s != NULL) {
*fnamep = s;
if (pbuf != NULL) {
......
......@@ -7965,7 +7965,7 @@ eval_vars (
"E495: no autocommand file name to substitute for \"<afile>\"");
return NULL;
}
result = shorten_fname1(result);
result = path_shorten_fname_if_possible(result);
break;
case SPEC_ABUF: /* buffer number for autocommand */
......
......@@ -869,7 +869,7 @@ char_u *vim_findfile(void *search_ctx_arg)
simplify_filename(file_path);
if (os_dirname(ff_expand_buffer, MAXPATHL)
== OK) {
p = shorten_fname(file_path,
p = path_shorten_fname(file_path,
ff_expand_buffer);
if (p != NULL)
STRMOVE(file_path, p);
......
......@@ -4654,7 +4654,7 @@ void shorten_fnames(int force)
|| path_is_absolute_path(buf->b_sfname))) {
vim_free(buf->b_sfname);
buf->b_sfname = NULL;
p = shorten_fname(buf->b_ffname, dirname);
p = path_shorten_fname(buf->b_ffname, dirname);
if (p != NULL) {
buf->b_sfname = vim_strsave(p);
buf->b_fname = buf->b_sfname;
......
......@@ -482,7 +482,7 @@ static void fname2fnum(xfmark_T *fm)
/* Try to shorten the file name. */
os_dirname(IObuff, IOSIZE);
p = shorten_fname(NameBuff, IObuff);
p = path_shorten_fname(NameBuff, IObuff);
/* buflist_new() will call fmarks_check_names() */
(void)buflist_new(NameBuff, p, (linenr_T)1, 0);
......
......@@ -796,7 +796,7 @@ static void uniquefy_paths(garray_T *gap, char_u *pattern)
* /file.txt / /file.txt
* c:\file.txt c:\ .\file.txt
*/
short_name = shorten_fname(path, curdir);
short_name = path_shorten_fname(path, curdir);
if (short_name != NULL && short_name > path + 1
) {
STRCPY(path, ".");
......@@ -817,7 +817,7 @@ static void uniquefy_paths(garray_T *gap, char_u *pattern)
/* If the {filename} is not unique, change it to ./{filename}.
* Else reduce it to {filename} */
short_name = shorten_fname(path, curdir);
short_name = path_shorten_fname(path, curdir);
if (short_name == NULL)
short_name = path;
if (is_unique(short_name, gap, i)) {
......@@ -1708,54 +1708,37 @@ int flags; /* EW_* flags */
}
#endif
#if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \
defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO)
/*
* Try to find a shortname by comparing the fullname with the current
* directory.
* Returns "full_path" or pointer into "full_path" if shortened.
*/
char_u *shorten_fname1(char_u *full_path)
char_u *path_shorten_fname_if_possible(char_u *full_path)
{
char_u *dirname;
char_u *p = full_path;
char_u *dirname = xmalloc(MAXPATHL);
char_u *p = full_path;
dirname = alloc(MAXPATHL);
if (os_dirname(dirname, MAXPATHL) == OK) {
p = shorten_fname(full_path, dirname);
if (p == NULL || *p == NUL)
p = path_shorten_fname(full_path, dirname);
if (p == NULL || *p == NUL) {
p = full_path;
}
}
vim_free(dirname);
return p;
}
#endif
/*
* Try to find a shortname by comparing the fullname with the current
* directory.
* Returns NULL if not shorter name possible, pointer into "full_path"
* otherwise.
*/
char_u *shorten_fname(char_u *full_path, char_u *dir_name)
char_u *path_shorten_fname(char_u *full_path, char_u *dir_name)
{
int len;
char_u *p;
if (full_path == NULL) {
return NULL;
}
assert(dir_name != NULL);
size_t len = strlen((char *)dir_name);
char_u *p = full_path + len;
if (full_path == NULL)
if (fnamencmp(dir_name, full_path, len) != 0
|| !vim_ispathsep(*p)) {
return NULL;
len = (int)STRLEN(dir_name);
if (fnamencmp(dir_name, full_path, len) == 0) {
p = full_path + len;
{
if (vim_ispathsep(*p))
++p;
else
p = NULL;
}
} else
p = NULL;
return p;
}
return p + 1;
}
/*
......
......@@ -83,8 +83,26 @@ int after_pathsep(char_u *b, char_u *p);
int same_directory(char_u *f1, char_u *f2);
int pathcmp(const char *p, const char *q, int maxlen);
int mch_expandpath(garray_T *gap, char_u *path, int flags);
char_u *shorten_fname1(char_u *full_path);
char_u *shorten_fname(char_u *full_path, char_u *dir_name);
/// Try to find a shortname by comparing the fullname with the current
/// directory.
///
/// @param full_path The full path of the file.
/// @return
/// - Pointer into `full_path` if shortened.
/// - `full_path` unchanged if no shorter name is possible.
/// - NULL if `full_path` is NULL.
char_u *path_shorten_fname_if_possible(char_u *full_path);
/// Try to find a shortname by comparing the fullname with `dir_name`.
///
/// @param full_path The full path of the file.
/// @param dir_name The directory to shorten relative to.
/// @return
/// - Pointer into `full_path` if shortened.
/// - NULL if no shorter name is possible.
char_u *path_shorten_fname(char_u *full_path, char_u *dir_name);
int expand_wildcards_eval(char_u **pat, int *num_file, char_u ***file,
int flags);
int expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u *
......
......@@ -2859,7 +2859,7 @@ void ex_vimgrep(exarg_T *eap)
seconds = (time_t)0;
for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi) {
fname = shorten_fname1(fnames[fi]);
fname = path_shorten_fname_if_possible(fnames[fi]);
if (time(NULL) > seconds) {
/* Display the file name every second or so, show the user we are
* working on it. */
......
......@@ -92,6 +92,48 @@ describe 'path function', ->
it 'returns empty string if given file contains no seperator', ->
eq '', path_next_component 'file.txt'
describe 'path_shorten_fname', ->
it 'returns NULL if `full_path` is NULL', ->
dir = to_cstr 'some/directory/file.txt'
eq NULL, (path.path_shorten_fname NULL, dir)
it 'returns NULL if the path and dir does not match', ->
dir = to_cstr 'not/the/same'
full = to_cstr 'as/this.txt'
eq NULL, (path.path_shorten_fname full, dir)
it 'returns NULL if the path is not separated properly', ->
dir = to_cstr 'some/very/long/'
full = to_cstr 'some/very/long/directory/file.txt'
eq NULL, (path.path_shorten_fname full, dir)
it 'shortens the filename if `dir_name` is the start of `full_path`', ->
full = to_cstr 'some/very/long/directory/file.txt'
dir = to_cstr 'some/very/long'
eq 'directory/file.txt', (ffi.string path.path_shorten_fname full, dir)
describe 'path_shorten_fname_if_possible', ->
before_each ->
lfs.mkdir 'ut_directory'
after_each ->
lfs.chdir '..'
lfs.rmdir 'ut_directory'
describe 'path_shorten_fname_if_possible', ->
it 'returns shortened path if possible', ->
lfs.chdir 'ut_directory'
full = to_cstr lfs.currentdir! .. '/subdir/file.txt'
eq 'subdir/file.txt', (ffi.string path.path_shorten_fname_if_possible full)
it 'returns `full_path` if a shorter version is not possible', ->
old = lfs.currentdir!
lfs.chdir 'ut_directory'
full = old .. '/subdir/file.txt'
eq full, (ffi.string path.path_shorten_fname_if_possible to_cstr full)
it 'returns NULL if `full_path` is NULL', ->
eq NULL, (path.path_shorten_fname_if_possible NULL)
describe 'more path function', ->
setup ->
lfs.mkdir 'unit-test-directory'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment