chore: use stylua for formatting (#1040)

* chore: stylua job and config

* reformat with stylua
This commit is contained in:
Simon Hauser
2021-07-23 17:42:37 +02:00
committed by GitHub
parent 664690029f
commit 79644ab677
75 changed files with 3201 additions and 2809 deletions

View File

@@ -1,45 +1,45 @@
local from_entry = require('telescope.from_entry')
local Path = require('plenary.path')
local utils = require('telescope.utils')
local putils = require('telescope.previewers.utils')
local Previewer = require('telescope.previewers.previewer')
local conf = require('telescope.config').values
local from_entry = require "telescope.from_entry"
local Path = require "plenary.path"
local utils = require "telescope.utils"
local putils = require "telescope.previewers.utils"
local Previewer = require "telescope.previewers.previewer"
local conf = require("telescope.config").values
local pfiletype = require('plenary.filetype')
local pscan = require('plenary.scandir')
local pfiletype = require "plenary.filetype"
local pscan = require "plenary.scandir"
local buf_delete = utils.buf_delete
local defaulter = utils.make_default_callable
local previewers = {}
local ns_previewer = vim.api.nvim_create_namespace('telescope.previewers')
local ns_previewer = vim.api.nvim_create_namespace "telescope.previewers"
local color_hash = {
['p'] = 'TelescopePreviewPipe',
['c'] = 'TelescopePreviewCharDev',
['d'] = 'TelescopePreviewDirectory',
['b'] = 'TelescopePreviewBlock',
['l'] = 'TelescopePreviewLink',
['s'] = 'TelescopePreviewSocket',
['.'] = 'TelescopePreviewNormal',
['r'] = 'TelescopePreviewRead',
['w'] = 'TelescopePreviewWrite',
['x'] = 'TelescopePreviewExecute',
['-'] = 'TelescopePreviewHyphen',
['T'] = 'TelescopePreviewSticky',
['S'] = 'TelescopePreviewSticky',
[2] = 'TelescopePreviewSize',
[3] = 'TelescopePreviewUser',
[4] = 'TelescopePreviewGroup',
[5] = 'TelescopePreviewDate',
["p"] = "TelescopePreviewPipe",
["c"] = "TelescopePreviewCharDev",
["d"] = "TelescopePreviewDirectory",
["b"] = "TelescopePreviewBlock",
["l"] = "TelescopePreviewLink",
["s"] = "TelescopePreviewSocket",
["."] = "TelescopePreviewNormal",
["r"] = "TelescopePreviewRead",
["w"] = "TelescopePreviewWrite",
["x"] = "TelescopePreviewExecute",
["-"] = "TelescopePreviewHyphen",
["T"] = "TelescopePreviewSticky",
["S"] = "TelescopePreviewSticky",
[2] = "TelescopePreviewSize",
[3] = "TelescopePreviewUser",
[4] = "TelescopePreviewGroup",
[5] = "TelescopePreviewDate",
}
color_hash[6] = function(line)
color_hash[6] = function(line)
return color_hash[line:sub(1, 1)]
end
local colorize_ls = function(bufnr, data, sections)
local windows_add = Path.path.sep == '\\' and 2 or 0
local windows_add = Path.path.sep == "\\" and 2 or 0
for lnum, line in ipairs(data) do
local section = sections[lnum]
for i = 1, section[1].end_index - 1 do -- Highlight permissions
@@ -48,7 +48,8 @@ local colorize_ls = function(bufnr, data, sections)
end
for i = 2, #section do -- highlights size, (user, group), date and name
local hl_group = color_hash[i + (i ~= 2 and windows_add or 0)]
vim.api.nvim_buf_add_highlight(bufnr,
vim.api.nvim_buf_add_highlight(
bufnr,
ns_previewer,
type(hl_group) == "function" and hl_group(line) or hl_group,
lnum - 1,
@@ -60,14 +61,16 @@ local colorize_ls = function(bufnr, data, sections)
end
local search_cb_jump = function(self, bufnr, query)
if not query then return end
if not query then
return
end
vim.api.nvim_buf_call(bufnr, function()
pcall(vim.fn.matchdelete, self.state.hl_id, self.state.winid)
vim.cmd "norm! gg"
vim.fn.search(query, "W")
vim.cmd "norm! zz"
self.state.hl_id = vim.fn.matchadd('TelescopePreviewMatch', query)
self.state.hl_id = vim.fn.matchadd("TelescopePreviewMatch", query)
end)
end
@@ -80,29 +83,44 @@ end
previewers.file_maker = function(filepath, bufnr, opts)
opts = opts or {}
if opts.use_ft_detect == nil then opts.use_ft_detect = true end
if opts.use_ft_detect == nil then
opts.use_ft_detect = true
end
local ft = opts.use_ft_detect and pfiletype.detect(filepath)
if opts.bufname ~= filepath then
if not vim.in_fast_event() then filepath = vim.fn.expand(filepath) end
if not vim.in_fast_event() then
filepath = vim.fn.expand(filepath)
end
vim.loop.fs_stat(filepath, function(_, stat)
if not stat then return end
if stat.type == 'directory' then
if not stat then
return
end
if stat.type == "directory" then
pscan.ls_async(filepath, {
hidden = true,
group_directories_first = true,
on_exit = vim.schedule_wrap(function(data, sections)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, data)
colorize_ls(bufnr, data, sections)
if opts.callback then opts.callback(bufnr) end
end)})
if opts.callback then
opts.callback(bufnr)
end
end),
})
else
Path:new(filepath):_read_async(vim.schedule_wrap(function(data)
if not vim.api.nvim_buf_is_valid(bufnr) then return end
local ok = pcall(vim.api.nvim_buf_set_lines, bufnr, 0, -1, false, vim.split(data, '[\r]?\n'))
if not ok then return end
if not vim.api.nvim_buf_is_valid(bufnr) then
return
end
local ok = pcall(vim.api.nvim_buf_set_lines, bufnr, 0, -1, false, vim.split(data, "[\r]?\n"))
if not ok then
return
end
if opts.callback then opts.callback(bufnr) end
if opts.callback then
opts.callback(bufnr)
end
putils.highlighter(bufnr, ft)
end))
end
@@ -110,7 +128,9 @@ previewers.file_maker = function(filepath, bufnr, opts)
else
if opts.callback then
if vim.in_fast_event() then
vim.schedule(function() opts.callback(bufnr) end)
vim.schedule(function()
opts.callback(bufnr)
end)
else
opts.callback(bufnr)
end
@@ -132,11 +152,13 @@ previewers.new_buffer_previewer = function(opts)
local old_bufs = {}
local bufname_table = {}
local global_state = require'telescope.state'
local global_state = require "telescope.state"
local preview_window_id
local function get_bufnr(self)
if not self.state then return nil end
if not self.state then
return nil
end
return self.state.bufnr
end
@@ -148,7 +170,9 @@ previewers.new_buffer_previewer = function(opts)
end
local function get_bufnr_by_bufname(self, value)
if not self.state then return nil end
if not self.state then
return nil
end
return bufname_table[value]
end
@@ -163,7 +187,7 @@ previewers.new_buffer_previewer = function(opts)
function opts.title(self)
if opt_title then
if type(opt_title) == 'function' then
if type(opt_title) == "function" then
return opt_title(self)
else
return opt_title
@@ -181,7 +205,9 @@ previewers.new_buffer_previewer = function(opts)
function opts.setup(self)
local state = {}
if opt_setup then vim.tbl_deep_extend("force", state, opt_setup(self)) end
if opt_setup then
vim.tbl_deep_extend("force", state, opt_setup(self))
end
return state
end
@@ -192,7 +218,7 @@ previewers.new_buffer_previewer = function(opts)
local last_nr
if opts.keep_last_buf then
last_nr = global_state.get_global_key('last_preview_bufnr')
last_nr = global_state.get_global_key "last_preview_bufnr"
-- Push in another buffer so the last one will not be cleaned up
if preview_window_id then
local bufnr = vim.api.nvim_create_buf(false, true)
@@ -231,21 +257,23 @@ previewers.new_buffer_previewer = function(opts)
end)
-- TODO(conni2461): We only have to set options once. Right?
vim.api.nvim_win_set_option(status.preview_win, 'winhl', 'Normal:TelescopePreviewNormal')
vim.api.nvim_win_set_option(status.preview_win, 'signcolumn', 'no')
vim.api.nvim_win_set_option(status.preview_win, 'foldlevel', 100)
vim.api.nvim_win_set_option(status.preview_win, 'wrap', false)
vim.api.nvim_win_set_option(status.preview_win, "winhl", "Normal:TelescopePreviewNormal")
vim.api.nvim_win_set_option(status.preview_win, "signcolumn", "no")
vim.api.nvim_win_set_option(status.preview_win, "foldlevel", 100)
vim.api.nvim_win_set_option(status.preview_win, "wrap", false)
self.state.winid = status.preview_win
self.state.bufname = nil
end
if opts.keep_last_buf then global_state.set_global_key("last_preview_bufnr", self.state.bufnr) end
if opts.keep_last_buf then
global_state.set_global_key("last_preview_bufnr", self.state.bufnr)
end
opts.define_preview(self, entry, status)
putils.with_preview_window(status, nil, function()
vim.cmd'do User TelescopePreviewerLoaded'
vim.cmd "do User TelescopePreviewerLoaded"
end)
if opts.get_buffer_by_name then
@@ -255,7 +283,9 @@ previewers.new_buffer_previewer = function(opts)
if not opts.scroll_fn then
function opts.scroll_fn(self, direction)
if not self.state then return end
if not self.state then
return
end
local input = direction > 0 and [[]] or [[]]
local count = math.abs(direction)
@@ -284,11 +314,13 @@ previewers.cat = defaulter(function(opts)
define_preview = function(self, entry, status)
local p = from_entry.path(entry, true)
if p == nil or p == '' then return end
if p == nil or p == "" then
return
end
conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname
bufname = self.state.bufname,
})
end
end,
}
end, {})
@@ -299,8 +331,10 @@ previewers.vimgrep = defaulter(function(opts)
local jump_to_line = function(self, bufnr, lnum)
if lnum and lnum > 0 then
pcall(vim.api.nvim_buf_add_highlight, bufnr, ns_previewer, "TelescopePreviewLine", lnum - 1, 0, -1)
pcall(vim.api.nvim_win_set_cursor, self.state.winid, {lnum, 0})
vim.api.nvim_buf_call(bufnr, function() vim.cmd"norm! zz" end)
pcall(vim.api.nvim_win_set_cursor, self.state.winid, { lnum, 0 })
vim.api.nvim_buf_call(bufnr, function()
vim.cmd "norm! zz"
end)
end
self.state.last_set_bufnr = bufnr
@@ -328,24 +362,28 @@ previewers.vimgrep = defaulter(function(opts)
define_preview = function(self, entry, status)
local p = from_entry.path(entry, true)
if p == nil or p == '' then return end
if p == nil or p == "" then
return
end
if self.state.last_set_bufnr then
pcall(vim.api.nvim_buf_clear_namespace, self.state.last_set_bufnr, ns_previewer, 0, -1)
end
-- Workaround for unnamed buffer when using builtin.buffer
if entry.bufnr and (p == '[No Name]' or vim.api.nvim_buf_get_option(entry.bufnr, 'buftype') ~= '') then
if entry.bufnr and (p == "[No Name]" or vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "") then
local lines = vim.api.nvim_buf_get_lines(entry.bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
jump_to_line(self, self.state.bufnr, entry.lnum)
else
conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname,
callback = function(bufnr) jump_to_line(self, bufnr, entry.lnum) end
callback = function(bufnr)
jump_to_line(self, bufnr, entry.lnum)
end,
})
end
end
end,
}
end, {})
@@ -355,17 +393,17 @@ previewers.ctags = defaulter(function(_)
local determine_jump = function(entry)
if entry.scode then
return function(self)
local scode = string.gsub(entry.scode, '[$]$', '')
local scode = string.gsub(entry.scode, "[$]$", "")
scode = string.gsub(scode, [[\\]], [[\]])
scode = string.gsub(scode, [[\/]], [[/]])
scode = string.gsub(scode, '[*]', [[\*]])
scode = string.gsub(scode, "[*]", [[\*]])
pcall(vim.fn.matchdelete, self.state.hl_id, self.state.winid)
vim.cmd "norm! gg"
vim.fn.search(scode, "W")
vim.cmd "norm! zz"
self.state.hl_id = vim.fn.matchadd('TelescopePreviewMatch', scode)
self.state.hl_id = vim.fn.matchadd("TelescopePreviewMatch", scode)
end
else
return function(self, bufnr)
@@ -401,9 +439,9 @@ previewers.ctags = defaulter(function(_)
vim.api.nvim_buf_call(bufnr, function()
determine_jump(entry)(self, bufnr)
end)
end
end,
})
end
end,
}
end, {})
@@ -417,21 +455,21 @@ previewers.builtin = defaulter(function(_)
end,
define_preview = function(self, entry, status)
local module_name = vim.fn.fnamemodify(entry.filename, ':t:r')
local module_name = vim.fn.fnamemodify(entry.filename, ":t:r")
local text
if entry.text:sub(1, #module_name) ~= module_name then
text = module_name .. '.' .. entry.text
text = module_name .. "." .. entry.text
else
text = entry.text:gsub('_', '.', 1)
text = entry.text:gsub("_", ".", 1)
end
conf.buffer_previewer_maker(entry.filename, self.state.bufnr, {
bufname = self.state.bufname,
callback = function(bufnr)
search_cb_jump(self, bufnr, text)
end
end,
})
end
end,
}
end, {})
@@ -452,17 +490,17 @@ previewers.help = defaulter(function(_)
conf.buffer_previewer_maker(entry.filename, self.state.bufnr, {
bufname = self.state.bufname,
callback = function(bufnr)
putils.regex_highlighter(bufnr, 'help')
putils.regex_highlighter(bufnr, "help")
search_cb_jump(self, bufnr, query)
end
end,
})
end
end,
}
end, {})
previewers.man = defaulter(function(opts)
local pager = utils.get_lazy_default(opts.PAGER, function()
return vim.fn.executable('col') == 1 and 'col -bx' or ''
return vim.fn.executable "col" == 1 and "col -bx" or ""
end)
return previewers.new_buffer_previewer {
title = "Man Preview",
@@ -472,13 +510,13 @@ previewers.man = defaulter(function(opts)
define_preview = function(self, entry, status)
local win_width = vim.api.nvim_win_get_width(self.state.winid)
putils.job_maker({'man', entry.section, entry.value}, self.state.bufnr, {
putils.job_maker({ "man", entry.section, entry.value }, self.state.bufnr, {
env = { ["PAGER"] = pager, ["MANWIDTH"] = win_width },
value = entry.value,
bufname = self.state.bufname
bufname = self.state.bufname,
})
putils.regex_highlighter(self.state.bufnr, 'man')
end
putils.regex_highlighter(self.state.bufnr, "man")
end,
}
end)
@@ -486,21 +524,21 @@ previewers.git_branch_log = defaulter(function(opts)
local highlight_buffer = function(bufnr, content)
for i = 1, #content do
local line = content[i]
local _, hstart = line:find('[%*%s|]*')
local _, hstart = line:find "[%*%s|]*"
if hstart then
local hend = hstart + 7
if hend < #line then
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, "TelescopeResultsIdentifier", i - 1, hstart - 1, hend)
end
end
local _, cstart = line:find('- %(')
local _, cstart = line:find "- %("
if cstart then
local cend = string.find(line, '%) ')
local cend = string.find(line, "%) ")
if cend then
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, "TelescopeResultsConstant", i - 1, cstart - 1, cend)
end
end
local dstart, _ = line:find(' %(%d')
local dstart, _ = line:find " %(%d"
if dstart then
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, "TelescopeResultsSpecialComment", i - 1, dstart, #line)
end
@@ -514,19 +552,29 @@ previewers.git_branch_log = defaulter(function(opts)
end,
define_preview = function(self, entry, status)
local cmd = { 'git', '--no-pager', 'log', '--graph', '--pretty=format:%h -%d %s (%cr)',
'--abbrev-commit', '--date=relative', entry.value }
local cmd = {
"git",
"--no-pager",
"log",
"--graph",
"--pretty=format:%h -%d %s (%cr)",
"--abbrev-commit",
"--date=relative",
entry.value,
}
putils.job_maker(cmd, self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname,
cwd = opts.cwd,
callback = function(bufnr, content)
if not content then return end
if not content then
return
end
highlight_buffer(bufnr, content)
end
end,
})
end
end,
}
end, {})
@@ -538,13 +586,13 @@ previewers.git_stash_diff = defaulter(function(opts)
end,
define_preview = function(self, entry, _)
putils.job_maker({ 'git', '--no-pager', 'stash', 'show', '-p', entry.value }, self.state.bufnr, {
putils.job_maker({ "git", "--no-pager", "stash", "show", "-p", entry.value }, self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname,
cwd = opts.cwd
cwd = opts.cwd,
})
putils.regex_highlighter(self.state.bufnr, 'diff')
end
putils.regex_highlighter(self.state.bufnr, "diff")
end,
}
end, {})
@@ -557,9 +605,9 @@ previewers.git_commit_diff_to_parent = defaulter(function(opts)
end,
define_preview = function(self, entry, status)
local cmd = { 'git', '--no-pager', 'diff', entry.value .. '^!' }
local cmd = { "git", "--no-pager", "diff", entry.value .. "^!" }
if opts.current_file then
table.insert(cmd, '--')
table.insert(cmd, "--")
table.insert(cmd, opts.current_file)
end
@@ -569,10 +617,10 @@ previewers.git_commit_diff_to_parent = defaulter(function(opts)
cwd = opts.cwd,
callback = function(bufnr)
search_cb_jump(self, bufnr, opts.current_line)
end
end,
})
putils.regex_highlighter(self.state.bufnr, 'diff')
end
putils.regex_highlighter(self.state.bufnr, "diff")
end,
}
end, {})
@@ -586,9 +634,9 @@ previewers.git_commit_diff_to_head = defaulter(function(opts)
end,
define_preview = function(self, entry, status)
local cmd = { 'git', '--no-pager', 'diff', '--cached', entry.value }
local cmd = { "git", "--no-pager", "diff", "--cached", entry.value }
if opts.current_file then
table.insert(cmd, '--')
table.insert(cmd, "--")
table.insert(cmd, opts.current_file)
end
@@ -598,10 +646,10 @@ previewers.git_commit_diff_to_head = defaulter(function(opts)
cwd = opts.cwd,
callback = function(bufnr)
search_cb_jump(self, bufnr, opts.current_line)
end
end,
})
putils.regex_highlighter(self.state.bufnr, 'diff')
end
putils.regex_highlighter(self.state.bufnr, "diff")
end,
}
end, {})
@@ -615,10 +663,10 @@ previewers.git_commit_diff_as_was = defaulter(function(opts)
end,
define_preview = function(self, entry, status)
local cmd = { 'git', '--no-pager', 'show' }
local cmd = { "git", "--no-pager", "show" }
local cf = opts.current_file and Path:new(opts.current_file):make_relative(opts.cwd)
local value = cf and (entry.value .. ':' .. cf) or (entry.value)
local ft = cf and pfiletype.detect(value) or 'diff'
local value = cf and (entry.value .. ":" .. cf) or entry.value
local ft = cf and pfiletype.detect(value) or "diff"
table.insert(cmd, value)
putils.job_maker(cmd, self.state.bufnr, {
@@ -627,18 +675,18 @@ previewers.git_commit_diff_as_was = defaulter(function(opts)
cwd = opts.cwd,
callback = function(bufnr)
search_cb_jump(self, bufnr, opts.current_line)
end
end,
})
putils.highlighter(self.state.bufnr, ft)
end
end,
}
end, {})
previewers.git_commit_message = defaulter(function(opts)
local hl_map = {
'TelescopeResultsIdentifier',
'TelescopePreviewUser',
'TelescopePreviewDate'
"TelescopeResultsIdentifier",
"TelescopePreviewUser",
"TelescopePreviewDate",
}
return previewers.new_buffer_previewer {
title = "Git Message",
@@ -647,23 +695,25 @@ previewers.git_commit_message = defaulter(function(opts)
end,
define_preview = function(self, entry, status)
local cmd = { 'git', '--no-pager', 'log', '-n 1', entry.value }
local cmd = { "git", "--no-pager", "log", "-n 1", entry.value }
putils.job_maker(cmd, self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname,
cwd = opts.cwd,
callback = function(bufnr, content)
if not content then return end
if not content then
return
end
for k, v in ipairs(hl_map) do
local _, s = content[k]:find('%s')
local _, s = content[k]:find "%s"
if s then
vim.api.nvim_buf_add_highlight(bufnr, ns_previewer, v, k - 1, s, #content[k])
end
end
end
end,
})
end
end,
}
end, {})
@@ -675,21 +725,23 @@ previewers.git_file_diff = defaulter(function(opts)
end,
define_preview = function(self, entry, status)
if entry.status and (entry.status == '??' or entry.status == 'A ') then
if entry.status and (entry.status == "??" or entry.status == "A ") then
local p = from_entry.path(entry, true)
if p == nil or p == '' then return end
if p == nil or p == "" then
return
end
conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname
bufname = self.state.bufname,
})
else
putils.job_maker({ 'git', '--no-pager', 'diff', entry.value }, self.state.bufnr, {
putils.job_maker({ "git", "--no-pager", "diff", entry.value }, self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname,
cwd = opts.cwd
cwd = opts.cwd,
})
putils.regex_highlighter(self.state.bufnr, 'diff')
putils.regex_highlighter(self.state.bufnr, "diff")
end
end
end,
}
end, {})
@@ -707,7 +759,7 @@ previewers.autocommands = defaulter(function(_)
end,
define_preview = function(self, entry, status)
local results = vim.tbl_filter(function (x)
local results = vim.tbl_filter(function(x)
return x.group == entry.group
end, status.picker.finder.results)
@@ -727,9 +779,7 @@ previewers.autocommands = defaulter(function(_)
if item == entry then
selected_row = idx
end
table.insert(display,
string.format(" %-14s▏%-08s %s", item.event, item.ft_pattern, item.command)
)
table.insert(display, string.format(" %-14s▏%-08s %s", item.event, item.ft_pattern, item.command))
end
vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", "vim")
@@ -745,7 +795,7 @@ previewers.autocommands = defaulter(function(_)
end
vim.api.nvim_buf_add_highlight(self.state.bufnr, ns_previewer, "TelescopePreviewLine", selected_row + 1, 0, -1)
vim.api.nvim_win_set_cursor(status.preview_win, {selected_row + 1, 0})
vim.api.nvim_win_set_cursor(status.preview_win, { selected_row + 1, 0 })
self.state.last_set_bufnr = self.state.bufnr
end,
@@ -768,12 +818,12 @@ previewers.highlights = defaulter(function(_)
define_preview = function(self, entry, status)
putils.with_preview_window(status, nil, function()
if not self.state.bufname then
local output = vim.split(vim.fn.execute('highlight'), '\n')
local output = vim.split(vim.fn.execute "highlight", "\n")
local hl_groups = {}
for _, v in ipairs(output) do
if v ~= '' then
if v:sub(1, 1) == ' ' then
local part_of_old = v:match('%s+(.*)')
if v ~= "" then
if v:sub(1, 1) == " " then
local part_of_old = v:match "%s+(.*)"
hl_groups[table.getn(hl_groups)] = hl_groups[table.getn(hl_groups)] .. part_of_old
else
table.insert(hl_groups, v)
@@ -783,24 +833,26 @@ previewers.highlights = defaulter(function(_)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, hl_groups)
for k, v in ipairs(hl_groups) do
local startPos = string.find(v, 'xxx', 1, true) - 1
local startPos = string.find(v, "xxx", 1, true) - 1
local endPos = startPos + 3
local hlgroup = string.match(v, '([^ ]*)%s+.*')
local hlgroup = string.match(v, "([^ ]*)%s+.*")
pcall(vim.api.nvim_buf_add_highlight, self.state.bufnr, 0, hlgroup, k - 1, startPos, endPos)
end
end
pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1)
vim.cmd "norm! gg"
vim.fn.search(entry.value .. ' ')
local lnum = vim.fn.line('.')
vim.fn.search(entry.value .. " ")
local lnum = vim.fn.line "."
-- That one is actually a match but its better to use it like that then matchadd
vim.api.nvim_buf_add_highlight(self.state.bufnr,
vim.api.nvim_buf_add_highlight(
self.state.bufnr,
ns_previewer,
"TelescopePreviewMatch",
lnum - 1,
0,
#entry.value)
#entry.value
)
end)
end,
}
@@ -810,11 +862,13 @@ previewers.display_content = defaulter(function(_)
return previewers.new_buffer_previewer {
define_preview = function(self, entry, status)
putils.with_preview_window(status, nil, function()
assert(type(entry.preview_command) == 'function',
'entry must provide a preview_command function which will put the content into the buffer')
assert(
type(entry.preview_command) == "function",
"entry must provide a preview_command function which will put the content into the buffer"
)
entry.preview_command(entry, self.state.bufnr)
end)
end
end,
}
end, {})

View File

@@ -21,9 +21,9 @@
--- :Telescope find_files previewer=false
---@brief ]]
local Previewer = require('telescope.previewers.previewer')
local term_previewer = require('telescope.previewers.term_previewer')
local buffer_previewer = require('telescope.previewers.buffer_previewer')
local Previewer = require "telescope.previewers.previewer"
local term_previewer = require "telescope.previewers.term_previewer"
local buffer_previewer = require "telescope.previewers.buffer_previewer"
local previewers = {}
@@ -99,7 +99,6 @@ end
--- flexible `buffer_previewer` and is now deprecated.
previewers.new_termopen_previewer = term_previewer.new_termopen_previewer
--- Provides a `termopen_previewer` which has the ability to display files.
--- It will always show the top of the file and has support for
--- `bat`(prioritized) and `cat`. Each entry has to provide either the field
@@ -134,7 +133,6 @@ previewers.vimgrep = term_previewer.vimgrep
--- case it's configured that way.
previewers.qflist = term_previewer.qflist
--- An interface to instantiate a new `buffer_previewer`.
--- That means that the content actually lives inside a vim buffer which
--- enables us more control over the actual content. For example, we can
@@ -241,7 +239,6 @@ previewers.new_buffer_previewer = buffer_previewer.new_buffer_previewer
---@param opts table: keys: `use_ft_detect`, `bufname` and `callback`
previewers.buffer_previewer_maker = buffer_previewer.file_maker
--- A previewer that is used to display a file. It uses the `buffer_previewer`
--- interface and won't jump to the line. To integrate this one into your
--- own picker make sure that the field `path` or `filename` is set for
@@ -273,10 +270,10 @@ previewers.vim_buffer_vimgrep = buffer_previewer.vimgrep
previewers.vim_buffer_qflist = buffer_previewer.qflist
--- A previewer that shows a log of a branch as graph
previewers.git_branch_log = buffer_previewer.git_branch_log
previewers.git_branch_log = buffer_previewer.git_branch_log
--- A previewer that shows a diff of a stash
previewers.git_stash_diff = buffer_previewer.git_stash_diff
previewers.git_stash_diff = buffer_previewer.git_stash_diff
--- A previewer that shows a diff of a commit to a parent commit.<br>
--- The run command is `git --no-pager diff SHA^! -- $CURRENT_FILE`
@@ -288,19 +285,19 @@ previewers.git_commit_diff_to_parent = buffer_previewer.git_commit_diff_to_paren
--- The run command is `git --no-pager diff --cached $SHA -- $CURRENT_FILE`
---
--- The current file part is optional. So is only uses it with bcommits.
previewers.git_commit_diff_to_head = buffer_previewer.git_commit_diff_to_head
previewers.git_commit_diff_to_head = buffer_previewer.git_commit_diff_to_head
--- A previewer that shows a diff of a commit as it was.<br>
--- The run command is `git --no-pager show $SHA:$CURRENT_FILE` or `git --no-pager show $SHA`
previewers.git_commit_diff_as_was = buffer_previewer.git_commit_diff_as_was
previewers.git_commit_diff_as_was = buffer_previewer.git_commit_diff_as_was
--- A previewer that shows the commit message of a diff.<br>
--- The run command is `git --no-pager log -n 1 $SHA`
previewers.git_commit_message = buffer_previewer.git_commit_message
previewers.git_commit_message = buffer_previewer.git_commit_message
--- A previewer that shows the current diff of a file. Used in git_status.<br>
--- The run command is `git --no-pager diff $FILE`
previewers.git_file_diff = buffer_previewer.git_file_diff
previewers.git_file_diff = buffer_previewer.git_file_diff
previewers.ctags = buffer_previewer.ctags
previewers.builtin = buffer_previewer.builtin
@@ -309,7 +306,6 @@ previewers.man = buffer_previewer.man
previewers.autocommands = buffer_previewer.autocommands
previewers.highlights = buffer_previewer.highlights
--- A deprecated way of displaying content more easily. Was written at a time,
--- where the buffer_previewer interface wasn't present. Nowadays it's easier
--- to just use this. We will keep it around for backwards compatibility

View File

@@ -56,7 +56,7 @@ function Previewer:send_input(input)
if self._send_input then
self:_send_input(input)
else
vim.api.nvim_err_writeln("send_input is not defined for this previewer")
vim.api.nvim_err_writeln "send_input is not defined for this previewer"
end
end
@@ -64,7 +64,7 @@ function Previewer:scroll_fn(direction)
if self._scroll_fn then
self:_scroll_fn(direction)
else
vim.api.nvim_err_writeln("scroll_fn is not defined for this previewer")
vim.api.nvim_err_writeln "scroll_fn is not defined for this previewer"
end
end

View File

@@ -1,9 +1,9 @@
local conf = require('telescope.config').values
local utils = require('telescope.utils')
local Path = require('plenary.path')
local putils = require('telescope.previewers.utils')
local from_entry = require('telescope.from_entry')
local Previewer = require('telescope.previewers.previewer')
local conf = require("telescope.config").values
local utils = require "telescope.utils"
local Path = require "plenary.path"
local putils = require "telescope.previewers.utils"
local from_entry = require "telescope.from_entry"
local Previewer = require "telescope.previewers.previewer"
local flatten = vim.tbl_flatten
local buf_delete = utils.buf_delete
@@ -14,41 +14,41 @@ local defaulter = utils.make_default_callable
local previewers = {}
-- TODO: Should play with these some more, ty @clason
local bat_options = {"--style=plain", "--color=always", "--paging=always"}
local has_less = (vim.fn.executable('less') == 1) and conf.use_less
local bat_options = { "--style=plain", "--color=always", "--paging=always" }
local has_less = (vim.fn.executable "less" == 1) and conf.use_less
local get_file_stat = function(filename)
return vim.loop.fs_stat(vim.fn.expand(filename)) or {}
end
local list_dir = (function()
if vim.fn.has('win32') == 1 then
if vim.fn.has "win32" == 1 then
return function(dirname)
return { 'cmd.exe', '/c', 'dir', vim.fn.expand(dirname) }
return { "cmd.exe", "/c", "dir", vim.fn.expand(dirname) }
end
else
return function(dirname)
return { 'ls', '-la', vim.fn.expand(dirname) }
return { "ls", "-la", vim.fn.expand(dirname) }
end
end
end)()
local bat_maker = function(filename, lnum, start, finish)
if get_file_stat(filename).type == 'directory' then
if get_file_stat(filename).type == "directory" then
return list_dir(filename)
end
local command = {"bat"}
local command = { "bat" }
if lnum then
table.insert(command, { "--highlight-line", lnum})
table.insert(command, { "--highlight-line", lnum })
end
if has_less then
if start then
table.insert(command, {"--pager", string.format("less -RS +%s", start)})
table.insert(command, { "--pager", string.format("less -RS +%s", start) })
else
table.insert(command, {"--pager", "less -RS"})
table.insert(command, { "--pager", "less -RS" })
end
else
if start and finish then
@@ -57,18 +57,21 @@ local bat_maker = function(filename, lnum, start, finish)
end
return flatten {
command, bat_options, "--", vim.fn.expand(filename)
command,
bat_options,
"--",
vim.fn.expand(filename),
}
end
local cat_maker = function(filename, _, start, _)
if get_file_stat(filename).type == 'directory' then
if get_file_stat(filename).type == "directory" then
return list_dir(filename)
end
if 1 == vim.fn.executable('file') then
local output = utils.get_os_command_output{ 'file', '--mime-type', '-b', filename }
local mime_type = vim.split(output[1], '/')[1]
if 1 == vim.fn.executable "file" then
local output = utils.get_os_command_output { "file", "--mime-type", "-b", filename }
local mime_type = vim.split(output[1], "/")[1]
if mime_type ~= "text" then
return { "echo", "Binary file found. These files cannot be displayed!" }
end
@@ -76,27 +79,29 @@ local cat_maker = function(filename, _, start, _)
if has_less then
if start then
return { 'less', '-RS', string.format('+%s', start), vim.fn.expand(filename) }
return { "less", "-RS", string.format("+%s", start), vim.fn.expand(filename) }
else
return { 'less', '-RS', vim.fn.expand(filename) }
return { "less", "-RS", vim.fn.expand(filename) }
end
else
return {
"cat", "--", vim.fn.expand(filename)
"cat",
"--",
vim.fn.expand(filename),
}
end
end
local get_maker = function(opts)
local maker = opts.maker
if not maker and 1 == vim.fn.executable("bat") then
if not maker and 1 == vim.fn.executable "bat" then
maker = bat_maker
elseif not maker and 1 == vim.fn.executable("cat") then
elseif not maker and 1 == vim.fn.executable "cat" then
maker = cat_maker
end
if not maker then
error("Needs maker")
error "Needs maker"
end
return maker
@@ -119,28 +124,40 @@ previewers.new_termopen_previewer = function(opts)
local old_bufs = {}
local function get_term_id(self)
if not self.state then return nil end
if not self.state then
return nil
end
return self.state.termopen_id
end
local function get_bufnr(self)
if not self.state then return nil end
if not self.state then
return nil
end
return self.state.termopen_bufnr
end
local function set_term_id(self, value)
if job_is_running(get_term_id(self)) then vim.fn.jobstop(get_term_id(self)) end
if self.state then self.state.termopen_id = value end
if job_is_running(get_term_id(self)) then
vim.fn.jobstop(get_term_id(self))
end
if self.state then
self.state.termopen_id = value
end
end
local function set_bufnr(self, value)
if get_bufnr(self) then table.insert(old_bufs, get_bufnr(self)) end
if self.state then self.state.termopen_bufnr = value end
if get_bufnr(self) then
table.insert(old_bufs, get_bufnr(self))
end
if self.state then
self.state.termopen_bufnr = value
end
end
function opts.title(self)
if opt_title then
if type(opt_title) == 'function' then
if type(opt_title) == "function" then
return opt_title(self)
else
return opt_title
@@ -158,7 +175,9 @@ previewers.new_termopen_previewer = function(opts)
function opts.setup(self)
local state = {}
if opt_setup then vim.tbl_deep_extend("force", state, opt_setup(self)) end
if opt_setup then
vim.tbl_deep_extend("force", state, opt_setup(self))
end
return state
end
@@ -192,12 +211,14 @@ previewers.new_termopen_previewer = function(opts)
local term_opts = {
cwd = opts.cwd or vim.fn.getcwd(),
env = conf.set_env
env = conf.set_env,
}
putils.with_preview_window(status, bufnr, function()
local cmd = opts.get_command(entry, status)
if cmd then set_term_id(self, vim.fn.termopen(cmd, term_opts)) end
if cmd then
set_term_id(self, vim.fn.termopen(cmd, term_opts))
end
end)
vim.api.nvim_buf_set_name(bufnr, tostring(bufnr))
@@ -223,7 +244,7 @@ previewers.new_termopen_previewer = function(opts)
local input = direction > 0 and "d" or "u"
local count = math.abs(direction)
self:send_input(count..input)
self:send_input(count .. input)
end
end
@@ -244,10 +265,12 @@ previewers.cat = defaulter(function(opts)
get_command = function(entry)
local p = from_entry.path(entry, true)
if p == nil or p == '' then return end
if p == nil or p == "" then
return
end
return maker(p)
end
end,
}
end, {})
@@ -268,8 +291,10 @@ previewers.vimgrep = defaulter(function(opts)
local height = vim.api.nvim_win_get_height(win_id)
local p = from_entry.path(entry, true)
if p == nil or p == '' then return end
if entry.bufnr and (p == '[No Name]' or vim.api.nvim_buf_get_option(entry.bufnr, 'buftype') ~= '') then
if p == nil or p == "" then
return
end
if entry.bufnr and (p == "[No Name]" or vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "") then
return
end
@@ -301,7 +326,9 @@ previewers.qflist = defaulter(function(opts)
local height = vim.api.nvim_win_get_height(win_id)
local p = from_entry.path(entry, true)
if p == nil or p == '' then return end
if p == nil or p == "" then
return
end
local lnum = entry.lnum
local start, finish
@@ -315,7 +342,7 @@ previewers.qflist = defaulter(function(opts)
end
return maker(p, lnum, start, finish)
end
end,
}
end, {})

View File

@@ -1,10 +1,10 @@
local context_manager = require('plenary.context_manager')
local context_manager = require "plenary.context_manager"
local has_ts, _ = pcall(require, 'nvim-treesitter')
local _, ts_configs = pcall(require, 'nvim-treesitter.configs')
local _, ts_parsers = pcall(require, 'nvim-treesitter.parsers')
local has_ts, _ = pcall(require, "nvim-treesitter")
local _, ts_configs = pcall(require, "nvim-treesitter.configs")
local _, ts_parsers = pcall(require, "nvim-treesitter.parsers")
local Job = require('plenary.job')
local Job = require "plenary.job"
local utils = {}
@@ -30,33 +30,41 @@ utils.job_maker = function(cmd, bufnr, opts)
-- if any of them are missing, cache will be skipped
if opts.bufname ~= opts.value or not opts.bufname or not opts.value then
local command = table.remove(cmd, 1)
Job:new({
command = command,
args = cmd,
env = opts.env,
cwd = opts.cwd,
on_exit = vim.schedule_wrap(function(j)
if not vim.api.nvim_buf_is_valid(bufnr) then return end
if opts.mode == "append" then
vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, j:result())
elseif opts.mode == "insert" then
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, j:result())
end
if opts.callback then opts.callback(bufnr, j:result()) end
end)
}):start()
Job
:new({
command = command,
args = cmd,
env = opts.env,
cwd = opts.cwd,
on_exit = vim.schedule_wrap(function(j)
if not vim.api.nvim_buf_is_valid(bufnr) then
return
end
if opts.mode == "append" then
vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, j:result())
elseif opts.mode == "insert" then
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, j:result())
end
if opts.callback then
opts.callback(bufnr, j:result())
end
end),
})
:start()
else
if opts.callback then opts.callback(bufnr) end
if opts.callback then
opts.callback(bufnr)
end
end
end
local function has_filetype(ft)
return ft and ft ~= ''
return ft and ft ~= ""
end
--- Attach default highlighter which will choose between regex and ts
utils.highlighter = function(bufnr, ft)
if not(utils.ts_highlighter(bufnr, ft)) then
if not (utils.ts_highlighter(bufnr, ft)) then
utils.regex_highlighter(bufnr, ft)
end
end
@@ -82,8 +90,10 @@ local treesitter_attach = function(bufnr, ft)
end
vim.treesitter.highlighter.new(ts_parsers.get_parser(bufnr, lang))
local is_table = type(config.additional_vim_regex_highlighting) == "table"
if config.additional_vim_regex_highlighting and
(not is_table or vim.tbl_contains(config.additional_vim_regex_highlighting, lang)) then
if
config.additional_vim_regex_highlighting
and (not is_table or vim.tbl_contains(config.additional_vim_regex_highlighting, lang))
then
vim.api.nvim_buf_set_option(bufnr, "syntax", ft)
end
return true
@@ -94,10 +104,10 @@ end
-- Attach ts highlighter
utils.ts_highlighter = function(bufnr, ft)
if not has_ts then
has_ts, _ = pcall(require, 'nvim-treesitter')
has_ts, _ = pcall(require, "nvim-treesitter")
if has_ts then
_, ts_configs = pcall(require, 'nvim-treesitter.configs')
_, ts_parsers = pcall(require, 'nvim-treesitter.parsers')
_, ts_configs = pcall(require, "nvim-treesitter.configs")
_, ts_parsers = pcall(require, "nvim-treesitter.parsers")
end
end