lua: add vim.highlight.range (#12401)

This commit is contained in:
Christian Clason 2020-05-31 20:56:00 +02:00 committed by GitHub
parent ed815c61fd
commit 91e41c8576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 32 deletions

View File

@ -1040,14 +1040,6 @@ get_current_line_to_cursor()
*vim.lsp.util.get_severity_highlight_name()* *vim.lsp.util.get_severity_highlight_name()*
get_severity_highlight_name({severity}) get_severity_highlight_name({severity})
TODO: Documentation
*vim.lsp.util.highlight_range()*
highlight_range({bufnr}, {ns}, {hiname}, {start}, {finish})
TODO: Documentation
*vim.lsp.util.highlight_region()*
highlight_region({ft}, {start}, {finish})
TODO: Documentation TODO: Documentation
jump_to_location({location}) *vim.lsp.util.jump_to_location()* jump_to_location({location}) *vim.lsp.util.jump_to_location()*

View File

@ -713,6 +713,16 @@ vim.highlight.on_yank([{higroup}, {timeout}, {event}])
in milliseconds ({timeout}, default `500`), and the event structure in milliseconds ({timeout}, default `500`), and the event structure
that is fired ({event}, default `vim.v.event`). that is fired ({event}, default `vim.v.event`).
vim.highlight.range({bufnr}, {ns}, {higroup}, {start}, {finish}, {rtype}, {inclusive})
*vim.highlight.range()*
Highlights the range between {start} and {finish} (tuples of {line,col})
in buffer {bufnr} with the highlight group {higroup} using the namespace
{ns}. Optional arguments are the type of range (characterwise, linewise,
or blockwise, see |setreg|; default to characterwise) and whether the
range is inclusive (default false).
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
VIM.REGEX *lua-regex* VIM.REGEX *lua-regex*

View File

@ -2,12 +2,34 @@ local api = vim.api
local highlight = {} local highlight = {}
--- Highlight range between two positions
---
--@param bufnr number of buffer to apply highlighting to
--@param ns namespace to add highlight to
--@param higroup highlight group to use for highlighting
--@param rtype type of range (:help setreg, default charwise)
--@param inclusive boolean indicating whether the range is end-inclusive (default false)
function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive)
rtype = rtype or 'v'
inclusive = inclusive or false
-- sanity check
if start[2] < 0 or finish[2] < start[2] then return end
local region = vim.region(bufnr, start, finish, rtype, inclusive)
for linenr, cols in pairs(region) do
api.nvim_buf_add_highlight(bufnr, ns, higroup, linenr, cols[1], cols[2])
end
end
--- Highlight the yanked region --- Highlight the yanked region
-- ---
--- use from init.vim via --- use from init.vim via
--- au TextYankPost * lua require'vim.highlight'.on_yank() --- au TextYankPost * lua require'vim.highlight'.on_yank()
--- customize highlight group and timeout via --- customize highlight group and timeout via
--- au TextYankPost * lua require'vim.highlight'.on_yank("IncSearch", 500) --- au TextYankPost * lua require'vim.highlight'.on_yank("IncSearch", 500)
---
-- @param higroup highlight group for yanked region -- @param higroup highlight group for yanked region
-- @param timeout time in ms before highlight is cleared -- @param timeout time in ms before highlight is cleared
-- @param event event structure -- @param event event structure
@ -27,10 +49,7 @@ function highlight.on_yank(higroup, timeout, event)
pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]} pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]}
pos2 = {pos2[2] - 1, pos2[3] - 1 + pos2[4]} pos2 = {pos2[2] - 1, pos2[3] - 1 + pos2[4]}
local region = vim.region(bufnr, pos1, pos2, event.regtype, event.inclusive) highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive)
for linenr, cols in pairs(region) do
api.nvim_buf_add_highlight(bufnr, yank_ns, higroup, linenr, cols[1], cols[2])
end
vim.defer_fn( vim.defer_fn(
function() api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1) end, function() api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1) end,

View File

@ -3,6 +3,7 @@ local vim = vim
local validate = vim.validate local validate = vim.validate
local api = vim.api local api = vim.api
local list_extend = vim.list_extend local list_extend = vim.list_extend
local highlight = require 'vim.highlight'
local M = {} local M = {}
@ -691,7 +692,7 @@ function M.fancy_floating_markdown(contents, opts)
vim.cmd("ownsyntax markdown") vim.cmd("ownsyntax markdown")
local idx = 1 local idx = 1
local function highlight_region(ft, start, finish) local function apply_syntax_to_region(ft, start, finish)
if ft == '' then return end if ft == '' then return end
local name = ft..idx local name = ft..idx
idx = idx + 1 idx = idx + 1
@ -707,8 +708,8 @@ function M.fancy_floating_markdown(contents, opts)
-- make sure that regions between code blocks are definitely markdown. -- make sure that regions between code blocks are definitely markdown.
-- local ph = {start = 0; finish = 1;} -- local ph = {start = 0; finish = 1;}
for _, h in ipairs(highlights) do for _, h in ipairs(highlights) do
-- highlight_region('markdown', ph.finish, h.start) -- apply_syntax_to_region('markdown', ph.finish, h.start)
highlight_region(h.ft, h.start, h.finish) apply_syntax_to_region(h.ft, h.start, h.finish)
-- ph = h -- ph = h
end end
@ -762,19 +763,6 @@ function M.open_floating_preview(contents, filetype, opts)
return floating_bufnr, floating_winnr return floating_bufnr, floating_winnr
end end
local function highlight_range(bufnr, ns, hiname, start, finish)
if start[1] == finish[1] then
-- TODO care about encoding here since this is in byte index?
api.nvim_buf_add_highlight(bufnr, ns, hiname, start[1], start[2], finish[2])
else
api.nvim_buf_add_highlight(bufnr, ns, hiname, start[1], start[2], -1)
for line = start[1] + 1, finish[1] - 1 do
api.nvim_buf_add_highlight(bufnr, ns, hiname, line, 0, -1)
end
api.nvim_buf_add_highlight(bufnr, ns, hiname, finish[1], 0, finish[2])
end
end
do do
local diagnostic_ns = api.nvim_create_namespace("vim_lsp_diagnostics") local diagnostic_ns = api.nvim_create_namespace("vim_lsp_diagnostics")
local reference_ns = api.nvim_create_namespace("vim_lsp_references") local reference_ns = api.nvim_create_namespace("vim_lsp_references")
@ -908,8 +896,7 @@ do
[protocol.DiagnosticSeverity.Hint]='Hint', [protocol.DiagnosticSeverity.Hint]='Hint',
} }
-- TODO care about encoding here since this is in byte index? highlight.range(bufnr, diagnostic_ns,
highlight_range(bufnr, diagnostic_ns,
underline_highlight_name..hlmap[diagnostic.severity], underline_highlight_name..hlmap[diagnostic.severity],
{start.line, start.character}, {start.line, start.character},
{finish.line, finish.character} {finish.line, finish.character}
@ -933,7 +920,7 @@ do
[protocol.DocumentHighlightKind.Write] = "LspReferenceWrite"; [protocol.DocumentHighlightKind.Write] = "LspReferenceWrite";
} }
local kind = reference["kind"] or protocol.DocumentHighlightKind.Text local kind = reference["kind"] or protocol.DocumentHighlightKind.Text
highlight_range(bufnr, reference_ns, document_highlight_kind[kind], start_pos, end_pos) highlight.range(bufnr, reference_ns, document_highlight_kind[kind], start_pos, end_pos)
end end
end end