mirror of
https://github.com/neovim/neovim.git
synced 2024-12-20 03:05:11 -07:00
Fix style issues.
* Rename mch_full_name to mch_get_absolute_path. * Rename mch_is_full_name to mch_is_absolute_path. * Add a lot of missing parentheses. * Remove yoda-conditions for consistency. * Remove spaces in function declaration.
This commit is contained in:
parent
fdba1761f6
commit
fc86866402
@ -2418,7 +2418,7 @@ void buflist_altfpos(win_T *win)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Return TRUE if 'ffname' is not the same file as current file.
|
* Return TRUE if 'ffname' is not the same file as current file.
|
||||||
* Fname must have a full path (expanded by mch_full_name()).
|
* Fname must have a full path (expanded by mch_get_absolute_path()).
|
||||||
*/
|
*/
|
||||||
int otherfile(char_u *ffname)
|
int otherfile(char_u *ffname)
|
||||||
{
|
{
|
||||||
|
@ -12468,7 +12468,7 @@ static void f_resolve(typval_T *argvars, typval_T *rettv)
|
|||||||
q[-1] = NUL;
|
q[-1] = NUL;
|
||||||
q = gettail(p);
|
q = gettail(p);
|
||||||
}
|
}
|
||||||
if (q > p && !mch_is_full_name(buf)) {
|
if (q > p && !mch_is_absolute_path(buf)) {
|
||||||
/* symlink is relative to directory of argument */
|
/* symlink is relative to directory of argument */
|
||||||
cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
|
cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
|
||||||
if (cpy != NULL) {
|
if (cpy != NULL) {
|
||||||
|
@ -3945,7 +3945,7 @@ expand_shellcmd (
|
|||||||
flags |= EW_FILE | EW_EXEC;
|
flags |= EW_FILE | EW_EXEC;
|
||||||
|
|
||||||
/* For an absolute name we don't use $PATH. */
|
/* For an absolute name we don't use $PATH. */
|
||||||
if (mch_is_full_name(pat))
|
if (mch_is_absolute_path(pat))
|
||||||
path = (char_u *)" ";
|
path = (char_u *)" ";
|
||||||
else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
|
else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
|
||||||
|| (pat[1] == '.' && vim_ispathsep(pat[2])))))
|
|| (pat[1] == '.' && vim_ispathsep(pat[2])))))
|
||||||
|
@ -4770,7 +4770,7 @@ void shorten_fnames(int force)
|
|||||||
&& !path_with_url(buf->b_fname)
|
&& !path_with_url(buf->b_fname)
|
||||||
&& (force
|
&& (force
|
||||||
|| buf->b_sfname == NULL
|
|| buf->b_sfname == NULL
|
||||||
|| mch_is_full_name(buf->b_sfname))) {
|
|| mch_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 = shorten_fname(buf->b_ffname, dirname);
|
||||||
|
@ -3382,7 +3382,7 @@ int resolve_symlink(char_u *fname, char_u *buf)
|
|||||||
* portion of the filename (if any) and the path the symlink
|
* portion of the filename (if any) and the path the symlink
|
||||||
* points to.
|
* points to.
|
||||||
*/
|
*/
|
||||||
if (mch_is_full_name(buf))
|
if (mch_is_absolute_path(buf))
|
||||||
STRCPY(tmp, buf);
|
STRCPY(tmp, buf);
|
||||||
else {
|
else {
|
||||||
char_u *tail;
|
char_u *tail;
|
||||||
|
@ -7945,7 +7945,7 @@ static void expand_path_option(char_u *curdir, garray_T *gap)
|
|||||||
else if (path_with_url(buf))
|
else if (path_with_url(buf))
|
||||||
/* URL can't be used here */
|
/* URL can't be used here */
|
||||||
continue;
|
continue;
|
||||||
else if (!mch_is_full_name(buf)) {
|
else if (!mch_is_absolute_path(buf)) {
|
||||||
/* Expand relative path to their full path equivalent */
|
/* Expand relative path to their full path equivalent */
|
||||||
len = (int)STRLEN(curdir);
|
len = (int)STRLEN(curdir);
|
||||||
if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
|
if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
|
||||||
@ -8086,7 +8086,7 @@ static void uniquefy_paths(garray_T *gap, char_u *pattern)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mch_is_full_name(path)) {
|
if (mch_is_absolute_path(path)) {
|
||||||
/*
|
/*
|
||||||
* Last resort: shorten relative to curdir if possible.
|
* Last resort: shorten relative to curdir if possible.
|
||||||
* 'possible' means:
|
* 'possible' means:
|
||||||
@ -8375,7 +8375,7 @@ gen_expand_wildcards (
|
|||||||
*/
|
*/
|
||||||
if (mch_has_exp_wildcard(p)) {
|
if (mch_has_exp_wildcard(p)) {
|
||||||
if ((flags & EW_PATH)
|
if ((flags & EW_PATH)
|
||||||
&& !mch_is_full_name(p)
|
&& !mch_is_absolute_path(p)
|
||||||
&& !(p[0] == '.'
|
&& !(p[0] == '.'
|
||||||
&& (vim_ispathsep(p[1])
|
&& (vim_ispathsep(p[1])
|
||||||
|| (p[1] == '.' && vim_ispathsep(p[2]))))
|
|| (p[1] == '.' && vim_ispathsep(p[2]))))
|
||||||
|
31
src/os/fs.c
31
src/os/fs.c
@ -50,35 +50,38 @@ int mch_full_dir_name(char *directory, char *buffer, int len)
|
|||||||
{
|
{
|
||||||
int retval = OK;
|
int retval = OK;
|
||||||
|
|
||||||
if(0 == STRLEN(directory)) {
|
if(STRLEN(directory) == 0) {
|
||||||
return mch_dirname((char_u *) buffer, len);
|
return mch_dirname((char_u *) buffer, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
char old_dir[MAXPATHL];
|
char old_dir[MAXPATHL];
|
||||||
|
|
||||||
/* Get current directory name. */
|
/* Get current directory name. */
|
||||||
if (FAIL == mch_dirname((char_u *) old_dir, MAXPATHL)) {
|
if (mch_dirname((char_u *) old_dir, MAXPATHL) == FAIL) {
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We have to get back to the current dir at the end, check if that works. */
|
/* We have to get back to the current dir at the end, check if that works. */
|
||||||
if (0 != mch_chdir(old_dir)) {
|
if (mch_chdir(old_dir) != 0) {
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 != mch_chdir(directory)) {
|
if (mch_chdir(directory) != 0) {
|
||||||
|
/* Do not return immediatly since we may be in the wrong directory. */
|
||||||
retval = FAIL;
|
retval = FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((FAIL == retval) || (FAIL == mch_dirname((char_u *) buffer, len))) {
|
if (retval == FAIL || mch_dirname((char_u *) buffer, len) == FAIL) {
|
||||||
|
/* Do not return immediatly since we are in the wrong directory. */
|
||||||
retval = FAIL;
|
retval = FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 != mch_chdir(old_dir)) {
|
if (mch_chdir(old_dir) != 0) {
|
||||||
/* That shouldn't happen, since we've tested if it works. */
|
/* That shouldn't happen, since we've tested if it works. */
|
||||||
retval = FAIL;
|
retval = FAIL;
|
||||||
EMSG(_(e_prev_dir));
|
EMSG(_(e_prev_dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,19 +94,23 @@ int append_path(char *path, char *to_append, int max_len)
|
|||||||
int to_append_length = STRLEN(to_append);
|
int to_append_length = STRLEN(to_append);
|
||||||
|
|
||||||
/* Do not append empty strings. */
|
/* Do not append empty strings. */
|
||||||
if (0 == to_append_length)
|
if (to_append_length == 0) {
|
||||||
return OK;
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
/* Do not append a dot. */
|
/* Do not append a dot. */
|
||||||
if (STRCMP(to_append, ".") == 0)
|
if (STRCMP(to_append, ".") == 0) {
|
||||||
return OK;
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
/* Glue both paths with a slash. */
|
/* Glue both paths with a slash. */
|
||||||
if (current_length > 0 && path[current_length-1] != '/') {
|
if (current_length > 0 && path[current_length-1] != '/') {
|
||||||
current_length += 1; /* Count the trailing slash. */
|
current_length += 1; /* Count the trailing slash. */
|
||||||
|
|
||||||
if (current_length > max_len)
|
/* +1 for the NUL at the end. */
|
||||||
|
if (current_length +1 > max_len) {
|
||||||
return FAIL;
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
STRCAT(path, "/");
|
STRCAT(path, "/");
|
||||||
}
|
}
|
||||||
@ -125,7 +132,7 @@ int append_path(char *path, char *to_append, int max_len)
|
|||||||
*
|
*
|
||||||
* return FAIL for failure, OK for success
|
* return FAIL for failure, OK for success
|
||||||
*/
|
*/
|
||||||
int mch_full_name(char_u *fname, char_u *buf, int len, int force)
|
int mch_get_absolute_path(char_u *fname, char_u *buf, int len, int force)
|
||||||
{
|
{
|
||||||
char_u *p;
|
char_u *p;
|
||||||
*buf = NUL;
|
*buf = NUL;
|
||||||
@ -134,7 +141,7 @@ int mch_full_name(char_u *fname, char_u *buf, int len, int force)
|
|||||||
char *end_of_path = (char *) fname;
|
char *end_of_path = (char *) fname;
|
||||||
|
|
||||||
/* expand it if forced or not an absolute path */
|
/* expand it if forced or not an absolute path */
|
||||||
if (force || !mch_is_full_name(fname)) {
|
if (force || !mch_is_absolute_path(fname)) {
|
||||||
if ((p = vim_strrchr(fname, '/')) != NULL) {
|
if ((p = vim_strrchr(fname, '/')) != NULL) {
|
||||||
|
|
||||||
STRNCPY(relative_directory, fname, p-fname);
|
STRNCPY(relative_directory, fname, p-fname);
|
||||||
@ -155,7 +162,7 @@ int mch_full_name(char_u *fname, char_u *buf, int len, int force)
|
|||||||
/*
|
/*
|
||||||
* Return TRUE if "fname" does not depend on the current directory.
|
* Return TRUE if "fname" does not depend on the current directory.
|
||||||
*/
|
*/
|
||||||
int mch_is_full_name(char_u *fname)
|
int mch_is_absolute_path(char_u *fname)
|
||||||
{
|
{
|
||||||
return *fname == '/' || *fname == '~';
|
return *fname == '/' || *fname == '~';
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
long_u mch_total_mem(int special);
|
long_u mch_total_mem(int special);
|
||||||
int mch_chdir(char *path);
|
int mch_chdir(char *path);
|
||||||
int mch_dirname(char_u *buf, int len);
|
int mch_dirname(char_u *buf, int len);
|
||||||
int mch_full_name (char_u *fname, char_u *buf, int len, int force);
|
int mch_get_absolute_path(char_u *fname, char_u *buf, int len, int force);
|
||||||
int mch_is_full_name (char_u *fname);
|
int mch_is_absolute_path(char_u *fname);
|
||||||
int mch_isdir(char_u *name);
|
int mch_isdir(char_u *name);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1344,7 +1344,7 @@ int mch_can_exe(char_u *name)
|
|||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
/* If it's an absolute or relative path don't need to use $PATH. */
|
/* If it's an absolute or relative path don't need to use $PATH. */
|
||||||
if (mch_is_full_name(name) || (name[0] == '.' && (name[1] == '/'
|
if (mch_is_absolute_path(name) || (name[0] == '.' && (name[1] == '/'
|
||||||
|| (name[1] == '.' &&
|
|| (name[1] == '.' &&
|
||||||
name[2] == '/'))))
|
name[2] == '/'))))
|
||||||
return executable_file(name);
|
return executable_file(name);
|
||||||
|
@ -4188,7 +4188,7 @@ static void syn_cmd_include(exarg_T *eap, int syncing)
|
|||||||
*/
|
*/
|
||||||
eap->argt |= (XFILE | NOSPC);
|
eap->argt |= (XFILE | NOSPC);
|
||||||
separate_nextcmd(eap);
|
separate_nextcmd(eap);
|
||||||
if (*eap->arg == '<' || *eap->arg == '$' || mch_is_full_name(eap->arg)) {
|
if (*eap->arg == '<' || *eap->arg == '$' || mch_is_absolute_path(eap->arg)) {
|
||||||
/* For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
|
/* For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
|
||||||
* file. Need to expand the file name first. In other cases
|
* file. Need to expand the file name first. In other cases
|
||||||
* ":runtime!" is used. */
|
* ":runtime!" is used. */
|
||||||
|
@ -5065,7 +5065,7 @@ int path_with_url(char_u *fname)
|
|||||||
*/
|
*/
|
||||||
int vim_isAbsName(char_u *name)
|
int vim_isAbsName(char_u *name)
|
||||||
{
|
{
|
||||||
return path_with_url(name) != 0 || mch_is_full_name(name);
|
return path_with_url(name) != 0 || mch_is_absolute_path(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -5090,7 +5090,7 @@ vim_FullName (
|
|||||||
|
|
||||||
url = path_with_url(fname);
|
url = path_with_url(fname);
|
||||||
if (!url)
|
if (!url)
|
||||||
retval = mch_full_name(fname, buf, len, force);
|
retval = mch_get_absolute_path(fname, buf, len, force);
|
||||||
if (url || retval == FAIL) {
|
if (url || retval == FAIL) {
|
||||||
/* something failed; use the file name (truncate when too long) */
|
/* something failed; use the file name (truncate when too long) */
|
||||||
vim_strncpy(buf, fname, len - 1);
|
vim_strncpy(buf, fname, len - 1);
|
||||||
|
@ -70,12 +70,12 @@ describe 'fs function', ->
|
|||||||
eq lfs.currentdir! .. '/empty-test-directory', (ffi.string buffer)
|
eq lfs.currentdir! .. '/empty-test-directory', (ffi.string buffer)
|
||||||
eq OK, result
|
eq OK, result
|
||||||
|
|
||||||
describe 'mch_full_name', ->
|
describe 'mch_get_absolute_path', ->
|
||||||
ffi.cdef 'int mch_full_name(char *fname, char *buf, int len, int force);'
|
ffi.cdef 'int mch_get_absolute_path(char *fname, char *buf, int len, int force);'
|
||||||
|
|
||||||
mch_full_name = (filename, buffer, length, force) ->
|
mch_get_absolute_path = (filename, buffer, length, force) ->
|
||||||
filename = cstr (string.len filename) + 1, filename
|
filename = cstr (string.len filename) + 1, filename
|
||||||
fs.mch_full_name filename, buffer, length, force
|
fs.mch_get_absolute_path filename, buffer, length, force
|
||||||
|
|
||||||
before_each ->
|
before_each ->
|
||||||
-- Create empty string buffer which will contain the resulting path.
|
-- Create empty string buffer which will contain the resulting path.
|
||||||
@ -92,12 +92,12 @@ describe 'fs function', ->
|
|||||||
|
|
||||||
it 'fails if given filename contains non-existing directory', ->
|
it 'fails if given filename contains non-existing directory', ->
|
||||||
force_expansion = 1
|
force_expansion = 1
|
||||||
result = mch_full_name 'non_existing_dir/test.file', buffer, len, force_expansion
|
result = mch_get_absolute_path 'non_existing_dir/test.file', buffer, len, force_expansion
|
||||||
eq FAIL, result
|
eq FAIL, result
|
||||||
|
|
||||||
it 'concatenates given filename if it does not contain a slash', ->
|
it 'concatenates given filename if it does not contain a slash', ->
|
||||||
force_expansion = 1
|
force_expansion = 1
|
||||||
result = mch_full_name 'test.file', buffer, len, force_expansion
|
result = mch_get_absolute_path 'test.file', buffer, len, force_expansion
|
||||||
expected = lfs.currentdir! .. '/test.file'
|
expected = lfs.currentdir! .. '/test.file'
|
||||||
eq expected, (ffi.string buffer)
|
eq expected, (ffi.string buffer)
|
||||||
eq OK, result
|
eq OK, result
|
||||||
@ -105,7 +105,7 @@ describe 'fs function', ->
|
|||||||
it 'concatenates given filename if it is a directory but does not contain a
|
it 'concatenates given filename if it is a directory but does not contain a
|
||||||
slash', ->
|
slash', ->
|
||||||
force_expansion = 1
|
force_expansion = 1
|
||||||
result = mch_full_name '..', buffer, len, force_expansion
|
result = mch_get_absolute_path '..', buffer, len, force_expansion
|
||||||
expected = lfs.currentdir! .. '/..'
|
expected = lfs.currentdir! .. '/..'
|
||||||
eq expected, (ffi.string buffer)
|
eq expected, (ffi.string buffer)
|
||||||
eq OK, result
|
eq OK, result
|
||||||
@ -115,7 +115,7 @@ describe 'fs function', ->
|
|||||||
it 'enters given directory (instead of just concatenating the strings) if
|
it 'enters given directory (instead of just concatenating the strings) if
|
||||||
possible and if path contains a slash', ->
|
possible and if path contains a slash', ->
|
||||||
force_expansion = 1
|
force_expansion = 1
|
||||||
result = mch_full_name '../test.file', buffer, len, force_expansion
|
result = mch_get_absolute_path '../test.file', buffer, len, force_expansion
|
||||||
old_dir = lfs.currentdir!
|
old_dir = lfs.currentdir!
|
||||||
lfs.chdir '..'
|
lfs.chdir '..'
|
||||||
expected = lfs.currentdir! .. '/test.file'
|
expected = lfs.currentdir! .. '/test.file'
|
||||||
@ -126,26 +126,26 @@ describe 'fs function', ->
|
|||||||
it 'just copies the path if it is already absolute and force=0', ->
|
it 'just copies the path if it is already absolute and force=0', ->
|
||||||
force_expansion = 0
|
force_expansion = 0
|
||||||
absolute_path = '/absolute/path'
|
absolute_path = '/absolute/path'
|
||||||
result = mch_full_name absolute_path, buffer, len, force_expansion
|
result = mch_get_absolute_path absolute_path, buffer, len, force_expansion
|
||||||
eq absolute_path, (ffi.string buffer)
|
eq absolute_path, (ffi.string buffer)
|
||||||
eq OK, result
|
eq OK, result
|
||||||
|
|
||||||
it 'fails when the path is relative to HOME', ->
|
it 'fails when the path is relative to HOME', ->
|
||||||
force_expansion = 1
|
force_expansion = 1
|
||||||
absolute_path = '~/home.file'
|
absolute_path = '~/home.file'
|
||||||
result = mch_full_name absolute_path, buffer, len, force_expansion
|
result = mch_get_absolute_path absolute_path, buffer, len, force_expansion
|
||||||
eq FAIL, result
|
eq FAIL, result
|
||||||
|
|
||||||
it 'works with some "normal" relative path with directories', ->
|
it 'works with some "normal" relative path with directories', ->
|
||||||
force_expansion = 1
|
force_expansion = 1
|
||||||
result = mch_full_name 'empty-test-directory/empty.file', buffer, len, force_expansion
|
result = mch_get_absolute_path 'empty-test-directory/empty.file', buffer, len, force_expansion
|
||||||
eq OK, result
|
eq OK, result
|
||||||
eq lfs.currentdir! .. '/empty-test-directory/empty.file', (ffi.string buffer)
|
eq lfs.currentdir! .. '/empty-test-directory/empty.file', (ffi.string buffer)
|
||||||
|
|
||||||
it 'does not modify the given filename', ->
|
it 'does not modify the given filename', ->
|
||||||
force_expansion = 1
|
force_expansion = 1
|
||||||
filename = cstr 100, 'empty-test-directory/empty.file'
|
filename = cstr 100, 'empty-test-directory/empty.file'
|
||||||
result = fs.mch_full_name filename, buffer, len, force_expansion
|
result = fs.mch_get_absolute_path filename, buffer, len, force_expansion
|
||||||
eq lfs.currentdir! .. '/empty-test-directory/empty.file', (ffi.string buffer)
|
eq lfs.currentdir! .. '/empty-test-directory/empty.file', (ffi.string buffer)
|
||||||
eq 'empty-test-directory/empty.file', (ffi.string filename)
|
eq 'empty-test-directory/empty.file', (ffi.string filename)
|
||||||
eq OK, result
|
eq OK, result
|
||||||
@ -188,18 +188,18 @@ describe 'fs function', ->
|
|||||||
eq OK, (fs.append_path path, to_append, 7)
|
eq OK, (fs.append_path path, to_append, 7)
|
||||||
eq '/path2', (ffi.string path)
|
eq '/path2', (ffi.string path)
|
||||||
|
|
||||||
describe 'mch_is_full_name', ->
|
describe 'mch_is_absolute_path', ->
|
||||||
ffi.cdef 'int mch_is_full_name(char *fname);'
|
ffi.cdef 'int mch_is_absolute_path(char *fname);'
|
||||||
|
|
||||||
mch_is_full_name = (filename) ->
|
mch_is_absolute_path = (filename) ->
|
||||||
filename = cstr (string.len filename) + 1, filename
|
filename = cstr (string.len filename) + 1, filename
|
||||||
fs.mch_is_full_name filename
|
fs.mch_is_absolute_path filename
|
||||||
|
|
||||||
it 'returns true if filename starts with a slash', ->
|
it 'returns true if filename starts with a slash', ->
|
||||||
eq OK, mch_is_full_name '/some/directory/'
|
eq OK, mch_is_absolute_path '/some/directory/'
|
||||||
|
|
||||||
it 'returns true if filename starts with a tilde', ->
|
it 'returns true if filename starts with a tilde', ->
|
||||||
eq OK, mch_is_full_name '~/in/my/home~/directory'
|
eq OK, mch_is_absolute_path '~/in/my/home~/directory'
|
||||||
|
|
||||||
it 'returns false if filename starts not with slash nor tilde', ->
|
it 'returns false if filename starts not with slash nor tilde', ->
|
||||||
eq FAIL, mch_is_full_name 'not/in/my/home~/directory'
|
eq FAIL, mch_is_absolute_path 'not/in/my/home~/directory'
|
||||||
|
Loading…
Reference in New Issue
Block a user