diff options
| author | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2022-04-11 08:41:09 +0200 |
|---|---|---|
| committer | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2022-04-11 08:41:09 +0200 |
| commit | 5f6142f224ac128d62c4cb5c7d4acf45db19e5c2 (patch) | |
| tree | 3630b6d24bac761c3e0c2381112fb4ec63d83f2e /lua/alpha/snips/ft | |
| parent | 76c51d6b471250550afd0461b4ea4316a5b4f506 (diff) | |
| download | nvim-conf-5f6142f224ac128d62c4cb5c7d4acf45db19e5c2.tar.gz nvim-conf-5f6142f224ac128d62c4cb5c7d4acf45db19e5c2.zip | |
Apply stylelua and clean
Diffstat (limited to 'lua/alpha/snips/ft')
| -rw-r--r-- | lua/alpha/snips/ft/go.lua | 188 | ||||
| -rw-r--r-- | lua/alpha/snips/ft/php.lua | 197 |
2 files changed, 192 insertions, 193 deletions
diff --git a/lua/alpha/snips/ft/go.lua b/lua/alpha/snips/ft/go.lua index 1021721..6038d52 100644 --- a/lua/alpha/snips/ft/go.lua +++ b/lua/alpha/snips/ft/go.lua @@ -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('errors.Wrap(%s, "%s")', info.err_name, info.func_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/php.lua b/lua/alpha/snips/ft/php.lua index 40560b8..daf1e77 100644 --- a/lua/alpha/snips/ft/php.lua +++ b/lua/alpha/snips/ft/php.lua @@ -8,68 +8,69 @@ 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 +function string.starts(String, 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', 'public'} - local options = {} +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 + 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) + return c(position, options) end -local namespace = function () - local dir = vim.fn.getcwd() - 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 ) +local namespace = function() + local dir = vim.fn.getcwd() + 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 () - local filename = vim.fn.expand('%:t:r') - return filename +local class_name = function() + return vim.fn.expand("%:t:r") end + local M = { - v = fmt( -[[ + v = fmt( + [[ /** * @var {} */ {} ${}; -]], { - i(1, "type"), - d(2, visibility, {}, "private"), - i(3, "var") - }), - class = fmt( -[[ +]], + { + i(1, "type"), + d(2, visibility, {}, "private"), + i(3, "var"), + } + ), + class = fmt( + [[ <?php declare(strict_types=1); @@ -81,66 +82,64 @@ 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( + { + 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), + 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);", + { + 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 |