Repository: iurimateus/luasnip-latex-snippets.nvim Branch: main Commit: a14821dd680d Files: 16 Total size: 39.8 KB Directory structure: gitextract_zfiap6pl/ ├── .luacheckrc ├── .stylua.toml ├── LICENSE ├── README.md └── lua/ └── luasnip-latex-snippets/ ├── bwA.lua ├── init.lua ├── math_i.lua ├── math_iA.lua ├── math_iA_no_backslash.lua ├── math_rA_no_backslash.lua ├── math_wA_no_backslash.lua ├── math_wRA_no_backslash.lua ├── math_wrA.lua ├── util/ │ ├── ts_utils.lua │ └── utils.lua └── wA.lua ================================================ FILE CONTENTS ================================================ ================================================ FILE: .luacheckrc ================================================ -- vim: ft=lua tw=80 stds.nvim = { globals = { vim = { fields = { "g" } }, }, read_globals = { "vim", }, } std = "lua51+nvim" -- Don't report unused self arguments of methods. self = false ignore = { "631", -- max_line_length "212/_.*", -- unused argument, for vars with "_" prefix } ================================================ FILE: .stylua.toml ================================================ column_width = 100 indent_type = "Spaces" indent_width = 2 quote_style = "AutoPreferDouble" no_call_parentheses = false ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: README.md ================================================ # luasnip-latex-snippets A port of [Gilles Castel's](https://github.com/gillescastel/latex-snippets) snippets for the [LuaSnip Engine](https://github.com/L3MON4D3/LuaSnip). ## Why? UltiSnips felt unbearably slow. See and . ## Installation Depends on [vimtex](https://github.com/lervag/vimtex) to determine if the cursor is within math mode. Alternatively, you can use [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) (experimental) by passing `{ use_treesitter = true }` to the setup call. It's necessary to enable `autosnippets` in luasnip: `require("luasnip").config.setup { enable_autosnippets = true }`. Can be installed like any neovim plugin. If using [wbthomason/packer.nvim](https://github.com/wbthomason/packer.nvim): ```lua use { "iurimateus/luasnip-latex-snippets.nvim", -- vimtex isn't required if using treesitter requires = { "L3MON4D3/LuaSnip", "lervag/vimtex" }, config = function() require'luasnip-latex-snippets'.setup() -- or setup({ use_treesitter = true }) require("luasnip").config.setup { enable_autosnippets = true } end, } ``` ## Configuration ``` { use_treesitter = false, -- whether to use treesitter to determine if cursor is in math mode; if false, vimtex is used allow_on_markdown = true, -- whether to add snippets to markdown filetype } ``` ## Misc The following convention, from [SirVer/ultisnips](https://github.com/SirVer/ultisnips), is used for naming lua tables and respective files: ``` A: Automatic snippet expansion - snippets will activate as soon as their trigger matches. w: Word boundary - With this option the snippet trigger will match when the trigger is a word boundary character. This is the default behavior. b: Beginning of line expansion - A snippet with this option is expanded only if the trigger is the first word on the line (i.e., only whitespace precedes the trigger). ``` Note that the _regex_ term used in LuaSnip is misleading: it is not a POSIX regexp. What's actually being used is Lua pattern matching, which has some limitations. In particular, it lacks positive lookbehind and alternation `|`. The first which is used in postfix completions (e.g. match `delta`, but not `\delta`) and can be handled with simple functions. The second was solved by splitting and/or partially rewriting the expressions. ### Adding your own snippets / overrides See discussion https://github.com/iurimateus/luasnip-latex-snippets.nvim/discussions/3#discussioncomment-11348745 ```lua local ls = require("luasnip") local utils = require("luasnip-latex-snippets.util.utils") local not_math = utils.not_math() -- pass true if using Treesitter -- set a higher priority (defaults to 0 for most snippets) local snip = ls.parser.parse_snippet( { trig = "mk", name = "Math", condition = not_math, priority = 10 }, "$ ${1:${TM_SELECTED_TEXT}} $$0" ) ls.add_snippets("tex", { snip }, { type = "autosnippets", }) ``` ## Roadmap - [ ] Tests ================================================ FILE: lua/luasnip-latex-snippets/bwA.lua ================================================ local ls = require("luasnip") local t = ls.text_node local i = ls.insert_node local M = {} function M.retrieve(not_math) local utils = require("luasnip-latex-snippets.util.utils") local pipe = utils.pipe local conds = require("luasnip.extras.expand_conditions") local condition = pipe({ conds.line_begin, not_math }) local parse_snippet = ls.extend_decorator.apply(ls.parser.parse_snippet, { condition = condition, }) --[[@as function]] local s = ls.extend_decorator.apply(ls.snippet, { condition = condition, }) --[[@as function]] return { s( { trig = "ali", name = "Align" }, { t({ "\\begin{align*}", "\t" }), i(1), t({ "", "\\end{align*}" }) } ), parse_snippet({ trig = "beg", name = "begin{} / end{}" }, "\\begin{$1}\n\t$0\n\\end{$1}"), s({ trig = "bigfun", name = "Big function" }, { t({ "\\begin{align*}", "\t" }), i(1), t(":"), t(" "), i(2), t("&\\longrightarrow "), i(3), t({ " \\", "\t" }), i(4), t("&\\longmapsto "), i(1), t("("), i(4), t(")"), t(" = "), i(0), t({ "", "\\end{align*}" }), }), } end return M ================================================ FILE: lua/luasnip-latex-snippets/init.lua ================================================ local M = {} local default_opts = { use_treesitter = false, allow_on_markdown = true, } M.setup = function(opts) opts = vim.tbl_deep_extend("force", default_opts, opts or {}) local augroup = vim.api.nvim_create_augroup("luasnip-latex-snippets", {}) vim.api.nvim_create_autocmd("FileType", { pattern = "tex", group = augroup, once = true, callback = function() local utils = require("luasnip-latex-snippets.util.utils") local is_math = utils.with_opts(utils.is_math, opts.use_treesitter) local not_math = utils.with_opts(utils.not_math, opts.use_treesitter) M.setup_tex(is_math, not_math) end, }) if opts.allow_on_markdown then vim.api.nvim_create_autocmd("FileType", { pattern = { "markdown", "quarto" }, group = augroup, once = true, callback = function() M.setup_markdown() end, }) end end local _autosnippets = function(is_math, not_math) local autosnippets = {} for _, s in ipairs({ "math_wRA_no_backslash", "math_rA_no_backslash", "math_wA_no_backslash", "math_iA_no_backslash", "math_iA", "math_wrA", }) do vim.list_extend( autosnippets, require(("luasnip-latex-snippets.%s"):format(s)).retrieve(is_math) ) end for _, s in ipairs({ "wA", "bwA", }) do vim.list_extend( autosnippets, require(("luasnip-latex-snippets.%s"):format(s)).retrieve(not_math) ) end return autosnippets end M.setup_tex = function(is_math, not_math) local ls = require("luasnip") ls.add_snippets("tex", { ls.parser.parse_snippet( { trig = "pac", name = "Package" }, "\\usepackage[${1:options}]{${2:package}}$0" ), -- ls.parser.parse_snippet({ trig = "nn", name = "Tikz node" }, { -- "$0", -- -- "\\node[$5] (${1/[^0-9a-zA-Z]//g}${2}) ${3:at (${4:0,0}) }{$${1}$};", -- "\\node[$5] (${1}${2}) ${3:at (${4:0,0}) }{$${1}$};", -- }), }) local math_i = require("luasnip-latex-snippets/math_i").retrieve(is_math) ls.add_snippets("tex", math_i, { default_priority = 0 }) ls.add_snippets("tex", _autosnippets(is_math, not_math), { type = "autosnippets", default_priority = 0, }) end M.setup_markdown = function() local ls = require("luasnip") local utils = require("luasnip-latex-snippets.util.utils") local pipe = utils.pipe local is_math = utils.with_opts(utils.is_math, true) local not_math = utils.with_opts(utils.not_math, true) local math_i = require("luasnip-latex-snippets/math_i").retrieve(is_math) ls.add_snippets("markdown", math_i, { default_priority = 0 }) local autosnippets = _autosnippets(is_math, not_math) local trigger_of_snip = function(s) return s.trigger end local to_filter = {} for _, str in ipairs({ "wA", "bwA", }) do local t = require(("luasnip-latex-snippets.%s"):format(str)).retrieve(not_math) vim.list_extend(to_filter, vim.tbl_map(trigger_of_snip, t)) end local filtered = vim.tbl_filter(function(s) return not vim.tbl_contains(to_filter, s.trigger) end, autosnippets) local parse_snippet = ls.extend_decorator.apply(ls.parser.parse_snippet, { condition = pipe({ not_math }), }) --[[@as function]] -- tex delimiters local normal_wA_tex = { parse_snippet({ trig = "mk", name = "Math" }, "$${1:${TM_SELECTED_TEXT}}$"), parse_snippet({ trig = "dm", name = "Block Math" }, "$$\n\t${1:${TM_SELECTED_TEXT}}\n$$"), } vim.list_extend(filtered, normal_wA_tex) ls.add_snippets("markdown", filtered, { type = "autosnippets", default_priority = 0, }) end return M ================================================ FILE: lua/luasnip-latex-snippets/math_i.lua ================================================ local M = {} local ls = require("luasnip") local utils = require("luasnip-latex-snippets.util.utils") local pipe = utils.pipe function M.retrieve(is_math) local parse_snippet = ls.extend_decorator.apply(ls.parser.parse_snippet, { wordTrig = false, condition = pipe({ is_math }), show_condition = is_math, }) --[[@as function]] return { parse_snippet({ trig = "sum", name = "sum" }, "\\sum_{n=${1:1}}^{${2:\\infty}} ${3:a_n z^n}"), parse_snippet( { trig = "taylor", name = "taylor" }, "\\sum_{${1:k}=${2:0}}^{${3:\\infty}} ${4:c_$1} (x-a)^$1 $0" ), parse_snippet({ trig = "lim", name = "limit" }, "\\lim_{${1:n} \\to ${2:\\infty}} "), parse_snippet({ trig = "limsup", name = "limsup" }, "\\limsup_{${1:n} \\to ${2:\\infty}} "), parse_snippet( { trig = "prod", name = "product" }, "\\prod_{${1:n=${2:1}}}^{${3:\\infty}} ${4:${TM_SELECTED_TEXT}} $0" ), parse_snippet( { trig = "part", name = "d/dx" }, "\\frac{\\partial ${1:V}}{\\partial ${2:x}} $0" ), parse_snippet( { trig = "ddx", name = "d/dx" }, "\\frac{\\mathrm{d${1:V}}}{\\mathrm{d${2:x}}} $0" ), parse_snippet({ trig = "pmat", name = "pmat" }, "\\begin{pmatrix} $1 \\end{pmatrix} $0"), parse_snippet( { trig = "lr", name = "left( right)" }, "\\left( ${1:${TM_SELECTED_TEXT}} \\right) $0" ), parse_snippet( { trig = "lr(", name = "left( right)" }, "\\left( ${1:${TM_SELECTED_TEXT}} \\right) $0" ), parse_snippet( { trig = "lr|", name = "left| right|" }, "\\left| ${1:${TM_SELECTED_TEXT}} \\right| $0" ), parse_snippet( { trig = "lr{", name = "left{ right}" }, "\\left\\{ ${1:${TM_SELECTED_TEXT}} \\right\\\\} $0" ), parse_snippet( { trig = "lr[", name = "left[ right]" }, "\\left[ ${1:${TM_SELECTED_TEXT}} \\right] $0" ), parse_snippet( { trig = "lra", name = "leftangle rightangle" }, "\\left< ${1:${TM_SELECTED_TEXT}} \\right>$0" ), parse_snippet( { trig = "lrb", name = "left\\{ right\\}" }, "\\left\\{ ${1:${TM_SELECTED_TEXT}} \\right\\\\} $0" ), parse_snippet( { trig = "sequence", name = "Sequence indexed by n, from m to infinity" }, "(${1:a}_${2:n})_{${2:n}=${3:m}}^{${4:\\infty}}" ), } end return M ================================================ FILE: lua/luasnip-latex-snippets/math_iA.lua ================================================ local M = {} local ls = require("luasnip") local f = ls.function_node function M.retrieve(is_math) local utils = require("luasnip-latex-snippets.util.utils") local pipe, no_backslash = utils.pipe, utils.no_backslash local decorator = { wordTrig = false, condition = pipe({ is_math, no_backslash }), } local parse_snippet = ls.extend_decorator.apply(ls.parser.parse_snippet, decorator) --[[@as function]] local s = ls.extend_decorator.apply(ls.snippet, decorator) --[[@as function]] return { s( { trig = "(%a+)bar", wordTrig = false, regTrig = true, name = "bar", priority = 100, }, f(function(_, snip) return string.format("\\overline{%s}", snip.captures[1]) end, {}) ), s( { trig = "(%a+)und", wordTrig = false, regTrig = true, name = "underline", priority = 100, }, f(function(_, snip) return string.format("\\underline{%s}", snip.captures[1]) end, {}) ), s( { trig = "(%a)dot", wordTrig = false, regTrig = true, name = "dot", priority = 100, }, f(function(_, snip) return string.format("\\dot{%s}", snip.captures[1]) end, {}) ), s( { trig = "(%a+)hat", wordTrig = false, regTrig = true, name = "hat", priority = 100, }, f(function(_, snip) return string.format("\\hat{%s}", snip.captures[1]) end, {}) ), s( { trig = "(%a+)ora", wordTrig = false, regTrig = true, name = "ora", priority = 100, }, f(function(_, snip) return string.format("\\overrightarrow{%s}", snip.captures[1]) end, {}) ), s( { trig = "(%a+)ola", wordTrig = false, regTrig = true, name = "ola", priority = 100, }, f(function(_, snip) return string.format("\\overleftarrow{%s}", snip.captures[1]) end, {}) ), parse_snippet({ trig = "td", name = "to the ... power ^{}" }, "^{$1}$0 "), parse_snippet({ trig = "rd", name = "to the ... power ^{()}" }, "^{($1)}$0 "), parse_snippet({ trig = "cb", name = "Cube ^3" }, "^3 "), parse_snippet({ trig = "sr", name = "Square ^2" }, "^2"), parse_snippet({ trig = "EE", name = "exists" }, "\\exists "), parse_snippet({ trig = "AA", name = "forall" }, "\\forall "), parse_snippet({ trig = "xnn", name = "xn" }, "x_{n}"), parse_snippet({ trig = "ynn", name = "yn" }, "y_{n}"), parse_snippet({ trig = "xii", name = "xi" }, "x_{i}"), parse_snippet({ trig = "yii", name = "yi" }, "y_{i}"), parse_snippet({ trig = "xjj", name = "xj" }, "x_{j}"), parse_snippet({ trig = "yjj", name = "yj" }, "y_{j}"), parse_snippet({ trig = "xp1", name = "x" }, "x_{n+1}"), parse_snippet({ trig = "xmm", name = "x" }, "x_{m}"), parse_snippet({ trig = "R0+", name = "R0+" }, "\\mathbb{R}_0^+"), parse_snippet({ trig = "notin", name = "not in " }, "\\not\\in "), parse_snippet({ trig = "cc", name = "subset" }, "\\subset "), parse_snippet({ trig = "<->", name = "leftrightarrow", priority = 200 }, "\\leftrightarrow"), parse_snippet({ trig = "...", name = "ldots", priority = 100 }, "\\ldots "), parse_snippet({ trig = "!>", name = "mapsto" }, "\\mapsto "), parse_snippet({ trig = "iff", name = "iff" }, "\\iff"), parse_snippet({ trig = "siff", name = "short iff", priority = 100}, "\\Leftrightarrow"), parse_snippet({ trig = "ooo", name = "\\infty" }, "\\infty"), parse_snippet({ trig = "rij", name = "mrij" }, "(${1:x}_${2:n})_{${3:$2}\\in${4:\\N}}$0"), parse_snippet({ trig = "nabl", name = "nabla" }, "\\nabla "), parse_snippet({ trig = "", name = "to", priority = 100 }, "\\to "), parse_snippet({ trig = "-->", name = "long to", priority = 200 }, "\\longrightarrow "), parse_snippet({ trig = "letw", name = "let omega" }, "Let $\\Omega \\subset \\C$ be open."), parse_snippet({ trig = "nnn", name = "bigcap" }, "\\bigcap_{${1:i \\in ${2: I}}} $0"), parse_snippet({ trig = "norm", name = "norm" }, "\\|$1\\|$0"), parse_snippet({ trig = "<>", name = "hokje" }, "\\diamond "), parse_snippet({ trig = ">>", name = ">>" }, "\\gg"), parse_snippet({ trig = "<<", name = "<<" }, "\\ll"), parse_snippet({ trig = "stt", name = "text subscript" }, "_\\text{$1} $0"), parse_snippet({ trig = "tt", name = "text" }, "\\text{$1}$0"), parse_snippet({ trig = "xx", name = "cross" }, "\\times "), parse_snippet({ trig = "**", name = "cdot", priority = 100 }, "\\cdot "), parse_snippet({ trig = ":=", name = "colon equals (lhs defined as rhs)" }, "\\coloneqq "), parse_snippet( { trig = "cvec", name = "column vector" }, "\\begin{pmatrix} ${1:x}_${2:1}\\\\ \\vdots\\\\ $1_${2:n} \\end{pmatrix}" ), parse_snippet({ trig = "ceil", name = "ceil" }, "\\left\\lceil $1 \\right\\rceil $0"), parse_snippet({ trig = "OO", name = "emptyset" }, "\\O"), parse_snippet({ trig = "RR", name = "R" }, "\\mathbb{R}"), parse_snippet({ trig = "QQ", name = "Q" }, "\\mathbb{Q}"), parse_snippet({ trig = "ZZ", name = "Z" }, "\\mathbb{Z}"), parse_snippet({ trig = "UU", name = "cup" }, "\\cup "), parse_snippet({ trig = "NN", name = "n" }, "\\mathbb{N}"), parse_snippet({ trig = "||", name = "mid" }, " \\mid "), parse_snippet({ trig = "Nn", name = "cap" }, "\\cap "), parse_snippet({ trig = "bmat", name = "bmat" }, "\\begin{bmatrix} $1 \\end{bmatrix} $0"), parse_snippet({ trig = "uuu", name = "bigcup" }, "\\bigcup_{${1:i \\in ${2: I}}} $0"), parse_snippet({ trig = "DD", name = "D" }, "\\mathbb{D}"), parse_snippet({ trig = "HH", name = "H" }, "\\mathbb{H}"), parse_snippet({ trig = "lll", name = "l" }, "\\ell"), parse_snippet( { trig = "dint", name = "integral", priority = 300 }, "\\int_{${1:-\\infty}}^{${2:\\infty}} ${3:${TM_SELECTED_TEXT}} $0" ), parse_snippet({ trig = "==", name = "equals" }, [[&= $1 \\\\]]), parse_snippet({ trig = "!=", name = "not equals" }, "\\neq "), parse_snippet({ trig = "compl", name = "complement" }, "^{c}"), parse_snippet({ trig = "__", name = "subscript" }, "_{$1}$0"), parse_snippet({ trig = "=>", name = "implies" }, "\\implies"), parse_snippet({ trig = "simp", name = "short implies" }, "\\Rightarrow"), parse_snippet({ trig = "=<", name = "implied by" }, "\\impliedby"), parse_snippet({ trig = "<<", name = "<<" }, "\\ll"), parse_snippet({ trig = "<=", name = "leq" }, "\\le "), parse_snippet({ trig = ">=", name = "geq" }, "\\ge "), parse_snippet({ trig = "invs", name = "inverse" }, "^{-1}"), parse_snippet({ trig = "~~", name = "~" }, "\\sim "), parse_snippet({ trig = "conj", name = "conjugate" }, "\\overline{$1}$0"), } end return M ================================================ FILE: lua/luasnip-latex-snippets/math_iA_no_backslash.lua ================================================ local M = {} local ls = require("luasnip") function M.retrieve(is_math) local utils = require("luasnip-latex-snippets.util.utils") local pipe, no_backslash = utils.pipe, utils.no_backslash local parse_snippet = ls.extend_decorator.apply(ls.parser.parse_snippet, { wordTrig = false, condition = pipe({ is_math, no_backslash }), }) --[[@as function]] local with_priority = ls.extend_decorator.apply(parse_snippet, { priority = 10, }) --[[@as function]] return { parse_snippet({ trig = "sq", name = "\\sqrt{}" }, "\\sqrt{${1:${TM_SELECTED_TEXT}}} $0"), parse_snippet({ trig = "case", name = "cases" }, "\\begin{cases}\n\t$1\n\\end{cases}"), with_priority({ trig = "hat", name = "hat" }, "\\hat{$1}$0 "), with_priority({ trig = "bar", name = "bar" }, "\\overline{$1}$0 "), parse_snippet({ trig = "inf", name = "\\infty" }, "\\infty"), parse_snippet({ trig = "inn", name = "in " }, "\\in "), parse_snippet({ trig = "SI", name = "SI" }, "\\SI{$1}{$2}"), } end return M ================================================ FILE: lua/luasnip-latex-snippets/math_rA_no_backslash.lua ================================================ local ls = require("luasnip") local s = ls.snippet local f = ls.function_node local M = {} M.decorator = {} local postfix_trig = function(match) return string.format("(%s)", match) end local postfix_node = f(function(_, snip) return string.format("\\%s ", snip.captures[1]) end, {}) local build_snippet = function(trig, node, match, priority, name) return s({ name = name and name(match) or match, trig = trig(match), priority = priority, }, vim.deepcopy(node)) end local build_with_priority = function(trig, node, priority, name) return function(match) return build_snippet(trig, node, match, priority, name) end end local vargreek_postfix_completions = function() local re = "varepsilon|varphi|varrho|vartheta" local build = build_with_priority(postfix_trig, postfix_node, 200) return vim.tbl_map(build, vim.split(re, "|")) end local greek_postfix_completions = function() local re = "[aA]lpha|[bB]eta|[cC]hi|[dD]elta|[eE]psilon|[gG]amma|[iI]ota|[kK]appa|[lL]ambda|[mM]u|[nN]u|[oO]mega|[pP]hi|[pP]i|[pP]si|[rR]ho|[sS]igma|[tT]au|[tT]heta|[zZ]eta|[eE]ta" local build = build_with_priority(postfix_trig, postfix_node, 200) return vim.tbl_map(build, vim.split(re, "|")) end local postfix_completions = function() local re = "sin|cos|tan|csc|sec|cot|ln|log|exp|star|perp|int" local build = build_with_priority(postfix_trig, postfix_node) return vim.tbl_map(build, vim.split(re, "|")) end local snippets = {} function M.retrieve(is_math) local utils = require("luasnip-latex-snippets.util.utils") local pipe = utils.pipe local no_backslash = utils.no_backslash M.decorator = { wordTrig = true, trigEngine = "pattern", condition = pipe({ is_math, no_backslash }), } s = ls.extend_decorator.apply(ls.snippet, M.decorator) --[[@as function]] vim.list_extend(snippets, vargreek_postfix_completions()) vim.list_extend(snippets, greek_postfix_completions()) vim.list_extend(snippets, postfix_completions()) vim.list_extend(snippets, { build_snippet(postfix_trig, postfix_node, "q?quad", 200) }) return snippets end return M ================================================ FILE: lua/luasnip-latex-snippets/math_wA_no_backslash.lua ================================================ local ls = require("luasnip") local utils = require("luasnip-latex-snippets.util.utils") local pipe = utils.pipe local no_backslash = utils.no_backslash local M = {} function M.retrieve(is_math) local parse_snippet = ls.extend_decorator.apply(ls.parser.parse_snippet, { condition = pipe({ is_math, no_backslash }), }) --[[@as function]] local with_priority = ls.extend_decorator.apply(parse_snippet, { priority = 10, }) --[[@as function]] return { with_priority({ trig = "arcsin", name = "arcsin" }, "\\arcsin "), with_priority({ trig = "arctan", name = "arctan" }, "\\arctan "), with_priority({ trig = "arcsec", name = "arcsec" }, "\\arcsec "), with_priority({ trig = "asin", name = "asin" }, "\\arcsin"), with_priority({ trig = "atan", name = "atan" }, "\\arctan"), with_priority({ trig = "asec", name = "asec" }, "\\arcsec"), parse_snippet({ trig = "set", name = "set" }, [[ \\{$1\\} $0 ]]), parse_snippet({ trig = "fun", name = "function map" }, "f \\colon $1 \\R \\to \\R \\colon $0"), parse_snippet( { trig = "abs", name = "absolute value \\abs{}" }, "\\abs{${1:${TM_SELECTED_TEXT}}}$0" ), } end return M ================================================ FILE: lua/luasnip-latex-snippets/math_wRA_no_backslash.lua ================================================ local ls = require("luasnip") local f = ls.function_node local M = {} local vec_node = { f(function(_, snip) return string.format("\\vec{%s} ", snip.captures[1]) end, {}), } M.math_wrA_no_backslash = { { "([%a][%a])(%.,)", vec_node }, { "([%a][%a])(,%.)", vec_node }, { "([%a])(%.,)", vec_node }, { "([%a])(,%.)", vec_node }, } M.decorator = {} function M.retrieve(is_math) local utils = require("luasnip-latex-snippets.util.utils") local pipe = utils.pipe local no_backslash = utils.no_backslash M.decorator = { wordTrig = true, trigEngine = "pattern", condition = pipe({ is_math, no_backslash }), } local s = ls.extend_decorator.apply(ls.snippet, M.decorator) --[[@as function]] return vim.tbl_map(function(x) local trig, node = unpack(x) return s({ trig = trig }, vim.deepcopy(node)) end, M.math_wrA_no_backslash) end return M ================================================ FILE: lua/luasnip-latex-snippets/math_wrA.lua ================================================ local M = {} local ls = require("luasnip") local f = ls.function_node local i = ls.insert_node local t = ls.text_node local frac_no_parens = { f(function(_, snip) return string.format("\\frac{%s}", snip.captures[1]) end, {}), t("{"), i(1), t("}"), i(0), } local frac_node = { f(function(_, snip) local match = snip.trigger local stripped = match:sub(1, #match - 1) i = #stripped local depth = 0 while i >= 0 do if stripped:sub(i, i) == ")" then depth = depth + 1 end if stripped:sub(i, i) == "(" then depth = depth - 1 end if depth == 0 then break end i = i - 1 end if depth ~= 0 then return string.format("%s\\frac{}", stripped) else return string.format("%s\\frac{%s}", stripped:sub(1, i - 1), stripped:sub(i + 1, #stripped - 1)) end end, {}), t("{"), i(1), t("}"), i(0), } local subscript_node = { f(function(_, snip) return string.format("%s_{%s}", snip.captures[1], snip.captures[2]) end, {}), i(0), } local frac_no_parens_triggers = { "(\\?[%w]+\\?^%w)/", "(\\?[%w]+\\?_%w)/", "(\\?[%w]+\\?^{%w*})/", "(\\?[%w]+\\?_{%w*})/", "(\\?%w+)/", } function M.retrieve(is_math) local utils = require("luasnip-latex-snippets.util.utils") local pipe = utils.pipe local s = ls.extend_decorator.apply(ls.snippet, { wordTrig = false, trigEngine = "pattern", condition = pipe({ is_math }), }) --[[@as function]] local snippets = { s({ trig = "([%a])(%d)", name = "auto subscript", }, vim.deepcopy(subscript_node)), s({ trig = "([%a])_(%d%d)", name = "auto subscript 2", }, vim.deepcopy(subscript_node)), s({ priority = 1000, trig = ".*%)/", name = "() frac", wordTrig = true, }, vim.deepcopy(frac_node)), } for _, trig in pairs(frac_no_parens_triggers) do snippets[#snippets + 1] = s({ name = "Fraction no ()", trig = trig, }, vim.deepcopy(frac_no_parens)) end return snippets end return M ================================================ FILE: lua/luasnip-latex-snippets/util/ts_utils.lua ================================================ local M = {} local MATH_NODES = { displayed_equation = true, inline_formula = true, math_environment = true, } local TEXT_NODES = { text_mode = true, label_definition = true, label_reference = true, } local CODE_BLOCK_NODES = { -- Add this to define code block node types fenced_code_block = true, indented_code_block = true, -- Optional: include indented code blocks as well if needed } function M.in_text(check_parent) local node = vim.treesitter.get_node({ ignore_injections = false }) -- Check for code blocks in any filetype local block_node = node while block_node do if CODE_BLOCK_NODES[block_node:type()] then return true -- If in a code block, always consider it text end block_node = block_node:parent() end while node do if node:type() == "text_mode" then if check_parent then -- For \text{} local parent = node:parent() if parent and MATH_NODES[parent:type()] then return false end end return true elseif MATH_NODES[node:type()] then return false end node = node:parent() end return true end function M.in_mathzone() local node = vim.treesitter.get_node({ ignore_injections = false }) local current_filetype = vim.bo.filetype -- Check if we are in a markdown file and inside a code block if current_filetype == "markdown" or current_filetype == "quarto" then local block_node = node while block_node do if CODE_BLOCK_NODES[block_node:type()] then return false -- If in a code block in markdown, never consider it math zone end block_node = block_node:parent() end end while node do if TEXT_NODES[node:type()] then return false elseif MATH_NODES[node:type()] then return true end node = node:parent() end return false end return M ================================================ FILE: lua/luasnip-latex-snippets/util/utils.lua ================================================ local M = {} -- local s = ls.snippet -- local sn = ls.snippet_node -- local isn = ls.indent_snippet_node -- local t = ls.text_node -- local i = ls.insert_node -- local f = ls.function_node -- local c = ls.choice_node -- local d = ls.dynamic_node -- local events = require("luasnip.util.events") -- local r = require("luasnip.extras").rep -- local fmt = require("luasnip.extras.fmt").fmt -- local fmta = require("luasnip.extras.fmt").fmta M.pipe = function(fns) return function(...) for _, fn in ipairs(fns) do if not fn(...) then return false end end return true end end M.no_backslash = function(line_to_cursor, matched_trigger) return not line_to_cursor:find("\\%a+$", -#line_to_cursor) end local ts_utils = require("luasnip-latex-snippets.util.ts_utils") M.is_math = function(treesitter) if treesitter then return ts_utils.in_mathzone() end return vim.fn["vimtex#syntax#in_mathzone"]() == 1 end M.not_math = function(treesitter) if treesitter then return ts_utils.in_text(true) end return not M.is_math() end M.comment = function() return vim.fn["vimtex#syntax#in_comment"]() == 1 end M.env = function(name) local x, y = unpack(vim.fn["vimtex#env#is_inside"](name)) return x ~= "0" and y ~= "0" end M.with_priority = function(snip, priority) snip.priority = priority return snip end M.with_opts = function(fn, opts) return function() return fn(opts) end end return M ================================================ FILE: lua/luasnip-latex-snippets/wA.lua ================================================ local ls = require("luasnip") local utils = require("luasnip-latex-snippets.util.utils") local pipe = utils.pipe local M = {} function M.retrieve(not_math) local parse_snippet = ls.extend_decorator.apply(ls.parser.parse_snippet, { condition = pipe({ not_math }), }) --[[@as function]] return { parse_snippet({ trig = "mk", name = "Math" }, "\\( ${1:${TM_SELECTED_TEXT}} \\)$0"), parse_snippet({ trig = "dm", name = "Block Math" }, "\\[\n\t${1:${TM_SELECTED_TEXT}}\n\\] $0"), } end return M