Files
telescope.nvim/lua/telescope/previewers/utils.lua
Simon Hauser 1d40ab5ccd feat: All buffer previewers are now async and more config options (#354)
Configure preview window with:
autocmd User TelescopePreviewerLoaded setlocal wrap
autocmd User TelescopePreviewerLoaded setlocal number

file_maker example: Use regex highlighting for certain filetype like `*min.js` because they slow
down things with treesitter highlighter. Just a snippet for tests. We will do an extension :)

local previewers = require('telescope.previewers')
local putils = require('telescope.previewers.utils')
local pfiletype = require('plenary.filetype')

local _bad = { '.*%.min%.js' }
local bad_files = function(filepath)
  for _, v in ipairs(_bad) do
    if filepath:match(v) then
      return true
    end
  end

  return false
end

local new_maker = function(filepath, bufnr, bufname, use_ft_detect, callback)
  if use_ft_detect == nil then use_ft_detect = true end

  if bad_files(filepath) then
    previewers.buffer_previewer_maker(filepath, bufnr, bufname, false, callback)
    local ft = pfiletype.detect(filepath)
    putils.regex_highlighter(bufnr, ft)
  else
    previewers.buffer_previewer_maker(filepath, bufnr, bufname, use_ft_detect, callback)
  end
end

require('telescope').setup {
  defaults = {
    buffer_previewer_maker = new_maker,
  }
}
2020-12-29 21:05:59 +01:00

70 lines
1.9 KiB
Lua

local context_manager = require('plenary.context_manager')
local has_ts, _ = pcall(require, 'nvim-treesitter')
local _, ts_highlight = pcall(require, 'nvim-treesitter.highlight')
local _, ts_parsers = pcall(require, 'nvim-treesitter.parsers')
local Job = require('plenary.job')
local utils = {}
utils.with_preview_window = function(status, bufnr, callable)
if bufnr and vim.api.nvim_buf_call and false then
vim.api.nvim_buf_call(bufnr, callable)
else
return context_manager.with(function()
vim.cmd(string.format("noautocmd call nvim_set_current_win(%s)", status.preview_win))
coroutine.yield()
vim.cmd(string.format("noautocmd call nvim_set_current_win(%s)", status.prompt_win))
end, callable)
end
end
-- API helper functions for buffer previewer
--- Job maker for buffer previewer
utils.job_maker = function(cmd, env, value, bufnr, bufname, callback)
if bufname ~= value then
local command = table.remove(cmd, 1)
Job:new({
command = command,
args = cmd,
env = env,
on_exit = vim.schedule_wrap(function(j)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, j:result())
if callback then callback(bufnr, j:result()) end
end)
}):start()
else
if callback then callback(bufnr) end
end
end
--- Attach default highlighter which will choose between regex and ts
utils.highlighter = function(bufnr, ft)
if ft and ft ~= '' then
if has_ts and ts_parsers.has_parser(ft) then
ts_highlight.attach(bufnr, ft)
else
vim.cmd(':ownsyntax ' .. ft)
end
end
end
--- Attach regex highlighter
utils.regex_highlighter = function(_, ft)
if ft and ft ~= '' then
vim.cmd(':ownsyntax ' .. ft)
end
end
-- Attach ts highlighter
utils.ts_highlighter = function(bufnr, ft)
if ft and ft ~= '' then
if has_ts and ts_parsers.has_parser(ft) then
ts_highlight.attach(bufnr, ft)
end
end
end
return utils