67
67
---
68
68
--- - `find_upwards` returns `nil` if file not found rather than an empty string
69
69
70
-
71
70
-- TODO: could probably do with more `make_relative` tests
72
71
-- - walk up close to root
73
72
-- - add "walk_up" in test name
74
- -- TODO: shorten: i think `vim.list_contains` is not nvim-0.7 compat (maybe use like a set?)
75
- -- TODO: verify unix tests pass
76
73
-- TODO: add windows test for path2_spec only?
77
74
78
75
local bit = require " plenary.bit"
@@ -388,20 +385,21 @@ path.root = (function()
388
385
end )()
389
386
390
387
--- @param parts string[]
391
- --- @param _flavor plenary._Path
388
+ --- @param flavor plenary._Path
392
389
--- @return string drv
393
390
--- @return string root
394
391
--- @return string[]
395
- local function parse_parts (parts , _flavor )
396
- local drv , root , rel , parsed = " " , " " , " " , {}
392
+ local function parse_parts (parts , flavor )
393
+ local rel
394
+ local drv , root , parsed = " " , " " , {}
397
395
398
396
if # parts == 0 then
399
397
return drv , root , parsed
400
398
end
401
399
402
- local sep = _flavor .sep
403
- local p = _flavor :join (unpack (parts ))
404
- drv , root , rel = _flavor :split_root (p )
400
+ local sep = flavor .sep
401
+ local p = flavor :join (unpack (parts ))
402
+ drv , root , rel = flavor :split_root (p )
405
403
406
404
if root == " " and drv :sub (1 , 1 ) == sep and drv :sub (- 1 ) ~= sep then
407
405
local drv_parts = vim .split (drv , sep )
@@ -423,6 +421,8 @@ local function parse_parts(parts, _flavor)
423
421
return drv , root , parsed
424
422
end
425
423
424
+ local FILE_MODE = 438 -- o666 (aka -rw-rw-rw-)
425
+
426
426
--- @class plenary.Path2
427
427
--- @field path plenary.path2
428
428
--- @field private _flavor plenary._Path
@@ -546,7 +546,9 @@ function Path:new(...)
546
546
elseif type (arg ) ~= " string" and not self .is_path (arg ) then
547
547
error (
548
548
string.format (
549
- " Invalid type passed to 'Path:new'. Expects any number of 'string' or 'Path' objects. Got type '%s', shape '%s'" ,
549
+ " Invalid type passed to 'Path:new'. "
550
+ .. " Expects any number of 'string' or 'Path' objects. "
551
+ .. " Got type '%s', shape '%s'" ,
550
552
type (arg ),
551
553
vim .inspect (arg )
552
554
)
@@ -582,13 +584,13 @@ end
582
584
--- @param relparts string[] ?
583
585
--- @return string
584
586
function Path :_filename (drv , root , relparts )
585
- drv = vim .F .if_nil (drv , self .drv )
587
+ drv = vim .F .if_nil (drv , self .drv ) -- luacheck: ignore
586
588
drv = self .drv ~= " " and self .drv :gsub (self ._flavor .sep , self .sep ) or " "
587
589
588
590
if self ._flavor .has_drv and drv == " " then
589
591
root = " "
590
592
else
591
- root = vim .F .if_nil (root , self .root )
593
+ root = vim .F .if_nil (root , self .root ) -- luacheck: ignore
592
594
root = self .root ~= " " and self .sep :rep (# self .root ) or " "
593
595
end
594
596
@@ -658,7 +660,7 @@ function Path:lstat()
658
660
return res
659
661
end
660
662
661
- --- @return integer
663
+ --- @return integer # numeric mode in octal values
662
664
function Path :permission ()
663
665
local stat = self :stat ()
664
666
local perm = bit .band (stat .mode , 0x1FF )
@@ -914,11 +916,15 @@ function Path:shorten(len, excludes)
914
916
len = vim .F .if_nil (len , 1 )
915
917
excludes = vim .F .if_nil (excludes , { # self .relparts })
916
918
917
- local new_parts = {}
919
+ local excl_set = {}
920
+ for _ , idx in ipairs (excludes ) do
921
+ excl_set [idx ] = true
922
+ end
918
923
924
+ local new_parts = {}
919
925
for i , part in ipairs (self .relparts ) do
920
926
local neg_i = - (# self .relparts + 1 ) + i
921
- if # part > len and not vim . list_contains ( excludes , i ) and not vim . list_contains ( excludes , neg_i ) then
927
+ if # part > len and not excl_set [ i ] and not excl_set [ neg_i ] then
922
928
part = part :sub (1 , len )
923
929
end
924
930
table.insert (new_parts , part )
@@ -928,7 +934,10 @@ function Path:shorten(len, excludes)
928
934
end
929
935
930
936
--- @class plenary.Path2.mkdirOpts
931
- --- @field mode integer ? permission to give to the directory , no umask effect will be applied (default : ` o777` )
937
+ --- permission to give to the directory, this is modified by the process's umask
938
+ --- (default: `o777`)
939
+ --- (currently not implemented in Windows by libuv)
940
+ --- @field mode integer ?
932
941
--- @field parents boolean ? creates parent directories if true and necessary (default : ` false` )
933
942
--- @field exists_ok boolean ? ignores error if true and target directory exists (default : ` false` )
934
943
@@ -942,7 +951,7 @@ function Path:mkdir(opts)
942
951
exists_ok = { opts .exists_ok , " b" , true },
943
952
}
944
953
945
- opts .mode = vim .F .if_nil (opts .mode , 511 )
954
+ opts .mode = vim .F .if_nil (opts .mode , 511 ) -- o777
946
955
opts .parents = vim .F .if_nil (opts .parents , false )
947
956
opts .exists_ok = vim .F .if_nil (opts .exists_ok , false )
948
957
@@ -998,7 +1007,7 @@ function Path:touch(opts)
998
1007
mode = { opts .mode , " n" , true },
999
1008
parents = { opts .parents , { " n" , " b" }, true },
1000
1009
}
1001
- opts .mode = vim .F .if_nil (opts .mode , 438 )
1010
+ opts .mode = vim .F .if_nil (opts .mode , FILE_MODE ) -- o666
1002
1011
opts .parents = vim .F .if_nil (opts .parents , false )
1003
1012
1004
1013
local abs_path = self :absolute ()
@@ -1101,7 +1110,9 @@ end
1101
1110
--- @field exists_ok boolean ? whether ok if ` opts.destination` exists , if so folders are merged (default : ` true` )
1102
1111
1103
1112
--- @param opts plenary.Path2.copyOpts
1104
- --- @return { [plenary.Path2] : { success : boolean , err : string ?}} # indicating success of copy; nested tables constitute sub dirs
1113
+ --- a flat dictionary of destination paths and their copy result.
1114
+ --- if successful, `{ success = true }`, else `{ success = false, err = "some msg" }`
1115
+ --- @return { [plenary.Path2] : { success : boolean , err : string ?}}
1105
1116
function Path :copy (opts )
1106
1117
vim .validate {
1107
1118
destination = { opts .destination , is_path_like },
@@ -1192,7 +1203,7 @@ end
1192
1203
function Path :_read_sync ()
1193
1204
local stat = self :_get_readable_stat ()
1194
1205
1195
- local fd , err = uv .fs_open (self :absolute (), " r" , 438 )
1206
+ local fd , err = uv .fs_open (self :absolute (), " r" , FILE_MODE )
1196
1207
if fd == nil then
1197
1208
error (err )
1198
1209
end
@@ -1213,7 +1224,7 @@ end
1213
1224
--- @private
1214
1225
--- @param callback fun ( data : string )
1215
1226
function Path :_read_async (callback )
1216
- uv .fs_open (self :absolute (), " r" , 438 , function (err_open , fd )
1227
+ uv .fs_open (self :absolute (), " r" , FILE_MODE , function (err_open , fd )
1217
1228
if err_open then
1218
1229
error (err_open )
1219
1230
end
@@ -1263,7 +1274,7 @@ function Path:head(lines)
1263
1274
lines = vim .F .if_nil (lines , 10 )
1264
1275
local chunk_size = 256
1265
1276
1266
- local fd , err = uv .fs_open (self :absolute (), " r" , 438 )
1277
+ local fd , err = uv .fs_open (self :absolute (), " r" , FILE_MODE )
1267
1278
if fd == nil then
1268
1279
error (err )
1269
1280
end
@@ -1318,7 +1329,7 @@ function Path:tail(lines)
1318
1329
lines = vim .F .if_nil (lines , 10 )
1319
1330
local chunk_size = 256
1320
1331
1321
- local fd , err = uv .fs_open (self :absolute (), " r" , 438 )
1332
+ local fd , err = uv .fs_open (self :absolute (), " r" , FILE_MODE )
1322
1333
if fd == nil then
1323
1334
error (err )
1324
1335
end
@@ -1378,7 +1389,7 @@ function Path:readbyterange(offset, length)
1378
1389
}
1379
1390
1380
1391
local stat = self :_get_readable_stat ()
1381
- local fd , err = uv .fs_open (self :absolute (), " r" , 438 )
1392
+ local fd , err = uv .fs_open (self :absolute (), " r" , FILE_MODE )
1382
1393
if fd == nil then
1383
1394
error (err )
1384
1395
end
@@ -1427,7 +1438,7 @@ function Path:write(data, flags, mode)
1427
1438
mode = { mode , " n" , true },
1428
1439
}
1429
1440
1430
- mode = vim .F .if_nil (mode , 438 )
1441
+ mode = vim .F .if_nil (mode , FILE_MODE )
1431
1442
local fd , err = uv .fs_open (self :absolute (), flags , mode )
1432
1443
if fd == nil then
1433
1444
error (err )
0 commit comments