mirror of
https://codeberg.org/leana8959/.files.git
synced 2025-12-06 06:39:14 +00:00
parent
dad88aa92f
commit
1bdcd3386e
13 changed files with 0 additions and 0 deletions
208
.config/nvim/plugin/cmp.lua
Normal file
208
.config/nvim/plugin/cmp.lua
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
local cmp = require("cmp")
|
||||
local ls = require("luasnip")
|
||||
|
||||
local s = ls.snippet
|
||||
local sn = ls.snippet_node
|
||||
local t = ls.text_node
|
||||
local cr = function() return t { "", "" } end -- linebreak
|
||||
local i = ls.insert_node
|
||||
local f = ls.function_node
|
||||
local c = ls.choice_node
|
||||
local d = ls.dynamic_node
|
||||
local r = ls.restore_node
|
||||
local l = require("luasnip.extras").lambda
|
||||
local rep = require("luasnip.extras").rep
|
||||
local p = require("luasnip.extras").partial
|
||||
local m = require("luasnip.extras").match
|
||||
local n = require("luasnip.extras").nonempty
|
||||
local dl = require("luasnip.extras").dynamic_lambda
|
||||
local fmt = require("luasnip.extras.fmt").fmt
|
||||
local fmta = require("luasnip.extras.fmt").fmta
|
||||
local types = require("luasnip.util.types")
|
||||
local conds = require("luasnip.extras.conditions")
|
||||
local conds_expand = require("luasnip.extras.conditions.expand")
|
||||
|
||||
local has_words_before = function()
|
||||
unpack = unpack or table.unpack
|
||||
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
|
||||
end
|
||||
|
||||
-----------------
|
||||
-- Lazy loader --
|
||||
-----------------
|
||||
require("luasnip.loaders.from_vscode").lazy_load { paths = { "./snippets" } }
|
||||
|
||||
----------
|
||||
-- Init --
|
||||
----------
|
||||
ls.setup { update_events = { "TextChanged", "TextChangedI" } }
|
||||
|
||||
-----------
|
||||
-- Typst --
|
||||
-----------
|
||||
local function show_date_typst_entry() return os.date('"%Y-%m-%d %H:%M:%S"') end
|
||||
ls.add_snippets("typst", {
|
||||
s("entry", {
|
||||
t("#entry("),
|
||||
f(show_date_typst_entry),
|
||||
t { ")[", "" },
|
||||
t(" "),
|
||||
i(0),
|
||||
t { "", "]" },
|
||||
}),
|
||||
s("lang", {
|
||||
t('#set text(lang: "'),
|
||||
i(0),
|
||||
t('")'),
|
||||
cr(),
|
||||
}),
|
||||
})
|
||||
|
||||
local function get_cms()
|
||||
assert(vim.bo.commentstring ~= "", "comment string is not set")
|
||||
local left = vim.bo.commentstring:gsub("%s*%%s.*", "")
|
||||
local right = vim.bo.commentstring:gsub(".*%%s%s*", "")
|
||||
if right == "" then
|
||||
right = left
|
||||
end
|
||||
return { left = left, right = right }
|
||||
end
|
||||
local function horizon(args)
|
||||
local cms = get_cms()
|
||||
local chr = cms.left:sub(-1)
|
||||
local len = vim.fn.strdisplaywidth(args[1][1])
|
||||
|
||||
local acc = cms.left
|
||||
for _ = cms.left:len(), len + cms.right:len() + 1, 1 do
|
||||
acc = acc .. chr
|
||||
end
|
||||
acc = acc .. cms.right
|
||||
|
||||
return acc
|
||||
end
|
||||
local function left()
|
||||
local cms = get_cms()
|
||||
return cms.left .. " "
|
||||
end
|
||||
local function right()
|
||||
local cms = get_cms()
|
||||
return " " .. cms.right
|
||||
end
|
||||
|
||||
-- stylua: ignore start
|
||||
ls.add_snippets("all", {
|
||||
s("banner", {
|
||||
f(horizon, { 1 }), cr(),
|
||||
f(left), i(1), f(right), cr(),
|
||||
f(horizon, { 1 }),
|
||||
}),
|
||||
})
|
||||
-- stylua: ignore end
|
||||
|
||||
-------------
|
||||
-- Haskell --
|
||||
-------------
|
||||
local haskell_snippets = require("haskell-snippets").all
|
||||
ls.add_snippets("haskell", haskell_snippets, { key = "haskell" })
|
||||
|
||||
------------
|
||||
-- Golang --
|
||||
------------
|
||||
-- stylua: ignore start
|
||||
ls.add_snippets("go", {
|
||||
s("ie", {
|
||||
t("if err != nil {"), cr(),
|
||||
t("\t"), i(0), cr(),
|
||||
t("}"),
|
||||
}),
|
||||
})
|
||||
-- stylua: ignore end
|
||||
|
||||
---------------
|
||||
-- Setup CMP --
|
||||
---------------
|
||||
local of_filetype = function(fts)
|
||||
local ft = vim.bo.filetype
|
||||
for _, v in ipairs(fts) do
|
||||
if v == ft then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
cmp.setup {
|
||||
snippet = {
|
||||
expand = function(args) ls.lsp_expand(args.body) end,
|
||||
},
|
||||
mapping = cmp.mapping.preset.insert {
|
||||
["<Tab>"] = cmp.mapping(function(fallback) -- Next or jump
|
||||
if cmp.visible() then
|
||||
cmp.select_next_item()
|
||||
elseif ls.expand_or_locally_jumpable() then
|
||||
ls.expand_or_jump()
|
||||
elseif has_words_before() and (not of_filetype { "ledger" }) then
|
||||
cmp.complete()
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, { "i", "s" }),
|
||||
["<S-Tab>"] = cmp.mapping(function(fallback) -- Previous
|
||||
if cmp.visible() then
|
||||
cmp.select_prev_item()
|
||||
elseif ls.jumpable(-1) then
|
||||
ls.jump(-1)
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, { "i", "s" }),
|
||||
["<A-Tab>"] = cmp.mapping(function(fallback) -- Force jump
|
||||
if ls.expand_or_locally_jumpable() then
|
||||
ls.expand_or_jump()
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, { "i", "s" }),
|
||||
["<CR>"] = cmp.mapping(function(fallback) -- Confirm
|
||||
if cmp.visible() then
|
||||
cmp.mapping.confirm {
|
||||
behavior = cmp.ConfirmBehavior.Insert,
|
||||
select = true,
|
||||
}()
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, { "i", "s" }),
|
||||
["<S-CR>"] = cmp.mapping(function(fallback) -- Confirm and replace
|
||||
if cmp.visible() then
|
||||
cmp.mapping.confirm {
|
||||
behavior = cmp.ConfirmBehavior.Replace,
|
||||
select = true,
|
||||
}()
|
||||
else
|
||||
fallback()
|
||||
end
|
||||
end, { "i", "s" }),
|
||||
},
|
||||
sources = {
|
||||
{ name = "luasnip" },
|
||||
{ name = "nvim_lsp" },
|
||||
{ name = "path" },
|
||||
{
|
||||
name = "buffer",
|
||||
keyword_length = 10,
|
||||
option = {
|
||||
enable_in_context = function() return of_filetype { "tex", "markdown", "typst" } end,
|
||||
},
|
||||
},
|
||||
{
|
||||
name = "spell",
|
||||
keyword_length = 10,
|
||||
option = {
|
||||
keep_all_entries = true,
|
||||
enable_in_context = function() return of_filetype { "tex", "markdown", "typst" } end,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue