aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/telescope_laravel.lua
blob: 562a1512a934381d318f050ffbc4c7a3337e3a46 (plain)
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
local actions = require('telescope.actions')
local pickers = require "telescope.pickers"
local finders = require "telescope.finders"
local previewers = require "telescope.previewers"
local conf = require("telescope.config").values
local putils = require "telescope.previewers.utils"
local action_state = require "telescope.actions.state"
local utils = require "telescope.utils"
local laravel = {}

function Split(s, delimiter)
    local result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end

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,
}

function getArtisanCommand(args)
    local artCmdStr = vim.g.laravel_artisan
    if artCmdStr == nil then
        artCmdStr = "php artisan"
    end
    if type(args) ~= "table" then
        return Split(artCmdStr .. ' ' .. args, " ")
    end
    local cmd = Split(artCmdStr, ' ')
    for _, value in ipairs(args) do
        table.insert(cmd, value)
    end

    return cmd
end

laravel.artisan = function()

    local results = utils.get_os_command_output(getArtisanCommand('--raw'))

    pickers.new({}, {
        prompt_title = "Artisan commands",
        finder = finders.new_table {
            results = results,
            entry_maker = function (line)
                return setmetatable({
                    Split(line, " ")[1],
                }, mt_string_entry)
            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 = getArtisanCommand({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()
                actions.close(prompt_bufnr)
                vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(":Artisan " .. entry.value .. " ", true, false, true), "t", true)
            end)
            return true
        end
    }):find()
end

return laravel