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

feat(scope): allow filtering patterns when updating cache on autocmd events #84

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,11 @@ require("grapple.scope").resolver(function()
return vim.fn.getcwd()
end, { cache = "DirChanged" })

-- You can even filter for patterns!
require("grapple.scope").resolver(function()
return vim.fn.getcwd()
end, { cache = "User PluginEvent"})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having to parse the cache it could be a table like so?

cache = {
    event = "User",
    pattern = "PluginEvent"


-- Create an scope resolver that asynchronously runs the "echo"
-- shell command and uses its output as the resolved scope
require("grapple.scope").resolver({
Expand Down
37 changes: 35 additions & 2 deletions lua/grapple/scope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,38 @@ local function is_async(scope_resolver)
end

---@private
---@param scope_resolver Grapple.ScopeResolver
---@param cache string | string[]
---@return string[], string[]
local function parse_events(cache)
local events = {}
local patterns = {}

local parse_event = function(event_string)
local words = {}
for word in event_string:gmatch("%S+") do
table.insert(words, word)
end

local event = table.remove(words, 1)
table.insert(events, event)
for _, pattern in ipairs(words) do
table.insert(patterns, pattern)
end
end

if type(cache) == "string" then
parse_event(cache)
elseif type(cache) == "table" then
for _, event_string in ipairs(cache) do
parse_event(event_string)
end
end

return events, patterns
end

---@private
---@param scope_resolver
---@return Grapple.ScopeResolver
local function update_watch(scope_resolver)
if scope_resolver.watch.type == watch_type.basic then
Expand All @@ -79,9 +110,11 @@ local function update_watch(scope_resolver)
goto fallthrough
end

local events, patterns = parse_events(scope_resolver.watch.events)
local group = vim.api.nvim_create_augroup("GrappleScope", { clear = false })
scope_resolver.watch.autocmd = vim.api.nvim_create_autocmd(scope_resolver.watch.events, {
scope_resolver.watch.autocmd = vim.api.nvim_create_autocmd(events, {
group = group,
pattern = patterns,
callback = function()
if is_async(scope_resolver) then
scope.update(scope_resolver)
Expand Down