cleanup: remove disclaimer before saving files

Oil has been around for a few months now and there haven't been any
reports of unexpected filesystem operations.
This commit is contained in:
Steven Arcangeli 2023-05-07 09:08:42 -07:00
parent 6b05c2e913
commit d0efcc0c10
4 changed files with 22 additions and 245 deletions

View file

@ -1,75 +0,0 @@
local config = require("oil.config")
local fs = require("oil.fs")
local ReplLayout = require("oil.repl_layout")
local M = {}
M.show = function(callback)
if config.silence_disclaimer then
return callback(true)
end
local marker_file = fs.join(vim.fn.stdpath("cache"), ".oil_accepted_disclaimer")
vim.loop.fs_stat(
marker_file,
vim.schedule_wrap(function(err, stat)
if stat and stat.type and not err then
callback(true)
return
end
local confirmation = "I understand this may destroy my files"
local lines = {
"WARNING",
"This plugin has been tested thoroughly, but it is still new.",
"There is a chance that there may be bugs that could lead to data loss.",
"I recommend that you ONLY use it for files that are checked in to version control.",
"",
string.format('Please type: "%s" below', confirmation),
"",
}
local hints = {
"Try again",
"Not quite!",
"It's right there ^^^^^^^^^^^",
"...seriously?",
"Just type this ^^^^",
}
local attempt = 0
local repl
repl = ReplLayout.new({
lines = lines,
on_submit = function(line)
if line:upper() ~= confirmation:upper() then
attempt = attempt % #hints + 1
vim.api.nvim_buf_set_lines(repl.input_bufnr, 0, -1, true, {})
vim.bo[repl.view_bufnr].modifiable = true
vim.api.nvim_buf_set_lines(repl.view_bufnr, 6, 7, true, { hints[attempt] })
vim.bo[repl.view_bufnr].modifiable = false
vim.bo[repl.view_bufnr].modified = false
else
fs.mkdirp(vim.fn.fnamemodify(marker_file, ":h"))
fs.touch(
marker_file,
vim.schedule_wrap(function(err2)
if err2 then
vim.notify(
string.format("Error recording response: %s", err2),
vim.log.levels.WARN
)
end
callback(true)
repl:close()
end)
)
end
end,
on_cancel = function()
callback(false)
end,
})
local ns = vim.api.nvim_create_namespace("Oil")
vim.api.nvim_buf_add_highlight(repl.view_bufnr, ns, "DiagnosticError", 0, 0, -1)
end)
)
end
return M

View file

@ -1,7 +1,6 @@
local cache = require("oil.cache")
local columns = require("oil.columns")
local config = require("oil.config")
local disclaimer = require("oil.mutator.disclaimer")
local oil = require("oil")
local parser = require("oil.mutator.parser")
local pathutil = require("oil.pathutil")
@ -505,38 +504,32 @@ M.try_write_changes = function(confirm)
end
local actions = M.create_actions_from_diffs(all_diffs)
-- TODO(2023-06-01) If no one has reported data loss by this time, we can remove the disclaimer
disclaimer.show(function(disclaimed)
if not disclaimed then
preview.show(actions, confirm, function(proceed)
if not proceed then
return unlock()
end
preview.show(actions, confirm, function(proceed)
if not proceed then
return unlock()
end
M.process_actions(
actions,
vim.schedule_wrap(function(err)
view.unlock_buffers()
if err then
vim.notify(string.format("[oil] Error applying actions: %s", err), vim.log.levels.ERROR)
view.rerender_all_oil_buffers({ preserve_undo = false })
else
local current_entry = oil.get_cursor_entry()
if current_entry then
-- get the entry under the cursor and make sure the cursor stays on it
view.set_last_cursor(
vim.api.nvim_buf_get_name(0),
vim.split(current_entry.parsed_name or current_entry.name, "/")[1]
)
end
view.rerender_all_oil_buffers({ preserve_undo = M.trash })
M.process_actions(
actions,
vim.schedule_wrap(function(err)
view.unlock_buffers()
if err then
vim.notify(string.format("[oil] Error applying actions: %s", err), vim.log.levels.ERROR)
view.rerender_all_oil_buffers({ preserve_undo = false })
else
local current_entry = oil.get_cursor_entry()
if current_entry then
-- get the entry under the cursor and make sure the cursor stays on it
view.set_last_cursor(
vim.api.nvim_buf_get_name(0),
vim.split(current_entry.parsed_name or current_entry.name, "/")[1]
)
end
mutation_in_progress = false
end)
)
end)
view.rerender_all_oil_buffers({ preserve_undo = M.trash })
end
mutation_in_progress = false
end)
)
end)
end

View file

@ -1,140 +0,0 @@
local layout = require("oil.layout")
local ReplLayout = {}
---@param opts table
--- min_height integer
--- min_width integer
--- lines string[]
--- on_submit fun(text: string): boolean
--- on_cancel nil|fun()
ReplLayout.new = function(opts)
opts = vim.tbl_deep_extend("keep", opts or {}, {
min_height = 10,
min_width = 120,
})
vim.validate({
lines = { opts.lines, "t" },
min_height = { opts.min_height, "n" },
min_width = { opts.min_width, "n" },
on_submit = { opts.on_submit, "f" },
on_cancel = { opts.on_cancel, "f", true },
})
local total_height = layout.get_editor_height()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].bufhidden = "wipe"
local width = math.min(opts.min_width, vim.o.columns - 2)
local height = math.min(opts.min_height, total_height - 3)
local row = math.floor((total_height - height) / 2)
local col = math.floor((vim.o.columns - width) / 2)
local view_winid = vim.api.nvim_open_win(bufnr, false, {
relative = "editor",
width = width,
height = height,
row = row,
col = col,
style = "minimal",
border = "rounded",
focusable = false,
})
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, opts.lines)
vim.bo[bufnr].modified = false
vim.bo[bufnr].modifiable = false
vim.api.nvim_win_set_cursor(view_winid, { #opts.lines, 0 })
local input_bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[input_bufnr].bufhidden = "wipe"
local input_winid = vim.api.nvim_open_win(input_bufnr, true, {
relative = "editor",
width = width,
height = 1,
row = row + height + 2,
col = col,
style = "minimal",
border = "rounded",
})
vim.api.nvim_create_autocmd("WinClosed", {
desc = "Close oil repl window when text input closes",
pattern = tostring(input_winid),
callback = function()
if view_winid and vim.api.nvim_win_is_valid(view_winid) then
vim.api.nvim_win_close(view_winid, true)
end
end,
once = true,
nested = true,
})
local self = setmetatable({
input_bufnr = input_bufnr,
view_bufnr = bufnr,
input_winid = input_winid,
view_winid = view_winid,
_cancel = nil,
_submit = nil,
}, {
__index = ReplLayout,
})
self._cancel = function()
self:close()
if opts.on_cancel then
opts.on_cancel()
end
end
self._submit = function()
local line = vim.trim(vim.api.nvim_buf_get_lines(input_bufnr, 0, 1, true)[1])
if line == "" then
return
end
if not opts.on_submit(line) then
vim.api.nvim_buf_set_lines(input_bufnr, 0, -1, true, {})
vim.bo[input_bufnr].modified = false
end
end
local cancel = function()
self._cancel()
end
vim.api.nvim_create_autocmd("BufLeave", {
callback = cancel,
once = true,
nested = true,
buffer = input_bufnr,
})
vim.api.nvim_create_autocmd("WinLeave", {
callback = cancel,
once = true,
nested = true,
})
vim.keymap.set("n", "<Esc>", cancel, { buffer = input_bufnr })
vim.keymap.set({ "n", "i" }, "<C-c>", cancel, { buffer = input_bufnr })
vim.keymap.set({ "n", "i" }, "<CR>", function()
self._submit()
end, { buffer = input_bufnr })
vim.cmd.startinsert()
return self
end
function ReplLayout:append_view_lines(lines)
local bufnr = self.view_bufnr
local num_lines = vim.api.nvim_buf_line_count(bufnr)
local last_line = vim.api.nvim_buf_get_lines(bufnr, num_lines - 1, num_lines, true)[1]
lines[1] = last_line .. lines[1]
for i, v in ipairs(lines) do
lines[i] = v:gsub("\r$", "")
end
vim.bo[bufnr].modifiable = true
vim.api.nvim_buf_set_lines(bufnr, num_lines - 1, -1, true, lines)
vim.bo[bufnr].modifiable = false
vim.bo[bufnr].modified = false
vim.api.nvim_win_set_cursor(self.view_winid, { num_lines + #lines - 1, 0 })
end
function ReplLayout:close()
self._submit = function() end
self._cancel = function() end
vim.cmd.stopinsert()
vim.api.nvim_win_close(self.input_winid, true)
end
return ReplLayout

View file

@ -8,7 +8,6 @@ M.reset_editor = function()
adapters = {
["oil-test://"] = "test",
},
silence_disclaimer = true,
prompt_save_on_select_new_entry = false,
})
vim.cmd.tabonly({ mods = { silent = true } })