Skip to content

Commit

Permalink
Merge pull request #1 from deptno/feature/navigate-between-react-nati…
Browse files Browse the repository at this point in the history
…ve-siblings

feature/navigate between react native siblings
  • Loading branch information
deptno committed Dec 19, 2023
2 parents c8f4e56 + 200f456 commit 7dc10ea
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 73 deletions.
20 changes: 20 additions & 0 deletions lua/custom/lib/every.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---every fn(all each items) == true
---@param fn function(a):b
---@param tlb table
---@return boolean
local some = function (fn, tlb)
assert(type(fn) == "function", "fn: function")
assert(type(tlb) == "table", "tlb: table(list)")

local ret = {}

for _, v in ipairs(tlb) do
if not fn(v) then
return false
end
end

return true
end

return some
86 changes: 24 additions & 62 deletions lua/custom/lib/get_suffix_siblings.lua
Original file line number Diff line number Diff line change
@@ -1,94 +1,56 @@
local file_exists = require('custom.lib.file_exists')
local map = require('custom.lib.map')
local file_exists = require("custom.lib.file_exists")
local map = require("custom.lib.map")
local slice = require("custom.lib.slice")
local every = require("custom.lib.every")
local is_equal = require("custom.lib.is_equal")
local some = require("custom.lib.some")

---주어진 infixes 를 기반으로 infix 가 없는 경우를 포함하여 존재하는 파일 목록을 리턴
---TODO: infix 가 여러개인 경우도 존재함 i.g. android.www
---@param infixes table(string) filepath
---@param path string filepath
---@return table|nil
local get_suffix_siblings = function (infixes, path)
local get_suffix_siblings = function(infixes, suffixes, path)
local dirname = vim.fs.dirname(path)
local filename = vim.fs.basename(path)
local parts = vim.split(filename, '%.')

if #parts <= 1 then
return vim.notify(string.format('invalid filename %s', filename), vim.log.levels.DEBUG)
return vim.notify(string.format("invalid filename %s", filename), vim.log.levels.DEBUG)
end

local prefix = parts[1]
local suffix = parts[#parts]

if not (suffix == 'ts' or suffix == 'tsx') then
return vim.notify(string.format('unsupported file ext %s', suffix), vim.log.levels.DEBUG)
if not some(is_equal(suffix), suffixes) then
return vim.notify(string.format("unsupported file ext %s", suffix), vim.log.levels.DEBUG)
end

local siblings = {}

-- index.ts
if #parts == 2 then
for _, infix in ipairs(infixes) do
local sibling = table.concat({ prefix, infix, suffix }, '.')

if file_exists(string.format('%s/%s', dirname, sibling)) then
table.insert(siblings, sibling)
end
end
else
-- index.index.ts
local infix = parts[#parts - 1]
local has_infix = false
for _, sibling_infix in ipairs(infixes) do
if infix == sibling_infix then
has_infix = true
end
end

local prefix_table = {}
local prefix_end_index = has_infix and #parts - 2 or #parts - 3

for i = 1, prefix_end_index do
table.insert(prefix_table, parts[i])
end

prefix = table.concat(prefix_table, '.')

-- index.{android,ios}.ts
if has_infix then
-- insert index.ts
local sibling = table.concat({ prefix, suffix }, '.')

if file_exists(string.format('%s/%s', dirname, sibling)) then
table.insert(siblings, sibling)
end

for _, sibling_infix in ipairs(infixes) do
if infix ~= sibling_infix then
sibling = table.concat({ prefix, sibling_infix, suffix }, '.')

if file_exists(string.format('%s/%s', dirname, sibling)) then
table.insert(siblings, sibling)
local files = map(vim.fs.basename, vim.fn.glob(dirname .. "/*." .. suffix, false, true))

for _, f in ipairs(files) do
if f ~= filename then
local _parts = vim.split(f, "%.")
if prefix == _parts[1] then
if #_parts == 2 then
table.insert(siblings, f)
elseif #_parts >= 3 then
local _infixes = slice(2, #_parts - 1, _parts)
if every(function (_i) return some(is_equal(_i), infixes) end, _infixes) then
table.insert(siblings, f)
end
end
end
else
-- index.not-infix.ts
for _, sibling_infix in ipairs(infixes) do
-- index.not-infix.{www,ios}.ts
local sibling = table.concat({ prefix, sibling_infix, suffix }, '.')

if file_exists(string.format('%s/%s', dirname, sibling)) then
table.insert(siblings, sibling)
end
end
end
end

if #siblings == 0 then
return
end

local with_dir = function (file)
return string.format('%s/%s', dirname, file)
local with_dir = function(file)
return string.format("%s/%s", dirname, file)
end

return map(with_dir, siblings)
Expand Down
10 changes: 10 additions & 0 deletions lua/custom/lib/is_equal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---is_equal curring (x) => (y) => x == y
---@param v any
---@return function (yy) => boolean
local is_equal = function (v)
return function(vv)
return vv == v
end
end

return is_equal
17 changes: 7 additions & 10 deletions lua/custom/lib/select_react_native_siblings.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
local get_suffix_siblings = require('custom/lib/get_suffix_siblings')
local get_git_root = require('custom/lib/get_git_root')

local select_react_native_siblings = function ()
local infixes = {
'zigbang',
'daum',
'native',
'www',
'ios',
'android',
}
local siblings = get_suffix_siblings(infixes, vim.fn.expand('%'))
---
---@param infixes table infix(string) list
---@param suffixes table suffix(string) list
---
local select_react_native_siblings = function (infixes, suffixes )
local filepath = vim.fn.expand('%')
local siblings = get_suffix_siblings(infixes, suffixes, filepath)

if siblings then
if #siblings > 0 then
Expand Down
22 changes: 22 additions & 0 deletions lua/custom/lib/slice.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---slice make table<a> subset
---@param startIndex number index
---@param endIndex number index
---@param tlb table list
---@return table
local slice = function (startIndex, endIndex, tlb)
assert(type(startIndex) == "number", "startIndex: number")
assert(type(endIndex) == "number", "endIndex: number")
assert(type(tlb) == "table", "tlb: table(list)")

local ret = {}

for i, v in ipairs(tlb) do
if i >= startIndex and i <= endIndex then
table.insert(ret, v)
end
end

return ret
end

return slice
20 changes: 20 additions & 0 deletions lua/custom/lib/some.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---some atleast one item meets fn(item) == true
---@param fn function(a):b
---@param tlb table
---@return boolean
local some = function (fn, tlb)
assert(type(fn) == "function", "fn: function")
assert(type(tlb) == "table", "tlb: table(list)")

local ret = {}

for _, v in ipairs(tlb) do
if fn(v) then
return true
end
end

return false
end

return some
17 changes: 16 additions & 1 deletion lua/custom/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,22 @@ M.wip = {
n = {
["<leader>;"] = {
function ()
select_react_native_siblings()
local infixes = {
'zigbang',
'daum',
'native',
'www',
'ios',
'android',
}
local suffixes = {
'ts',
'tsx',
'js',
'jsx',
}

select_react_native_siblings(infixes, suffixes)
end,
'wip: select react native siblings'
}
Expand Down

0 comments on commit 7dc10ea

Please sign in to comment.