add preview target line hl

neovim_0_5
ray-x 3 years ago
parent e2bf480d22
commit f815e7dfb9

@ -7,7 +7,7 @@ Easy code navigation.
- LSP easy setup. Support some of the most commonly used lsp client setup - LSP easy setup. Support some of the most commonly used lsp client setup
- GUI with floating windows - GUI with floating windows
- fzy search - fzy search with Lua-JIT
- Better navigation for diagnostic errors, Navigate through files that contain errors/warnings - Better navigation for diagnostic errors, Navigate through files that contain errors/warnings
- Group references/implementation/incomming/outgoing based on file names. - Group references/implementation/incomming/outgoing based on file names.
@ -22,6 +22,7 @@ to tune the plugins to fit my requirements.
- [nvim-fzy](https://github.com/mfussenegger/nvim-fzy.git) - [nvim-fzy](https://github.com/mfussenegger/nvim-fzy.git)
- [fuzzy](https://github.com/amirrezaask/fuzzy.nvim) - [fuzzy](https://github.com/amirrezaask/fuzzy.nvim)
- [lspsaga](https://github.com/glepnir/lspsaga.nvim) - [lspsaga](https://github.com/glepnir/lspsaga.nvim)
- [fzf-lsp lsp with fzf as gui backend](https://github.com/gfanto/fzf-lsp.nvim)
# Install # Install

@ -38,7 +38,7 @@ local function def_preview(timeout_ms)
-- log("def-preview", result) -- log("def-preview", result)
for key, value in pairs(result) do for key, value in pairs(result) do
if result[key] ~= nil then if result[key] ~= nil then
table.insert(data, result[key].result[1]) table.insert(data, value.result[1])
end end
end end
local range = data[1].targetRange or data[1].range local range = data[1].targetRange or data[1].range
@ -55,7 +55,7 @@ local function def_preview(timeout_ms)
if not vim.api.nvim_buf_is_loaded(bufnr) then if not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr) vim.fn.bufload(bufnr)
end end
-- TODO: 12 should be an option
local definition = vim.api.nvim_buf_get_lines(bufnr, row, range["end"].line + 12, false) local definition = vim.api.nvim_buf_get_lines(bufnr, row, range["end"].line + 12, false)
local def_line = vim.api.nvim_buf_get_lines(bufnr, range.start.line, range.start.line + 1, false) local def_line = vim.api.nvim_buf_get_lines(bufnr, range.start.line, range.start.line + 1, false)
for _ = 1, math.min(3, #definition), 1 do for _ = 1, math.min(3, #definition), 1 do
@ -85,7 +85,7 @@ local function def_preview(timeout_ms)
vim.cmd('set cursorline') vim.cmd('set cursorline')
if #def_line > 0 then if #def_line > 0 then
local niddle = require('guihua.util').add_escape(def_line[1]) local niddle = require('guihua.util').add_escape(def_line[1])
log(def_line[1], niddle) -- log(def_line[1], niddle)
vim.fn.matchadd("Search", niddle) vim.fn.matchadd("Search", niddle)
end end
-- TODO: -- TODO:

@ -5,14 +5,15 @@ local View = require "guihua.view"
local util = require "navigator.util" local util = require "navigator.util"
local log = require "navigator.util".log local log = require "navigator.util".log
local verbose = require "navigator.util".verbose local verbose = require "navigator.util".verbose
local api = vim.api
function M.new_preview(opts) function M.new_preview(opts)
return TextView:new( return TextView:new(
{ {
loc = "top_center", loc = "top_center",
rect = { rect = {
height = #opts.items + 4, height = opts.preview_heigh or 12,
width = opts.width or 90, width = opts.width or 100,
pos_x = opts.pos_x or 0, pos_x = opts.pos_x or 0,
pos_y = opts.pos_y or 4 pos_y = opts.pos_y or 4
}, },
@ -20,16 +21,16 @@ function M.new_preview(opts)
relative = opts.relative, relative = opts.relative,
data = opts.items, data = opts.items,
syntax = opts.syntax, syntax = opts.syntax,
enter = opts.enter or false enter = opts.enter or false,
hl_line = opts.hl_line
} }
) )
end end
function M._preview_location(location, width, pos_x, pos_y) function M._preview_location(opts) --location, width, pos_x, pos_y
local api = vim.api local uri = opts.location.targetUri or opts.location.uri
local uri = location.targetUri or location.uri if opts.uri == nil then
if uri == nil then log("invalid/nil uri ")
log("invalid uri ", location)
return return
end end
local bufnr = vim.uri_to_bufnr(uri) local bufnr = vim.uri_to_bufnr(uri)
@ -37,8 +38,10 @@ function M._preview_location(location, width, pos_x, pos_y)
vim.fn.bufload(bufnr) vim.fn.bufload(bufnr)
end end
-- --
local range = location.targetRange or location.range
local contents = api.nvim_buf_get_lines(bufnr, range.start.line, range["end"].line + 1, false) local range = opts.location.targetRange or opts.location.range
local contents = api.nvim_buf_get_lines(bufnr, range.start.line, range["end"].line + 10, false)
-- --
local syntax = api.nvim_buf_get_option(bufnr, "syntax") local syntax = api.nvim_buf_get_option(bufnr, "syntax")
if syntax == nil or #syntax < 1 then if syntax == nil or #syntax < 1 then
@ -46,39 +49,25 @@ function M._preview_location(location, width, pos_x, pos_y)
end end
verbose(syntax, contents) verbose(syntax, contents)
local opts = {syntax = syntax, width = width, pos_x = pos_x or 0, pos_y = pos_y or 10} local win_opts = {syntax = syntax, width = opts.width, pos_x = opts.offset_x or 0, pos_y = opts.offset_y or 10}
opts.items = contents win_opts.items = contents
return M.new_preview(opts) win_opts.hl_line = opts.lnum - range.start.line
end local w = M.new_preview(win_opts)
-- local bufnr, winnr =lsp.util.open_floating_preview(contents, syntax, {offset_x=30, offset_y=20}) return w
--
-- vim.api.nvim_buf_set_var(bufnr, "lsp_floating", true)
-- return bufnr, winnr
--
function M.preview_file(filename, width, line, col, offset_x, offset_y)
verbose("file", filename, line, offset_x, offset_y)
if line >= 2 then
line = line - 2
end
local loc = {uri = "file:///" .. filename, targetRange = {start = {line = line}}}
offset_x = offset_x or 0
offset_y = offset_y or 6
loc.targetRange["end"] = {line = line + 4}
return M._preview_location(loc, width, offset_x, offset_y)
end end
function M.preview_uri(uri, width, line, col, offset_x, offset_y) function M.preview_uri(opts) -- uri, width, line, col, offset_x, offset_y
verbose("uri", uri, line, offset_x, offset_y) verbose("uri", opts.uri, opts.lnum, opts.offset_x, opts.offset_y)
if line >= 2 then local line_beg = opts.line
line = line - 2 if opts.lnum >= 2 then
line_beg = opts.lnum - 2
end end
offset_x = offset_x or 0 local loc = {uri = opts.uri, targetRange = {start = {line = line_beg}}}
offset_y = offset_y or 6 -- TODO: options for 8
local loc = {uri = uri, targetRange = {start = {line = line}}} loc.targetRange["end"] = {line = opts.lnum + 8}
loc.targetRange["end"] = {line = line + 4} opts.location = loc
return M._preview_location(loc, width, offset_x, offset_y) return M._preview_location(opts)
end end
function M.new_list_view(opts) function M.new_list_view(opts)
@ -89,9 +78,9 @@ function M.new_list_view(opts)
else else
data = require "guihua.util".aggregate_filename(items, opts) data = require "guihua.util".aggregate_filename(items, opts)
end end
local wwidth = vim.api.nvim_get_option("columns") local wwidth = api.nvim_get_option("columns")
local width = opts.width or math.floor(wwidth * 0.8) local width = opts.width or math.floor(wwidth * 0.8)
local wheight = math.floor(vim.api.nvim_get_option("lines") * 0.8) local wheight = math.floor(api.nvim_get_option("lines") * 0.8)
local prompt = opts.prompt or false local prompt = opts.prompt or false
if data and not vim.tbl_isempty(data) then if data and not vim.tbl_isempty(data) then
-- replace -- replace
@ -128,18 +117,20 @@ function M.new_list_view(opts)
util.open_file_at(l.filename, l.lnum) util.open_file_at(l.filename, l.lnum)
end end
end, end,
on_move = opts.on_move or function(pos) on_move = opts.on_move or
function(pos)
if pos == 0 then if pos == 0 then
pos = 1 pos = 1
end end
local l = data[pos] local l = data[pos]
verbose("on move", pos, l.text or l, l.uri, l.filename) verbose("on move", pos, l.text or l, l.uri, l.filename)
-- todo fix -- todo fix
if l.uri ~= nil then if l.uri == nil then
return M.preview_uri(l.uri, width, l.lnum, l.col, 0, offset_y) l.uri = "file:///" .. l.filename
else
return M.preview_file(l.filename, width, l.lnum, l.col, 0, offset_y)
end end
return M.preview_uri(
{uri = l.uri, width = width, lnum = l.lnum, col = l.col, offsetx = 0, offset_y = offset_y}
)
end end
} }
) )

Loading…
Cancel
Save