Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conditions memoization #600

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/luasnip.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*luasnip.txt* For NVIM v0.5.0 Last change: 2022 September 15
*luasnip.txt* For NVIM v0.5.0 Last change: 2022 September 19

==============================================================================
Table of Contents *luasnip-table-of-contents*
Expand Down
80 changes: 78 additions & 2 deletions lua/luasnip/extras/expand_conditions.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,89 @@
local M = {}

function M.line_begin(line_to_cursor, matched_trigger)
-----------------------
-- CONDITION OBJECTS --
-----------------------
local memoization_mt = {
-- logic operators
-- not
__unm = function(o1)
return function(...)
return not o1(...)
end
end,
-- or
__add = function(o1, o2)
return function(...)
return o1(...) or o2(...)
end
end,
-- and
__mul = function(o1, o2)
return function(...)
return o1(...) and o2(...)
end
end,
-- xnor
__eq = function(o1, o2)
return function(...)
return o1(...) == o2(...)
end
end,
-- use table like a function by overloading __call
__call = function(tab, line_to_cursor, matched_trigger, captures)
if
not tab.mem
or tab.invalidate(tab, line_to_cursor, matched_trigger, captures)
then
tab.mem = tab.func(line_to_cursor, matched_trigger, captures)
end
return tab.mem
end,
}
-- low level factory
-- invalidate(table) -> bool: decides if the memoization should be invalidated,
-- can store state in table
function M.memoization_factory(func, invalidate)
-- always invalidare by default
invalidate = invalidate or function()
return true
end
return setmetatable(
{ func = func, invalidate = invalidate },
memoization_mt
)
end

-------------------
-- INVAL PRESETS --
-------------------
-- TODO provide invalidate defaults (buffer, cursor, changes, none)
M.inval = {}
-- just a small example to illustrate the usage (will most probably have to be
-- changed)
function M.inval.line(tab, _, _, _, line)
return tab.line ~= line
end

------------------
-- COND PRESETS --
------------------
local function line_begin(line_to_cursor, matched_trigger)
-- +1 because `string.sub("abcd", 1, -2)` -> abc
return line_to_cursor:sub(1, -(#matched_trigger + 1)):match("^%s*$")
end
function M.line_begin()
-- TODO invalidation function
return M.memoization_factory(line_begin, nil)
end

function M.line_end(line_to_cursor)
local function line_end(line_to_cursor)
local line = vim.api.nvim_get_current_line()
return #line_to_cursor == #line
end
function M.line_end()
-- TODO invalidation function
return M.memoization_factory(line_end, nil)
end

return M