-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath.d.lua
More file actions
527 lines (490 loc) · 13.5 KB
/
Copy pathpath.d.lua
File metadata and controls
527 lines (490 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
---@meta _
---@module "mods.fs"
local fs
---
---Cross-platform path operations with host-platform semantics.
---
---## Usage
---
---```lua
---path = mods.path
---
---print(path.join("src", "mods", "path.lua")) --> "src/mods/path.lua"
---print(path.normpath("a//b/./c")) --> "a/b/c"
---print(path.splitext("archive.tar.gz")) --> "archive.tar", ".gz"
---```
---
---@class mods.path
local M = {}
---
---Split extension from a path.
---
---@param path string
---@param sep string
---@param altsep? string
---@param extsep string
---@return string root
---@return string ext
---@private
function M._splitext(path, sep, altsep, extsep) end
---
---Normalize path case using the active path semantics.
---
---```lua
---path.normcase("ABC") --> "abc"
---path.normcase("/A/B") --> "\\a\\b"
---```
---
---> [!NOTE]
--->
---> On POSIX semantics this returns the input unchanged. Use `mods.ntpath` to
---> force Windows-style case folding and separator normalization.
---
---@section Normalization
---@param s string Input path value.
---@return string normalizedPath Path after case normalization.
---@nodiscard
function M.normcase(s) end
---
---Join path components.
---
---```lua
---path.join("/usr", "bin") --> "/usr/bin"
---path.join([[C:/a]], [[b]]) --> [[C:/a\b]]
---```
---
---> [!NOTE]
--->
---> Single input is returned as-is.
---
---@section Normalization
---@param path string Base path component.
---@param ... string Additional path components.
---@return string joinedPath Joined path.
---@nodiscard
function M.join(path, ...) end
---
---Normalize separators and dot segments.
---
---```lua
---path.normpath("/a//./b/..") --> "/a"
---path.normpath([[A/foo/../B]]) --> [[A\B]]
---```
---
---@section Normalization
---@param path string Path to normalize.
---@return string normalizedPath Normalized path.
---@nodiscard
function M.normpath(path) end
---Return `true` when `path` is absolute.
---
---```lua
---path.isabs("/a/b") --> true
---```
---
---@section Normalization
---@param path string Input path.
---@return boolean isAbsolute True when `path` is absolute.
---@nodiscard
function M.isabs(path) end
---
---Split path into directory head and tail component.
---
---```lua
---path.split("/a/b.txt") --> "/a", "b.txt"
---```
---
---@section Decomposition
---@param path string Input path.
---@return string head Directory portion.
---@return string tail Final path component.
---@nodiscard
function M.split(path) end
---
---Split path into a root and extension.
---
---```lua
---path.splitext("archive.tar.gz") --> "archive.tar", ".gz"
---```
---
---@section Decomposition
---@param path string Input path.
---@return string root Path without the final extension.
---@return string ext Final extension including leading dot.
---@nodiscard
function M.splitext(path) end
---
---Split drive prefix from remainder.
---
---```lua
---path.splitdrive("/a/b") --> "", "/a/b"
---```
---
---> [!NOTE]
--->
---> On POSIX semantics the drive portion is always empty.
---
---@section Decomposition
---@param path string Input path.
---@return string drive Drive or share prefix when present.
---@return string rest Path remainder.
---@nodiscard
function M.splitdrive(path) end
---
---Split path into drive, root, and tail components.
---
---```lua
---path.splitroot("/a/b") --> "", "/", "a/b"
---path.splitroot([[C:\a\b]]) --> "C:", [[\]], "a\\b"
---```
---
---@section Decomposition
---@param path string Path to split.
---@return string drive Drive or share prefix (empty on POSIX).
---@return string root Root separator segment.
---@return string tail Remaining path without leading root separator.
---@nodiscard
function M.splitroot(path) end
---
---Return final path component.
---
---```lua
---path.basename("/a/b.txt") --> "b.txt"
---path.basename([[C:\a\b.txt]]) --> "b.txt"
---```
---
---@section Decomposition
---@param path string Path to inspect.
---@return string basename Final path component.
---@nodiscard
function M.basename(path) end
---
---Return directory portion of a path.
---
---```lua
---path.dirname("/a/b.txt") --> "/a"
---path.dirname([[C:\a\b.txt]]) --> [[C:\a]]
---```
---
---@section Decomposition
---@param path string Path to inspect.
---@return string dirname Parent directory path.
---@nodiscard
function M.dirname(path) end
---
---Expand `~` home segment when available.
---
---```lua
---path.expanduser("~/tmp") --> "<HOME>/tmp" (when HOME is set)
---path.expanduser([[x\y]]) --> [[x\y]]
---```
---
---@section Environment
---@param path string Path that may begin with `~`.
---@return string? expandedPath Path with the home segment expanded when available.
---@return string? err Error message when `~` expansion cannot be resolved.
---@nodiscard
function M.expanduser(path) end
---
---Expand vars in a path (`$VAR`/`${VAR}` everywhere, `%VAR%` on Windows).
---
---```lua
---path.expandvars("$HOME/bin") --> "/home/me/bin"
---path.expandvars("${XDG_CONFIG_HOME}/nvim") --> "/home/me/.config/nvim"
---path.expandvars("%USERPROFILE%\\bin") --> "C:\\Users\\me\\bin"
---path.expandvars("$UNKNOWN/bin") --> "$UNKNOWN/bin"
---```
---
---@section Environment
---@param path string Path containing variable placeholders.
---@return string expandedPath Path with variable values substituted.
---@nodiscard
function M.expandvars(path) end
---
---Return the current user's home directory path.
---
---```lua
---path.home()
---```
---
---@section Environment
---@return string? homePath Home directory path when available.
---@return string? err Error message when the home directory cannot be resolved.
---@nodiscard
function M.home() end
---Return the current working directory path.
---@section Environment
M.cwd = fs.cwd
---
---Return normalized absolute path.
---
---```lua
---path.abspath("/a/./b") --> "/a/b"
---path.abspath([[C:\a\..\b]]) --> [[C:\b]]
---```
---
---@section Derived
---@param path string Path to absolutize.
---@return string absolutePath Absolute normalized path.
---@nodiscard
function M.abspath(path) end
---
---Return `path` relative to optional `start` path.
---
---```lua
---path.relpath("/a/b/c", "/a") --> "b/c"
---path.relpath([[C:\a\b\c]], [[C:\a]]) --> [[b\c]]
---```
---
---@section Derived
---@param path string Input path.
---@param start? string Optional base path.
---@return string? relativePath Relative path from `start` to `path`.
---@return string? err Error message when the path cannot be made relative.
---@nodiscard
function M.relpath(path, start) end
---
---Return longest common sub-path from a path list.
---
---```lua
---path.commonpath({ "/a/b/c", "/a/b/d" }) --> "/a/b"
---path.commonpath({ [[C:\a\b\c]], [[c:/a/b/d]] }) --> [[C:\a\b]]
---```
---
---@section Derived
---@param paths string[] List of paths.
---@return string? commonPath Longest common sub-path.
---@return string? err Error message when inputs are incompatible.
---@nodiscard
function M.commonpath(paths) end
---
---Return longest common leading string prefix.
---
---```lua
---path.commonprefix({"abc", "abd"}) --> "ab"
---path.commonprefix({"/home/swen/spam", "/home/swen/eggs"}) --> "/home/swen/"
---path.commonprefix({"abc", "xyz"}) --> ""
---```
---
---@section Derived
---@param paths string[] List of paths.
---@return string commonPrefix Longest common string prefix.
---@nodiscard
function M.commonprefix(paths) end
---
---Return drive prefix when present.
---
---```lua
---path.drive("c:a/b") --> "c:"
---path.drive("a/b") --> ""
---```
---
---@section Anchors
---@param path string Input path.
---@return string drivePrefix Drive prefix.
---@nodiscard
function M.drive(path) end
---
---Return root separator segment when present.
---
---```lua
---path.root("/tmp/a.txt") --> "/"
---path.root("c:/") --> "\\"
---path.root("a/b") --> ""
---```
---
---@section Anchors
---@param path string Input path.
---@return string rootSeparator Root separator segment.
---@nodiscard
function M.root(path) end
---
---Return drive and root combined.
---
---```lua
---path.anchor("c:\\") --> "c:\\"
---```
---
---@section Anchors
---@param path string Input path.
---@return string anchor Drive and root anchor.
---@nodiscard
function M.anchor(path) end
---
---Split path into logical parts, including anchor when present.
---
---```lua
---path.parts("a/b.txt") --> {"a", "b.txt"}
---path.parts("/a/b") --> {"/", "a", "b"}
---path.parts("c:a\\b") --> {"c:", "a", "b"}
---```
---
---@section Components
---@param path string Input path.
---@return mods.List<string> paths Path parts including anchor when present.
---@nodiscard
function M.parts(path) end
---
---Return filename without its final suffix.
---
---```lua
---path.stem("archive.tar.gz") --> "archive.tar"
---path.stem("c:a/b") --> "b"
---```
---
---@section Components
---@param path string Input path.
---@return string stem Filename stem.
---@nodiscard
function M.stem(path) end
---
---Return all filename suffixes in order.
---
---```lua
---path.suffixes("archive.tar.gz") --> {".tar", ".gz"}
---path.suffixes("a/b") --> {}
---```
---
---@section Components
---@param path string Input path.
---@return mods.List<string> suffixes Filename suffixes.
---@nodiscard
function M.suffixes(path) end
---
---Return logical parent paths from nearest to farthest.
---
---```lua
---path.parents("a/b/c") --> {"a/b", "a", "."}
---path.parents("c:a/b") --> {"c:a", "c:"}
---```
---
---@section Components
---@param path string Input path.
---@return mods.List<string> parents Ancestor paths from nearest to farthest.
---@nodiscard
function M.parents(path) end
---
---Return `path` relative to `other`, or `nil` with an error when it is not under `other`.
---
---When `walk_up` is `true`, allow `..` segments to walk up to a shared prefix.
---
---```lua
---path.relative_to("/a/b/c.txt", "/a") --> "b/c.txt"
---path.relative_to("/a/b", "/a/c", true) --> "../b"
---path.relative_to("/a/b", "/a/x") --> nil, "'/a/b' is not in the subpath of '/a/x'"
---```
---
---@section Relations
---@param path string Input path.
---@param other string Reference path.
---@param walk_up? boolean Allow walking up to a shared prefix.
---@return string? relativePath Path relative to `other`, or `nil` on error.
---@return string? err Error message when the path cannot be made relative.
---@nodiscard
function M.relative_to(path, other, walk_up) end
---
---Return `true` when `path` is under `other`.
---
---```lua
---path.is_relative_to("a/b/c", "a/b") --> true
---path.is_relative_to("C:A/B", "c:a") --> true
---path.is_relative_to("a/b", "a/b/c") --> false
---```
---
---@section Relations
---@param path string Input path.
---@param other string Reference path.
---@return boolean isRelative True when `path` is under `other`.
---@nodiscard
function M.is_relative_to(path, other) end
---
---Return a path with the final filename replaced.
---
---```lua
---path.with_name("a/b", "c.txt") --> "a/c.txt"
---path.with_name("a/b.txt", "c.lua") --> "a/c.lua"
---path.with_name("a/b", "c/d") --> nil, "invalid name 'c/d'"
---path.with_name("/", "d.xml") --> nil, "'/' has an empty name"
---```
---
---@section Relations
---@param path string Input path.
---@param name string Replacement filename.
---@return string? updatedPath Path with replaced filename, or `nil` on error.
---@return string? err Error message when replacement fails.
---@nodiscard
function M.with_name(path, name) end
---
---Return a path with the final filename stem replaced.
---
---```lua
---path.with_stem("a/b", "d") --> "/a/d"
---path.with_stem("a/b.lua", "d") --> "/a/d.lua"
---path.with_stem("/", "d") --> "'/' has an empty name"
---path.with_stem("a/b", "d") --> "invalid name ''."
---```
---
---@section Relations
---@param path string Input path.
---@param stem string Replacement filename stem.
---@return string? updatedPath Path with replaced filename stem, or `nil` on error.
---@return string? err Error message when replacement fails.
---@nodiscard
function M.with_stem(path, stem) end
---
---Return a path with the final filename suffix replaced.
---
---```lua
---path.with_suffix("a/b", ".gz") --> "a/b/.gz"
---path.with_suffix("a/b.gz", ".lua") --> "a/b/.lua"
---path.with_suffix("a/b", "gz") --> nil, "invalid suffix 'gz'"
---path.with_suffix("//a/b", "gz") --> nil, "'//a/b' has an empty name"
---```
---
---@section Relations
---@param path string Input path.
---@param suffix string Replacement suffix.
---@return string? updatedPath Path with replaced suffix, or `nil` on error.
---@return string? err Error message when replacement fails.
---@nodiscard
function M.with_suffix(path, suffix) end
---
---Convert backslashes (`\`) to forward slashes (`/`).
---
---```lua
---path.as_posix("a\\b\\c") --> "a/b/c"
---```
---
---@section Conversions
---@param path string Input path.
---@return string posixPath POSIX-style path.
---@nodiscard
function M.as_posix(path) end
---
---Convert a local path to a `file://` URI.
---
---```lua
---path.as_uri("/home/user/report.txt") --> "file:///home/user/report.txt"
---path.as_uri("c:/a/b.c") --> "file:///c:/a/b.c"
---path.as_uri("/a/b%#c") --> "file:///a/b%25%23c"
---```
---
---@section Conversions
---@param path string Input path.
---@return string? fileUri File URI.
---@return string? err Error message when conversion fails.
---@nodiscard
function M.as_uri(path) end
---
---Convert a `file://` URI to a local absolute path.
---
---```lua
---path.from_uri("file://localhost/tmp/a.txt") --> "/tmp/a.txt"
---```
---
---@section Conversions
---@param uri string URI value.
---@return string? path Resolved absolute path.
---@return string? err Error message when conversion fails.
function M.from_uri(uri) end
return M