summaryrefslogtreecommitdiff
path: root/lua/lvim
diff options
context:
space:
mode:
Diffstat (limited to 'lua/lvim')
-rw-r--r--lua/lvim/core/cmp.lua100
-rw-r--r--lua/lvim/core/luasnip.lua163
-rw-r--r--lua/lvim/core/luasnip/init.lua59
-rw-r--r--lua/lvim/core/luasnip/snippets.lua34
4 files changed, 167 insertions, 189 deletions
diff --git a/lua/lvim/core/cmp.lua b/lua/lvim/core/cmp.lua
index c116f122..bfbd4942 100644
--- a/lua/lvim/core/cmp.lua
+++ b/lua/lvim/core/cmp.lua
@@ -1,4 +1,5 @@
local M = {}
+
M.methods = {}
local has_words_before = function()
@@ -22,100 +23,7 @@ end
M.methods.feedkeys = feedkeys
----when inside a snippet, seeks to the nearest luasnip field if possible, and checks if it is jumpable
----@param dir number 1 for forward, -1 for backward; defaults to 1
----@return boolean true if a jumpable luasnip field is found while inside a snippet
-local function jumpable(dir)
- local luasnip_ok, luasnip = pcall(require, "luasnip")
- if not luasnip_ok then
- return false
- end
-
- local win_get_cursor = vim.api.nvim_win_get_cursor
- local get_current_buf = vim.api.nvim_get_current_buf
-
- ---sets the current buffer's luasnip to the one nearest the cursor
- ---@return boolean true if a node is found, false otherwise
- local function seek_luasnip_cursor_node()
- -- TODO(kylo252): upstream this
- -- for outdated versions of luasnip
- if not luasnip.session.current_nodes then
- return false
- end
-
- local node = luasnip.session.current_nodes[get_current_buf()]
- if not node then
- return false
- end
-
- local snippet = node.parent.snippet
- local exit_node = snippet.insert_nodes[0]
-
- local pos = win_get_cursor(0)
- pos[1] = pos[1] - 1
-
- -- exit early if we're past the exit node
- if exit_node then
- local exit_pos_end = exit_node.mark:pos_end()
- if (pos[1] > exit_pos_end[1]) or (pos[1] == exit_pos_end[1] and pos[2] > exit_pos_end[2]) then
- snippet:remove_from_jumplist()
- luasnip.session.current_nodes[get_current_buf()] = nil
-
- return false
- end
- end
-
- node = snippet.inner_first:jump_into(1, true)
- while node ~= nil and node.next ~= nil and node ~= snippet do
- local n_next = node.next
- local next_pos = n_next and n_next.mark:pos_begin()
- local candidate = n_next ~= snippet and next_pos and (pos[1] < next_pos[1])
- or (pos[1] == next_pos[1] and pos[2] < next_pos[2])
-
- -- Past unmarked exit node, exit early
- if n_next == nil or n_next == snippet.next then
- snippet:remove_from_jumplist()
- luasnip.session.current_nodes[get_current_buf()] = nil
-
- return false
- end
-
- if candidate then
- luasnip.session.current_nodes[get_current_buf()] = node
- return true
- end
-
- local ok
- ok, node = pcall(node.jump_from, node, 1, true) -- no_move until last stop
- if not ok then
- snippet:remove_from_jumplist()
- luasnip.session.current_nodes[get_current_buf()] = nil
-
- return false
- end
- end
-
- -- No candidate, but have an exit node
- if exit_node then
- -- to jump to the exit node, seek to snippet
- luasnip.session.current_nodes[get_current_buf()] = snippet
- return true
- end
-
- -- No exit node, exit from snippet
- snippet:remove_from_jumplist()
- luasnip.session.current_nodes[get_current_buf()] = nil
- return false
- end
-
- if dir == -1 then
- return luasnip.in_snippet() and luasnip.jumpable(-1)
- else
- return luasnip.in_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable(1)
- end
-end
-
-M.methods.jumpable = jumpable
+M.methods.jumpable = require("lvim.core.luasnip").methods.jumpable
M.config = function()
local status_cmp_ok, cmp_types = pcall(require, "cmp.types.cmp")
@@ -302,9 +210,9 @@ M.config = function()
cmp.select_next_item()
elseif luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
- elseif jumpable(1) then
+ elseif M.methods.jumpable(1) then
luasnip.jump(1)
- elseif has_words_before() then
+ elseif M.methods.has_words_before() then
-- cmp.complete()
fallback()
else
diff --git a/lua/lvim/core/luasnip.lua b/lua/lvim/core/luasnip.lua
new file mode 100644
index 00000000..10782d0e
--- /dev/null
+++ b/lua/lvim/core/luasnip.lua
@@ -0,0 +1,163 @@
+local M = {}
+
+-- see luasnip/util/types.lua
+local types = {
+ textNode = 1,
+ insertNode = 2,
+ functionNode = 3,
+ snippetNode = 4,
+ choiceNode = 5,
+ dynamicNode = 6,
+ snippet = 7,
+ exitNode = 8,
+ restoreNode = 9,
+ node_types = { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
+}
+
+function M.config()
+ lvim.builtin.luasnip = {
+ sources = {
+ friendly_snippets = true,
+ },
+ config = {
+ history = false,
+ updateevents = "TextChanged,TextChangedI",
+ ext_opts = {
+ -- Will be populated within config function
+ },
+ },
+ ext_opts = {
+ -- Show virtual text to signal when you are inside an sippets
+ [types.insertNode] = {
+ active = {
+ virt_text = { { "<-- snip insert", "BufferInactiveIndex" } },
+ },
+ },
+ -- Helps to notice when you are within a choice node
+ [types.choiceNode] = {
+ active = {
+ virt_text = { { "<-- choice", "BufferInactiveIndex" } },
+ },
+ },
+ },
+ }
+end
+
+function M.setup()
+ 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", "packer", "start", "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
+
+ local luasnip = require "luasnip"
+ luasnip.config.set_config(lvim.builtin.luasnip.config)
+
+ -- When no paths are provided, luasnip will search in the runtimepath
+ require("luasnip.loaders.from_lua").lazy_load()
+ require("luasnip.loaders.from_vscode").lazy_load { paths = paths }
+ require("luasnip.loaders.from_snipmate").lazy_load()
+end
+
+---when inside a snippet, seeks to the nearest luasnip field if possible, and checks if it is jumpable
+---@param dir number 1 for forward, -1 for backward; defaults to 1
+---@return boolean true if a jumpable luasnip field is found while inside a snippet
+local function jumpable(dir)
+ local luasnip_ok, luasnip = pcall(require, "luasnip")
+ if not luasnip_ok then
+ return false
+ end
+
+ local win_get_cursor = vim.api.nvim_win_get_cursor
+ local get_current_buf = vim.api.nvim_get_current_buf
+
+ ---sets the current buffer's luasnip to the one nearest the cursor
+ ---@return boolean true if a node is found, false otherwise
+ local function seek_luasnip_cursor_node()
+ -- TODO(kylo252): upstream this
+ -- for outdated versions of luasnip
+ if not luasnip.session.current_nodes then
+ return false
+ end
+
+ local node = luasnip.session.current_nodes[get_current_buf()]
+ if not node then
+ return false
+ end
+
+ local snippet = node.parent.snippet
+ local exit_node = snippet.insert_nodes[0]
+
+ local pos = win_get_cursor(0)
+ pos[1] = pos[1] - 1
+
+ -- exit early if we're past the exit node
+ if exit_node then
+ local exit_pos_end = exit_node.mark:pos_end()
+ if (pos[1] > exit_pos_end[1]) or (pos[1] == exit_pos_end[1] and pos[2] > exit_pos_end[2]) then
+ snippet:remove_from_jumplist()
+ luasnip.session.current_nodes[get_current_buf()] = nil
+
+ return false
+ end
+ end
+
+ node = snippet.inner_first:jump_into(1, true)
+ while node ~= nil and node.next ~= nil and node ~= snippet do
+ local n_next = node.next
+ local next_pos = n_next and n_next.mark:pos_begin()
+ local candidate = n_next ~= snippet and next_pos and (pos[1] < next_pos[1])
+ or (pos[1] == next_pos[1] and pos[2] < next_pos[2])
+
+ -- Past unmarked exit node, exit early
+ if n_next == nil or n_next == snippet.next then
+ snippet:remove_from_jumplist()
+ luasnip.session.current_nodes[get_current_buf()] = nil
+
+ return false
+ end
+
+ if candidate then
+ luasnip.session.current_nodes[get_current_buf()] = node
+ return true
+ end
+
+ local ok
+ ok, node = pcall(node.jump_from, node, 1, true) -- no_move until last stop
+ if not ok then
+ snippet:remove_from_jumplist()
+ luasnip.session.current_nodes[get_current_buf()] = nil
+
+ return false
+ end
+ end
+
+ -- No candidate, but have an exit node
+ if exit_node then
+ -- to jump to the exit node, seek to snippet
+ luasnip.session.current_nodes[get_current_buf()] = snippet
+ return true
+ end
+
+ -- No exit node, exit from snippet
+ snippet:remove_from_jumplist()
+ luasnip.session.current_nodes[get_current_buf()] = nil
+ return false
+ end
+
+ if dir == -1 then
+ return luasnip.in_snippet() and luasnip.jumpable(-1) else
+ return luasnip.in_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable(1)
+ end
+end
+
+M.methods = {}
+
+M.methods.jumpable = jumpable
+
+return M
diff --git a/lua/lvim/core/luasnip/init.lua b/lua/lvim/core/luasnip/init.lua
deleted file mode 100644
index adf6a202..00000000
--- a/lua/lvim/core/luasnip/init.lua
+++ /dev/null
@@ -1,59 +0,0 @@
-local M = {}
-function M.config()
- lvim.builtin.luasnip = {
- sources = {
- friendly_snippets = true,
- lunarvim = true,
- },
- config = {
- history = false,
- updateevents = "TextChanged,TextChangedI",
- ext_opts = {
- -- Will be populated within config function
- },
- },
- }
-end
-
-function M.setup()
- 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", "packer", "start", "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
- -- When no paths are provided, luasnip will search in the runtimepath
- require("luasnip.loaders.from_lua").lazy_load()
- require("luasnip.loaders.from_vscode").lazy_load {
- paths = paths,
- }
- require("luasnip.loaders.from_snipmate").lazy_load()
-
- local luasnip = require "luasnip"
- local types = require "luasnip.util.types"
-
- local ext_opts = {
- -- Show virtual text to signal when you are inside an sippets
- [types.insertNode] = {
- active = {
- virt_text = { { "<-- snip insert", "BufferInactiveIndex" } },
- },
- },
- -- Helps to notice when you are within a choice node
- [types.choiceNode] = {
- active = {
- virt_text = { { "<-- choice", "BufferInactiveIndex" } },
- },
- },
- }
- -- Add lunarvim options giving preference to user ones
- lvim.builtin.luasnip.config.ext_opts = vim.tbl_deep_extend("keep", lvim.builtin.luasnip.config.ext_opts, ext_opts)
- luasnip.config.set_config(lvim.builtin.luasnip.config)
- if lvim.builtin.luasnip.sources.lunarvim then
- luasnip.add_snippets("lua", require "lvim.core.luasnip.snippets")
- end
-end
-return M
diff --git a/lua/lvim/core/luasnip/snippets.lua b/lua/lvim/core/luasnip/snippets.lua
deleted file mode 100644
index 3ef34afd..00000000
--- a/lua/lvim/core/luasnip/snippets.lua
+++ /dev/null
@@ -1,34 +0,0 @@
--- About how to create snippets:
--- https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md
--- uncoment nodes as you need them
--- local sn = ls.snippet_node
--- local isn = ls.indent_snippet_node
--- local t = ls.text_node
--- local f = ls.function_node
--- local c = ls.choice_node
--- local d = ls.dynamic_node
--- local r = ls.restore_node
--- local m = extras.m
--- local rep = extras.rep
--- local postfix = require("luasnip.extras.postfix").postfix
-local fmt = require("luasnip.extras.fmt").fmt
-local extras = require "luasnip.extras"
-local l = extras.l
-local dl = extras.dynamic_lambda
-local ls = require "luasnip"
-local s = ls.snippet
-local i = ls.insert_node
-
-return {
- s(
- { trig = "preq", dscr = "Protected require call" },
- fmt(
- [[
- local ok, {} = pcall(require,'{}')
- if not ok then
- return
- end]],
- { i(1), dl(2, "lvim.core." .. l._1, 1) }
- )
- ),
-}