57 lines
2.2 KiB
Lua
57 lines
2.2 KiB
Lua
local null_ls = require('null-ls')
|
|
local plenary = require('plenary')
|
|
|
|
local kustomize_condition = function(node, params)
|
|
if node == nil then return false end
|
|
|
|
if vim.treesitter.get_node_text(node, params['bufnr']) ~= 'resources' then return false end
|
|
|
|
return true
|
|
end
|
|
|
|
---@module 'null-ls'
|
|
---@type
|
|
local kustomize_action = {
|
|
method = null_ls.methods.CODE_ACTION,
|
|
filetypes = { 'yaml' },
|
|
generator = {
|
|
fn = function(params)
|
|
local out = {}
|
|
local node = vim.treesitter.get_node()
|
|
if kustomize_condition(node, params) then
|
|
table.insert(out, {
|
|
title = '📁 Create resources',
|
|
action = function()
|
|
local node_below = vim.treesitter.get_node({ bufnr = 0, pos = { params['row'] + 1, 0 } })
|
|
|
|
if node_below == nil or node_below:type() ~= 'block_sequence' then
|
|
vim.api.nvim_echo({ { '❌Failed to get list items.', 'Normal' } }, false, {})
|
|
return
|
|
end
|
|
|
|
local checked_files = ''
|
|
for list_item, _ in node_below:iter_children() do
|
|
if list_item ~= nil then
|
|
local item_value_node = list_item:child(1) -- 0 is `-`, 1 is the actual value
|
|
if item_value_node ~= nil then
|
|
local file_name = vim.treesitter.get_node_text(item_value_node, params['bufnr'])
|
|
local file_name_path = plenary.path.new(file_name)
|
|
local current_file = plenary.path.new(vim.fn.expand('%'))
|
|
local parent = plenary.path.new(current_file:parents()[1])
|
|
local new_file = parent / file_name
|
|
new_file:touch({mode=644})
|
|
end
|
|
end
|
|
end
|
|
end,
|
|
})
|
|
end
|
|
return out
|
|
end,
|
|
},
|
|
}
|
|
|
|
-- null_ls.deregister(kustomize_action)
|
|
null_ls.register(kustomize_action)
|
|
|