summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorSimon Hauser <Simon-Hauser@outlook.de>2020-12-03 08:41:10 +0100
committerGitHub <noreply@github.com>2020-12-03 08:41:10 +0100
commit486ab5677618ffc6b05b4f071f79349fa4081022 (patch)
tree9b0b7d28ee9095c796cb561ddac33ff81e53fc21 /lua
parent424b98df154a333c3b85d48e7e435892c5854dfe (diff)
Feat: Displayer now supports highlights for separator and elements (#304)
Separator Example: When creating the displayer ```lua local displayer = entry_display.create { separator = " ", separator_hl = 'Visual', -- New items = { { width = opts.bufnr_width }, { width = 4 }, { remaining = true }, }, } ``` Elements Example: When submitting data ```lua local hl_group = x == y then 'Normal' or 'Visual' return displayer { entry.bufnr, { entry.indicator, hl_group }, display_bufname .. ":" .. entry.lnum, } ```
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/make_entry.lua2
-rw-r--r--lua/telescope/pickers/entry_display.lua36
2 files changed, 33 insertions, 5 deletions
diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua
index e1389d4..66cbcd1 100644
--- a/lua/telescope/make_entry.lua
+++ b/lua/telescope/make_entry.lua
@@ -329,7 +329,7 @@ function make_entry.gen_from_buffer(opts)
bufnr = entry.bufnr,
filename = bufname,
- lnum = entry.info.lnum and entry.info.lnum or 1,
+ lnum = entry.info.lnum ~= 0 and entry.info.lnum or 1,
indicator = indicator,
}
end
diff --git a/lua/telescope/pickers/entry_display.lua b/lua/telescope/pickers/entry_display.lua
index aef3047..2a156b6 100644
--- a/lua/telescope/pickers/entry_display.lua
+++ b/lua/telescope/pickers/entry_display.lua
@@ -57,24 +57,52 @@ entry_display.create = function(configuration)
local justify = not v.right_justify and "-" or ""
local format_str = "%" .. justify .. v.width .. "s"
table.insert(generator, function(item)
- return string.format(format_str, truncate(item, v.width))
+ if type(item) == 'table' then
+ return string.format(format_str, truncate(item[1], v.width)), item[2]
+ else
+ return string.format(format_str, truncate(item, v.width))
+ end
end)
else
table.insert(generator, function(item)
- return item
+ if type(item) == 'table' then
+ return item[1], item[2]
+ else
+ return item
+ end
end)
end
end
return function(self, picker)
local results = {}
+ local highlights = {}
for i = 1, table.getn(generator) do
if self[i] ~= nil then
- table.insert(results, generator[i](self[i], picker))
+ local str, hl = generator[i](self[i], picker)
+ if hl then
+ local hl_start = 0
+ for j = 1, (i - 1) do
+ hl_start = hl_start + #results[j] + (#configuration.separator or 1)
+ end
+ local hl_end = hl_start + #str:gsub('%s*$', '')
+ table.insert(highlights, { { hl_start, hl_end }, hl })
+ end
+ table.insert(results, str)
+ end
+ end
+
+ if configuration.separator_hl then
+ local width = #configuration.separator or 1
+ local hl_start, hl_end = 0, 0
+ for _, v in ipairs(results) do
+ hl_start = hl_end + #tostring(v)
+ hl_end = hl_start + width
+ table.insert(highlights, { { hl_start, hl_end }, configuration.separator_hl })
end
end
- return table.concat(results, configuration.separator or "│")
+ return table.concat(results, configuration.separator or "│"), highlights
end
end