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

Add filter to only autosave specified file types #30

Merged
merged 1 commit into from
Jul 30, 2022
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ These are the conditions that every file must meet so that it can be saved. If e
+ `modifiable`: (Boolean) if true, enables this condition. If the file isn't modifiable, then this condition isn't met.
+ `filename_is_not`: (Table, Strings) if there is one or more filenames (should be strings) in the table, it enables this condition. Use this to exclude filenames that you don't want to automatically save.
+ `filetype_is_not`: (Table, Strings) if there is one or more filetypes (should be strings) in the table, it enables this condition. Use this to exclude filetypes that you don't want to automatically save.
+ `filetype_is`: (Table, Strings) if there is one or more filetypes (should be strings) in the table, it enables this condition. Use this to restrict the filetypes that are autosaved to only those specified.

## Hooks
Use them to execute code at certain events [described by their names]. These are the ones available:
Expand Down
1 change: 1 addition & 0 deletions lua/autosave/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ config.options = {
exists = true,
filename_is_not = {},
filetype_is_not = {},
filetype_is = {},
modifiable = true,
},
write_all_buffers = false,
Expand Down
13 changes: 10 additions & 3 deletions lua/autosave/modules/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ local function actual_save()
end

local function assert_user_conditions()
local sc_exists, sc_filename, sc_filetype, sc_modifiable = true, true, true, true
local sc_exists, sc_filename, sc_filetype_is_not, sc_filetype_is, sc_modifiable = true, true, true, true, true

for condition, value in pairs(opts["conditions"]) do
if condition == "exists" then
Expand All @@ -80,14 +80,21 @@ local function assert_user_conditions()
elseif condition == "filetype_is_not" then
if not (next(opts["conditions"]["filetype_is_not"]) == nil) then
if table_has_value(opts["conditions"]["filetype_is_not"], api.nvim_eval([[&filetype]])) == true then
sc_filetype = false
sc_filetype_is_not = false
break
end
end
elseif condition == "filetype_is" then
if not (next(opts["conditions"]["filetype_is"]) == nil) then
if table_has_value(opts["conditions"]["filetype_is"], api.nvim_eval([[&filetype]])) == false then
sc_filetype_is = false
break
end
end
end
end

return { sc_exists, sc_filename, sc_filetype, sc_modifiable }
return { sc_exists, sc_filename, sc_filetype_is_not, sc_filetype_is, sc_modifiable }
end

local function assert_return(values, expected)
Expand Down