Skip to content
Open
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
87 changes: 73 additions & 14 deletions lua/goto-alias/init.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
--TODO:
--tests
--does .nuxt directory detection using string.concat have better preformance?
--newly created components do not seem to workb
--newly created components do not seem to work

local M = {}

M.is_nuxt_project = false
M.original_definition = vim.lsp.buf.definition
M.is_nuxt_project = false
M.nuxt_directory_path = ""
M.it_worked = false

local default_check_directories = { "", "/apps/web", "/apps/nuxt", "/apps/frontend", "/apps/website" }

M.setup = function()
local dirList = vim.fn.systemlist("ls -a")
M.setup = function(opts)
opts = opts or {}

for _, dirname in ipairs(dirList) do
if dirname == ".nuxt" then
local check_directories = vim.list_extend(default_check_directories, opts.check_directories or {})

for _, directory in ipairs(check_directories) do
if vim.fn.isdirectory(vim.loop.cwd() .. directory .. "/.nuxt") == 1 then
M.is_nuxt_project = true
if directory ~= "" then
M.nuxt_directory_path = string.sub(directory, 2)
end
break
end
end

Expand All @@ -27,20 +37,69 @@ M.setup = function()
vim.lsp.buf.definition = M.watch
end,
})

-- this is in the original repo. whyyy???
-- - maybe for when you change repo without closing neovim?
-- M.is_nuxt_project = false
-- M.original_definition = vim.lsp.buf.definition
-- M.nuxt_directory_path = ""
end

local redirect_if_auto_import = function()
if M.it_worked then
return
end

local line = vim.fn.getline(".")
local path = string.match(line, '".-/(.-)"')
local file = vim.fn.expand("%")

local auto_import_files = { "components.d.ts", "imports.d.ts" }

for _, auto_import_file in ipairs(auto_import_files) do
if string.find(file, auto_import_file) then
vim.api.nvim_buf_delete(0, { force = false })
vim.cmd("edit " .. M.nuxt_directory_path .. "/" .. path)
M.it_worked = true
break
elseif string.find(line, auto_import_file) then
if string.find(line, "stores") then
local storePath = string.match(line, "'.-/(.-)'")
vim.cmd("cclose")
vim.cmd("edit " .. M.nuxt_directory_path .. "/stores/" .. storePath .. ".ts")
elseif string.find(line, "composables") then
local composablePath = string.match(line, "'.-/(.-)'")
vim.cmd("cclose")
vim.cmd("edit " .. M.nuxt_directory_path .. "/composables/" .. composablePath .. ".ts")
elseif string.find(line, "utils") then
local utilsPath = string.match(line, "'.-/(.-)'")
vim.cmd("cclose")
vim.cmd("edit " .. M.nuxt_directory_path .. "/utils/" .. utilsPath .. ".ts")
else
vim.cmd("cclose")
vim.cmd("edit " .. M.nuxt_directory_path .. "/" .. path)
end
M.it_worked = true
break
end
end
end

M.watch = function()
M.original_definition()

vim.defer_fn(function()
local line = vim.fn.getline(".")
local path = string.match(line, '".-/(.-)"')
local file = vim.fn.expand("%")
if not M.is_nuxt_project then
return
end

if string.find(file, "components.d.ts") then
vim.cmd("edit " .. path)
end
end, 100)
-- retry with exponential backoff
vim.defer_fn(redirect_if_auto_import, 10)
vim.defer_fn(redirect_if_auto_import, 50)
vim.defer_fn(redirect_if_auto_import, 100)
vim.defer_fn(redirect_if_auto_import, 1000)
vim.defer_fn(function()
M.it_worked = false
end, 1100)
end

return M