1
Fork 0

Add miros support!

This commit is contained in:
prescientmoon 2024-04-16 23:25:20 +02:00
commit 1ed3a529c9
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
26 changed files with 910 additions and 1153 deletions
home/features/neovim/config/lua/my

View file

@ -2,21 +2,20 @@ local M = {}
function M.setup()
vim.opt.runtimepath:append(vim.g.nix_extra_runtime)
local tempest = require("my.tempest")
local nix = require("nix")
-- Import my other files
tempest.configureMany(nix.pre)
require("my.keymaps").setup()
require("my.lazy").setup()
tempest.configureMany(nix.post)
vim.api.nvim_create_autocmd("FileType", {
pattern = "tex",
group = vim.api.nvim_create_augroup("luasnip-latex-snippets", {}),
once = true,
callback = function()
require("my.snippets.tex").setup()
end,
require("luasnip.loaders.from_lua").lazy_load({
fs_event_providers = {
libuv = true,
},
})
end

View file

@ -0,0 +1,76 @@
local M = {}
local conditions = require("luasnip.extras.conditions")
-- {{{ Math mode detection
-- Taken from: https://github.com/iurimateus/luasnip-latex-snippets.nvim/blob/main/lua/luasnip-latex-snippets/util/ts_utils.lua
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 function get_node_at_cursor()
local pos = vim.api.nvim_win_get_cursor(0)
-- Subtract one to account for 1-based row indexing in nvim_win_get_cursor
local row, col = pos[1] - 1, pos[2]
local parser = vim.treesitter.get_parser(0, "latex")
if not parser then
return
end
local root_tree = parser:parse({ row, col, row, col })[1]
local root = root_tree and root_tree:root()
if not root then
return
end
return root:named_descendant_for_range(row, col, row, col)
end
local function in_text(check_parent)
local node = get_node_at_cursor()
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
M.math = conditions.make_condition(function()
local node = get_node_at_cursor()
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)
M.text = conditions.make_condition(function()
return in_text(true)
end)
-- }}}
return M

View file

@ -1,264 +0,0 @@
local M = {}
local ls = require("luasnip")
local extras = require("luasnip.extras")
local conds = require("luasnip.extras.expand_conditions")
local t = ls.text_node
local i = ls.insert_node
local d = ls.dynamic_node
local c = ls.choice_node
local f = ls.function_node
local sn = ls.snippet_node
local fmt = require("luasnip.extras.fmt").fmt
local n = extras.nonempty
local autosnippets = {}
-- {{{ Helpers
local function unlines(...)
return table.concat({ ... }, "\n")
end
local function flatten(arr)
local result = {}
for _, subarray in ipairs(arr) do
if type(subarray) == "table" then
for _, value in ipairs(subarray) do
table.insert(result, value)
end
else
table.insert(result, subarray)
end
end
return result
end
local function trig(name)
return { name = name, trig = name }
end
-- }}}
-- {{{ Math mode detection
-- Taken from: https://github.com/iurimateus/luasnip-latex-snippets.nvim/blob/main/lua/luasnip-latex-snippets/util/ts_utils.lua
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 function get_node_at_cursor()
local pos = vim.api.nvim_win_get_cursor(0)
-- Subtract one to account for 1-based row indexing in nvim_win_get_cursor
local row, col = pos[1] - 1, pos[2]
local parser = vim.treesitter.get_parser(0, "latex")
if not parser then
return
end
local root_tree = parser:parse({ row, col, row, col })[1]
local root = root_tree and root_tree:root()
if not root then
return
end
return root:named_descendant_for_range(row, col, row, col)
end
local function in_text(check_parent)
local node = get_node_at_cursor()
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
local function in_mathzone()
local node = get_node_at_cursor()
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
local function not_math()
return in_text(true)
end
-- }}}
-- {{{ Start of line & non-math
local beginTextCondition = function(...)
return conds.line_begin(...) and not_math()
end
local parseBeginText = ls.extend_decorator.apply(ls.parser.parse_snippet, {
condition = beginTextCondition,
}) --[[@as function]]
local snipBeginText = ls.extend_decorator.apply(ls.snippet, {
condition = beginTextCondition,
}) --[[@as function]]
local function env(name)
return "\\begin{" .. name .. "}\n\t$0\n\\end{" .. name .. "}"
end
local function optional_square_bracket_arg(index, default)
return sn(index, {
n(1, "[", ""),
i(1, default),
n(1, "]", ""),
})
end
local function theorem_env(name, prefix)
return snipBeginText(trig(name), {
t("\\begin{" .. name .. "}"),
optional_square_bracket_arg(1),
n(2, "\\label{" .. prefix .. ":", ""),
i(2),
n(2, "}", ""),
t({ "", "\t" }),
i(0),
t({ "", "\\end{" .. name .. "}" }),
})
end
vim.list_extend(autosnippets, {
parseBeginText(
{ trig = "begin", name = "Begin / end environment" },
env("$1")
),
-- {{{ Chapters / sections
parseBeginText(trig("chapter"), "\\chapter{$1}\n$0"),
parseBeginText(trig("section"), "\\section{$1}\n$0"),
parseBeginText(trig("subsection"), "\\subsection{$1}\n$0"),
parseBeginText(trig("subsubsection"), "\\subsubsection{$1}\n$0"),
-- }}}
-- {{{ Lists
parseBeginText({ trig = "item", name = "List item" }, "\\item"),
parseBeginText({ trig = "olist", name = "Ordered list" }, env("enumerate")),
parseBeginText({ trig = "ulist", name = "Unordered list" }, env("itemize")),
-- }}}
-- {{{ Theorem envs
theorem_env("theorem", "thm"),
theorem_env("lemma", "lem"),
theorem_env("exercise", "exe"),
theorem_env("definition", "def"),
theorem_env("corollary", "cor"),
theorem_env("example", "exa"),
snipBeginText(trig("proof"), {
t("\\begin{proof}"),
optional_square_bracket_arg(1),
t({ "", "\t" }),
i(0),
t({ "", "\\end{proof}" }),
}),
-- }}}
-- {{{ Special structures
parseBeginText(
{ trig = "ciff", name = "If and only if cases" },
unlines(
"\\begin{enumerate}",
"\t\\item[$\\implies$]$1",
"\t\\item[$\\impliedby$]$2",
"\\end{enumerate}",
"$0"
)
),
-- }}}
})
-- }}}
-- {{{ Non-math
local parseText = ls.extend_decorator.apply(ls.parser.parse_snippet, {
condition = not_math,
}) --[[@as function]]
local snipText = ls.extend_decorator.apply(ls.snippet, {
condition = not_math,
}) --[[@as function]]
local function ref(name, prefix)
return {
parseText(
{ trig = "r" .. name, name = name .. " reference" },
"\\ref{" .. prefix .. ":$1}$0"
),
parseText(
{ trig = "pr" .. name, name = name .. " reference" },
"(\\ref{" .. prefix .. ":$1})$0"
),
}
end
vim.list_extend(
autosnippets,
flatten({
-- {{{ References
ref("theorem", "thm"),
ref("lemma", "lem"),
ref("exercise", "exe"),
ref("definition", "def"),
ref("corollary", "cor"),
ref("example", "exa"),
{
parseText({ trig = "ref", name = "reference" }, "\\ref{$1}$0"),
parseText({ trig = "pref", name = "reference" }, "(\\ref{$1})$0"),
},
-- }}}
{
-- {{{ Misc
parseText(trig("quote"), "``$1''$0"),
parseText(trig("forcecr"), "{\\ \\\\\\\\}"),
-- }}}
-- {{{ Let ...
snipText(
{ trig = "([Ll]et)", trigEngine = "pattern", name = "definition" },
{
f(function(_, snip)
return snip.captures[1]
end),
t(" "),
sn(1, fmt("${} ≔ {}$", { i(1), i(2) })),
}
),
-- }}}
-- {{{ Display / inline math
parseText({ trig = "dm", name = "display math" }, env("align*")),
parseText({ trig = "im", name = "inline math" }, "\\$$1\\$$0"),
-- }}}
},
})
)
-- }}}
function M.setup()
ls.add_snippets("tex", autosnippets, {
type = "autosnippets",
default_priority = 0,
})
end
return M

View file

@ -1,103 +0,0 @@
for thmenv <- @{theorem,lemma,exercise,definition,corollary,example}
for thmprefix <- @{@thmenv:thm,lem,exe,def,cor,exa}
block auto !math start
for env <- @{$1,enumerate,itemize,align*}
string @{@env:begin,olist,ulist,dm}
name @{@env:environment,ordered list,unordered list,display math}
snip
\begin{@env}
$0
\end{@env}
end
string @thmprefix
snip
\begin{@thmenv}$?1{[@^{$1}]} \label{@thmprefix:$2}
$0
\end{@thmenv}
end
string proof
snip
\begin{proof}$?1{[Proof of @^{$1}]}
$0
\end{proof}
end
string ciff
name cases for ⟺
snip
\begin{enumerate}
\item[$\implies$]$1,
\item[$\impliedby$]$2,
\end{enumerate}
$0
end
string item
snip \item$|1{,[$1] $0}
block auto !math
string ref
snip \ref{$1}$0
string r@thmprefix
snip \ref{@thmprefix:$1}$0
string pref
snip (\ref{$1})$0
string pr@thmprefix
snip (\ref{@thmprefix:$1})$0
string quote
snip ``$1''$0
string forcecr
snip {\ \\\\}
pattern ([Ll]et)
name definition
snip @1 \$$1 = $2\$
string im
name inline math
snip \$$1\$$0
block math end
for suffix <- @{add,sub,rmul,pow,inv}
string ex@suffix
snip
&& \lrb{\square @{@suffix:
+ $1,
- $1,
\cdot $1,
^{$1\},
^{-1\},
}}$0
end
for prefix <- @{neg,mul,ln}
string ex@prefix
snip && \lrb{@{@prefix:-,$1,\ln} \square}$0
string exdiv
snip && \lrb{\frac \square {$1}}$0
string exint
snip && \lrb{\int\square \dif $1}$0
string exdint
snip && \lrb{\int_{$1}^{$2}\square \dif $3}$0
for kind <- @{texpl,extrin,exbound,exdef}
string @kind
snip
&& \lrb{\text{@{kind:
$1,
triangle inequality on \$$1\$,
\$$1\$ is bounded,
definition of \$$1\$,
}}}$0
end
for operator <- {overline,hat,abs,norm}
string @operator
snip \\@operator $|1{ $1,{$1\}$0}

View file

@ -1,3 +1,5 @@
-- This is the runtime for my custom nix-based neovim configuration system
local M = {}
local H = {}
M.helpers = H