From b1b7a050da10fa0509153ba857fdcfca041e29da Mon Sep 17 00:00:00 2001 From: angryluck Date: Tue, 17 Sep 2024 16:35:13 +0200 Subject: [PATCH] =?UTF-8?q?=C3=86ndrede=20diverse=20ting,=20is=C3=A6r=20ti?= =?UTF-8?q?lf=C3=B8jede=20jeg=20en=20masse=20lua-snippets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- after/ftplugin/tex.lua | 8 ++- lua/angryluck/options.lua | 2 +- lua/angryluck/plugins/autocompletion.lua | 60 ++++++++++++++-- lua/angryluck/plugins/autopairs.lua | 4 +- lua/angryluck/plugins/conform.lua | 2 + lua/luasnip-helpers.lua | 62 ++++++++++++++++ luasnippets/all.lua | 14 ++++ luasnippets/lua.lua | 35 +++++++++ luasnippets/tex/commands.lua | 30 ++++++++ luasnippets/tex/environments.lua | 91 ++++++++++++++++++++++++ luasnippets/tex/math.lua | 82 +++++++++++++++++++++ luasnippets/tex/misc.lua | 37 ++++++++++ luasnippets/tex/templates.lua | 0 snippets/tex.snippets | 20 +++--- 14 files changed, 428 insertions(+), 19 deletions(-) create mode 100644 lua/luasnip-helpers.lua create mode 100644 luasnippets/all.lua create mode 100644 luasnippets/lua.lua create mode 100644 luasnippets/tex/commands.lua create mode 100644 luasnippets/tex/environments.lua create mode 100644 luasnippets/tex/math.lua create mode 100644 luasnippets/tex/misc.lua create mode 100644 luasnippets/tex/templates.lua diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua index b641eb1c..8b9cf840 100644 --- a/after/ftplugin/tex.lua +++ b/after/ftplugin/tex.lua @@ -1,8 +1,10 @@ -vim.opt.breakindentopt = "shift:2" -vim.opt.shiftwidth = 4 +-- vim.opt.breakindentopt = "shift:2" +-- Replacement for this: vim.opt.showbreak = "↪ " +vim.opt.shiftwidth = 2 +vim.opt.softtabstop = 2 +vim.opt.tabstop = 2 vim.opt.textwidth = 0 - vim.cmd([[ function! MyFormatExpr(start, end) silent execute a:start.','.a:end.'s/[.!?]\zs /\r/g' diff --git a/lua/angryluck/options.lua b/lua/angryluck/options.lua index e47715af..50759d86 100644 --- a/lua/angryluck/options.lua +++ b/lua/angryluck/options.lua @@ -33,7 +33,7 @@ vim.opt.signcolumn = "yes" vim.opt.updatetime = 250 -- For which-key -vim.opt.timeoutlen = 300 +vim.opt.timeoutlen = 500 vim.opt.list = true vim.opt.listchars = { tab = "» ", trail = "·", nbsp = "␣" } diff --git a/lua/angryluck/plugins/autocompletion.lua b/lua/angryluck/plugins/autocompletion.lua index 255190a1..d00f14d7 100644 --- a/lua/angryluck/plugins/autocompletion.lua +++ b/lua/angryluck/plugins/autocompletion.lua @@ -25,6 +25,10 @@ return { }, config = function() require("luasnip.loaders.from_snipmate").lazy_load() + require("luasnip.loaders.from_lua").lazy_load({ + -- If you set path manually, luasnip has to scan less + -- paths = "~/.config/nvim/snippets/", + }) end, }, "saadparwaiz1/cmp_luasnip", @@ -36,7 +40,17 @@ return { config = function() local cmp = require("cmp") local luasnip = require("luasnip") - luasnip.config.setup({ enable_autosnippets = true }) + + vim.keymap.set( + "n", + "L", + "lua require('luasnip.loaders.from_lua').load({paths = '~/.config/nvim/luasnippets/'})" + ) + luasnip.config.setup({ + enable_autosnippets = true, + store_selection_keys = "", + update_events = "TextChanged,TextChangedI", + }) cmp.setup({ snippet = { @@ -51,22 +65,60 @@ return { [""] = cmp.mapping.select_prev_item(), [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), - [""] = cmp.mapping.scroll_docs(-8), - [""] = cmp.mapping.scroll_docs(8), + -- [""] = cmp.mapping.scroll_docs(-8), + -- [""] = cmp.mapping.scroll_docs(8), -- Accept ([y]es) the completion [""] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + if luasnip.expandable() then + luasnip.expand() + else + cmp.confirm({ select = true }) + end + else + fallback() + end + end), -- Manually trigger a completion from nvim-cmp (normally not needed) [""] = cmp.mapping.complete({}), [""] = cmp.mapping(function() - if luasnip.expand_or_locally_jumpable() then + if luasnip.expand_or_jumpable() then luasnip.expand_or_jump() end end, { "i", "s" }), + [""] = cmp.mapping(function() + if luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + end + end, { "n", "i", "s" }), + [""] = cmp.mapping(function() + if luasnip.locally_jumpable(-1) then + luasnip.jump(-1) + end + end, { "n", "i", "s" }), [""] = cmp.mapping(function() if luasnip.locally_jumpable(-1) then luasnip.jump(-1) end end, { "i", "s" }), + -- Better completions, especially for latex + [""] = cmp.mapping(function(fallback) + if luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif luasnip.locally_jumpable(1) then + luasnip.jump(1) + else + fallback() + end + end, { "i", "s" }), + [""] = cmp.mapping(function(fallback) + if luasnip.locally_jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), }), sources = { { name = "nvim_lsp" }, diff --git a/lua/angryluck/plugins/autopairs.lua b/lua/angryluck/plugins/autopairs.lua index 33874a98..ce319691 100644 --- a/lua/angryluck/plugins/autopairs.lua +++ b/lua/angryluck/plugins/autopairs.lua @@ -1,5 +1,7 @@ return { "windwp/nvim-autopairs", event = "InsertEnter", - opts = {} + opts = { + disable_filetype = { "tex" }, + }, } diff --git a/lua/angryluck/plugins/conform.lua b/lua/angryluck/plugins/conform.lua index 071584b6..9f2606a9 100644 --- a/lua/angryluck/plugins/conform.lua +++ b/lua/angryluck/plugins/conform.lua @@ -21,6 +21,8 @@ return { -- Autoformat -- Conform can also run multiple formatters sequentially -- python = { "isort", "black" }, python = { "autopep8", "black" }, + -- latex = { "latexindent" }, + -- tex = { "latexindent" }, -- -- You can use a sub-list to tell conform to run *until* a formatter is found. -- javascript = { { "prettierd", "prettier" } }, diff --git a/lua/luasnip-helpers.lua b/lua/luasnip-helpers.lua new file mode 100644 index 00000000..448a14dd --- /dev/null +++ b/lua/luasnip-helpers.lua @@ -0,0 +1,62 @@ +local M = {} + +local ls = require("luasnip") +local s = ls.snippet + +local in_mathzone = function() + return vim.fn["vimtex#syntax#in_mathzone"]() == 1 +end + +local in_text = function() + return vim.fn["vimtex#syntax#in_mathzone"]() == 0 +end + +M.in_mathzone = in_mathzone +M.in_text = in_text + +-- "as" for "Auto snippet" +local function as(trigger, nodes, opts) + opts = opts or {} + -- Add snippetType = "autosnippet" to the first parameter + if type(trigger) == "table" then + trigger.snippetType = "autosnippet" + else + trigger = { trig = trigger, snippetType = "autosnippet" } + end + return s(trigger, nodes, opts) +end + +M.as = as + +-- "asm" for "Auto snippet math" +function M.asm(trigger, nodes, opts) + -- Only trigger commands in mathzones + opts = opts or {} + opts.condition = in_mathzone + return as(trigger, nodes, opts) +end +-- Add snippetType = "autosnippet" to the first parameter +-- if type(trigger) == "table" then +-- trigger.snippetType = "autosnippet" +-- else +-- trigger = { trig = trigger, snippetType = "autosnippet" } +-- end +-- return s(trigger, nodes, opts) +-- end + +-- "ast" for "Auto snippet text" +function M.ast(trigger, nodes, opts) + -- Only trigger commands in mathzones + opts = opts or {} + opts.condition = in_text + return as(trigger, nodes, opts) + -- -- Add snippetType = "autosnippet" to the first parameter + -- if type(trigger) == "table" then + -- trigger.snippetType = "autosnippet" + -- else + -- trigger = { trig = trigger, snippetType = "autosnippet" } + -- end + -- return s(trigger, nodes, opts) +end +-- +return M diff --git a/luasnippets/all.lua b/luasnippets/all.lua new file mode 100644 index 00000000..fc7549bb --- /dev/null +++ b/luasnippets/all.lua @@ -0,0 +1,14 @@ +-- Abbreviations used in this article and the LuaSnip docs +local ls = require("luasnip") +local s = ls.snippet +-- local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +-- local f = ls.function_node +-- local d = ls.dynamic_node +-- local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +-- local rep = require("luasnip.extras").rep +return { + s({ trig = "lolol" }, { t("LuaSnip test") }), +} diff --git a/luasnippets/lua.lua b/luasnippets/lua.lua new file mode 100644 index 00000000..a9878373 --- /dev/null +++ b/luasnippets/lua.lua @@ -0,0 +1,35 @@ +-- Abbreviations used in this article and the LuaSnip docs +local ls = require("luasnip") +local s = ls.snippet +-- local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +-- local f = ls.function_node +-- local d = ls.dynamic_node +-- local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +-- local rep = require("luasnip.extras").rep +return { + -- Snippet snippets + s( + { trig = "asm", descr = "Autosnippet in math environment" }, + fmta( + [[asm("<>", fmta("<>", { <> })),]], + { i(1, "trig"), i(2), i(3, "i(1)") } + ) + ), + s( + { trig = "as", descr = "Autosnippet (own defined function)" }, + fmta( + [[as("<>", fmta("<>", { <> })),]], + { i(1, "trig"), i(2), i(3, "i(1)") } + ) + ), + s( + { trig = "ast", descr = "Autosnippet in text mode" }, + fmta( + [[ast("<>", fmta("<>", { <> })),]], + { i(1, "trig"), i(2), i(3, "i(1)") } + ) + ), +} diff --git a/luasnippets/tex/commands.lua b/luasnippets/tex/commands.lua new file mode 100644 index 00000000..6f5c3fbb --- /dev/null +++ b/luasnippets/tex/commands.lua @@ -0,0 +1,30 @@ +-- Abbreviations used in this article and the LuaSnip docs +local ls = require("luasnip") +local s = ls.snippet +-- local sn = ls.snippet_node +-- local t = ls.text_node +local i = ls.insert_node +-- local f = ls.function_node +-- local d = ls.dynamic_node +-- local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +-- local rep = require("luasnip.extras").rep + +-- from nvim/lua/ +local helpers = require("luasnip-helpers") +local as = helpers.as +local ast = helpers.ast +return { + -- Fonts + ast("ttt", fmta("\\texttt{<>}", { i(1) })), + -- asm("rm", fmta("\\mathrm{<>}", { i(1) })), + ast("tbf", fmta("\\textbf{<>}", { i(1) })), + ast("tsf", fmta("\\textsf{<>}", { i(1) })), + ast("tit", fmta("\\textit{<>}", { i(1) })), + -- Idk, maybe emph can be used in mathmode? + ast("emp", fmta("\\emph{<>}", { i(1) })), + -- These two normally not needed, as you would define "\R = \mathbb{R}", + -- and so on. + -- as("bb", fmta("\\mathbb{<>}", { i(1) })), + -- as("cal", fmta("\\mathcal{<>}", { i(1) })), +} diff --git a/luasnippets/tex/environments.lua b/luasnippets/tex/environments.lua new file mode 100644 index 00000000..ae156940 --- /dev/null +++ b/luasnippets/tex/environments.lua @@ -0,0 +1,91 @@ +-- Abbreviations used in this article and the LuaSnip docs +local ls = require("luasnip") +local s = ls.snippet +-- local sn = ls.snippet_node +-- local t = ls.text_node +local i = ls.insert_node +-- local f = ls.function_node +-- local d = ls.dynamic_node +-- local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +local rep = require("luasnip.extras").rep +local line_begin = require("luasnip.extras.expand_conditions").line_begin +-- +-- AutoSnippet function "as": +local function as(trigger, nodes, opts) + opts = opts or {} + -- Add snippetType = "autosnippet" to the first parameter + if type(trigger) == "table" then + trigger.snippetType = "autosnippet" + else + trigger = { trig = trigger, snippetType = "autosnippet" } + end + return s(trigger, nodes, opts) +end + +return { + as( + "eq", + fmta( + [[ + \[ + <> + \] + ]], + { i(1) } + ), + { condition = line_begin } + ), + + as( + "\\[", + fmta( + [[ + \[ + <> + \] + ]], + { i(1) } + ), + { condition = line_begin } + ), + + as( + "beg", + fmta( + [[ + \begin{<>} + <> + \end{<>} + ]], + { i(1), i(2), rep(1) } + ), + { condition = line_begin } + ), + + as( + "als", + fmta( + [[ + \begin{align*} + <> + \end{align*} + ]], + { i(1) } + ), + { condition = line_begin } + ), + + as( + "ali", + fmta( + [[ + \begin{align} + <> + \end{align} + ]], + { i(1) } + ), + { condition = line_begin } + ), +} diff --git a/luasnippets/tex/math.lua b/luasnippets/tex/math.lua new file mode 100644 index 00000000..06000a01 --- /dev/null +++ b/luasnippets/tex/math.lua @@ -0,0 +1,82 @@ +-- Abbreviations used in this article and the LuaSnip docs +local ls = require("luasnip") +local s = ls.snippet +-- local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +-- local f = ls.function_node +-- local d = ls.dynamic_node +-- local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +-- local rep = require("luasnip.extras").rep + +-- from nvim/lua/ +local helpers = require("luasnip-helpers") +-- Autosnippet, only for math environments +local asm = helpers.asm +return { + -- Greek letters + asm(";a", { t("\\alpha") }), + asm(";b", { t("\\beta") }), + asm(";g", { t("\\gamma") }), + asm(";G", { t("\\Gamma") }), + asm(";d", { t("\\delta") }), + asm(";D", { t("\\Delta") }), + -- Next two are swapped on purpose - always use varepsilon! + asm(";e", { t("\\varepsilon") }), + asm(";ve", { t("\\epsilon") }), + asm(";z", { t("\\zeta") }), + asm(";t", { t("\\theta") }), + asm(";vt", { t("\\vartheta") }), + asm(";T", { t("\\Theta") }), + asm(";i", { t("\\iota") }), + asm(";k", { t("\\kappa") }), + asm(";l", { t("\\lambda") }), + asm(";L", { t("\\Lambda") }), + asm(";m", { t("\\mu") }), + asm(";n", { t("\\nu") }), + asm(";x", { t("\\xi") }), + asm(";X", { t("\\Xi") }), + asm(";pi", { t("\\pi") }), + asm(";Pi", { t("\\Pi") }), + asm(";r", { t("\\rho") }), + asm(";vr", { t("\\varrho") }), + asm(";s", { t("\\sigma") }), + asm(";S", { t("\\Sigma") }), + asm(";t", { t("\\tau") }), + asm(";u", { t("\\upsilon") }), + asm(";U", { t("\\Upsilon") }), + asm(";ph", { t("\\phi") }), + -- Could be ";vph", but two letters seems nicer + asm(";vp", { t("\\varphi") }), + asm(";Ph", { t("\\Phi") }), + asm(";c", { t("\\chi") }), + asm(";ps", { t("\\psi") }), + asm(";Ps", { t("\\Psi") }), + asm(";o", { t("\\omega") }), + asm(";O", { t("\\Omega") }), + + asm("ff", fmta("\\frac{<>}{<>}", { i(1), i(2) })), + asm("tf", fmta("\\tfrac{<>}{<>}", { i(1), i(2) })), + + asm({ trig = "__", wordTrig = false }, fmta("_{<>}", { i(1) })), + asm({ trig = "^^", wordTrig = false }, fmta("^{<>}", { i(1) })), + + -- Math fonts (in this document, so they only trigger in math environments) + -- See https://tex.stackexchange.com/questions/58098/what-are-all-the-font-styles-i-can-use-in-math-mode + asm("rm", fmta("\\mathrm{<>}", { i(1) })), + asm("bf", fmta("\\boldsymbol{<>}", { i(1) })), + asm("sf", fmta("\\mathsf{<>}", { i(1) })), + asm("it", fmta("\\mathit{<>}", { i(1) })), + asm("tt", fmta("\\mathtt{<>}", { i(1) })), + -- These two normally not needed, as you would define "\R = \mathbb{R}", + -- and so on. + -- asm("bb", fmta("\\mathbb{<>}", { i(1) })), + asm("cal", fmta("\\mathcal{<>}", { i(1) })), + + -- Delimeters (in math). NEED CORRESPONDING DEFINITIONS IN PREAMBLE + asm("pp", fmta("\\lr{<>}", { i(1) })), + asm("ss", fmta("\\lrs{<>}", { i(1) })), + asm("cc", fmta("\\lrc{<>}", { i(1) })), + asm("sq", fmta("\\sqrt{<>}", { i(1) })), +} diff --git a/luasnippets/tex/misc.lua b/luasnippets/tex/misc.lua new file mode 100644 index 00000000..e809fe2d --- /dev/null +++ b/luasnippets/tex/misc.lua @@ -0,0 +1,37 @@ +-- Abbreviations used in this article and the LuaSnip docs +local ls = require("luasnip") +local s = ls.snippet +-- local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +-- local f = ls.function_node +-- local d = ls.dynamic_node +-- local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +-- local rep = require("luasnip.extras").rep +-- local in_mathzone = function() +-- return vim.fn["vimtex#syntax#in_mathzone"]() == 1 +-- end +-- AutoSnippet function "as": +local function as(trigger, nodes, opts) + -- Only trigger commands in mathzones + opts = opts or {} + -- Add snippetType = "autosnippet" to the first parameter + if type(trigger) == "table" then + trigger.snippetType = "autosnippet" + else + trigger = { trig = trigger, snippetType = "autosnippet" } + end + return s(trigger, nodes, opts) +end + +return { + as({ trig = "{{", wordTrig = false }, fmta("{<>}", { i(1) })), + as({ trig = "[[", wordTrig = false }, fmta("[<>]", { i(1) })), + as({ trig = "((", wordTrig = false }, fmta("(<>)", { i(1) })), + as({ trig = "{}", wordTrig = false }, fmta("{<>}", { i(1) })), + as({ trig = "[]", wordTrig = false }, fmta("[<>]", { i(1) })), + as({ trig = "()", wordTrig = false }, fmta("(<>)", { i(1) })), + as("mm", fmta("$<>$", { i(1) })), + as("$$", fmta("$<>$", { i(1) })), +} diff --git a/luasnippets/tex/templates.lua b/luasnippets/tex/templates.lua new file mode 100644 index 00000000..e69de29b diff --git a/snippets/tex.snippets b/snippets/tex.snippets index 4cdf1c5f..69ab9ded 100644 --- a/snippets/tex.snippets +++ b/snippets/tex.snippets @@ -1,13 +1,13 @@ -snippet dmo "\DeclareMathOperator" - \\DeclareMathOperator{${1}}{${2}} +#snippet dmo "\DeclareMathOperator" +# \\DeclareMathOperator{${1}}{${2}} -snippet $$ "$Math$" - \$${1:VISUAL}\$ +#snippet $$ "$Math$" +# \$${1:VISUAL}\$ -snippet em "\emph{...}" - \emph{${1:VISUAL}} +#snippet em "\emph{...}" +# \emph{${1:VISUAL}} -snippet \ "\[ ... \]" - \[ - ${1} - \] +#snippet \ "\[ ... \]" +# \[ +# ${1} +# \]