aboutsummaryrefslogtreecommitdiffstats
path: root/lua/pi/snips/ft
diff options
context:
space:
mode:
authorElvis Claros Castro <elvis@claros.ar>2024-07-12 11:18:14 -0300
committerElvis Claros Castro <elvis@claros.ar>2024-07-12 11:18:14 -0300
commitea0f4bf8ffade657b1d45152dbc183d4679e75c4 (patch)
treecc1fb023bc308e019ddce33a4b0aed39f56805dc /lua/pi/snips/ft
parent1fd0af14d592d612c2aaab7717c04c8806ba3daa (diff)
downloadnvim-conf-ea0f4bf8ffade657b1d45152dbc183d4679e75c4.tar.gz
nvim-conf-ea0f4bf8ffade657b1d45152dbc183d4679e75c4.zip
Feat: PI
Diffstat (limited to 'lua/pi/snips/ft')
-rw-r--r--lua/pi/snips/ft/cucumber.lua71
-rw-r--r--lua/pi/snips/ft/go.lua134
-rw-r--r--lua/pi/snips/ft/lua.lua69
-rw-r--r--lua/pi/snips/ft/php.lua163
-rw-r--r--lua/pi/snips/ft/yaml.lua225
5 files changed, 662 insertions, 0 deletions
diff --git a/lua/pi/snips/ft/cucumber.lua b/lua/pi/snips/ft/cucumber.lua
new file mode 100644
index 0000000..6176a87
--- /dev/null
+++ b/lua/pi/snips/ft/cucumber.lua
@@ -0,0 +1,71 @@
+local ls = require "luasnip"
+local t = ls.text_node
+local i = ls.insert_node
+local c = ls.choice_node
+local fmt = require("luasnip.extras.fmt").fmt
+
+local M = {
+ clean = t [[Given clean stubs list]],
+ all = t [[And all stubs should be matched]],
+ status_code = fmt([[Then the response status code should be {}]], { i(1, "200") }),
+ no_error = t [[And the JSON path expression errors should not have result]],
+ json_data = fmt([[And the JSON path expression "data" should be equal to:
+"""
+{{
+ {}
+}}
+"""
+ ]], {i(1)}),
+ feature = fmt(
+ [[Feature: {}
+
+ {}
+]],
+ {
+ i(1, "Feature Name"),
+ i(0),
+ }
+ ),
+ scenario = fmt(
+ [[Scenario: {}
+ {}]],
+ {
+ i(1, "Scenario name"),
+ i(0),
+ }
+ ),
+ stub = fmt([[And the APIClient will send a request with a stub from "{}"]], { i(1) }),
+ request = fmt(
+ [[When I send the GraphQL request:
+"""
+{}
+"""]],
+ { i(1) }
+ ),
+ result = fmt(
+ [[And the JSON path expression "data" should be equal to:
+"""
+{{
+ "result": {{
+ {}
+ }}
+}}
+"""]],
+ { i(1) }
+ ),
+ query = fmt(
+ [[{} {{
+result: {}({}) {{
+ {}
+}}
+ }}]],
+ { c(1, { t "mutation", t "query" }), i(2, "mutation"), i(3, ""), i(4) }
+ ),
+ access = fmt(
+ [[And the Access Cache has Allowed the userId "{}" for inspectionId "{}"]],
+ { i(1, "3414054"), i(2, "42") }
+ ),
+ branch_access = fmt([[And the Access Cache has branch "{}" for userId "{}"]], { i(1, "1"), i(2, "3414054") }),
+}
+
+return M
diff --git a/lua/pi/snips/ft/go.lua b/lua/pi/snips/ft/go.lua
new file mode 100644
index 0000000..9c3977f
--- /dev/null
+++ b/lua/pi/snips/ft/go.lua
@@ -0,0 +1,134 @@
+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 i = ls.insert_node
+local rep = require("luasnip.extras").rep
+local fmt = require("luasnip.extras.fmt").fmt
+
+local ts_locals = require "nvim-treesitter.locals"
+local ts_utils = require "nvim-treesitter.ts_utils"
+
+local get_node_text = vim.treesitter.get_node_text
+
+vim.treesitter.query.set(
+ "go",
+ "LuaSnip_Result",
+ [[
+ [
+ (method_declaration result: (_) @id)
+ (function_declaration result: (_) @id)
+ (func_literal result: (_) @id)
+ ]
+]]
+)
+
+local transform = function(text, info)
+ if text == "int" then
+ return t "0"
+ elseif text == "error" then
+ if info then
+ info.index = info.index + 1
+
+ return c(info.index, {
+ t(string.format('fmt.Errorf("%s %%w", %s)', info.func_name, info.err_name)),
+ t(info.err_name),
+ })
+ else
+ return t "err"
+ end
+ elseif text == "bool" then
+ return t "false"
+ elseif text == "string" then
+ return t '""'
+ elseif string.find(text, "*", 1, true) then
+ return t "nil"
+ end
+
+ return t(text)
+end
+
+local handlers = {
+ ["parameter_list"] = function(node, info)
+ local result = {}
+
+ local count = node:named_child_count()
+ for idx = 0, count - 1 do
+ table.insert(result, transform(get_node_text(node:named_child(idx), 0), info))
+ if idx ~= count - 1 then
+ table.insert(result, t { ", " })
+ end
+ end
+
+ return result
+ end,
+
+ ["type_identifier"] = function(node, info)
+ local text = get_node_text(node, 0)
+ return { transform(text, info) }
+ end,
+}
+
+local function go_result_type(info)
+ local cursor_node = ts_utils.get_node_at_cursor()
+ local scope = ts_locals.get_scope_tree(cursor_node, 0)
+
+ local function_node
+ for _, v in ipairs(scope) do
+ if v:type() == "function_declaration" or v:type() == "method_declaration" or v:type() == "func_literal" then
+ function_node = v
+ break
+ end
+ end
+
+ local query = vim.treesitter.query.get("go", "LuaSnip_Result")
+ for _, node in query:iter_captures(function_node, 0) do
+ if handlers[node:type()] then
+ return handlers[node:type()](node, info)
+ end
+ end
+end
+
+local go_ret_vals = function(args)
+ local info = { index = 0, err_name = args[1][1], func_name = args[2][1] }
+ return snippet_from_nodes(nil, go_result_type(info))
+end
+
+local M = {
+ main = {
+ t { "func main() {", "\t" },
+ i(0),
+ t { "", "}" },
+ },
+ ef = {
+ i(1, { "val" }),
+ t ", err := ",
+ i(2, { "f" }),
+ t "(",
+ i(3),
+ t ")",
+ i(0),
+ },
+ efi = {
+ i(1, { "val" }),
+ ", ",
+ i(2, { "err" }),
+ " := ",
+ i(3, { "f" }),
+ "(",
+ i(4),
+ ")",
+ t { "", "if " },
+ rep(2),
+ t { " != nil {", "\treturn " },
+ d(5, go_ret_vals, { 2, 3 }),
+ t { "", "}" },
+ i(0),
+ },
+}
+
+M.f = fmt("func {}({}) {} {{\n\t{}\n}}", { i(1, "name"), i(2), i(3), i(0) })
+
+return M
diff --git a/lua/pi/snips/ft/lua.lua b/lua/pi/snips/ft/lua.lua
new file mode 100644
index 0000000..d896f3e
--- /dev/null
+++ b/lua/pi/snips/ft/lua.lua
@@ -0,0 +1,69 @@
+local ls = require "luasnip"
+
+-- local snippet = ls.s
+local snippet_from_nodes = ls.sn
+local c = ls.choice_node
+local d = ls.dynamic_node
+local i = ls.insert_node
+local f = ls.function_node
+local t = ls.text_node
+
+local fmt = require("luasnip.extras.fmt").fmt
+
+local require_var = function(args, _)
+ local text = args[1][1] or ""
+ local split = vim.split(text, ".", { plain = true })
+
+ local options = {}
+ for len = 0, #split - 1 do
+ table.insert(options, t(table.concat(vim.list_slice(split, #split - len, #split), "_")))
+ end
+
+ return snippet_from_nodes(nil, {
+ c(1, options),
+ })
+end
+
+local newline = function(text)
+ return t { "", text }
+end
+
+return {
+ req = fmt([[local {} = require("{}")]], {
+ d(2, require_var, { 1 }),
+ i(1),
+ }),
+ lf = {
+ desc = "table function",
+ "local ",
+ i(1),
+ " = function(",
+ i(2),
+ ")",
+ newline " ",
+ i(0),
+ newline "end",
+ },
+
+ preq = fmt(
+ [[local ok, {} = pcall(require, "{}")
+if not ok then
+ return
+end]],
+ {
+ f(function(args)
+ return args[1][1]
+ end, { 1 }),
+ i(1),
+ }
+ ),
+
+ plugin = fmt(
+ [[return {{
+ "{}"
+}}]],
+ {
+ i(1, "plugin"),
+ }
+ ),
+}
diff --git a/lua/pi/snips/ft/php.lua b/lua/pi/snips/ft/php.lua
new file mode 100644
index 0000000..72936d8
--- /dev/null
+++ b/lua/pi/snips/ft/php.lua
@@ -0,0 +1,163 @@
+local ls = require "luasnip"
+local composer = require "composer"
+
+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
+
+local visibility = function(position, default)
+ local possibles = { "private", "protected", "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 var_name = function(args)
+ return snippet_from_nodes(nil, {
+ i(1, args[1][1]:gsub("^%u", string.lower):gsub("Interface", "") or ""),
+ })
+end
+
+local class_name = function()
+ return vim.fn.expand "%:t:r"
+end
+
+local M = {}
+
+M.v = fmt(
+ [[
+/**
+ * @var {}
+ */
+{} {} ${};
+]],
+ {
+ i(1, "type"),
+ visibility(2, "private"),
+ i(3, "Type"),
+ i(4, "var"),
+ }
+)
+
+M.class = fmt(
+ [[
+<?php
+
+declare(strict_types=1);
+
+namespace {};
+
+class {}
+{{
+ {}
+}}
+]],
+ {
+ f(composer.resolve_php_namespace),
+ f(class_name),
+ i(0),
+ }
+)
+
+M.pro = fmt([[{} {}{} ${},]], {
+ visibility(1, "private"),
+ c(2, {
+ t "readonly ",
+ t "",
+ }),
+ i(3, "Type"),
+ d(4, var_name, { 3 }),
+})
+
+M.op = fmt(
+ [[
+ /**
+ * @var ObjectProphecy<{}>
+ */
+ private ObjectProphecy ${};
+]],
+ {
+ i(1, "Type"),
+ d(2, var_name, { 1 }),
+ }
+)
+
+M.iop = fmt(
+ [[$this->{} = $this->prophesize({}::class);]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+)
+
+M.arg = fmt([[{} ${}]], {
+ i(1, "Type"),
+ d(2, var_name, { 1 }),
+})
+
+M._c = fmt(
+ [[{} function __construct({}) {{
+ {}
+}}]],
+ {
+ visibility(1, "public"),
+ i(2),
+ i(0),
+ }
+)
+
+M._p = fmt(
+ [[{} function __construct(
+ {}
+) {{
+}}]],
+ {
+ visibility(1, "public"),
+ i(2),
+ }
+)
+
+M.strict = t "declare(strict_types=1);"
+
+M["fn"] = fmt(
+ [[{} function {}({}): {}
+{{
+ {}
+}}]],
+ { visibility(1, "public"), i(2), i(3), i(4, "void"), i(0) }
+)
+
+M["then"] = fmt(
+ [[->then(
+ function ({}) {{
+ {}
+ }}
+)]],
+ { i(1, ""), i(2, "") }
+)
+
+M.test = fmt(
+ [[/**
+ * @test
+ */
+public function it_{}(): void
+{{
+ {}
+}}
+]],
+ { i(1, ""), i(0, "") }
+)
+
+return M
diff --git a/lua/pi/snips/ft/yaml.lua b/lua/pi/snips/ft/yaml.lua
new file mode 100644
index 0000000..3924c6f
--- /dev/null
+++ b/lua/pi/snips/ft/yaml.lua
@@ -0,0 +1,225 @@
+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
+local rep = require("luasnip.extras").rep
+
+local M = {
+ str = fmt(
+ [[{}:
+ type: string
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ bool = fmt(
+ [[{}:
+ type: boolean
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ float = fmt(
+ [[{}:
+ type: number
+ format: float
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ int = fmt(
+ [[{}:
+ type: integer
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ id = fmt(
+ [[{}:
+ type: integer
+ format: int64
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ class = fmt(
+ [[{}:
+ $ref: '#/components/schemas/{}'
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ i(3, ""),
+ }
+ ),
+ date = fmt(
+ [[{}:
+ type: string
+ format: date-time
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ arr = fmt(
+ [[{}:
+ type: array
+ items:
+ {}
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ i(3, ""),
+ }
+ ),
+ ref = fmt([[$ref: '#/components/schemas/{}']], { i(1, "") }),
+ obj = fmt(
+ [[type: object
+properties:
+ {}]],
+ i(1, "")
+ ),
+ aint = fmt(
+ [[{}:
+ type: array
+ items:
+ type: int
+ format: int64
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ astr = fmt(
+ [[{}:
+ type: array
+ items:
+ type: string
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+
+ mut = fmt(
+ [[{}:
+ type: "{}"
+ resolve: '@=mutation("App\\GraphQL\\Mutation\\{}::{}", {}, info)'
+ args:
+ {}
+]],
+ {
+ i(1, ""),
+ i(2, ""),
+ i(3, ""),
+ rep(1),
+ i(5, ""),
+ i(6, ""),
+ }
+ ),
+
+ input = fmt(
+ [[{}:
+ type: input-object
+ config:
+ fields:
+ {}
+]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ f = fmt([[{}: {{ type: "{}" }}]], { i(1, ""), i(2, "") }),
+
+ api = fmt(
+ [[openapi: 3.0.2
+info:
+ title: {}
+ contact:
+ name: eva-team-be
+ version: {}
+x-bundle-apis: true
+tags:
+ - name: {}
+ description: {}
+]],
+ { i(1, "my app"), i(2, "1.0.0"), i(3, "tag"), i(4, "tag description") }
+ ),
+ paths = fmt(
+ [[paths:
+ {}
+]],
+ { i(0, "") }
+ ),
+ param = fmt(
+ [[parameters:
+ - name: {}
+ description: {}
+ in: path
+ required: true
+ schema:
+ type: integer
+ format: int64
+]],
+ { i(1, ""), i(2, "") }
+ ),
+ get = fmt(
+ [[get:
+ operationId: {}
+ description: {}
+ tags: [ {} ]
+ responses:
+ 200:
+ description: Response
+ content:
+ application/json:
+ schema:
+ {}
+ ]],
+ { i(1, "GetOperation"), i(2, "description"), i(3, "tag"), i(4, "") }
+ ),
+
+ put = fmt(
+ [[put:
+ operationId: {}
+ tags: [ {} ]
+ description: {}
+ requestBody:
+ content:
+ application/json:
+ schema:
+ {}
+ required: true
+ responses:
+ 200:
+ description: {}
+ content:
+ application/json:
+ schema:
+ {}
+ ]],
+ { i(1, "PutOperation"), i(2, "tag"), i(3, "description"), i(4, ""), i(5, "description"), i(6, "") }
+ ),
+}
+
+return M