Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lua/js-teleporter/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ local logger = require("js-teleporter.logger")
local M = {}

---Return true if the given file is a JS file
---@param context "test" | "story"
---@param context TeleporterContext
---@param filepath string
---@param opts TeleporterConfig
---@return boolean
function M.is_js_file(context, filepath, opts)
function M.is_js_file(context, filepath)
local config = require("js-teleporter.config")
local extension = filepath:match(".*(%.[^.]+)$")
if extension == nil then
return false
end

local extensions = {}
if context == "test" then
extensions = opts.test_extensions
extensions = config:context_extensions("test")
elseif context == "story" then
extensions = opts.story_extensions
extensions = config:context_extensions("story")
end

for _, v in ipairs(extensions) do
Expand All @@ -29,19 +29,19 @@ function M.is_js_file(context, filepath, opts)
end

---Return true if the given file is the other context file
---@param context "test" | "story"
---@param context TeleporterContext
---@param filepath string
---@param opts TeleporterConfig
---@return boolean
function M.is_other_file(context, filepath, opts)
function M.is_other_file(context, filepath)
local config = require("js-teleporter.config")
local basename = vim.fs.basename(filepath)
local filename, extension = basename:match("(.*)(%.[^.]+)$")

local extensions = {}
if context == "test" then
extensions = opts.test_extensions
extensions = config:context_extensions("test")
elseif context == "story" then
extensions = opts.story_extensions
extensions = config:context_extensions("story")
end

if not vim.tbl_contains(extensions, extension) then
Expand All @@ -50,9 +50,9 @@ function M.is_other_file(context, filepath, opts)

local suffix = ""
if context == "test" then
suffix = opts.test_file_suffix
suffix = config:context_suffix("test")
elseif context == "story" then
suffix = opts.story_file_suffix
suffix = config:context_suffix("story")
end

if not filename:match(suffix .. "$") then
Expand Down
10 changes: 6 additions & 4 deletions lua/js-teleporter/command.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
local _switch = {
["test"] = function(opts)
require("js-teleporter").teleport("test", opts)
["test"] = function()
local config = require("js-teleporter.config")
require("js-teleporter").teleport("test", config)
end,
["story"] = function(opts)
require("js-teleporter").teleport("story", opts)
["story"] = function()
local config = require("js-teleporter.config")
require("js-teleporter").teleport("story", config)
end,
}

Expand Down
114 changes: 62 additions & 52 deletions lua/js-teleporter/config.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
_TeleporterConfigurationValues = _TeleporterConfigurationValues or {}
---@alias TeleporterContext "test" | "story"

---@class TeleporterConfigObject
---@field set_options function
---@field context_roots function
---@field context_suffix function
---@field src_root function
---@field values TeleporterConfig
local config = {}
config.values = _TeleporterConfigurationValues
---@field setup function(opts: TeleporterConfig?): void
---@field src_root function(self): string
---@field context_roots function(self, context: TeleporterContext): Array<string>
---@field context_suffix function(self, context: TeleporterContext): string
---@field context_extensions function(self, context: TeleporterContext): Array<string>
---@field _options TeleporterConfig

local M = {}

---@class TeleporterConfig
---@field source_root? string
---@field test_roots? Array<string>
---@field test_file_suffix? string
---@field story_roots? Array<string>
---@field story_file_suffix? string
---@field test_extensions? Array<string>
---@field story_extensions? Array<string>
---@field ignore_path? Array<string>
---@field source_root string
---@field test_roots Array<string>
---@field test_file_suffix string
---@field story_roots Array<string>
---@field story_file_suffix string
---@field test_extensions Array<string>
---@field story_extensions Array<string>
---@field ignore_path Array<string>

---@type TeleporterConfig
local teleporter_default = {
local _default = {
-- Root directory of source.
source_root = "src",
-- Root directories of tests.
Expand All @@ -41,77 +42,86 @@ local teleporter_default = {
ignore_path = { "node_modules" },
}

local first_non_nil = function(...)
local n = select("#", ...)
for i = 1, n do
local value = select(i, ...)
if value ~= nil then
return value
end
end
end
---@class TeleporterConfigOptions
---@field source_root? string
---@field test_roots? Array<string>
---@field test_file_suffix? string
---@field story_roots? Array<string>
---@field story_file_suffix? string
---@field test_extensions? Array<string>
---@field story_extensions? Array<string>
---@field ignore_path? Array<string>

---@param opts TeleporterConfig
config.set_options = function(opts)
local get = function(name, default_value)
return first_non_nil(opts[name], teleporter_default[name], default_value)
---@param opts? TeleporterConfigOptions
M.setup = function(opts)
if vim.fn.has("nvim-0.11") == 1 then
vim.validate("opts", opts, "table", true)
else
vim.validate({ opts = { opts, "table", true } })
end

local set = function(name, default_value)
config.values[name] = get(name, default_value)
end
opts = opts or {}

for k, v in pairs(teleporter_default) do
set(k, v)
end
local merged = vim.tbl_deep_extend("force", _default, opts)

local M = {}
M.get = get
return M
M._options = merged
end

config.set_options({})

---@param self TeleporterConfigObject
---@return string
config.src_root = function(self)
return self.values.source_root
M.src_root = function(self)
return self._options.source_root or ""
end

---@param self TeleporterConfigObject
---@param context "test" | "story"
---@param context TeleporterContext
---@return Array<string>
config.context_roots = function(self, context)
M.context_roots = function(self, context)
if context ~= "test" and context ~= "story" then
require("js-teleporter.logger").print_err("Invalid context: " .. context)
return {}
end

if context == "test" then
return self.values.test_roots
return self._options.test_roots
elseif context == "story" then
return self.values.test_roots
return self._options.test_roots
end

return {}
end

---@param self TeleporterConfigObject
---@param context "test" | "story"
---@param context TeleporterContext
---@return string
config.context_suffix = function(self, context)
M.context_suffix = function(self, context)
if context ~= "test" and context ~= "story" then
require("js-teleporter.logger").print_err("Invalid context: " .. context)
return ""
end

if context == "test" then
return self.values.test_file_suffix
return self._options.test_file_suffix
elseif context == "story" then
return self.values.story_file_suffix
return self._options.story_file_suffix
end

return ""
end

return config
M.context_extensions = function(self, context)
if context ~= "test" and context ~= "story" then
require("js-teleporter.logger").print_err("Invalid context: " .. context)
return {}
end

if context == "test" then
return self._options.test_extensions
elseif context == "story" then
return self._options.story_extensions
end

return {}
end

return M
25 changes: 14 additions & 11 deletions lua/js-teleporter/init.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
local buffer = require("js-teleporter.buffer")
local logger = require("js-teleporter.logger")

local M = {}

---@param opts TeleporterConfig Configuration options
---@param opts TeleporterConfigOptions Partial configuration options
M.setup = function(opts)
require("js-teleporter.config").set_options(opts)
require("js-teleporter.config").setup(opts)
end

---Suggest user to create new file if the destination file is not found
Expand All @@ -23,17 +26,17 @@ M.suggest_to_create_file = function(context, suggestions)
return
end

local filepath = require("js-teleporter.buffer").new_file(choice)
local filepath = buffer.new_file(choice)
if filepath then
vim.cmd.edit(filepath)
require("js-teleporter.logger").print_msg('"' .. choice .. '" created!')
logger.print_msg('"' .. choice .. '" created!')
end
end)
end

---Run teleport
---@param context "test" | "story"
---@param opts TeleporterConfig
---@param context TeleporterContext
---@param opts TeleporterConfigObject
M.teleport = function(context, opts)
local teleporter = require("js-teleporter.teleporter")

Expand All @@ -43,23 +46,23 @@ M.teleport = function(context, opts)
return
end

if not require("js-teleporter.buffer").is_js_file(context, bufname, opts) then
require("js-teleporter.logger").print_err("The file is not javascript/typescript.")
if not buffer.is_js_file(context, bufname, opts) then
logger.print_err("The file is not javascript/typescript.")
return
end

local workspace_path = vim.fn.getcwd()

local destination = teleporter.teleport(context, bufname, opts)
if not destination or destination == "" then
if require("js-teleporter.buffer").is_other_file(context, bufname, opts) then
require("js-teleporter.logger").print_err("Teleport destination is not found.")
if buffer.is_other_file(context, bufname, opts) then
logger.print_err("Teleport destination is not found.")
return
end

local suggestions = teleporter.suggest_other_file(context, bufname, workspace_path, opts)
if #suggestions == 0 then
require("js-teleporter.logger").print_err("Teleport destination is not found.")
logger.print_err("Teleport destination is not found.")
return
end

Expand Down
Loading