mirror of
https://github.com/Myzel394/easytables.nvim.git
synced 2025-06-18 14:55:26 +02:00
Adding highlighting
This commit is contained in:
parent
19ddbe7007
commit
bd1c0dba89
@ -32,6 +32,7 @@
|
||||
"/home/myzel394/.local/share/nvim/site/pack/packer/start/tailwindcss-colorizer-cmp.nvim/lua",
|
||||
"/home/myzel394/.local/share/nvim/site/pack/packer/start/telescope.nvim/lua",
|
||||
"/home/myzel394/.config/nvim/custom_plugins/lua",
|
||||
"${3rd}/luv/library"
|
||||
"${3rd}/luv/library",
|
||||
"${3rd}/luassert/library"
|
||||
]
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
local Input = require("nui.input")
|
||||
local event = require("nui.utils.autocmd").event
|
||||
local table = require("easytables.table")
|
||||
local table_builder = require("easytables.tablebuilder")
|
||||
local window = require("easytables.window")
|
||||
|
||||
local function create_win()
|
||||
end
|
||||
@ -56,50 +56,12 @@ end
|
||||
|
||||
local function a()
|
||||
local own_table = table:create(6, 3)
|
||||
local buffer = vim.api.nvim_create_buf(false, true)
|
||||
own_table:highlight_cell(1, 1)
|
||||
|
||||
-- Center
|
||||
local width = 40
|
||||
local height = 20
|
||||
local x = math.floor((vim.o.columns - width) / 2)
|
||||
local win = vim.api.nvim_open_win(buffer, true, {
|
||||
relative = "win",
|
||||
row = math.floor(((vim.o.lines - height) / 2) - 1),
|
||||
col = x,
|
||||
width = width,
|
||||
height = height,
|
||||
style = "minimal",
|
||||
border = "rounded",
|
||||
title = "",
|
||||
title_pos = "center",
|
||||
})
|
||||
local window = window:create()
|
||||
|
||||
vim.api.nvim_set_option_value('winhl', 'Normal:MyHighlight', { win = win })
|
||||
|
||||
|
||||
local new_win = vim.api.nvim_open_win(buffer, true, {
|
||||
relative = "win",
|
||||
row = math.floor(((vim.o.lines - height) / 2) - 1) + height + 1,
|
||||
col = x - 1,
|
||||
width = width,
|
||||
height = 2,
|
||||
style = "minimal",
|
||||
border = "rounded",
|
||||
title = "",
|
||||
title_pos = "center",
|
||||
})
|
||||
|
||||
local representation = table_builder.draw_representation(own_table)
|
||||
|
||||
vim.api.nvim_buf_set_lines(buffer, 0, -1, false, representation)
|
||||
|
||||
vim.api.nvim_buf_attach(buffer, false, {
|
||||
on_lines = function(_, handle, _, firstline, lastline, new_lastline)
|
||||
local new_text = vim.api.nvim_buf_get_lines(handle, firstline, new_lastline, true)
|
||||
|
||||
print(new_text[1])
|
||||
end,
|
||||
})
|
||||
window:show()
|
||||
window:draw_table(own_table)
|
||||
end
|
||||
|
||||
return {
|
||||
|
@ -43,4 +43,41 @@ function M:cols_amount()
|
||||
return #self.table[1]
|
||||
end
|
||||
|
||||
function M:highlight_cell(col, row)
|
||||
self.highlighted_cell = { row = row, col = col }
|
||||
end
|
||||
|
||||
function M:get_highlighted_cell()
|
||||
return self.highlighted_cell
|
||||
end
|
||||
|
||||
function M:get_cell_positions(col, row, min_value_width)
|
||||
local start_position = 1
|
||||
|
||||
for i, cell in ipairs(self.table[row]) do
|
||||
if i == col then
|
||||
break
|
||||
end
|
||||
|
||||
start_position = start_position + #cell + 1
|
||||
end
|
||||
|
||||
local end_position = start_position + math.max(min_value_width, #self.table[row][col])
|
||||
|
||||
return start_position, end_position
|
||||
end
|
||||
|
||||
function M:get_highlighted_position()
|
||||
if self.highlighted_cell == nil then
|
||||
return nil
|
||||
end
|
||||
|
||||
local row = self.highlighted_cell.row
|
||||
local col = self.highlighted_cell.col
|
||||
|
||||
local cell_start, cell_end = self:get_cell_positions(row, col)
|
||||
|
||||
return cell_start, cell_end
|
||||
end
|
||||
|
||||
return M
|
||||
|
136
lua/easytables/window.lua
Normal file
136
lua/easytables/window.lua
Normal file
@ -0,0 +1,136 @@
|
||||
local table_builder = require("easytables.tablebuilder")
|
||||
local math = require("math")
|
||||
|
||||
local M = {}
|
||||
|
||||
DEFAULT_OPTIONS = {
|
||||
title = "Table",
|
||||
prompt_title = "Content",
|
||||
width = 60,
|
||||
height = 30,
|
||||
min_value_width = 3,
|
||||
}
|
||||
|
||||
function M:create(options)
|
||||
options = options or {}
|
||||
|
||||
self.title = options.title or DEFAULT_OPTIONS.title
|
||||
self.prompt_title = options.prompt_title or DEFAULT_OPTIONS.prompt_title
|
||||
self.width = options.width or DEFAULT_OPTIONS.width
|
||||
self.height = options.height or DEFAULT_OPTIONS.height
|
||||
self.min_value_width = options.min_value_width or DEFAULT_OPTIONS.min_value_width
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function M:get_x()
|
||||
return math.floor((vim.o.columns - self.width) / 2)
|
||||
end
|
||||
|
||||
function M:get_y()
|
||||
return math.floor(((vim.o.lines - self.height) / 2) - 1)
|
||||
end
|
||||
|
||||
function M:_open_preview_window()
|
||||
self.preview_buffer = vim.api.nvim_create_buf(false, true)
|
||||
self.preview_window = vim.api.nvim_open_win(self.preview_buffer, true, {
|
||||
relative = "win",
|
||||
col = self:get_x(),
|
||||
row = self:get_y(),
|
||||
width = self.width,
|
||||
height = self.height,
|
||||
style = "minimal",
|
||||
border = "rounded",
|
||||
title = self.title,
|
||||
title_pos = "center",
|
||||
})
|
||||
|
||||
vim.api.nvim_set_option_value("readonly", true, { win = self.preview_window })
|
||||
-- Disable default highlight
|
||||
vim.api.nvim_set_option_value("winhighlight", "Normal:Normal",
|
||||
{ win = self.preview_window })
|
||||
end
|
||||
|
||||
function M:_open_prompt_window()
|
||||
self.prompt_buffer = vim.api.nvim_create_buf(false, false)
|
||||
self.prompt_window = vim.api.nvim_open_win(self.prompt_buffer, true, {
|
||||
relative = "win",
|
||||
col = self:get_x() - 1,
|
||||
row = self:get_y() + self.height + 2,
|
||||
width = self.width,
|
||||
height = 2,
|
||||
style = "minimal",
|
||||
border = "rounded",
|
||||
title = self.prompt_title,
|
||||
title_pos = "center",
|
||||
})
|
||||
|
||||
vim.api.nvim_set_option_value('winhighlight', "Normal:Normal", { win = self.prompt_window })
|
||||
end
|
||||
|
||||
function M:show()
|
||||
-- Don't open window again if it's already opened
|
||||
if self.preview_window then
|
||||
return
|
||||
end
|
||||
|
||||
self:_open_preview_window()
|
||||
self:_open_prompt_window()
|
||||
end
|
||||
|
||||
function M:_draw_highlight(table)
|
||||
local cell = table:get_highlighted_cell()
|
||||
|
||||
if cell == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local row = 1 + math.max(0, cell.row - 1) * 2
|
||||
local cell_start, cell_end = table:get_cell_positions(cell.col, cell.row, self.min_value_width)
|
||||
|
||||
print(cell_start, cell_end, row)
|
||||
|
||||
vim.api.nvim_buf_set_extmark(
|
||||
self.preview_buffer,
|
||||
vim.api.nvim_create_namespace("easytables"),
|
||||
row - 1,
|
||||
cell_start,
|
||||
{
|
||||
end_col = cell_end * 4,
|
||||
hl_group = "NormalFloat",
|
||||
hl_mode = "combine",
|
||||
}
|
||||
)
|
||||
vim.api.nvim_buf_set_extmark(
|
||||
self.preview_buffer,
|
||||
vim.api.nvim_create_namespace("easytables"),
|
||||
row + 1,
|
||||
cell_start,
|
||||
{
|
||||
end_col = cell_end * 4,
|
||||
hl_group = "NormalFloat",
|
||||
hl_mode = "combine",
|
||||
}
|
||||
)
|
||||
vim.api.nvim_buf_set_extmark(
|
||||
self.preview_buffer,
|
||||
vim.api.nvim_create_namespace("easytables"),
|
||||
row,
|
||||
cell_start,
|
||||
{
|
||||
end_col = cell_end * 2 + 1,
|
||||
hl_group = "NormalFloat",
|
||||
hl_mode = "combine",
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
function M:draw_table(table)
|
||||
local representation = table_builder.draw_representation(table)
|
||||
|
||||
vim.api.nvim_buf_set_lines(self.preview_buffer, 0, -1, false, representation)
|
||||
|
||||
self:_draw_highlight(table)
|
||||
end
|
||||
|
||||
return M
|
Loading…
x
Reference in New Issue
Block a user