2021-04-19 02:56:32 +00:00
|
|
|
local M = {}
|
|
|
|
local ListView = require "guihua.listview"
|
|
|
|
local TextView = require "guihua.textview"
|
|
|
|
local util = require "navigator.util"
|
2021-05-17 03:15:15 +00:00
|
|
|
local log = require"navigator.util".log
|
2021-05-21 11:39:46 +00:00
|
|
|
local trace = require"navigator.util".trace
|
2021-04-20 07:42:03 +00:00
|
|
|
local api = vim.api
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
local top_center = require"guihua.location".top_center
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2021-06-25 00:33:04 +00:00
|
|
|
local path_sep = require"navigator.util".path_sep()
|
|
|
|
local path_cur = require"navigator.util".path_cur()
|
2021-05-17 03:15:15 +00:00
|
|
|
function M._preview_location(opts) -- location, width, pos_x, pos_y
|
2021-06-13 04:03:01 +00:00
|
|
|
local uri = opts.uri
|
|
|
|
if uri == nil then
|
2021-04-20 07:42:03 +00:00
|
|
|
log("invalid/nil uri ")
|
2021-04-19 02:56:32 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local bufnr = vim.uri_to_bufnr(uri)
|
2021-05-21 11:39:46 +00:00
|
|
|
if not api.nvim_buf_is_loaded(bufnr) then
|
|
|
|
vim.fn.bufload(bufnr)
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
--
|
2021-04-20 07:42:03 +00:00
|
|
|
|
2021-05-17 03:15:15 +00:00
|
|
|
local display_range = opts.location.range
|
|
|
|
-- if range.start == nil then
|
|
|
|
-- print("error invalid range")
|
|
|
|
-- return
|
|
|
|
-- end
|
|
|
|
-- if range.start.line == nil then
|
|
|
|
-- range.start.line = range["end"].line - 1
|
|
|
|
-- opts.lnum = range["end"].line + 1
|
|
|
|
-- log(opts)
|
|
|
|
-- end
|
|
|
|
-- if range["end"].line == nil then
|
|
|
|
-- range["end"].line = range.start.line + 1
|
|
|
|
-- opts.lnum = range.start.line + 1
|
|
|
|
-- log(opts)
|
|
|
|
-- end
|
|
|
|
-- TODO: preview height
|
|
|
|
-- local contents = api.nvim_buf_get_lines(bufnr, range.start.line, range["end"].line, false)
|
2021-04-19 02:56:32 +00:00
|
|
|
--
|
2021-05-17 03:15:15 +00:00
|
|
|
local syntax = api.nvim_buf_get_option(bufnr, "ft")
|
2021-05-21 11:39:46 +00:00
|
|
|
if syntax == nil or #syntax < 1 then
|
|
|
|
syntax = "c"
|
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
-- trace(syntax, contents)
|
2021-05-17 03:15:15 +00:00
|
|
|
local win_opts = {
|
|
|
|
syntax = syntax,
|
|
|
|
width = opts.width,
|
|
|
|
height = display_range['end'].line - display_range.start.line + 1,
|
2021-06-25 10:18:55 +00:00
|
|
|
preview_height = opts.height or opts.preview_height,
|
2021-05-21 11:39:46 +00:00
|
|
|
pos_x = opts.offset_x,
|
|
|
|
pos_y = opts.offset_y,
|
2021-05-17 03:15:15 +00:00
|
|
|
range = opts.range,
|
|
|
|
display_range = display_range,
|
|
|
|
uri = uri,
|
2021-06-13 04:03:01 +00:00
|
|
|
allow_edit = opts.enable_edit
|
2021-05-17 03:15:15 +00:00
|
|
|
}
|
2021-09-19 02:42:34 +00:00
|
|
|
|
|
|
|
if _NgConfigValues.external then
|
|
|
|
win_opts.external = true
|
|
|
|
win_opts.relative = nil
|
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
-- win_opts.items = contents
|
|
|
|
win_opts.hl_line = opts.lnum - display_range.start.line
|
2021-05-21 11:39:46 +00:00
|
|
|
if win_opts.hl_line < 0 then
|
|
|
|
win_opts.hl_line = 1
|
|
|
|
end
|
|
|
|
trace(opts.lnum, opts.range.start.line, win_opts.hl_line)
|
2021-06-13 04:03:01 +00:00
|
|
|
log(win_opts)
|
2021-05-21 11:39:46 +00:00
|
|
|
local w = TextView:new({
|
|
|
|
loc = "offset_center",
|
|
|
|
rect = {
|
|
|
|
height = win_opts.height, -- opts.preview_heigh or 12, -- TODO 12
|
|
|
|
width = win_opts.width,
|
|
|
|
pos_x = win_opts.pos_x,
|
|
|
|
pos_y = win_opts.pos_y
|
|
|
|
},
|
|
|
|
list_view_height = win_opts.height,
|
|
|
|
-- data = display_data,
|
|
|
|
relative = win_opts.relative,
|
|
|
|
-- data = opts.items, -- either items or uri
|
|
|
|
uri = win_opts.uri,
|
|
|
|
syntax = win_opts.syntax,
|
|
|
|
enter = win_opts.enter or false,
|
|
|
|
range = win_opts.range,
|
2021-06-13 04:03:01 +00:00
|
|
|
border = opts.border,
|
2021-05-21 11:39:46 +00:00
|
|
|
display_range = win_opts.display_range,
|
2021-06-13 04:03:01 +00:00
|
|
|
hl_line = win_opts.hl_line,
|
2021-09-19 02:42:34 +00:00
|
|
|
allow_edit = win_opts.allow_edit,
|
|
|
|
external = win_opts.external
|
2021-05-21 11:39:46 +00:00
|
|
|
})
|
2021-04-20 07:42:03 +00:00
|
|
|
return w
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
2021-04-20 07:42:03 +00:00
|
|
|
function M.preview_uri(opts) -- uri, width, line, col, offset_x, offset_y
|
2021-09-09 02:59:58 +00:00
|
|
|
-- local handle = vim.loop.new_async(vim.schedule_wrap(function()
|
2021-04-23 04:21:22 +00:00
|
|
|
local line_beg = opts.lnum - 1
|
2021-09-07 11:08:00 +00:00
|
|
|
if line_beg >= _NgConfigValues.preview_lines_before then
|
|
|
|
line_beg = line_beg - _NgConfigValues.preview_lines_before
|
|
|
|
elseif line_beg >= 2 then
|
2021-05-21 11:39:46 +00:00
|
|
|
line_beg = line_beg - 2
|
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
local loc = {uri = opts.uri, range = {start = {line = line_beg}}}
|
|
|
|
|
|
|
|
-- TODO: preview height
|
2021-05-21 11:39:46 +00:00
|
|
|
loc.range["end"] = {line = opts.lnum + opts.preview_height}
|
2021-04-20 07:42:03 +00:00
|
|
|
opts.location = loc
|
2021-04-23 04:21:22 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
trace("uri", opts.uri, opts.lnum, opts.location.range.start.line, opts.location.range['end'].line)
|
2021-04-20 07:42:03 +00:00
|
|
|
return M._preview_location(opts)
|
2021-09-09 02:59:58 +00:00
|
|
|
-- handle:close()
|
|
|
|
-- end))
|
|
|
|
-- handle:send()
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.new_list_view(opts)
|
2021-04-25 02:43:18 +00:00
|
|
|
local config = require("navigator").config_values()
|
|
|
|
|
2021-04-19 02:56:32 +00:00
|
|
|
local items = opts.items
|
|
|
|
local data = {}
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-04-20 07:42:03 +00:00
|
|
|
local wwidth = api.nvim_get_option("columns")
|
2021-05-21 11:39:46 +00:00
|
|
|
|
|
|
|
local loc = "top_center"
|
2021-06-25 10:18:55 +00:00
|
|
|
|
|
|
|
local mwidth = _NgConfigValues.width
|
|
|
|
local width = math.floor(wwidth * mwidth)
|
2021-05-21 11:39:46 +00:00
|
|
|
if config.width ~= nil and config.width > 0.3 and config.width < 0.99 then
|
|
|
|
width = math.floor(wwidth * config.width)
|
|
|
|
end
|
2021-09-19 02:42:34 +00:00
|
|
|
width = math.min(120, width, opts.width or 120)
|
2021-06-25 10:18:55 +00:00
|
|
|
local wheight = math.floor(1 + api.nvim_get_option("lines")
|
|
|
|
* (_NgConfigValues.height + _NgConfigValues.preview_height))
|
2021-09-04 08:49:08 +00:00
|
|
|
local pheight = math.max(_NgConfigValues.preview_lines, math.floor(
|
|
|
|
api.nvim_get_option("lines") * _NgConfigValues.preview_height))
|
2021-04-19 02:56:32 +00:00
|
|
|
local prompt = opts.prompt or false
|
2021-05-17 03:15:15 +00:00
|
|
|
if opts.rawdata then
|
|
|
|
data = items
|
|
|
|
else
|
2021-08-29 07:35:46 +00:00
|
|
|
trace(items)
|
2021-05-21 11:39:46 +00:00
|
|
|
data = require"navigator.render".prepare_for_render(items, opts)
|
2021-05-17 03:15:15 +00:00
|
|
|
end
|
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
local border = _NgConfigValues.border or 'shadow'
|
|
|
|
|
2021-09-13 13:18:54 +00:00
|
|
|
if not data or vim.tbl_isempty(data) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- replace
|
|
|
|
-- TODO: 10 vimrc opt
|
|
|
|
if #data > 10 and opts.prompt == nil then
|
|
|
|
loc = "top_center"
|
|
|
|
prompt = true
|
|
|
|
end
|
2021-05-21 11:39:46 +00:00
|
|
|
|
2021-09-13 13:18:54 +00:00
|
|
|
local lheight = math.min(#data, math.floor(wheight * _NgConfigValues.height))
|
2021-05-21 11:39:46 +00:00
|
|
|
|
2021-09-13 13:18:54 +00:00
|
|
|
local r, _ = top_center(lheight, width)
|
2021-05-21 11:39:46 +00:00
|
|
|
|
2021-09-13 13:18:54 +00:00
|
|
|
local offset_y = r + lheight
|
|
|
|
-- style shadow took 1 lines
|
|
|
|
if border ~= 'none' then
|
|
|
|
if border == 'shadow' then
|
|
|
|
offset_y = offset_y + 1
|
|
|
|
else
|
|
|
|
offset_y = offset_y + 1 -- single?
|
2021-08-18 23:56:17 +00:00
|
|
|
end
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
2021-09-13 13:18:54 +00:00
|
|
|
-- if border is not set, this should be r+lheigh
|
|
|
|
if prompt then
|
|
|
|
offset_y = offset_y + 1 -- need to check this out
|
|
|
|
end
|
|
|
|
local idx = require"guihua.util".fzy_idx
|
|
|
|
local transparency = _NgConfigValues.transparency
|
|
|
|
if transparency == 100 then
|
|
|
|
transparency = nil
|
|
|
|
end
|
2021-09-19 02:42:34 +00:00
|
|
|
local ext = _NgConfigValues.external
|
|
|
|
if ext then
|
|
|
|
opts.relative = nil
|
|
|
|
end
|
2021-09-13 13:18:54 +00:00
|
|
|
return ListView:new({
|
|
|
|
loc = loc,
|
|
|
|
prompt = prompt,
|
|
|
|
relative = opts.relative,
|
|
|
|
style = opts.style,
|
|
|
|
api = opts.api,
|
|
|
|
total = opts.total,
|
|
|
|
rect = {height = lheight, width = width, pos_x = 0, pos_y = 0},
|
|
|
|
-- preview_height = pheight,
|
|
|
|
ft = opts.ft or 'guihua',
|
|
|
|
-- data = display_data,
|
|
|
|
data = data,
|
|
|
|
border = border,
|
2021-09-19 02:42:34 +00:00
|
|
|
external = ext,
|
2021-09-13 13:18:54 +00:00
|
|
|
on_confirm = opts.on_confirm or function(item, split_opts)
|
|
|
|
log(split_opts)
|
|
|
|
split_opts = split_opts or {}
|
|
|
|
if item.filename ~= nil then
|
|
|
|
log("openfile ", item.filename, item.lnum, item.col)
|
|
|
|
util.open_file_at(item.filename, item.lnum, item.col, split_opts.split)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
transparency = transparency,
|
|
|
|
on_move = opts.on_move or function(item)
|
|
|
|
trace("on move", item)
|
|
|
|
trace("on move", item.text or item, item.uri, item.filename)
|
|
|
|
-- todo fix
|
|
|
|
if item.uri == nil then
|
|
|
|
item.uri = "file:///" .. item.filename
|
|
|
|
end
|
|
|
|
return M.preview_uri({
|
|
|
|
uri = item.uri,
|
|
|
|
width = width,
|
|
|
|
height = lheight, -- this is to cal offset
|
|
|
|
preview_height = pheight,
|
|
|
|
lnum = item.lnum,
|
|
|
|
col = item.col,
|
|
|
|
range = item.range,
|
|
|
|
offset_x = 0,
|
|
|
|
offset_y = offset_y,
|
|
|
|
border = border,
|
2021-09-19 02:42:34 +00:00
|
|
|
external = ext,
|
2021-09-13 13:18:54 +00:00
|
|
|
enable_edit = opts.enable_preview_edit or false
|
|
|
|
})
|
|
|
|
end
|
|
|
|
})
|
2021-04-19 02:56:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|
2021-05-17 03:15:15 +00:00
|
|
|
|
|
|
|
-- Doc
|
|
|
|
|
|
|
|
--[[
|
|
|
|
-- each item should look like this
|
|
|
|
-- update if API changes
|
|
|
|
{
|
|
|
|
call_by = { <table 1> },
|
|
|
|
col = 40,
|
|
|
|
display_filename = "./curry.js",
|
2021-08-10 05:27:20 +00:00
|
|
|
filename = "/Users/username/lsp_test/js/curry.js",
|
2021-05-17 03:15:15 +00:00
|
|
|
lnum = 4,
|
|
|
|
range = {
|
|
|
|
end = {
|
|
|
|
character = 46,
|
|
|
|
line = 3 -- note: C index
|
|
|
|
},
|
|
|
|
start = {
|
|
|
|
character = 39,
|
|
|
|
line = 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
rpath = "js/curry.js",
|
|
|
|
text = " (sum, element, index) => (sum += element * vector2[index]),",
|
2021-08-10 05:27:20 +00:00
|
|
|
uri = "file:///Users/username/lsp_test/js/curry.js"
|
2021-05-17 03:15:15 +00:00
|
|
|
}
|
|
|
|
--]]
|
|
|
|
|
|
|
|
-- on move item:
|
|
|
|
--[[
|
|
|
|
|
|
|
|
call_by = { {
|
|
|
|
kind = " ",
|
|
|
|
node_scope = {
|
|
|
|
end = {
|
|
|
|
character = 1,
|
|
|
|
line = 7
|
|
|
|
},
|
|
|
|
start = {
|
|
|
|
character = 0,
|
|
|
|
line = 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
node_text = "curriedDot",
|
|
|
|
type = "var"
|
|
|
|
} },
|
|
|
|
col = 22,
|
|
|
|
display_filename = "./curry.js",
|
2021-08-10 05:27:20 +00:00
|
|
|
filename = "/Users/username/lsp_test/js/curry.js",
|
2021-05-17 03:15:15 +00:00
|
|
|
lnum = 4,
|
|
|
|
range = {
|
|
|
|
end = {
|
|
|
|
character = 26,
|
|
|
|
line = 3
|
|
|
|
},
|
|
|
|
start = {
|
|
|
|
character = 21,
|
|
|
|
line = 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
rpath = "js/curry.js",
|
|
|
|
text = " 4: (sum, element, index) => (sum += element * vector curriedDot()",
|
2021-08-10 05:27:20 +00:00
|
|
|
uri = "file:///Users/username/lsp_test/js/curry.js"
|
2021-05-17 03:15:15 +00:00
|
|
|
--
|
|
|
|
]]
|