From b397b0f2191778bd642e9e8326c119f3f84ab508 Mon Sep 17 00:00:00 2001 From: Lintao Date: Sat, 10 Aug 2024 21:02:12 +0800 Subject: [PATCH] Add file picker options for fzf-lua and Telescope in scratch.nvim --- README.md | 12 ++++++++++-- lua/scratch/api.lua | 16 +++++++++++++++- lua/scratch/config.lua | 5 +++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a605a03..90d7f30 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,19 @@ Create temporary playground files effortlessly. Find them later without worrying ```lua return { "LintaoAmons/scratch.nvim", + event = "VeryLazy", + dependencies = { + {"ibhagwan/fzf-lua"}, --optional: if you want to use fzf-lua to pick scratch file. Recommanded, since it will order the files by modification datetime desc. (require rg) + {"nvim-telescope/telescope.nvim"}, -- optional: if you want to pick scratch file by telescope + {"stevearc/dressing.nvim"} -- optional: to have the same UI shown in the GIF + } config = function() require("scratch").setup({ scratch_file_dir = vim.fn.stdpath("cache") .. "/scratch.nvim", -- where your scratch files will be put + window_cmd = "rightbelow vsplit", -- 'vsplit' | 'split' | 'edit' | 'tabedit' | 'rightbelow vsplit' + use_telescope = true, + -- fzf-lua is recommanded, since it will order the files by modification datetime desc. (require rg) + file_picker = "fzflua", -- "fzflua" | "telescope" | nil filetypes = { "lua", "js", "sh", "ts" }, -- you can simply put filetype here filetype_details = { -- or, you can have more control here json = {}, -- empty table is fine @@ -42,8 +52,6 @@ return { }, }, }, - window_cmd = "rightbelow vsplit", -- 'vsplit' | 'split' | 'edit' | 'tabedit' | 'rightbelow vsplit' - use_telescope = true, localKeys = { { filenameContains = { "sh" }, diff --git a/lua/scratch/api.lua b/lua/scratch/api.lua index 93ff174..a280681 100644 --- a/lua/scratch/api.lua +++ b/lua/scratch/api.lua @@ -162,6 +162,18 @@ local function scratchWithName() end) end +local function open_scratch_fzflua() + local ok, fzf_lua = pcall(require, "fzf-lua") + if not ok then + utils.log_err("Can't find fzf-lua, please check your configuration") + end + + if vim.fn.executable("rg") ~= 1 then + utils.log_err("Can't find rg executable, please check your configuration") + end + fzf_lua.files({ cmd = "rg --files --sortr modified " .. vim.g.scratch_config.scratch_file_dir }) +end + local function open_scratch_telescope() local config_data = vim.g.scratch_config @@ -213,8 +225,10 @@ end local function openScratch() local config_data = vim.g.scratch_config - if config_data.use_telescope then + if config_data.file_picker == "telescope" then open_scratch_telescope() + elseif config_data.file_picker == "fzflua" then + open_scratch_fzflua() else open_scratch_vim_ui() end diff --git a/lua/scratch/config.lua b/lua/scratch/config.lua index f939e55..1dbcae8 100644 --- a/lua/scratch/config.lua +++ b/lua/scratch/config.lua @@ -40,18 +40,19 @@ local utils = require("scratch.utils") ---@field scratch_file_dir string ---@field filetypes string[] ---@field window_cmd string ----@field use_telescope boolean +---@field file_picker? "fzflua" | "telescope" | nil ---@field filetype_details Scratch.FiletypeDetails ---@field localKeys Scratch.LocalKeyConfig[] local default_config = { scratch_file_dir = vim.fn.stdpath("cache") .. slash .. "scratch.nvim", -- where your scratch files will be put filetypes = { "lua", "js", "py", "sh" }, -- you can simply put filetype here window_cmd = "edit", -- 'vsplit' | 'split' | 'edit' | 'tabedit' | 'rightbelow vsplit' - use_telescope = true, + file_picker = "fzflua", filetype_details = {}, localKeys = {}, } +---@type Scratch.Config vim.g.scratch_config = default_config ---@param user_config? Scratch.Config