Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/go to file #129

Merged
merged 4 commits into from
Feb 7, 2025
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
1 change: 1 addition & 0 deletions lua/laravel/providers/laravel_provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function laravel_provider:register(app)
app:bindIf("ui_handler", "laravel.services.ui_handler")
app:bindIf("view_finder", "laravel.services.view_finder")
app:bindIf("views", "laravel.services.views")
app:bindIf("gf", "laravel.services.gf")

app:singeltonIf("cache", "laravel.services.cache")
app:singeltonIf("env", "laravel.services.environment")
Expand Down
31 changes: 16 additions & 15 deletions lua/laravel/providers/user_command_provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function user_command_provider:register(app)
app:bindIf("resources_command", "laravel.services.commands.resources", { tags = { "command" } })
app:bindIf("view_finder_command", "laravel.services.commands.view_finder", { tags = { "command" } })
app:bindIf("flush_cache_command", "laravel.services.commands.flush_cache", { tags = { "command" } })
app:bindIf("gf_command", "laravel.services.commands.gf", { tags = { "command" } })

app:singeltonIf("serve_command", "laravel.services.commands.serve", { tags = { "command" } })
app:singeltonIf("assets_command", "laravel.services.commands.assets", { tags = { "command" } })
Expand All @@ -31,12 +32,12 @@ function user_command_provider:boot(app)
if not args.fargs[1] then
vim.ui.select(
vim
.iter(app("user_commands"))
:map(function(command)
return command:commands()
end)
:flatten()
:totable(),
.iter(app("user_commands"))
:map(function(command)
return command:commands()
end)
:flatten()
:totable(),
{ prompt = "Laravel command: " },
function(selected)
if not selected then
Expand Down Expand Up @@ -67,15 +68,15 @@ function user_command_provider:boot(app)
local fCmdLine = vim.split(cmdLine, " ")
if #fCmdLine <= 2 then
return vim
.iter(app("user_commands"))
:map(function(command)
return command:commands()
end)
:flatten()
:filter(function(subcommand)
return vim.startswith(subcommand, argLead)
end)
:totable()
.iter(app("user_commands"))
:map(function(command)
return command:commands()
end)
:flatten()
:filter(function(subcommand)
return vim.startswith(subcommand, argLead)
end)
:totable()
elseif #fCmdLine == 3 then
local command = vim.iter(app("user_commands")):find(function(cmd)
return vim.iter(cmd:commands()):any(function(name)
Expand Down
65 changes: 65 additions & 0 deletions lua/laravel/services/commands/gf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local split = require("laravel.utils").split
local actions = require("laravel.pickers.common.actions")

local gf_command = {}

function gf_command:new(views, gf, cache_routes_repository)
local instance = {
views = views,
gf = gf,
routes = cache_routes_repository,
}
setmetatable(instance, self)
self.__index = self
return instance
end

function gf_command:commands()
return { "gf" }
end

function gf_command:handle()
local node, resource_type = self.gf:cursor_on_resource()
if not node then
return
end

if resource_type == "view" then
self.views:open(vim.treesitter.get_node_text(node, 0, {}))
return
end

if resource_type == "config" then
-- app.name
local config_name = vim.treesitter.get_node_text(node, 0, {})
local s = split(config_name, ".")

-- TODO: can be improve by parsing the file with treesitter.
-- find the return and with the the array elements with the next items
vim.cmd("e config/" .. s[1] .. ".php")
return
end

if resource_type == "env" then
local env_name = vim.treesitter.get_node_text(node, 0, {})
vim.cmd("e .env")
vim.fn.search(env_name)
vim.cmd("normal zt")
return
end

if resource_type == "route" then
local route_name = vim.treesitter.get_node_text(node, 0, {})
self.routes:all():thenCall(function(routes)
for _, route in ipairs(routes) do
if route.name == route_name then
actions.open_route(route)
return
end
end
vim.notify("Route not found", vim.log.levels.WARN)
end)
end
end

return gf_command
44 changes: 44 additions & 0 deletions lua/laravel/services/gf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local gf = {}

function gf:new()
local instance = {
}
setmetatable(instance, self)
self.__index = self
return instance
end

function gf:cursor_on_resource()
local node = vim.treesitter.get_node()
if not node then
return false
end

if node:type() ~= "string_content" then
return false
end

local parent = node:parent()
while parent ~= nil and parent:type() ~= "function_call_expression" do
parent = parent:parent()
end

if not parent then
return false
end

local func_node = parent:child(0)
if not func_node then
return false
end

local func_name = vim.treesitter.get_node_text(func_node, 0, {})

if vim.tbl_contains({'route', 'view', 'config', 'env'}, func_name) then
return node, func_name
end

return false
end

return gf