aboutsummaryrefslogtreecommitdiffstats
path: root/lua/pi/treesitter.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/pi/treesitter.lua')
0 files changed, 0 insertions, 0 deletions
n44'>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
local ls = require("luasnip")

local snippet_from_nodes = ls.sn
local c = ls.choice_node
local t = ls.text_node
local d = ls.dynamic_node
local f = ls.function_node
local i = ls.insert_node
local fmt = require("luasnip.extras.fmt").fmt

function string.starts(String, Start)
	return string.sub(String, 1, string.len(Start)) == Start
end

local newline = function(text)
	return t({ "", text })
end

local visibility = function(position, default)
	local possibles = { "private", "public" }
	local options = {}

	for _, value in pairs(possibles) do
		if value == default then
			table.insert(options, 1, t(value))
		else
			table.insert(options, t(value))
		end
	end

	return c(position, options)
end

local namespace = function()
	local dir = vim.fn.expand("%:h")
	local autoloads = vim.call("composer#query", "autoload.psr-4")
	if autoloads == nil then
		return (dir:gsub("^%l", string.upper))
	end

	local globalNamespace
	for key, value in pairs(autoloads) do
		if string.starts(dir, value:sub(1, -2)) then
			globalNamespace = key:sub(1, -2)
			dir = dir:sub(#key + 1)
			break
		end
	end
	dir = dir:gsub("/", "\\")

	return string.format("%s\\%s", globalNamespace, dir)
end

local class_name = function()
	return vim.fn.expand("%:t:r")
end

local M = {
	v = fmt(
		[[
/**
 * @var {}
 */
{} ${};
]],
		{
			i(1, "type"),
			visibility(2, "private"),
			i(3, "var"),
		}
	),
	class = fmt(
		[[
<?php

declare(strict_types=1);

namespace {};

class {}
{{
    {}
}}
]],
		{
			f(namespace),
			f(class_name),
			i(0),
		}
	),

	pro = fmt([[{} {} ${},]], {
		visibility(1, "private"),
		i(2, "Type"),
		f(function(args)
			return args[1][1]:gsub("^%u", string.lower)
		end, { 2 }),
	}),

	_c = fmt(
[[{} function __construct({}) {{
    {}
}}]],
		{
			visibility(1, "public"),
			i(2),
			i(0),
		}
	),

	_p = fmt(
		[[{} function __construct(
    {}
) {{
}}]],
		{
			visibility(1, "public"),
			i(2),
		}
	),

	fn = {
		visibility(1, "public"),
		t(" function "),
		i(2, "name"),
		t("("),
		i(3, "$arg"),
		t(")"),
		c(4, {
			t(""),
			snippet_from_nodes(nil, {
				t(": "),
				i(1, "void"),
			}),
		}),
		newline("{"),
		newline("\t"),
		i(0, ""),
		newline("}"),
	},

	strict = t("declare(strict_types=1);"),
}

return M