Created
August 22, 2024 05:22
-
-
Save adalessa/cfcae0221b6a3b2a163f9746ffa5a99e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local app = require("laravel").app | |
local function model_info(bufnr) | |
local namespace = vim.api.nvim_create_namespace("laravel.model") | |
app("class"):get(bufnr, function(class) | |
app("api"):tinker( | |
string.format( | |
[[ | |
$r = new ReflectionClass("%s"); | |
$isModel = $r->isSubclassOf("Illuminate\Database\Eloquent\Model"); | |
echo json_encode([ | |
'is_model' => $isModel, | |
'class_start' => $r->getStartLine(), | |
]); | |
]], | |
class.fqn | |
), | |
function(res) | |
if res:successful() and res:json().is_model then | |
app("api"):async( | |
"artisan", | |
{ "model:show", "--json", string.format("\\%s", class.fqn) }, | |
function(result) | |
if result:successful() then | |
local info = result:json() | |
local virt_lines = { | |
{ { "[", "comment" } }, | |
{ { " Database: ", "comment" }, { info.database, "@enum" } }, | |
{ { " Table: ", "comment" }, { info.table, "@enum" } }, | |
{ { " Attributes: ", "comment" } }, | |
} | |
for _, attribute in ipairs(info.attributes) do | |
table.insert(virt_lines, { | |
{ " " .. attribute.name, "@enum" }, | |
{ " " .. attribute.type, "comment" }, | |
attribute.cast and { " -> " .. attribute.cast, "@enum" } or nil, | |
}) | |
end | |
table.insert(virt_lines, { { "]", "comment" } }) | |
vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) | |
vim.api.nvim_buf_set_extmark(bufnr, namespace, res:json().class_start - 1, 0, { | |
virt_lines = virt_lines, | |
virt_lines_above = true, | |
}) | |
else | |
vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) | |
end | |
end, | |
{ wrap = true } | |
) | |
else | |
vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) | |
end | |
end | |
) | |
end, function() | |
vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1) | |
end) | |
end | |
local group = vim.api.nvim_create_augroup("laravel.model_info", {}) | |
vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost" }, { | |
pattern = "*.php", | |
group = group, | |
callback = function(ev) | |
model_info(ev.buf) | |
end, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment