-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #612 from atticus-sullivan/condition_objects_noMem
[feature] implement condition objects
- Loading branch information
Showing
9 changed files
with
427 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
local cond_obj = require("luasnip.extras.conditions") | ||
|
||
-- use the functions from show as basis and extend/overwrite functions specific for expand here | ||
local M = vim.deepcopy(require("luasnip.extras.conditions.show")) | ||
----------------------- | ||
-- PRESET CONDITIONS -- | ||
----------------------- | ||
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 | ||
M.line_begin = cond_obj.make_condition(line_begin) | ||
|
||
return M |
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
local M = {} | ||
|
||
----------------------- | ||
-- CONDITION OBJECTS -- | ||
----------------------- | ||
local condition_mt = { | ||
-- logic operators | ||
-- not '-' | ||
__unm = function(o1) | ||
return M.make_condition(function(...) | ||
return not o1(...) | ||
end) | ||
end, | ||
-- or '+' | ||
__add = function(o1, o2) | ||
return M.make_condition(function(...) | ||
return o1(...) or o2(...) | ||
end) | ||
end, | ||
-- and '*' | ||
__mul = function(o1, o2) | ||
return M.make_condition(function(...) | ||
return o1(...) and o2(...) | ||
end) | ||
end, | ||
-- xor '^' | ||
__pow = function(o1, o2) | ||
return M.make_condition(function(...) | ||
return o1(...) ~= o2(...) | ||
end) | ||
end, | ||
-- xnor '%' | ||
-- might be counter intuitive, but as we can't use '==' (must return bool) | ||
-- it's best to use something weird (doesn't have to be used) | ||
__mod = 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) | ||
return tab.func(line_to_cursor, matched_trigger, captures) | ||
end, | ||
} | ||
function M.make_condition(func) | ||
return setmetatable({ func = func }, condition_mt) | ||
end | ||
|
||
return M |
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
local cond_obj = require("luasnip.extras.conditions") | ||
|
||
local M = {} | ||
----------------------- | ||
-- PRESET CONDITIONS -- | ||
----------------------- | ||
local function line_end(line_to_cursor) | ||
local line = vim.api.nvim_get_current_line() | ||
-- looks pretty inefficient, but as lue interns strings, this is just a | ||
-- comparision of pointers (which probably is faster than calculate the | ||
-- length and then checking) | ||
return line_to_cursor == line | ||
end | ||
M.line_end = cond_obj.make_condition(line_end) | ||
|
||
return M |
This file contains 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 @@ | ||
local M = {} | ||
|
||
function M.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_end(line_to_cursor) | ||
local line = vim.api.nvim_get_current_line() | ||
return #line_to_cursor == #line | ||
end | ||
|
||
return M | ||
return require("luasnip.extras.conditions.expand") |
Oops, something went wrong.