From 9c5a1b12ef3223b95281ed6b92c019ce7f7a4302 Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Sat, 12 Aug 2023 22:35:06 +0800 Subject: [PATCH] feat: neovim - fix performance issue of telescope preview --- .../desktop/neovim/astronvim_user/init.lua | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/home/base/desktop/neovim/astronvim_user/init.lua b/home/base/desktop/neovim/astronvim_user/init.lua index 1f163b7b..e8090150 100644 --- a/home/base/desktop/neovim/astronvim_user/init.lua +++ b/home/base/desktop/neovim/astronvim_user/init.lua @@ -271,6 +271,52 @@ return { }) end }, + + { + 'nvim-telescope/telescope.nvim', branch = '0.1.x', + dependencies = { 'nvim-lua/plenary.nvim' }, + init = function() + -- 1. Disable highlighting for certain filetypes + -- 2. Ignore files larger than a certain filesize + local previewers = require("telescope.previewers") + + local _bad = { ".*%.csv", ".*%.min.js" } -- Put all filetypes that slow you down in this array + local filesize_threshold = 300 * 1024 -- 300KB + local bad_files = function(filepath) + for _, v in ipairs(_bad) do + if filepath:match(v) then + return false + end + end + return true + end + + local new_maker = function(filepath, bufnr, opts) + opts = opts or {} + if opts.use_ft_detect == nil then opts.use_ft_detect = true end + + -- 1. Check if the file is in the bad_files array, and if so, don't highlight it + opts.use_ft_detect = opts.use_ft_detect == false and false or bad_files(filepath) + + -- 2. Check the file size, and ignore it if it's too big(preview nothing). + filepath = vim.fn.expand(filepath) + vim.loop.fs_stat(filepath, function(_, stat) + if not stat then return end + if stat.size > filesize_threshold then + return + else + previewers.buffer_previewer_maker(filepath, bufnr, opts) + end + end) + end + + require("telescope").setup { + defaults = { + buffer_previewer_maker = new_maker, + } + } + end, + } }, -- Configure require("lazy").setup() options