Explorar el Código

feat(luasnip): setup tables snip

opalmay hace 2 años
padre
commit
845522742d
Se han modificado 3 ficheros con 77 adiciones y 14 borrados
  1. 35 0
      lua/lvim/core/luasnip.lua
  2. 1 14
      lua/lvim/plugins.lua
  3. 41 0
      lua/lvim/utils.lua

+ 35 - 0
lua/lvim/core/luasnip.lua

@@ -0,0 +1,35 @@
+local M = {}
+local utils = require "lvim.utils"
+local paths = {}
+
+function M.gen_setup_snips()
+  local ls = require "luasnip"
+  local lua_snippets = {
+    -- ls.parser.parse_snippet("cmp", utils.r_inspect_settings(lvim.builtin.cmp, "lvim.builtin.cmp", 10000, ".")),
+    -- ls.parser.parse_snippet("lir", utils.r_inspect_settings(lvim.builtin.lir, "lvim.builtin.lir", 10000, ".")),
+  }
+  for i, v in pairs(lvim.builtin) do
+    lua_snippets[#lua_snippets + 1] =
+      ls.parser.parse_snippet("lvim.builtin." .. i, utils.r_inspect_settings(v, "lvim.builtin." .. i, 10000, "."))
+  end
+  ls.add_snippets("lua", lua_snippets)
+end
+
+function M.setup()
+  if lvim.builtin.luasnip.sources.friendly_snippets then
+    paths[#paths + 1] = utils.join_paths(get_runtime_dir(), "site", "pack", "lazy", "opt", "friendly-snippets")
+  end
+  local user_snippets = utils.join_paths(get_config_dir(), "snippets")
+  if utils.is_directory(user_snippets) then
+    paths[#paths + 1] = user_snippets
+  end
+  require("luasnip.loaders.from_lua").lazy_load()
+  require("luasnip.loaders.from_vscode").lazy_load {
+    paths = paths,
+  }
+  require("luasnip.loaders.from_snipmate").lazy_load()
+
+  M.gen_setup_snips()
+end
+
+return M

+ 1 - 14
lua/lvim/plugins.lua

@@ -67,20 +67,7 @@ local core_plugins = {
   {
     "L3MON4D3/LuaSnip",
     config = function()
-      local utils = require "lvim.utils"
-      local paths = {}
-      if lvim.builtin.luasnip.sources.friendly_snippets then
-        paths[#paths + 1] = utils.join_paths(get_runtime_dir(), "site", "pack", "lazy", "opt", "friendly-snippets")
-      end
-      local user_snippets = utils.join_paths(get_config_dir(), "snippets")
-      if utils.is_directory(user_snippets) then
-        paths[#paths + 1] = user_snippets
-      end
-      require("luasnip.loaders.from_lua").lazy_load()
-      require("luasnip.loaders.from_vscode").lazy_load {
-        paths = paths,
-      }
-      require("luasnip.loaders.from_snipmate").lazy_load()
+      require("lvim.core.luasnip").setup()
     end,
     event = "InsertEnter",
     dependencies = {

+ 41 - 0
lua/lvim/utils.lua

@@ -1,6 +1,47 @@
 local M = {}
 local uv = vim.loop
 
+function M.r_inspect_settings(structure, base_structure_str, limit, separator)
+  limit = limit or 100 -- default item limit
+  separator = separator or "." -- indent string
+  local output = ""
+  if limit < 1 then
+    return "ERROR: Item limit reached."
+  end
+  if structure == nil then
+    output = output .. "-- O" .. separator:sub(2) .. " = nil\n"
+    return output
+  end
+  local ts = type(structure)
+
+  if ts == "table" then
+    for k, v in pairs(structure) do
+      -- replace non alpha keys with ["key"]
+      if tostring(k):match "[^%a_]" then
+        k = '["' .. tostring(k) .. '"]'
+      end
+      output = output .. M.r_inspect_settings(v, base_structure_str, limit, separator .. "." .. tostring(k))
+      if limit < 0 then
+        break
+      end
+    end
+    return output
+  end
+
+  if ts == "string" then
+    -- escape sequences
+    structure = string.format("%q", structure)
+  end
+  separator = separator:gsub("%.%[", "%[")
+  if type(structure) == "function" then
+    -- don't print functions
+    output = output .. "-- " .. base_structure_str .. separator:sub(2) .. " = function ()\n"
+  else
+    output = output .. base_structure_str .. separator:sub(2) .. " = " .. tostring(structure) .. "\n"
+  end
+  return output
+end
+
 -- recursive Print (structure, limit, separator)
 local function r_inspect_settings(structure, limit, separator)
   limit = limit or 100 -- default item limit