aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha/snips
diff options
context:
space:
mode:
authorAriel D'Alessandro <dalessandro.ariel@gmail.com>2023-01-31 09:44:13 +0100
committerAriel D'Alessandro <dalessandro.ariel@gmail.com>2023-01-31 09:44:13 +0100
commit669561e21073e6efafddda25ba3ace69fb43819e (patch)
treef94ebd60ad4cab480b5cbcb7e65dd2060696c339 /lua/alpha/snips
parent5044c70e9f62cdd06ee91e754423b07c1c18c12d (diff)
downloadnvim-conf-669561e21073e6efafddda25ba3ace69fb43819e.tar.gz
nvim-conf-669561e21073e6efafddda25ba3ace69fb43819e.zip
feat: apply stylua
Diffstat (limited to 'lua/alpha/snips')
-rw-r--r--lua/alpha/snips/ft/cucumber.lua7
-rw-r--r--lua/alpha/snips/ft/go.lua190
-rw-r--r--lua/alpha/snips/ft/lua.lua82
-rw-r--r--lua/alpha/snips/ft/php.lua198
-rw-r--r--lua/alpha/snips/ft/yaml.lua258
5 files changed, 372 insertions, 363 deletions
diff --git a/lua/alpha/snips/ft/cucumber.lua b/lua/alpha/snips/ft/cucumber.lua
index b6ea422..5c183b2 100644
--- a/lua/alpha/snips/ft/cucumber.lua
+++ b/lua/alpha/snips/ft/cucumber.lua
@@ -1,10 +1,9 @@
-
-local ls = require("luasnip")
+local ls = require "luasnip"
local t = ls.text_node
local M = {
- clean = t([[Given clean stubs list]]),
- all = t([[And all stubs should be matched]]),
+ clean = t [[Given clean stubs list]],
+ all = t [[And all stubs should be matched]],
}
return M
diff --git a/lua/alpha/snips/ft/go.lua b/lua/alpha/snips/ft/go.lua
index 6038d52..58c6bfc 100644
--- a/lua/alpha/snips/ft/go.lua
+++ b/lua/alpha/snips/ft/go.lua
@@ -1,4 +1,4 @@
-local ls = require("luasnip")
+local ls = require "luasnip"
local snippet_from_nodes = ls.sn
local c = ls.choice_node
@@ -8,15 +8,15 @@ 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 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.set_query(
- "go",
- "LuaSnip_Result",
- [[
+ "go",
+ "LuaSnip_Result",
+ [[
[
(method_declaration result: (_) @id)
(function_declaration result: (_) @id)
@@ -26,107 +26,107 @@ vim.treesitter.set_query(
)
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)
+ 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,
+ ["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.get_query("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
+ 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.get_query("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))
+ 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),
- },
+ 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) })
diff --git a/lua/alpha/snips/ft/lua.lua b/lua/alpha/snips/ft/lua.lua
index 9f6b6fa..d896f3e 100644
--- a/lua/alpha/snips/ft/lua.lua
+++ b/lua/alpha/snips/ft/lua.lua
@@ -1,4 +1,4 @@
-local ls = require("luasnip")
+local ls = require "luasnip"
-- local snippet = ls.s
local snippet_from_nodes = ls.sn
@@ -11,57 +11,59 @@ 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 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
+ 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),
- })
+ return snippet_from_nodes(nil, {
+ c(1, options),
+ })
end
local newline = function(text)
- return t({ "", 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"),
- },
+ 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, "{}")
+ preq = fmt(
+ [[local ok, {} = pcall(require, "{}")
if not ok then
return
end]],
- {
- f(function(args)
- return args[1][1]
- end, { 1 }),
- i(1)
- }
- ),
+ {
+ f(function(args)
+ return args[1][1]
+ end, { 1 }),
+ i(1),
+ }
+ ),
- plugin = fmt(
-[[return {{
+ plugin = fmt(
+ [[return {{
"{}"
-}}]], {
- i(1, "plugin")
- }),
+}}]],
+ {
+ i(1, "plugin"),
+ }
+ ),
}
diff --git a/lua/alpha/snips/ft/php.lua b/lua/alpha/snips/ft/php.lua
index d70db2e..abfc6f7 100644
--- a/lua/alpha/snips/ft/php.lua
+++ b/lua/alpha/snips/ft/php.lua
@@ -1,5 +1,5 @@
-local ls = require("luasnip")
-local composer = require("composer")
+local ls = require "luasnip"
+local composer = require "composer"
local snippet_from_nodes = ls.sn
local c = ls.choice_node
@@ -10,72 +10,72 @@ 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
+ return string.sub(String, 1, string.len(Start)) == Start
end
local newline = function(text)
- return t({ "", text })
+ return t { "", text }
end
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)
+ 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 namespace = function()
- local dir = vim.fn.expand("%:h")
- local autoloads = 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("/", "\\")
- if dir == "" then
- return globalNamespace
- end
-
- return string.format("%s\\%s", globalNamespace, dir)
+ local dir = vim.fn.expand "%:h"
+ local autoloads = 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("/", "\\")
+ if dir == "" then
+ return globalNamespace
+ end
+
+ return string.format("%s\\%s", globalNamespace, dir)
end
local class_name = function()
- return vim.fn.expand("%:t:r")
+ return vim.fn.expand "%:t:r"
end
local M = {}
M.v = fmt(
- [[
+ [[
/**
* @var {}
*/
{} ${};
]],
- {
- i(1, "type"),
- visibility(2, "private"),
- i(3, "var"),
- }
+ {
+ i(1, "type"),
+ visibility(2, "private"),
+ i(3, "var"),
+ }
)
M.class = fmt(
- [[
+ [[
<?php
declare(strict_types=1);
@@ -87,89 +87,89 @@ class {}
{}
}}
]],
- {
- f(namespace),
- f(class_name),
- i(0),
- }
+ {
+ f(namespace),
+ f(class_name),
+ i(0),
+ }
)
M.pro = fmt([[{} {}{} ${},]], {
- visibility(1, "private"),
- c(2, {
- t(""),
- t("readonly "),
- }),
- i(3, "Type"),
- d(4, function(args)
- return snippet_from_nodes(nil, {
- i(1, args[1][1]:gsub("^%u", string.lower) or ""),
- })
- end, { 3 }),
+ visibility(1, "private"),
+ c(2, {
+ t "",
+ t "readonly ",
+ }),
+ i(3, "Type"),
+ d(4, function(args)
+ return snippet_from_nodes(nil, {
+ i(1, args[1][1]:gsub("^%u", string.lower) or ""),
+ })
+ end, { 3 }),
})
M.arg = fmt([[{} ${}]], {
- i(1, "Type"),
- d(2, function(args)
- return snippet_from_nodes(nil, {
- i(1, args[1][1]:gsub("^%u", string.lower) or ""),
- })
- end, { 1 }),
+ i(1, "Type"),
+ d(2, function(args)
+ return snippet_from_nodes(nil, {
+ i(1, args[1][1]:gsub("^%u", string.lower) or ""),
+ })
+ end, { 1 }),
})
M._c = fmt(
- [[{} function __construct({}) {{
+ [[{} function __construct({}) {{
{}
}}]],
- {
- visibility(1, "public"),
- i(2),
- i(0),
- }
+ {
+ visibility(1, "public"),
+ i(2),
+ i(0),
+ }
)
M._p = fmt(
- [[{} function __construct(
+ [[{} function __construct(
{}
) {{
}}]],
- {
- visibility(1, "public"),
- i(2),
- }
+ {
+ visibility(1, "public"),
+ i(2),
+ }
)
-M.strict = t("declare(strict_types=1);")
+M.strict = t "declare(strict_types=1);"
M.fn = {
- visibility(1, "public"),
- t(" function "),
- i(2, "name"),
- t("("),
- i(3),
- t(")"),
- c(4, {
- t(""),
- snippet_from_nodes(nil, {
- t(": "),
- i(1, "void"),
- }),
- }),
- newline("{"),
- newline("\t"),
- i(0, ""),
- newline("}"),
+ visibility(1, "public"),
+ t " function ",
+ i(2, "name"),
+ t "(",
+ i(3),
+ t ")",
+ c(4, {
+ t "",
+ snippet_from_nodes(nil, {
+ t ": ",
+ i(1, "void"),
+ }),
+ }),
+ newline "{",
+ newline "\t",
+ i(0, ""),
+ newline "}",
}
M["then"] = fmt(
- [[->then(function ({}) {{
+ [[->then(function ({}) {{
{}
}})]],
- { i(1, ""), i(2, "") }
+ { i(1, ""), i(2, "") }
)
M.test = fmt(
- [[/**
+ [[/**
* @test
*/
public function it_{}(): void
@@ -177,7 +177,7 @@ public function it_{}(): void
{}
}}
]],
- { i(1, ""), i(0, "") }
+ { i(1, ""), i(0, "") }
)
return M
diff --git a/lua/alpha/snips/ft/yaml.lua b/lua/alpha/snips/ft/yaml.lua
index 796512e..3924c6f 100644
--- a/lua/alpha/snips/ft/yaml.lua
+++ b/lua/alpha/snips/ft/yaml.lua
@@ -1,4 +1,4 @@
-local ls = require("luasnip")
+local ls = require "luasnip"
local snippet_from_nodes = ls.sn
local c = ls.choice_node
@@ -10,149 +10,149 @@ local fmt = require("luasnip.extras.fmt").fmt
local rep = require("luasnip.extras").rep
local M = {
- str = fmt(
- [[{}:
+ str = fmt(
+ [[{}:
type: string
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
- bool = fmt(
- [[{}:
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ bool = fmt(
+ [[{}:
type: boolean
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
- float = fmt(
- [[{}:
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ float = fmt(
+ [[{}:
type: number
format: float
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
- int = fmt(
- [[{}:
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ int = fmt(
+ [[{}:
type: integer
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
- id = fmt(
- [[{}:
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ id = fmt(
+ [[{}:
type: integer
format: int64
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
- class = fmt(
- [[{}:
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ class = fmt(
+ [[{}:
$ref: '#/components/schemas/{}'
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- i(3, ""),
- }
- ),
- date = fmt(
- [[{}:
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ i(3, ""),
+ }
+ ),
+ date = fmt(
+ [[{}:
type: string
format: date-time
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
- arr = fmt(
- [[{}:
+{}]],
+ {
+ 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
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ i(3, ""),
+ }
+ ),
+ ref = fmt([[$ref: '#/components/schemas/{}']], { i(1, "") }),
+ obj = fmt(
+ [[type: object
properties:
- {}]] ,
- i(1, "")
- ),
- aint = fmt(
- [[{}:
+ {}]],
+ i(1, "")
+ ),
+ aint = fmt(
+ [[{}:
type: array
items:
type: int
format: int64
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
- astr = fmt(
- [[{}:
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ astr = fmt(
+ [[{}:
type: array
items:
type: string
-{}]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
+{}]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
- mut = fmt(
- [[{}:
+ mut = fmt(
+ [[{}:
type: "{}"
resolve: '@=mutation("App\\GraphQL\\Mutation\\{}::{}", {}, info)'
args:
{}
-]] ,
- {
- i(1, ""),
- i(2, ""),
- i(3, ""),
- rep(1),
- i(5, ""),
- i(6, ""),
- }
- ),
+]],
+ {
+ i(1, ""),
+ i(2, ""),
+ i(3, ""),
+ rep(1),
+ i(5, ""),
+ i(6, ""),
+ }
+ ),
- input = fmt(
- [[{}:
+ input = fmt(
+ [[{}:
type: input-object
config:
fields:
{}
-]] ,
- {
- i(1, ""),
- i(2, ""),
- }
- ),
- f = fmt([[{}: {{ type: "{}" }}]], { i(1, ""), i(2, "") }),
+]],
+ {
+ i(1, ""),
+ i(2, ""),
+ }
+ ),
+ f = fmt([[{}: {{ type: "{}" }}]], { i(1, ""), i(2, "") }),
- api = fmt(
- [[openapi: 3.0.2
+ api = fmt(
+ [[openapi: 3.0.2
info:
title: {}
contact:
@@ -162,15 +162,17 @@ 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(1, "my app"), i(2, "1.0.0"), i(3, "tag"), i(4, "tag description") }
+ ),
+ paths = fmt(
+ [[paths:
{}
]],
- {i(0, "")}
- ),
- param = fmt([[parameters:
+ { i(0, "") }
+ ),
+ param = fmt(
+ [[parameters:
- name: {}
description: {}
in: path
@@ -179,9 +181,10 @@ tags:
type: integer
format: int64
]],
- {i(1, ""), i(2, "")}
- ),
- get = fmt([[get:
+ { i(1, ""), i(2, "") }
+ ),
+ get = fmt(
+ [[get:
operationId: {}
description: {}
tags: [ {} ]
@@ -192,9 +195,12 @@ tags:
application/json:
schema:
{}
- ]], {i(1, "GetOperation"), i(2, "description"), i(3, "tag"), i(4, "")}),
+ ]],
+ { i(1, "GetOperation"), i(2, "description"), i(3, "tag"), i(4, "") }
+ ),
- put = fmt([[put:
+ put = fmt(
+ [[put:
operationId: {}
tags: [ {} ]
description: {}
@@ -211,7 +217,9 @@ tags:
application/json:
schema:
{}
- ]], {i(1, "PutOperation"), i(2, "tag"), i(3, "description"), i(4, ""), i(5, "description"), i(6, "")})
+ ]],
+ { i(1, "PutOperation"), i(2, "tag"), i(3, "description"), i(4, ""), i(5, "description"), i(6, "") }
+ ),
}
return M