Skip to content

Commit 28608d9

Browse files
author
cht
committed
add two more overflow
1 parent a6656bd commit 28608d9

2 files changed

Lines changed: 79 additions & 29 deletions

File tree

lua/csvtools.lua

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,31 @@ local api = vim.api
22
local highlight = require("csvtools.highlight")
33
local overflow = require("csvtools.overflowtext")
44
local M = {
5-
winid = nil,
6-
buf = nil,
7-
mainwindowbuf = nil,
8-
header = {},
5+
--winid = nil,
6+
--buf = nil,
7+
--mainwindowbuf = nil,
8+
--header = {},
99
before = 20,
1010
after = 20,
1111
clearafter = true,
1212
showoverflow = true,
13-
overflowtext = {
14-
markid = nil,
15-
ns_id = nil,
16-
id = nil,
17-
},
13+
--overflowtext = {
14+
-- markid = nil,
15+
-- ns_id = nil,
16+
-- id = nil,
17+
--},
18+
}
19+
-- buf's status
20+
local Status = {
21+
winid = nil,
22+
buf = nil,
23+
mainwindowbuf = nil,
24+
header = {},
25+
overflowtext = {},
1826
}
27+
function M.printheader()
28+
return Status.header
29+
end
1930
function M.Ifclear()
2031
if M.clearafter then
2132
M.clearafter = false
@@ -24,8 +35,8 @@ function M.Ifclear()
2435
end
2536
end
2637
function M.NewWindow()
27-
if M.winid == nil then
28-
M.mainwindowbuf = vim.api.nvim_get_current_buf()
38+
if Status.winid == nil then
39+
Status.mainwindowbuf = vim.api.nvim_get_current_buf()
2940
--local file = vim.api.nvim_buf_get_name(0)
3041
--local f = io.open(file, "r")
3142
local messages = unpack(api.nvim_buf_get_lines(M.mainwindowbuf, 0, 1, true))
@@ -40,19 +51,19 @@ function M.NewWindow()
4051
local win = vim.api.nvim_get_current_win()
4152
api.nvim_buf_set_lines(buf, 0, -1, false, { messages })
4253
api.nvim_win_set_buf(win, buf)
43-
M.header = highlight.highlighttop(buf, messages)
44-
M.winid = win
45-
M.buf = buf
54+
Status.header = highlight.highlighttop(buf, messages)
55+
Status.winid = win
56+
Status.buf = buf
4657
M.add_mappings()
4758
end
4859
end
4960

5061
function M.CloseWindow()
51-
if M.winid ~= nil then
62+
if Status.winid ~= nil then
5263
vim.api.nvim_win_close(M.winid, true)
53-
M.winid = nil
54-
M.buf = nil
55-
M.header = {}
64+
Status.winid = nil
65+
Status.buf = nil
66+
Status.header = {}
5667
end
5768
end
5869

@@ -70,8 +81,30 @@ local function getrange(line, length)
7081
end
7182
return start, final
7283
end
84+
-- only 3 overflow
85+
local function getrangeoverflow(line, length)
86+
local start = 1
87+
if line - 1 > 1 then
88+
start = line - 1
89+
end
90+
local final = length
91+
if line + 1 < length then
92+
final = line + 1
93+
end
94+
return start, final
95+
end
7396
function M.Highlight()
7497
if vim.o.filetype == "csv" then
98+
if M.showoverflow then
99+
for count = 1, #Status.overflowtext do
100+
vim.api.nvim_buf_del_extmark(
101+
Status.overflowtext[count].markid,
102+
Status.overflowtext[count].ns_id,
103+
Status.overflowtext[count].id
104+
)
105+
end
106+
end
107+
Status.overflowtext = {}
75108
M.mainwindowbuf = vim.api.nvim_get_current_buf()
76109
local line, _ = unpack(vim.api.nvim_win_get_cursor(0))
77110
--print(line)
@@ -80,22 +113,39 @@ function M.Highlight()
80113
api.nvim_buf_clear_highlight(M.mainwindowbuf, -1, 0, length)
81114
end
82115
local start, final = getrange(line, length)
116+
local start2, final2 = getrangeoverflow(line, length)
83117
--print(start)
84118
--print(final)
85-
if M.showoverflow then
86-
M.overflowtext = overflow.OverFlow(line, M.header)
87-
end
88-
for i = start, line, 1 do
119+
highlight.highlight(M.mainwindowbuf, line)
120+
for i = start, line - 1, 1 do
89121
highlight.highlight(M.mainwindowbuf, i)
90122
end
91-
for i = line, final, 1 do
123+
for i = line + 1, final, 1 do
92124
highlight.highlight(M.mainwindowbuf, i)
93125
end
126+
if M.showoverflow then
127+
table.insert(Status.overflowtext, overflow.OverFlow(line, Status.header, 1))
128+
local count = 2
129+
for i = start2, line - 1, 1 do
130+
table.insert(Status.overflowtext, overflow.OverFlow(i, Status.header, count))
131+
--highlight.highlight(M.mainwindowbuf, count)
132+
count = count + 1
133+
end
134+
for i = line + 1, final2, 1 do
135+
table.insert(Status.overflowtext, overflow.OverFlow(i, Status.header, count))
136+
--highlight.highlight(M.mainwindowbuf, count)
137+
count = count + 1
138+
end
139+
end
94140
end
95141
end
96142
function M.deleteMark()
97143
if M.showoverflow then
98-
vim.api.nvim_buf_del_extmark(M.overflowtext.markid, M.overflowtext.ns_id, M.overflowtext.id)
144+
vim.api.nvim_buf_del_extmark(
145+
Status.overflowtext[1].markid,
146+
Status.overflowtext[1].ns_id,
147+
Status.overflowtext[1].id
148+
)
99149
end
100150
end
101151
function M.add_mappings()

lua/csvtools/overflowtext.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ local function Split(s, delimiter)
66
end
77
return result
88
end
9-
local ns_id = vim.api.nvim_create_namespace("demo")
10-
function M.OverFlow(line_num, header)
9+
local ns_id = vim.api.nvim_create_namespace("csvoverview")
10+
function M.OverFlow(line_num, header, id)
1111
local bnr = vim.fn.bufnr("%")
1212
local line = unpack(vim.api.nvim_buf_get_lines(0, line_num - 1, line_num, true))
1313
local output = Split(line, ",")
@@ -40,8 +40,8 @@ function M.OverFlow(line_num, header)
4040
--print(output[count])
4141
end
4242
local opts = {
43-
end_line = 10,
44-
id = 1,
43+
end_line = 1,
44+
id = id,
4545
virt_text = virt_text,
4646
virt_text_pos = "overlay",
4747
-- virt_text_win_col = 20,
@@ -51,7 +51,7 @@ function M.OverFlow(line_num, header)
5151
return {
5252
markid = vim.api.nvim_buf_set_extmark(bnr, ns_id, line_num - 1, 0, opts),
5353
ns_id = ns_id,
54-
id = 1,
54+
id = id,
5555
}
5656
end
5757
return M

0 commit comments

Comments
 (0)