Test and refactor shorten_fname and shorten_fname1

Rename `shorten_fname` -> `path_shorten_fname`
Rename `shorten_fname1` -> `path_shorten_fname_if_possible`
This commit is contained in:
John
2014-04-13 20:30:53 +02:00
committed by Thiago de Arruda
parent c57002a7c3
commit 42efbfd2fd
10 changed files with 91 additions and 48 deletions

View File

@@ -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. /* Expand "~/" in the file name at "line + 1" to a full path.
* Then try shortening it by comparing with the current directory */ * Then try shortening it by comparing with the current directory */
expand_env(xline, NameBuff, MAXPATHL); 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); buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
if (buf != NULL) { /* just in case... */ if (buf != NULL) { /* just in case... */

View File

@@ -19553,7 +19553,7 @@ repeat:
if (p != NULL) { if (p != NULL) {
if (c == '.') { if (c == '.') {
os_dirname(dirname, MAXPATHL); os_dirname(dirname, MAXPATHL);
s = shorten_fname(p, dirname); s = path_shorten_fname(p, dirname);
if (s != NULL) { if (s != NULL) {
*fnamep = s; *fnamep = s;
if (pbuf != NULL) { if (pbuf != NULL) {

View File

@@ -7965,7 +7965,7 @@ eval_vars (
"E495: no autocommand file name to substitute for \"<afile>\""); "E495: no autocommand file name to substitute for \"<afile>\"");
return NULL; return NULL;
} }
result = shorten_fname1(result); result = path_shorten_fname_if_possible(result);
break; break;
case SPEC_ABUF: /* buffer number for autocommand */ case SPEC_ABUF: /* buffer number for autocommand */

View File

@@ -869,7 +869,7 @@ char_u *vim_findfile(void *search_ctx_arg)
simplify_filename(file_path); simplify_filename(file_path);
if (os_dirname(ff_expand_buffer, MAXPATHL) if (os_dirname(ff_expand_buffer, MAXPATHL)
== OK) { == OK) {
p = shorten_fname(file_path, p = path_shorten_fname(file_path,
ff_expand_buffer); ff_expand_buffer);
if (p != NULL) if (p != NULL)
STRMOVE(file_path, p); STRMOVE(file_path, p);

View File

@@ -4654,7 +4654,7 @@ void shorten_fnames(int force)
|| path_is_absolute_path(buf->b_sfname))) { || path_is_absolute_path(buf->b_sfname))) {
vim_free(buf->b_sfname); vim_free(buf->b_sfname);
buf->b_sfname = NULL; buf->b_sfname = NULL;
p = shorten_fname(buf->b_ffname, dirname); p = path_shorten_fname(buf->b_ffname, dirname);
if (p != NULL) { if (p != NULL) {
buf->b_sfname = vim_strsave(p); buf->b_sfname = vim_strsave(p);
buf->b_fname = buf->b_sfname; buf->b_fname = buf->b_sfname;

View File

@@ -482,7 +482,7 @@ static void fname2fnum(xfmark_T *fm)
/* Try to shorten the file name. */ /* Try to shorten the file name. */
os_dirname(IObuff, IOSIZE); os_dirname(IObuff, IOSIZE);
p = shorten_fname(NameBuff, IObuff); p = path_shorten_fname(NameBuff, IObuff);
/* buflist_new() will call fmarks_check_names() */ /* buflist_new() will call fmarks_check_names() */
(void)buflist_new(NameBuff, p, (linenr_T)1, 0); (void)buflist_new(NameBuff, p, (linenr_T)1, 0);

View File

@@ -796,7 +796,7 @@ static void uniquefy_paths(garray_T *gap, char_u *pattern)
* /file.txt / /file.txt * /file.txt / /file.txt
* c:\file.txt c:\ .\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 if (short_name != NULL && short_name > path + 1
) { ) {
STRCPY(path, "."); 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}. /* If the {filename} is not unique, change it to ./{filename}.
* Else reduce 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) if (short_name == NULL)
short_name = path; short_name = path;
if (is_unique(short_name, gap, i)) { if (is_unique(short_name, gap, i)) {
@@ -1708,54 +1708,37 @@ int flags; /* EW_* flags */
} }
#endif #endif
#if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ char_u *path_shorten_fname_if_possible(char_u *full_path)
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 *dirname; char_u *dirname = xmalloc(MAXPATHL);
char_u *p = full_path; char_u *p = full_path;
dirname = alloc(MAXPATHL);
if (os_dirname(dirname, MAXPATHL) == OK) { if (os_dirname(dirname, MAXPATHL) == OK) {
p = shorten_fname(full_path, dirname); p = path_shorten_fname(full_path, dirname);
if (p == NULL || *p == NUL) if (p == NULL || *p == NUL) {
p = full_path; p = full_path;
}
} }
vim_free(dirname); vim_free(dirname);
return p; return p;
} }
#endif
/* char_u *path_shorten_fname(char_u *full_path, char_u *dir_name)
* 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)
{ {
int len; if (full_path == NULL) {
char_u *p;
if (full_path == NULL)
return NULL; return NULL;
len = (int)STRLEN(dir_name); }
if (fnamencmp(dir_name, full_path, len) == 0) {
p = full_path + len; assert(dir_name != NULL);
{ size_t len = strlen((char *)dir_name);
if (vim_ispathsep(*p)) char_u *p = full_path + len;
++p;
else if (fnamencmp(dir_name, full_path, len) != 0
p = NULL; || !vim_ispathsep(*p)) {
} return NULL;
} else }
p = NULL;
return p; return p + 1;
} }
/* /*

View File

@@ -83,8 +83,26 @@ int after_pathsep(char_u *b, char_u *p);
int same_directory(char_u *f1, char_u *f2); int same_directory(char_u *f1, char_u *f2);
int pathcmp(const char *p, const char *q, int maxlen); int pathcmp(const char *p, const char *q, int maxlen);
int mch_expandpath(garray_T *gap, char_u *path, int flags); 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 expand_wildcards_eval(char_u **pat, int *num_file, char_u ***file,
int flags); int flags);
int expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u * int expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u *

View File

@@ -2859,7 +2859,7 @@ void ex_vimgrep(exarg_T *eap)
seconds = (time_t)0; seconds = (time_t)0;
for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi) { 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) { if (time(NULL) > seconds) {
/* Display the file name every second or so, show the user we are /* Display the file name every second or so, show the user we are
* working on it. */ * working on it. */

View File

@@ -92,6 +92,48 @@ describe 'path function', ->
it 'returns empty string if given file contains no seperator', -> it 'returns empty string if given file contains no seperator', ->
eq '', path_next_component 'file.txt' 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', -> describe 'more path function', ->
setup -> setup ->
lfs.mkdir 'unit-test-directory' lfs.mkdir 'unit-test-directory'