1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local utils = require("telescope.utils")
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local previewers = require("telescope.previewers")
local putils = require("telescope.previewers.utils")
local conf = require("telescope.config").values
if vim.fn.findfile("composer.json") == nil then
return
end
local composer_json = vim.fn.json_decode(vim.fn.readfile(vim.fn.getcwd() .. "/composer.json"))
if composer_json["require"]["laravel/framework"] == nil then
return
end
local artisan_command
if composer_json["require-dev"]["laravel/sail"] ~= nil then
artisan_command = "sail"
else
artisan_command = "php"
end
local get_artisan_command = function()
return artisan_command
end
-- TODO review why do I have this
-- local lookup_keys = {
-- display = 1,
-- ordinal = 1,
-- value = 1,
-- }
-- local mt_string_entry = {
-- __index = function(t, k)
-- return rawget(t, rawget(lookup_keys, k))
-- end,
-- }
local function is_make_command(command)
local command_split = vim.split(command, ":")
return command_split[1] == "make"
end
local make_commands_map = {
cast = "app/Casts",
channel = "app/Broadcasting",
command = "app/Console/Commands",
component = "app/View/Components",
controller = "app/Http/Controllers",
event = "app/Events",
exception = "app/Exceptions",
factory = "",
job = "app/Jobs",
listener = "app/Listeners",
mail = "app/Mail",
middleware = "app/Http/Middleware",
migration = "",
model = "app/Models",
notification = "app/Notifications",
observer = "app/Observers",
policy = "app/Policies",
provider = "app/Providers",
request = "app/Http/Requests",
resource = "app/Http/Resources",
rule = "app/Rules",
scope = "app/Models/Scopes",
seeder = "",
test = "tests/Feature",
}
local function get_directory_by_command(name)
return make_commands_map[name]
end
local function handle_make_command(command)
-- change prompt from "name" to args
-- parse args from help
-- parse options from help
-- can parse as json
-- how to present the information
vim.ui.input({ prompt = "Name: " }, function(name)
vim.fn.jobstart({ get_artisan_command(), "artisan", command, name }, {
on_exit = function()
local directory = get_directory_by_command(vim.split(command, ":")[2])
if directory == "" then
return
end
local filename = string.format("%s/%s.php", directory, name)
if vim.fn.findfile(filename) then
local uri = vim.uri_from_fname(string.format("%s/%s", vim.fn.getcwd(), filename))
local buffer = vim.uri_to_bufnr(uri)
vim.api.nvim_win_set_buf(0, buffer)
end
end,
})
end)
end
local laravel = function(opts)
-- TODO check if cmd == sail check if sail is running
opts = opts or {}
local results = utils.get_os_command_output({ get_artisan_command(), "artisan", "list", "--raw" })
-- do I want to change from raw to json and decode it?
-- what advantages will I get
-- I have group by the id like "make" the command is the same
pickers
.new({}, {
prompt_title = "Artisan commands",
finder = finders.new_table({
results = results,
entry_maker = function(line)
local artisan_cmd = vim.split(line, " ")[1]
return {
value = artisan_cmd,
display = artisan_cmd,
ordinal = artisan_cmd,
}
end,
}),
previewer = previewers.new_buffer_previewer({
title = "Help",
get_buffer_by_name = function(_, entry)
return entry.value
end,
define_preview = function(self, entry)
local cmd = { get_artisan_command(), "artisan", entry.value, "-h" }
putils.job_maker(cmd, self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname,
})
end,
}),
sorter = conf.file_sorter(),
attach_mappings = function(_, map)
map("i", "<cr>", function(prompt_bufnr)
local entry = action_state.get_selected_entry()
-- check if entry.value is a make
actions.close(prompt_bufnr)
-- local cmd = string.format(":!%s %s ", get_artisan_command(), entry.value)
-- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(cmd, true, false, true), "t", true)
if is_make_command(entry.value) then
handle_make_command(entry.value)
end
end)
return true
end,
})
:find()
end
laravel({})
-- TODO list
-- open output buffer to show result of not make commands
-- Add option to add arguments for command
-- set a key when choosing the command or config, to ask for properties as well as arguments
-- take arguments in the same input
--
-- Create Sail user command
-- Create Artisan user command
--
-- local uri = vim.uri_from_fname(vim.fn.getcwd() .. "/composer.json")
-- local buffer = vim.uri_to_bufnr(uri)
-- if not vim.api.nvim_buf_is_loaded(buffer) then
-- vim.api.nvim_buf_attach(buffer, false, {})
-- end
-- local content = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
-- local json = vim.fn.join(content)
-- local decoded_json = vim.fn.json_decode(json)
-- P(decoded_json["require-dev"]["laravel/sail"])
--
-- P(vim.fn.fnamemodify(vim.fn.findfile("composer.json"), ":p:h"))
-- local decoded_json = vim.fn.json_decode(vim.fn.readfile(vim.fn.getcwd() .. "/composer.json"))
-- P(decoded_json["require-dev"]["laravel/sail"])
-- P(decoded_json["require"]["laravel/sail"])
-- find file composer.json
-- generate base path with fnamemodify or with getcwd
-- to run the commands can use jobstart. but think this response line by line, but have on exit as well
-- I can use plenary job but not sure if makes sence
-- vim.fn.jobstart({"sail", "artisan", "make:controller", "MyController"}, {
-- stdout_buffered = true,
-- stderr_buffered = true,
-- on_exit = function ()
-- P("job ended")
-- if vim.fn.findfile("app/Http/Controllers/MyController.php") then
-- local uri = vim.uri_from_fname(vim.fn.getcwd() .. "/app/Http/Controllers/MyController.php")
-- local buffer = vim.uri_to_bufnr(uri)
-- vim.api.nvim_win_set_buf(0, buffer)
-- end
-- end,
-- -- on_stdout = function (_, data)
-- -- P("stdout")
-- -- P(data)
-- -- end,
-- -- on_stderr = function (_, data)
-- -- P("stderr")
-- -- P(data)
-- -- end,
-- })
-- local Job = require("plenary.job")
-- Job:new({
-- command = 'sail',
-- args = {'artisan', 'make:controller', 'MyController'},
-- on_exit = function (j, return_val)
-- P(return_val)
-- P(j:result())
-- P(j)
-- end
-- }):sync()
|