irongit

first commit

huncholanehuncholaneauthored
commit b396ec28919158cf49fbd55a57c124fd3e77b58dBrowse files

4 files changed, +79 -0

+4-0lua/scratch/commands.lua
@@ -0,0 +1,4 @@
1+local scratch = require("scratch.scratch")
2+vim.api.nvim_create_user_command("Scratch", function()
3+ scratch.relative_scratch()
4+end, {})
+8-0lua/scratch/init.lua
@@ -0,0 +1,8 @@
1+local M = {}
2+
3+M.setup = function()
4+ require("scratch.commands")
5+ require("scratch.scratch").load()
6+end
7+
8+return M
+66-0lua/scratch/scratch.lua
@@ -0,0 +1,66 @@
1+local M = {}
2+
3+M.files = {}
4+
5+function M.load()
6+ local cwd = vim.fn.getcwd()
7+ M.hash = vim.fn.sha256(cwd)
8+ M.scratchdir = vim.fn.stdpath("data") .. "/scratch"
9+ if vim.fn.isdirectory(M.scratchdir) == 0 then
10+ vim.fn.mkdir(M.scratchdir, "p")
11+ return
12+ end
13+end
14+
15+---Gets the name of the file that lives in the scratch directory
16+---@param ext string The file extension
17+---@return string
18+function M.getfile(ext)
19+ return M.scratchdir .. "/" .. M.hash .. "." .. ext
20+end
21+
22+---Opens a buffer for the given file extension
23+---@param ext string
24+function M.scratch(ext)
25+ -- open scratch for the ext
26+ local filename = M.getfile(ext)
27+ local buf = vim.api.nvim_create_buf(false, true)
28+
29+ -- immediately move into floating window
30+ local width = math.floor(vim.o.columns * 0.8)
31+ local height = math.floor(vim.o.lines * 0.8)
32+ local row = math.floor((vim.o.lines - height) / 2)
33+ local col = math.floor((vim.o.columns - width) / 2)
34+ vim.api.nvim_open_win(buf, true, {
35+ relative = "editor",
36+ width = width,
37+ height = height,
38+ row = row,
39+ col = col,
40+ style = "minimal",
41+ border = "rounded",
42+ })
43+ vim.cmd("edit " .. filename)
44+
45+ -- mark hidden
46+ vim.bo[buf].bufhidden = "wipe"
47+ vim.bo[buf].buflisted = false
48+
49+ -- easy close
50+ vim.keymap.set("n", "q", ":q!<cr>", { buffer = buf, nowait = true })
51+ vim.keymap.set("n", "<C-c>", ":q!<cr>", { buffer = buf, nowait = true })
52+
53+ -- execute on save
54+ vim.api.nvim_create_autocmd("BufWritePost", {
55+ buffer = buf,
56+ callback = function()
57+ vim.cmd("luafile %")
58+ end,
59+ })
60+end
61+
62+function M.relative_scratch()
63+ M.scratch(vim.fn.expand("%:e"))
64+end
65+
66+return M
+1-0scratch.nvim
@@ -0,0 +1 @@
1+/home/huncho/code/nvim-plugins/scratch.nvim