Skip to content

Commit

Permalink
Add can_match
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Jun 22, 2023
1 parent 2df1ee4 commit 65ab98d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lua/gesture/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ function M.draw(raw_opts)
end

local gesture = state.matcher:match(ctx)
local has_forward_match = state.matcher:has_forward_match(ctx)
state.view:render_input(ctx.inputs, gesture, has_forward_match, opts.show_board)
local can_match = state.matcher:can_match(ctx)
state.view:render_input(ctx.inputs, gesture, can_match, opts.show_board)
end

function M.suspend()
Expand Down
34 changes: 21 additions & 13 deletions lua/gesture/core/gesture.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Gesture.new(info)
action = { info.action, { "string", "callable" } },
inputs = { info.inputs, "table", true },
match = { info.match, "function", true },
can_match = { info.can_match, "function", true },
nowait = { info.nowait, "boolean", true },
buffer = { info.buffer, { "string", "number" }, true },
})
Expand All @@ -37,10 +38,24 @@ function Gesture.new(info)
input_defs = require("gesture.core.input_definitions").new(info.inputs or {})
end

local can_match
if info.can_match then
can_match = info.can_match
elseif info.match then
can_match = function(_)
return true
end
elseif input_defs then
can_match = function(ctx)
return input_defs:has_forward_match(ctx.inputs)
end
end

local tbl = {
name = info.name or "",
input_defs = input_defs,
_match = info.match,
can_match = can_match,
nowait = info.nowait or false,
buffer = bufnr,
_action = action,
Expand All @@ -55,13 +70,6 @@ function Gesture.match(self, ctx)
return self._match(ctx)
end

function Gesture.has_forward_match(self, ctx)
if not self._match then
return self.input_defs:has_forward_match(ctx.inputs)
end
return true
end

function Gesture.execute(self, ctx)
local ok, result = pcall(self._action, ctx)
if not ok then
Expand Down Expand Up @@ -92,9 +100,9 @@ function Gestures.add(self, gesture)
table.insert(self._gestures, gesture)
end

function Gestures.has_forward_match(self, inputs)
function Gestures.can_match(self, ctx)
for _, gesture in ipairs(self._gestures) do
if gesture:has_forward_match(inputs) then
if gesture.can_match(ctx) then
return true
end
end
Expand Down Expand Up @@ -176,7 +184,7 @@ function GestureMap._match(self, key, ctx)
return gestures:match(ctx)
end

function GestureMap.has_forward_match(self, bufnr, ctx)
function GestureMap.can_match(self, bufnr, ctx)
vim.validate({ bufnr = { bufnr, "number" } })
local input_strs = require("gesture.core.inputs").strings(ctx.inputs)

Expand All @@ -199,7 +207,7 @@ function GestureMap.has_forward_match(self, bufnr, ctx)
},
}
for _, key_pair in ipairs(key_pairs) do
local matched = self:_has_forward_match(key_pair[1], key_pair[2], ctx)
local matched = self:_can_match(key_pair[1], key_pair[2], ctx)
if matched then
return matched
end
Expand All @@ -208,10 +216,10 @@ function GestureMap.has_forward_match(self, bufnr, ctx)
return false
end

function GestureMap._has_forward_match(self, nowait_key, key, ctx)
function GestureMap._can_match(self, nowait_key, key, ctx)
for k, gestures in pairs(self._map) do
local key_matched = vim.startswith(key, nowait_key) or vim.startswith(k, key)
if key_matched and gestures:has_forward_match(ctx) then
if key_matched and gestures:can_match(ctx) then
return true
end
end
Expand Down
4 changes: 2 additions & 2 deletions lua/gesture/core/matcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function Matcher.match(self, ctx)
return self._gesture_map:match(self._bufnr, ctx, false)
end

function Matcher.has_forward_match(self, ctx)
return self._gesture_map:has_forward_match(self._bufnr, ctx)
function Matcher.can_match(self, ctx)
return self._gesture_map:can_match(self._bufnr, ctx)
end

return Matcher
4 changes: 2 additions & 2 deletions lua/gesture/view/board.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

local hl_groups = require("gesture.view.highlight_group")

function GestureBoard.create(inputs, gesture, has_forward_match, show_board)
function GestureBoard.create(inputs, gesture, can_match, show_board)
if #inputs == 0 or not show_board then
return {}
end
Expand All @@ -38,7 +38,7 @@ function GestureBoard.create(inputs, gesture, has_forward_match, show_board)
local end_col = math.min(center + half_width, editor_width)

local hl_group = hl_groups.GestureInput
if not has_forward_match then
if not can_match then
hl_group = hl_groups.GestureInputNotMatched
end

Expand Down
4 changes: 2 additions & 2 deletions lua/gesture/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function View.open(winblend)
return setmetatable(tbl, View), window_id
end

function View.render_input(self, inputs, gesture, has_forward_match, show_board)
local board_range_map = GestureBoard.create(inputs, gesture, has_forward_match, show_board)
function View.render_input(self, inputs, gesture, can_match, show_board)
local board_range_map = GestureBoard.create(inputs, gesture, can_match, show_board)
self._canvas:draw(board_range_map, self._new_points)
end

Expand Down

0 comments on commit 65ab98d

Please sign in to comment.