Skip to content

Commit

Permalink
fix(base): add string concat mode to make it work with old code (#40)
Browse files Browse the repository at this point in the history
* ci: install type deps via luarocks

* ci(types): specify lua version

* ci(types): show dir structure

* ci(types): more structures

* ci(types): check if relpath works

* ci(types): try abs path

* fix(base): add string concat mode to make it work with old code
  • Loading branch information
pysan3 committed Mar 21, 2024
1 parent fdd4d58 commit 6e7219f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 37 deletions.
56 changes: 28 additions & 28 deletions .github/workflows/.luarc.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"runtime.version": "LuaJIT",
"runtime.path": [
"lua/?.lua",
"lua/?/init.lua",
"${3rd}/busted/library/?.lua",
"${3rd}/busted/library/?/init.lua"
],
"workspace.library": [
"/home/runner/work/pathlib.nvim/pathlib.nvim/deps/neodev.nvim/types/stable",
"/home/runner/work/pathlib.nvim/pathlib.nvim/deps/nvim-nio/lua",
"${3rd}/busted/library",
"${3rd}/luassert/library"
],
"diagnostics.globals": [
"describe",
"assert",
"it",
"vim",
"setup",
"teardown"
],
"runtime.special": {
"luassert": "loadfile"
},
"workspace.maxPreload": 100000,
"diagnostics.libraryFiles": "Disable",
"workspace.checkThirdParty": false
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"runtime.version": "LuaJIT",
"runtime.path": [
"lua/?.lua",
"lua/?/init.lua",
"${3rd}/busted/library/?.lua",
"${3rd}/busted/library/?/init.lua"
],
"workspace.library": [
"/home/runner/work/pathlib.nvim/pathlib.nvim/deps/neodev.nvim/types/stable",
"/home/runner/work/pathlib.nvim/pathlib.nvim/lua_modules/share/lua/5.1",
"${3rd}/busted/library",
"${3rd}/luassert/library"
],
"diagnostics.globals": [
"describe",
"assert",
"it",
"vim",
"setup",
"teardown"
],
"runtime.special": {
"luassert": "loadfile"
},
"workspace.maxPreload": 100000,
"diagnostics.libraryFiles": "Disable",
"workspace.checkThirdParty": false
}
20 changes: 15 additions & 5 deletions .github/workflows/lua_ls-typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@ jobs:
repository: "folke/neodev.nvim"
path: "deps/neodev.nvim"

- name: Checkout dependency nio
uses: actions/checkout@v3
- uses: leafo/gh-actions-lua@v9
with:
repository: "nvim-neotest/nvim-nio"
path: "deps/nvim-nio"
luaVersion: "5.1"
- uses: leafo/gh-actions-luarocks@v4

- name: install dependencies
run: |
luarocks init
luarocks config --scope project lua_version 5.1
luarocks install --only-deps ./pathlib.nvim-scm-1.rockspec
- name: Print Filetree
run: echo $PWD && ls -la && ls -la deps
run: |
echo $PWD && ls -la && ls -la deps
ls -la lua_modules
ls -la lua_modules/*
ls -la lua_modules/share/*
ls -la lua_modules/share/lua/*
- name: Type Check Code Base
uses: mrcjkb/lua-typecheck-action@v0
Expand Down
3 changes: 2 additions & 1 deletion .neoconf.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"library": {
"enabled": true,
"plugins": [
"neodev.nvim"
"neodev.nvim",
"nvim-nio"
]
}
},
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ assert(tostring(foo:parent()) == "folder")
assert(foo == Path("./folder/foo.txt")) -- Path object can be created with arguments
assert(foo == Path(folder, "foo.txt")) -- Unpack any of them if you want!

-- Create siblings (just like `./<foo>/../bar.txt`)
-- Create siblings mode (just like `./<foo>/../bar.txt`)
local bar = foo .. "bar.txt"
assert(tostring(bar) == "folder/bar.txt")
-- Mimic string concat mode (to work with your old code).
-- This makes old code with string paths work mostly as expected.
-- This is enabled only if rhs starts with "/" or "\\".
local old_path = folder .. "/" .. "old-way" .. "/to/make/path.txt"
assert(old_path == Path("folder/old-way/to/make/path.txt"))

-- Calculate relativily
assert(foo:is_relative_to(Path("folder")))
Expand Down
7 changes: 6 additions & 1 deletion README.norg
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ version: 1.1.1
assert(foo == Path("./folder/foo.txt")) -- Path object can be created with arguments
assert(foo == Path(folder, "foo.txt")) -- Unpack any of them if you want!

-- Create siblings (just like `./<foo>/../bar.txt`)
-- Create siblings mode (just like `./<foo>/../bar.txt`)
local bar = foo .. "bar.txt"
assert(tostring(bar) == "folder/bar.txt")
-- Mimic string concat mode (to work with your old code).
-- This makes old code with string paths work mostly as expected.
-- This is enabled only if rhs starts with "/" or "\\".
local old_path = folder .. "/" .. "old-way" .. "/to/make/path.txt"
assert(old_path == Path("folder/old-way/to/make/path.txt"))

-- Calculate relativily
assert(foo:is_relative_to(Path("folder")))
Expand Down
22 changes: 21 additions & 1 deletion lua/pathlib/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local watcher = require("pathlib.utils.watcher")
---@field public __fs_event_callbacks table<string, PathlibWatcherCallback>|nil # List of functions called when a fs_event is triggered.
---@field public __string_cache string|nil # Cache result of `tostring(self)`.
---@field public __parent_cache PathlibPath|nil # Cache reference to parent object.
---@field public __concat_dir_mode boolean|nil # If the object was created with `Path() .. "/"`, next string concat will append as a file.
local Path = setmetatable({
mytype = const.path_module_enum.PathlibPath,
sep_str = "/",
Expand Down Expand Up @@ -933,13 +934,32 @@ function Path:__div(other)
end

---Concatenate paths with the parent of lhs. `Path("./foo/foo.txt") .. "bar.txt" == "./foo/bar.txt"`
---
---To keep backwards compatibility, when rhs is a string and starts with "[/\\]", it behaves as like a string.
---
--->>> Path("./foo/foo.txt") .. "bar.txt" -- sibling mode
---Path("./foo/bar.txt")
---
--->>> Path("./foo") .. "/" .. "bar.txt" -- mimic string concat mode
---Path("./foo/bar.txt")
---
---@param other PathlibPath | string
---@return PathlibPath
-- Path.__concat = function(self, other)
function Path:__concat(other)
if not utils.tables.is_path_module(self) and not utils.tables.is_path_module(other) then
-- one of objects must be a path object
errs.value_error("__concat", other)
elseif utils.tables.is_path_module(self) and type(other) == "string" then
if self.__concat_dir_mode then
other = "/" .. other
end
local first_letter = other:sub(1, 1)
local last_letter = other:sub(#other)
if first_letter == "/" or first_letter == "\\" then
local new = self.new(self, other)
new.__concat_dir_mode = last_letter == "/" or last_letter == "\\"
return new
end
end
return self.new(self:parent(), other)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/path_dunder_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ describe("Test Dunder Methods of Path Object;", function()
assert.are.equal(foo, sibling_file .. Posix.new("./foo.txt"))
end)

it("backwards compat", function()
local path = Posix.new("./folder")
assert.are.equal(foo, path .. "/foo.txt")
assert.are.equal(foo, path .. "/" .. "foo.txt")
assert.are.equal(foo, path .. "/" .. "./foo.txt")
assert.are_not.equal(foo, path .. "foo.txt")
end)

it("raise error", function()
assert.has_error(function()
return Posix.new(".") / {}
Expand Down

0 comments on commit 6e7219f

Please sign in to comment.