-
-
Notifications
You must be signed in to change notification settings - Fork 261
WIP: Add MANY type annotations π #1396
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
bew
wants to merge
23
commits into
L3MON4D3:master
Choose a base branch
from
bew:add-MANY-types
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 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
01634c7
Add MANY type annotations π
bew 12eaf52
Event moar types & docstrings! π
bew e00160c
Add few more types & docs
bew 41a1113
Auto generate docs
bew 1a1f929
Format with stylua
bew 22c82a8
Fix SnippetString type errors
bew 344ecda
Apply easy suggestions (part1)
bew f1b6dea
Format with stylua
bew 02cabf1
Apply easy suggestions (part2)
bew ec94a55
Auto generate docs
bew 281fdee
Format with stylua
bew 6204bbd
Apply easy suggestions (part3)
bew 1c42996
Format with stylua
bew 1641838
Fill InsertNode doc, fix few other docs
bew 68b7376
Rewrite condition objects: fully typed & readable/explicit combinations
bew 85b9618
Apply easy suggestions (part4)
bew 1606b23
Auto generate docs
bew ce7f8a0
Format with stylua
bew d2b494a
Move/adapt docs from DOC.md for condition object
bew 84d7d32
Format with stylua
bew 05be5e2
Misc with few types, doc & FIXMEs..
bew 6d0b593
Add more docs around marks & positions, fix node_callbacks type
bew 8a61d9b
Auto generate docs
bew 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
Some comments aren't visible on the classic Files Changed page.
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,55 +1,99 @@ | ||
| 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, | ||
| __sub = function(o1, o2) | ||
| return M.make_condition(function(...) | ||
| return o1(...) and not 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, | ||
| --- A composable condition object, can be used for `condition` in a snippet | ||
| --- context. | ||
| --- | ||
| ---@class LuaSnip.SnipContext.ConditionObj | ||
| ---@field func LuaSnip.SnipContext.ConditionFn | ||
| --- | ||
| ---@overload fun(line_to_cursor: string, matched_trigger: string, captures: string[]): boolean | ||
| --- (note: same signature as `func` field) | ||
| --- | ||
| ---@operator unm: LuaSnip.SnipContext.ConditionObj | ||
| ---@operator add(LuaSnip.SnipContext.Condition): LuaSnip.SnipContext.ConditionObj | ||
| ---@operator sub(LuaSnip.SnipContext.Condition): LuaSnip.SnipContext.ConditionObj | ||
| ---@operator mul(LuaSnip.SnipContext.Condition): LuaSnip.SnipContext.ConditionObj | ||
| ---@operator pow(LuaSnip.SnipContext.Condition): LuaSnip.SnipContext.ConditionObj | ||
| ---@operator mod(LuaSnip.SnipContext.Condition): LuaSnip.SnipContext.ConditionObj | ||
| local ConditionObj = {} | ||
| local ConditionObj_mt = { | ||
| __index = ConditionObj, | ||
| -- 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) | ||
| __call = function(self, line_to_cursor, matched_trigger, captures) | ||
| return self.func(line_to_cursor, matched_trigger, captures) | ||
| end, | ||
| } | ||
|
|
||
| function M.make_condition(func) | ||
| return setmetatable({ func = func }, condition_mt) | ||
| --- Wrap the given `condition` function in a composable condition object. | ||
| ---@param func LuaSnip.SnipContext.Condition | ||
| ---@return LuaSnip.SnipContext.ConditionObj | ||
| function ConditionObj.make_condition(func) | ||
| return setmetatable({ func = func }, ConditionObj_mt) | ||
| end | ||
|
|
||
| --- Returns a condition object equivalent to `not self(...)` | ||
| ---@return LuaSnip.SnipContext.ConditionObj | ||
| function ConditionObj:inverted() | ||
| return ConditionObj.make_condition(function(...) | ||
| return not self(...) | ||
| end) | ||
| end | ||
| -- (e.g. `-cond`) | ||
| ConditionObj_mt.__unm = ConditionObj.inverted | ||
|
|
||
| --- Returns a condition object equivalent to `self(...) or other(...)` | ||
| ---@param other LuaSnip.SnipContext.Condition | ||
| ---@return LuaSnip.SnipContext.ConditionObj | ||
| function ConditionObj:or_(other) | ||
| return ConditionObj.make_condition(function(...) | ||
| return self(...) or other(...) | ||
| end) | ||
| end | ||
| -- (e.g. `cond1 + cond2`) | ||
| ConditionObj_mt.__add = ConditionObj.or_ | ||
|
|
||
| --- Returns a condition object equivalent to `self(...) and not other(...)` | ||
| ---@param other LuaSnip.SnipContext.Condition | ||
| ---@return LuaSnip.SnipContext.ConditionObj | ||
| function ConditionObj:and_not(other) | ||
| return ConditionObj.make_condition(function(...) | ||
| return self(...) and not other(...) | ||
| end) | ||
| end | ||
| -- (e.g. `cond1 - cond2`) | ||
| ConditionObj_mt.__sub = ConditionObj.and_not | ||
|
|
||
| --- Returns a condition object equivalent to `self(...) and other(...)` | ||
| ---@param other LuaSnip.SnipContext.Condition | ||
| ---@return LuaSnip.SnipContext.ConditionObj | ||
| function ConditionObj:and_(other) | ||
| return ConditionObj.make_condition(function(...) | ||
| return self(...) and other(...) | ||
| end) | ||
| end | ||
| -- (e.g. `cond1 * cond2`) | ||
| ConditionObj_mt.__mul = ConditionObj.and_ | ||
|
|
||
| --- Returns a condition object equivalent to `self(...) ~= other(...)` | ||
| ---@param other LuaSnip.SnipContext.Condition | ||
| ---@return LuaSnip.SnipContext.ConditionObj | ||
| function ConditionObj:not_same_as(other) | ||
| return ConditionObj.make_condition(function(...) | ||
| return self(...) ~= other(...) | ||
| end) | ||
| end | ||
| -- (e.g. `cond1 ^ cond2`) | ||
| ConditionObj_mt.__pow = ConditionObj.not_same_as | ||
|
|
||
| --- Returns a condition object equivalent to `self(...) == other(...)` | ||
| ---@param other LuaSnip.SnipContext.Condition | ||
| ---@return LuaSnip.SnipContext.ConditionObj | ||
| function ConditionObj:same_as(other) | ||
| return ConditionObj.make_condition(function(...) | ||
| return self(...) == other(...) | ||
| end) | ||
| end | ||
| -- (e.g. `cond1 % cond2`) | ||
| -- This operator might be counter intuitive, but '==' can't be used as it must | ||
| -- return a boolean. It's best to use something weird (doesn't have to be used) | ||
| ConditionObj_mt.__mod = ConditionObj.same_as | ||
|
|
||
| return M | ||
| return ConditionObj |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure about why these are possible to specify in the snippet opts, is it for backward compat?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah exactly, they used to be in that table, and until now I don't think there's been a big reason to actually remove them. Best put something about them being deprecated in there? Unfortunately
@deprecatedonly works for functions π’