Skip to content

Commit bf4cb70

Browse files
committed
Recover functionality of diff summary virtual text
This introduces a new more general concept where window class derivations can set virtual text. It also finally makes use of a namespace. I'm not too happy with the solution. It might be necessary to change the approach in future if new applications for virtual text come up.
1 parent 832b528 commit bf4cb70

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

lua/code_action_menu/windows/base_window.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local BaseWindow = {
77
buffer_name = 'CodeActionMenuBase',
88
focusable = false,
99
filetype = '',
10+
namespace_id = vim.api.nvim_create_namespace('code_action_menu'),
1011
}
1112

1213
function BaseWindow:new(base_object)
@@ -20,7 +21,13 @@ function BaseWindow:get_content()
2021
return {}
2122
end
2223

24+
function BaseWindow:update_virtual_text()
25+
return
26+
end
27+
2328
function BaseWindow:update_buffer_content()
29+
vim.api.nvim_buf_clear_namespace(self.buffer_number, self.namespace_id, 0, -1)
30+
2431
local content = self:get_content()
2532

2633
if #content == 0 then
@@ -30,6 +37,8 @@ function BaseWindow:update_buffer_content()
3037
vim.api.nvim_buf_set_option(self.buffer_number, 'filetype', '')
3138
vim.api.nvim_buf_set_lines(self.buffer_number, 0, -1, false, content)
3239
vim.api.nvim_buf_set_option(self.buffer_number, 'filetype', self.filetype)
40+
41+
self:update_virtual_text()
3342
end
3443
end
3544

lua/code_action_menu/windows/diff_window.lua

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,9 @@ local function get_diff_square_counts(text_document_edit)
6969
}
7070
end
7171

72-
-- We need to use virtual text here as coloring the squares differently simply
73-
-- is not possible with traditional syntax highlighting. At least not without
74-
-- much pita.
75-
local function add_colored_diff_square_as_virtual_text(buffer_number, action)
76-
local workspace_edit = action:get_workspace_edit()
77-
78-
for index, text_document_edit in ipairs(workspace_edit.all_text_document_edits) do
79-
local square_counts = get_diff_square_counts(text_document_edit)
80-
local chunks = {}
81-
82-
if square_counts.added > 0 then
83-
table.insert(chunks, { string.rep('', square_counts.added), 'CodeActionMenuDetailsAddedSquares' })
84-
end
85-
86-
if square_counts.deleted > 0 then
87-
table.insert(chunks, { string.rep('', square_counts.deleted), 'CodeActionMenuDetailsDeletedSquares' })
88-
end
89-
90-
if square_counts.neutral > 0 then
91-
table.insert(chunks, { string.rep('', square_counts.neutral), 'CodeActionMenuDetailsNeutralSquares' })
92-
end
93-
94-
vim.api.nvim_buf_set_virtual_text(buffer_number, -1, index -1 , chunks, {} )
95-
end
72+
function get_count_of_edits_diff_lines(text_document_edit)
73+
local diff_lines = get_diff_lines_formatted(text_document_edit)
74+
return #diff_lines
9675
end
9776

9877
DiffWindow = StackingWindow:new()
@@ -121,7 +100,37 @@ function DiffWindow:get_content()
121100
end
122101

123102
return content
124-
-- add_colored_diff_square_as_virtual_text(buffer_number, self.action)
103+
end
104+
105+
function DiffWindow:update_virtual_text()
106+
local workspace_edit = self.action:get_workspace_edit()
107+
local summary_line_index = 0
108+
109+
for _, text_document_edit in ipairs(workspace_edit.all_text_document_edits) do
110+
local square_counts = get_diff_square_counts(text_document_edit)
111+
local chunks = {}
112+
113+
if square_counts.added > 0 then
114+
table.insert(chunks, { string.rep('', square_counts.added), 'CodeActionMenuDetailsAddedSquares' })
115+
end
116+
117+
if square_counts.deleted > 0 then
118+
table.insert(chunks, { string.rep('', square_counts.deleted), 'CodeActionMenuDetailsDeletedSquares' })
119+
end
120+
121+
if square_counts.neutral > 0 then
122+
table.insert(chunks, { string.rep('', square_counts.neutral), 'CodeActionMenuDetailsNeutralSquares' })
123+
end
124+
125+
vim.api.nvim_buf_set_virtual_text(
126+
self.buffer_number,
127+
self.namespace_id,
128+
summary_line_index,
129+
chunks,
130+
{}
131+
)
132+
summary_line_index = summary_line_index + get_count_of_edits_diff_lines(text_document_edit)
133+
end
125134
end
126135

127136
function DiffWindow:set_action(action)

0 commit comments

Comments
 (0)