-
-
Notifications
You must be signed in to change notification settings - Fork 256
conditions memoization #600
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
Draft
atticus-sullivan
wants to merge
7
commits into
L3MON4D3:master
Choose a base branch
from
atticus-sullivan:condition_objects
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6ced4d3
add memoization first draft/idea
atticus-sullivan d4f8be1
fix the argument forwarding (comply to the current interfaces)
atticus-sullivan 63925f0
add xnor operator
atticus-sullivan 6112008
add condition presets
atticus-sullivan 4e13374
fiction example for invalidation function
atticus-sullivan a493913
Auto generate docs
atticus-sullivan f37dcd6
Format with stylua
atticus-sullivan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.