  1| --- Path.lua
  2| ---
  3| --- Goal: Create objects that are extremely similar to Python's `Path` Objects.
  4| --- Reference: https://docs.python.org/3/library/pathlib.html
  6| local bit = require "plenary.bit"
  7| local uv = vim.loop
  9| local F = require "plenary.functional"
 11| local S_IF = {
 16| }
 18| local path = {}
 19| path.home = vim.loop.os_homedir()
 21| path.sep = (function()
 22|   if jit then
 23|     local os = string.lower(jit.os)
 24|     if os ~= "windows" then
 25|       return "/"
 26|     else
 27|       return "\\"
 28|     end
 29|   else
 30|     return package.config:sub(1, 1)
 31|   end
 32| end)()
 34| path.root = (function()
 35|   if path.sep == "/" then
 36|     return function()
 37|       return "/"
 38|     end
 39|   else
 40|     return function(base)
 41|       base = base or vim.loop.cwd()
 42|       return base:sub(1, 1) .. ":\\"
 43|     end
 44|   end
 45| end)()
 47| path.S_IF = S_IF
 49| local band = function(reg, value)
 50|   return bit.band(reg, value) == reg
 51| end
 53| local concat_paths = function(...)
 54|   return table.concat({ ... }, path.sep)
 55| end
 57| local function is_root(pathname)
 58|   if path.sep == "\\" then
 59|     return string.match(pathname, "^[A-Z]:\\?$")
 60|   end
 61|   return pathname == "/"
 62| end
 64| local _split_by_separator = (function()
 65|   local formatted = string.format("([^%s]+)", path.sep)
 66|   return function(filepath)
 67|     local t = {}
 68|     for str in string.gmatch(filepath, formatted) do
 69|       table.insert(t, str)
 70|     end
 71|     return t
 72|   end
 73| end)()
 75| local is_uri = function(filename)
 76|   return string.match(filename, "^%a[%w+-.]*://") ~= nil
 77| end
 79| local is_absolute = function(filename, s

... [truncated 23367 chars] ...

read
910|     if offset < 0 then
911|       offset = 0
912|     end
913|   end
915|   local data = ""
916|   while #data < length do
917|     local read_chunk = assert(uv.fs_read(fd, length - #data, offset))
918|     if #read_chunk == 0 then
919|       break
920|     end
921|     data = data .. read_chunk
922|     offset = offset + #read_chunk
923|   end
925|   assert(uv.fs_close(fd))
927|   return data
928| end
930| function Path:find_upwards(filename)
931|   local folder = Path:new(self)
932|   local root = path.root(folder:absolute())
934|   while true do
935|     local p = folder:joinpath(filename)
936|     if p:exists() then
937|       return p
938|     end
939|     if folder:absolute() == root then
940|       break
941|     end
942|     folder = folder:parent()
943|   end
944|   return nil
945| end
947| return Path