From 84268fae70669aaa40510ba218f79d64fbf4c8ff Mon Sep 17 00:00:00 2001 From: pysan3 Date: Fri, 22 Mar 2024 03:56:05 +0900 Subject: [PATCH 1/7] ci: install type deps via luarocks --- .github/workflows/.luarc.json | 56 +++++++++++++------------- .github/workflows/lua_ls-typecheck.yml | 13 +++--- .neoconf.json | 3 +- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/.github/workflows/.luarc.json b/.github/workflows/.luarc.json index 295a535..18f28ad 100644 --- a/.github/workflows/.luarc.json +++ b/.github/workflows/.luarc.json @@ -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", + "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 } diff --git a/.github/workflows/lua_ls-typecheck.yml b/.github/workflows/lua_ls-typecheck.yml index 6b3c8e6..4879787 100644 --- a/.github/workflows/lua_ls-typecheck.yml +++ b/.github/workflows/lua_ls-typecheck.yml @@ -21,11 +21,14 @@ jobs: repository: "folke/neodev.nvim" path: "deps/neodev.nvim" - - name: Checkout dependency nio - uses: actions/checkout@v3 - with: - repository: "nvim-neotest/nvim-nio" - path: "deps/nvim-nio" + - uses: leafo/gh-actions-lua@v9 + - 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 diff --git a/.neoconf.json b/.neoconf.json index 589f034..bc574d6 100644 --- a/.neoconf.json +++ b/.neoconf.json @@ -3,7 +3,8 @@ "library": { "enabled": true, "plugins": [ - "neodev.nvim" + "neodev.nvim", + "nvim-nio" ] } }, From 3445eb47a5d785dea92058944c16785eb5321d8b Mon Sep 17 00:00:00 2001 From: pysan3 Date: Fri, 22 Mar 2024 03:57:53 +0900 Subject: [PATCH 2/7] ci(types): specify lua version --- .github/workflows/lua_ls-typecheck.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/lua_ls-typecheck.yml b/.github/workflows/lua_ls-typecheck.yml index 4879787..e410089 100644 --- a/.github/workflows/lua_ls-typecheck.yml +++ b/.github/workflows/lua_ls-typecheck.yml @@ -22,6 +22,8 @@ jobs: path: "deps/neodev.nvim" - uses: leafo/gh-actions-lua@v9 + with: + luaVersion: "5.1" - uses: leafo/gh-actions-luarocks@v4 - name: install dependencies From e86e7ed0f6fcee61bc10a036c25b6cd89ecfa195 Mon Sep 17 00:00:00 2001 From: pysan3 Date: Fri, 22 Mar 2024 04:05:56 +0900 Subject: [PATCH 3/7] ci(types): show dir structure --- .github/workflows/lua_ls-typecheck.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lua_ls-typecheck.yml b/.github/workflows/lua_ls-typecheck.yml index e410089..274335c 100644 --- a/.github/workflows/lua_ls-typecheck.yml +++ b/.github/workflows/lua_ls-typecheck.yml @@ -33,7 +33,10 @@ jobs: 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/* - name: Type Check Code Base uses: mrcjkb/lua-typecheck-action@v0 From 502d3af2f3f797468e584813eab8adc4fdcfe214 Mon Sep 17 00:00:00 2001 From: pysan3 Date: Fri, 22 Mar 2024 04:08:35 +0900 Subject: [PATCH 4/7] ci(types): more structures --- .github/workflows/lua_ls-typecheck.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/lua_ls-typecheck.yml b/.github/workflows/lua_ls-typecheck.yml index 274335c..ea39e84 100644 --- a/.github/workflows/lua_ls-typecheck.yml +++ b/.github/workflows/lua_ls-typecheck.yml @@ -37,6 +37,8 @@ jobs: 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 From a9f4ccbd33f3e76cc64b97afbcd36a2771db5f53 Mon Sep 17 00:00:00 2001 From: pysan3 Date: Fri, 22 Mar 2024 04:11:09 +0900 Subject: [PATCH 5/7] ci(types): check if relpath works --- .github/workflows/.luarc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/.luarc.json b/.github/workflows/.luarc.json index 18f28ad..b045eb5 100644 --- a/.github/workflows/.luarc.json +++ b/.github/workflows/.luarc.json @@ -8,7 +8,7 @@ "${3rd}/busted/library/?/init.lua" ], "workspace.library": [ - "/home/runner/work/pathlib.nvim/pathlib.nvim/deps/neodev.nvim/types/stable", + "deps/neodev.nvim/types/stable", "lua_modules/share/lua/5.1", "${3rd}/busted/library", "${3rd}/luassert/library" From 5bcf97c131ce120b981cab78e7b2ff9ff1c2f6d9 Mon Sep 17 00:00:00 2001 From: pysan3 Date: Fri, 22 Mar 2024 04:16:36 +0900 Subject: [PATCH 6/7] ci(types): try abs path --- .github/workflows/.luarc.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/.luarc.json b/.github/workflows/.luarc.json index b045eb5..2eb686f 100644 --- a/.github/workflows/.luarc.json +++ b/.github/workflows/.luarc.json @@ -8,8 +8,8 @@ "${3rd}/busted/library/?/init.lua" ], "workspace.library": [ - "deps/neodev.nvim/types/stable", - "lua_modules/share/lua/5.1", + "/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" ], From 18cda01ae38a9a847bfef500135b4a452d12ffa1 Mon Sep 17 00:00:00 2001 From: pysan3 Date: Fri, 22 Mar 2024 04:59:56 +0900 Subject: [PATCH 7/7] fix(base): add string concat mode to make it work with old code --- README.md | 7 ++++++- README.norg | 7 ++++++- lua/pathlib/base.lua | 22 +++++++++++++++++++++- spec/path_dunder_spec.lua | 8 ++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e13c50e..2720e53 100644 --- a/README.md +++ b/README.md @@ -90,9 +90,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 `.//../bar.txt`) +-- Create siblings mode (just like `.//../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"))) diff --git a/README.norg b/README.norg index bb65542..cc9db7d 100644 --- a/README.norg +++ b/README.norg @@ -103,9 +103,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 `.//../bar.txt`) + -- Create siblings mode (just like `.//../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"))) diff --git a/lua/pathlib/base.lua b/lua/pathlib/base.lua index 15369c3..3b9d2c0 100644 --- a/lua/pathlib/base.lua +++ b/lua/pathlib/base.lua @@ -15,6 +15,7 @@ local watcher = require("pathlib.utils.watcher") ---@field public __fs_event_callbacks table|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 = "/", @@ -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 diff --git a/spec/path_dunder_spec.lua b/spec/path_dunder_spec.lua index 313eaa6..3c92ef5 100644 --- a/spec/path_dunder_spec.lua +++ b/spec/path_dunder_spec.lua @@ -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(".") / {}