@@ -537,6 +537,19 @@ function Path.is_path(x)
537
537
return getmetatable (x ) == Path
538
538
end
539
539
540
+ --- @param x any
541
+ --- @return boolean
542
+ local function is_path_like (x )
543
+ return type (x ) == " string" or Path .is_path (x )
544
+ end
545
+
546
+ local function is_path_like_opt (x )
547
+ if x == nil then
548
+ return true
549
+ end
550
+ return is_path_like (x )
551
+ end
552
+
540
553
--- @return boolean
541
554
function Path :is_absolute ()
542
555
if self .root == " " then
710
723
--- @param to plenary.Path2 | string path to compare to
711
724
--- @return boolean
712
725
function Path :is_relative (to )
726
+ vim .validate { to = { to , is_path_like } }
727
+
713
728
if not Path .is_path (to ) then
714
729
to = Path :new (to )
715
730
end
739
754
--- @param walk_up boolean ? walk up to the provided path using ' ..' (default : ` false ` )
740
755
--- @return string
741
756
function Path :make_relative (to , walk_up )
757
+ vim .validate {
758
+ to = { to , is_path_like_opt },
759
+ walk_up = { walk_up , " b" , true },
760
+ }
761
+
742
762
-- NOTE: could probably take some shortcuts and avoid some `Path:new` calls
743
763
-- by allowing _WindowsPath/_PosixPath handle this individually.
744
764
-- As always, Windows root complicates things, so generating a new Path often
800
820
--- @param excludes integer[] ?
801
821
--- @return string
802
822
function Path :shorten (len , excludes )
823
+ vim .validate {
824
+ len = { len , " n" , true },
825
+ excludes = { excludes , " t" , true },
826
+ }
827
+
803
828
len = vim .F .if_nil (len , 1 )
804
829
excludes = vim .F .if_nil (excludes , { # self .relparts })
805
830
825
850
--- @param opts plenary.Path2.mkdirOpts ?
826
851
function Path :mkdir (opts )
827
852
opts = opts or {}
853
+ vim .validate {
854
+ mode = { opts .mode , " n" , true },
855
+ parents = { opts .parents , " b" , true },
856
+ exists_ok = { opts .exists_ok , " b" , true },
857
+ }
858
+
828
859
opts .mode = vim .F .if_nil (opts .mode , 511 )
829
860
opts .parents = vim .F .if_nil (opts .parents , false )
830
861
opts .exists_ok = vim .F .if_nil (opts .exists_ok , false )
877
908
--- @param opts plenary.Path2.touchOpts ?
878
909
function Path :touch (opts )
879
910
opts = opts or {}
911
+ vim .validate {
912
+ mode = { opts .mode , " n" , true },
913
+ parents = { opts .parents , { " n" , " b" }, true },
914
+ }
880
915
opts .mode = vim .F .if_nil (opts .mode , 438 )
881
916
opts .parents = vim .F .if_nil (opts .parents , false )
882
917
912
947
--- @param opts plenary.Path2.rmOpts ?
913
948
function Path :rm (opts )
914
949
opts = opts or {}
950
+ vim .validate { recursive = { opts .recursive , " b" , true } }
915
951
opts .recursive = vim .F .if_nil (opts .recursive , false )
916
952
917
953
if not opts .recursive or not self :is_dir () then
950
986
--- @param opts plenary.Path2.renameOpts
951
987
--- @return plenary.Path2
952
988
function Path :rename (opts )
989
+ vim .validate { new_name = { opts .new_name , is_path_like } }
990
+
953
991
if not opts .new_name or opts .new_name == " " then
954
992
error " Please provide the new name!"
955
993
end
@@ -979,6 +1017,12 @@ end
979
1017
--- @param opts plenary.Path2.copyOpts
980
1018
--- @return { [plenary.Path2] : { success : boolean , err : string ?}} # indicating success of copy; nested tables constitute sub dirs
981
1019
function Path :copy (opts )
1020
+ vim .validate {
1021
+ destination = { opts .destination , is_path_like },
1022
+ recursive = { opts .recursive , " b" , true },
1023
+ override = { opts .override , " b" , true },
1024
+ }
1025
+
982
1026
opts .recursive = vim .F .if_nil (opts .recursive , false )
983
1027
opts .override = vim .F .if_nil (opts .override , true )
984
1028
@@ -996,6 +1040,13 @@ function Path:copy(opts)
996
1040
error (string.format (" Warning: %s was not copied as `recursive=false`" , self :absolute ()))
997
1041
end
998
1042
1043
+ vim .validate {
1044
+ respect_gitignore = { opts .respect_gitignore , " b" , true },
1045
+ hidden = { opts .hidden , " b" , true },
1046
+ parents = { opts .parents , " b" , true },
1047
+ exists_ok = { opts .exists_ok , " b" , true },
1048
+ }
1049
+
999
1050
opts .respect_gitignore = vim .F .if_nil (opts .respect_gitignore , false )
1000
1051
opts .hidden = vim .F .if_nil (opts .hidden , true )
1001
1052
opts .parents = vim .F .if_nil (opts .parents , false )
@@ -1028,6 +1079,8 @@ end
1028
1079
--- @param callback fun ( data : string )? callback to use for async version , nil for default
1029
1080
--- @return string ? data
1030
1081
function Path :read (callback )
1082
+ vim .validate { callback = { callback , " f" , true } }
1083
+
1031
1084
if not self :is_file () then
1032
1085
error (string.format (" '%s' is not a file" , self :absolute ()))
1033
1086
end
@@ -1117,9 +1170,7 @@ end
1117
1170
--- @param lines integer ? number of lines to read from the head of the file (default : ` 10` )
1118
1171
--- @return string data
1119
1172
function Path :head (lines )
1120
- vim .validate {
1121
- lines = { lines , " n" , true },
1122
- }
1173
+ vim .validate { lines = { lines , " n" , true } }
1123
1174
1124
1175
local stat = self :_get_readable_stat ()
1125
1176
@@ -1174,9 +1225,7 @@ end
1174
1225
--- @param lines integer ? number of lines to read from the tail of the file (default : ` 10` )
1175
1226
--- @return string data
1176
1227
function Path :tail (lines )
1177
- vim .validate {
1178
- lines = { lines , " n" , true },
1179
- }
1228
+ vim .validate { lines = { lines , " n" , true } }
1180
1229
1181
1230
local stat = self :_get_readable_stat ()
1182
1231
@@ -1265,6 +1314,7 @@ function Path:write(data, flags, mode)
1265
1314
return b
1266
1315
end
1267
1316
1317
+ --- iterate over contents in the current path recursive
1268
1318
--- @param top_down boolean ? walk from current path down (default : ` true ` )
1269
1319
--- @return fun (): plenary.Path2 ?, string[] ?, string[] ? # iterator which yields (dirpath, dirnames, filenames)
1270
1320
function Path :walk (top_down )
0 commit comments