Skip to content

Commit

Permalink
Add file picker options for fzf-lua and Telescope in scratch.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
LintaoAmons committed Aug 10, 2024
1 parent 0c3badb commit b397b0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,8 +52,6 @@ return {
},
},
},
window_cmd = "rightbelow vsplit", -- 'vsplit' | 'split' | 'edit' | 'tabedit' | 'rightbelow vsplit'
use_telescope = true,
localKeys = {
{
filenameContains = { "sh" },
Expand Down
16 changes: 15 additions & 1 deletion lua/scratch/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lua/scratch/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b397b0f

Please sign in to comment.