-
Notifications
You must be signed in to change notification settings - Fork 28
Model related files picker #45
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
Merged
adalessa
merged 9 commits into
adalessa:main
from
dorkster100:model-related-files-picker
Aug 31, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f760c5e
Fix nil check when env not defined
a449be8
fix style
e82e550
more style
c6ac8ad
Merge remote-tracking branch 'upstream/main'
6c8adde
add related picker
c049c6f
add description to README
e3b7597
separate picker into separate file and better logs
4aec90d
fix linter issues
be55b70
more style fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| local make_related = function(pickers, application, lsp, make_entry, finders, conf, actions, action_state) | ||
| local utils = require "laravel.utils" | ||
| return function(opts) | ||
| opts = opts or {} | ||
|
|
||
| local file_type = vim.bo.filetype | ||
| local lang = vim.treesitter.language.get_lang(file_type) | ||
| if lang ~= "php" then | ||
| return false | ||
| end | ||
|
|
||
| local get_model_class_name = function() | ||
| local query = vim.treesitter.query.parse( | ||
| lang, | ||
| [[ (namespace_definition name: (namespace_name) @namespace) | ||
| (class_declaration name: (name) @class) ]] | ||
| ) | ||
| local tree = vim.treesitter.get_parser():parse()[1]:root() | ||
| local bufNr = vim.fn.bufnr() | ||
| local class = "" | ||
| for id, node, _ in query:iter_captures(tree, bufNr, tree:start(), tree:end_()) do | ||
| if query.captures[id] == "class" then | ||
| class = class .. "\\" .. vim.treesitter.get_node_text(node, 0) | ||
| elseif query.captures[id] == "namespace" then | ||
| class = vim.treesitter.get_node_text(node, 0) .. class | ||
| end | ||
| end | ||
| return class | ||
| end | ||
|
|
||
| local class = get_model_class_name() | ||
| if class ~= "" then | ||
| local result, ok = application.run("artisan", { "model:show", class, "--json" }, { runner = "sync" }) | ||
| if not ok then | ||
| utils.notify( | ||
| "Artisan", | ||
| { msg = "'php artisan model:show " .. class .. " --json' command failed", level = "ERROR" } | ||
| ) | ||
| return nil | ||
| end | ||
|
|
||
| if result.exit_code ~= 0 or string.sub(result.out[1], 1, 1) ~= "{" or string.sub(result.out[1], -1) ~= "}" then | ||
| utils.notify( | ||
| "Artisan", | ||
| { msg = "'php artisan model:show" .. class .. " --json' response could not be decoded", level = "ERROR" } | ||
| ) | ||
| return nil | ||
| end | ||
|
|
||
| local model_info = vim.fn.json_decode(result.out[1]) | ||
| if model_info == nil then | ||
| utils.notify( | ||
| "Artisan", | ||
| { msg = "'php artisan model:show" .. class .. " --json' response could not be decoded", level = "ERROR" } | ||
| ) | ||
| return nil | ||
| end | ||
|
|
||
| ---@return ModelRelation|nil | ||
| local build_relation = function(info, relation_type) | ||
| if next(info) == nil then | ||
| return nil | ||
| end | ||
| if relation_type == "observers" and info["observer"][2] ~= nil then | ||
| return { | ||
| class = info["observer"][2], | ||
| type = relation_type, | ||
| extra_information = info["event"], | ||
| } | ||
| elseif relation_type == "relations" then | ||
| return { | ||
| class = info["related"], | ||
| type = relation_type, | ||
| extra_information = info["type"] .. " " .. info["name"], | ||
| } | ||
| elseif relation_type == "policy" then | ||
| return { | ||
| class = info[1], | ||
| type = relation_type, | ||
| extra_information = "", | ||
| } | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| local relations = {} | ||
| local types = { "observers", "relations", "policy" } | ||
| for _, relation_type in ipairs(types) do | ||
| if model_info[relation_type] ~= vim.NIL and #model_info[relation_type] > 0 then | ||
| if type(model_info[relation_type]) == "table" and model_info[relation_type][1] ~= vim.NIL then | ||
| for _, info in ipairs(model_info[relation_type]) do | ||
| local relation = build_relation(info, relation_type) | ||
| if relation ~= nil then | ||
| table.insert(relations, relation) | ||
| end | ||
| end | ||
| else | ||
| local relation = build_relation({ model_info[relation_type] }, relation_type) | ||
| if relation ~= nil then | ||
| table.insert(relations, relation) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| pickers | ||
| .new(opts, { | ||
| prompt_title = "Related Files", | ||
| finder = finders.new_table { | ||
| results = relations, | ||
| entry_maker = make_entry.gen_from_model_relations(opts), | ||
| }, | ||
| sorter = conf.prefilter_sorter { | ||
| sorter = conf.generic_sorter(opts or {}), | ||
| }, | ||
| attach_mappings = function(_, map) | ||
| map("i", "<cr>", function(prompt_bufnr) | ||
| actions.close(prompt_bufnr) | ||
| local entry = action_state.get_selected_entry() | ||
| vim.schedule(function() | ||
| local action = vim.fn.split(entry.value.class, "@") | ||
| lsp.go_to(action[1], action[2]) | ||
| end) | ||
| end) | ||
|
|
||
| return true | ||
| end, | ||
| }) | ||
| :find() | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return make_related |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The luacheck is failing due wrong indent. please fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do, it will take some time as there are some personal things I am going through and work has piled up as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adalessa i am back and will do the changes soon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adalessa fixes are pushed, please check.