diff --git a/README.md b/README.md index 3beae20..64b8fd8 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Goals: - Works with or without nix - LSP integration with the current project's settings if available -Old pre nix config: https://github.com/RingOfStorms/nvim/tree/40eadc9 for reference +Old pre nix config: https://git.joshuabell.xyz/nvim/~files/40eadc9b714fa29c5b28aca49f77c1ea62141763 for reference ## Running diff --git a/lua/tools/scratch-files.lua b/lua/tools/scratch-files.lua new file mode 100644 index 0000000..7a63eba --- /dev/null +++ b/lua/tools/scratch-files.lua @@ -0,0 +1,131 @@ +local extensions = { + "json", + "sql", + "html", + "txt", + "md", + "lua", + "css", + "http", + "js", + "ts", + "sh", + "vim", + "yaml", + "toml", + "conf", + "nix", + "rs", + "py", + "go", + "rb", + "java", + "c", + "cpp", + "h", + "hpp", + "cs", + "php", + "pl", + "r", + "swift", + "kt", + "clj", + "cljs", + "hs", + "elm", + "erl", + "ex", + "exs", + "scala", + "groovy", + "dart", + "ts", + "tsx", + "jsx", + "rs", +} + +local xdg_data_home = os.getenv("XDG_DATA_HOME") or (os.getenv("HOME") .. "/.local/share") +local scratches_dir = xdg_data_home .. "/scratches" + +-- Function to create a scratch file +local function create_scratch_file(extension) + vim.fn.mkdir(scratches_dir, "p") + + local date = os.date("%Y_%m_%d_%H_%M_%S") + local filename = string.format("%s/%s.%s", scratches_dir, date, extension) + vim.cmd("edit " .. filename) +end + +-- Function to prompt for file extension +local function prompt_for_extension() + U.safeRequire("telescope", function() + local pickers = require("telescope.pickers") + local finders = require("telescope.finders") + local sorters = require("telescope.config").values.generic_sorter + local actions = require("telescope.actions") + local action_state = require("telescope.actions.state") + + pickers + .new({ + layout_strategy = "vertical", + layout_config = { + width = 0.3, + height = 0.4, + prompt_position = "top", + }, + }, { + prompt_title = "Choose or enter file extension", + finder = finders.new_table({ + results = extensions, + entry_maker = function(entry) + return { + value = entry, + display = entry, + ordinal = entry, + } + end, + }), + sorter = sorters({}), + attach_mappings = function(prompt_bufnr, map) + local function on_select() + local selection = action_state.get_selected_entry() + actions.close(prompt_bufnr) + create_scratch_file(selection.value) + end + + map("i", "", on_select) + map("n", "", on_select) + return true + end, + }) + :find() + end, function() + local extension = vim.fn.input("Enter file extension: ") + create_scratch_file(extension) + end) +end + +-- Set up the keymap +U.keymaps({ + { + "fsw", + function() + require("telescope.builtin").live_grep({ + search_dirs = { scratches_dir }, + }) + end, + desc = "Find Words in Scratches", + }, + { + "fss", + function() + require("telescope.builtin").find_files({ + search_dirs = { scratches_dir }, + }) + end, + desc = "Find Scratches", + }, + { "ss", prompt_for_extension, mode = { "n", "v", "x" }, desc = "Create a scratch file" }, +})