feat(parser): Flatten outline items while parsing
* Removes the need for flatten_outline_items * Improves performance
This commit is contained in:
@@ -22,12 +22,11 @@ end
|
|||||||
-------------------------
|
-------------------------
|
||||||
M.state = {
|
M.state = {
|
||||||
outline_items = {},
|
outline_items = {},
|
||||||
flattened_outline_items = {},
|
|
||||||
code_win = 0,
|
code_win = 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
local function wipe_state()
|
local function wipe_state()
|
||||||
M.state = { outline_items = {}, flattened_outline_items = {}, code_win = 0 }
|
M.state = { outline_items = {}, code_win = 0 }
|
||||||
end
|
end
|
||||||
|
|
||||||
local function __refresh()
|
local function __refresh()
|
||||||
@@ -41,9 +40,8 @@ local function __refresh()
|
|||||||
|
|
||||||
M.state.code_win = vim.api.nvim_get_current_win()
|
M.state.code_win = vim.api.nvim_get_current_win()
|
||||||
M.state.outline_items = items
|
M.state.outline_items = items
|
||||||
M.state.flattened_outline_items = parser.flatten(items)
|
|
||||||
|
|
||||||
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
|
writer.parse_and_write(M.view.bufnr, M.state.outline_items)
|
||||||
end
|
end
|
||||||
|
|
||||||
providers.request_symbols(refresh_handler)
|
providers.request_symbols(refresh_handler)
|
||||||
@@ -54,7 +52,7 @@ M._refresh = utils.debounce(__refresh, 100)
|
|||||||
|
|
||||||
local function goto_location(change_focus)
|
local function goto_location(change_focus)
|
||||||
local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1]
|
local current_line = vim.api.nvim_win_get_cursor(M.view.winnr)[1]
|
||||||
local node = M.state.flattened_outline_items[current_line]
|
local node = M.state.outline_items[current_line]
|
||||||
vim.api.nvim_win_set_cursor(
|
vim.api.nvim_win_set_cursor(
|
||||||
M.state.code_win,
|
M.state.code_win,
|
||||||
{ node.line + 1, node.character }
|
{ node.line + 1, node.character }
|
||||||
@@ -95,7 +93,7 @@ function M._highlight_current_item(winnr)
|
|||||||
local hovered_line = vim.api.nvim_win_get_cursor(win)[1] - 1
|
local hovered_line = vim.api.nvim_win_get_cursor(win)[1] - 1
|
||||||
|
|
||||||
local nodes = {}
|
local nodes = {}
|
||||||
for index, value in ipairs(M.state.flattened_outline_items) do
|
for index, value in ipairs(M.state.outline_items) do
|
||||||
if
|
if
|
||||||
value.line == hovered_line
|
value.line == hovered_line
|
||||||
or (hovered_line > value.range_start and hovered_line < value.range_end)
|
or (hovered_line > value.range_start and hovered_line < value.range_end)
|
||||||
@@ -175,9 +173,8 @@ local function handler(response)
|
|||||||
local items = parser.parse(response)
|
local items = parser.parse(response)
|
||||||
|
|
||||||
M.state.outline_items = items
|
M.state.outline_items = items
|
||||||
M.state.flattened_outline_items = parser.flatten(items)
|
|
||||||
|
|
||||||
writer.parse_and_write(M.view.bufnr, M.state.flattened_outline_items)
|
writer.parse_and_write(M.view.bufnr, M.state.outline_items)
|
||||||
|
|
||||||
M._highlight_current_item(M.state.code_win)
|
M._highlight_current_item(M.state.code_win)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ end
|
|||||||
|
|
||||||
function M.show_code_actions()
|
function M.show_code_actions()
|
||||||
local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
|
local current_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
|
||||||
local node = main.state.flattened_outline_items[current_line]
|
local node = main.state.outline_items[current_line]
|
||||||
|
|
||||||
local params = get_action_params(node, main.state.code_win)
|
local params = get_action_params(node, main.state.code_win)
|
||||||
vim.lsp.buf_request(
|
vim.lsp.buf_request(
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ end
|
|||||||
-- handler yoinked from the default implementation
|
-- handler yoinked from the default implementation
|
||||||
function M.show_hover()
|
function M.show_hover()
|
||||||
local current_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1]
|
local current_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1]
|
||||||
local node = so.state.flattened_outline_items[current_line]
|
local node = so.state.outline_items[current_line]
|
||||||
|
|
||||||
local hover_params = get_hover_params(node, so.state.code_win)
|
local hover_params = get_hover_params(node, so.state.code_win)
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ local function parse_result(result, depth, hierarchy)
|
|||||||
-- whether this node is the last in its group
|
-- whether this node is the last in its group
|
||||||
local isLast = index == #result
|
local isLast = index == #result
|
||||||
|
|
||||||
local children = nil
|
local children = {}
|
||||||
if value.children ~= nil then
|
if value.children ~= nil then
|
||||||
-- copy by value because we dont want it messing with the hir table
|
-- copy by value because we dont want it messing with the hir table
|
||||||
local child_hir = t_utils.array_copy(hir)
|
local child_hir = t_utils.array_copy(hir)
|
||||||
@@ -54,11 +54,15 @@ local function parse_result(result, depth, hierarchy)
|
|||||||
character = selectionRange.start.character,
|
character = selectionRange.start.character,
|
||||||
range_start = range.start.line,
|
range_start = range.start.line,
|
||||||
range_end = range['end'].line,
|
range_end = range['end'].line,
|
||||||
children = children,
|
-- children = children,
|
||||||
depth = level,
|
depth = level,
|
||||||
isLast = isLast,
|
isLast = isLast,
|
||||||
hierarchy = hir,
|
hierarchy = hir,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
for _, node in pairs(children) do
|
||||||
|
table.insert(ret, node)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return ret
|
return ret
|
||||||
@@ -139,25 +143,11 @@ function M.parse(response)
|
|||||||
return parse_result(sorted, nil, nil)
|
return parse_result(sorted, nil, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.flatten(outline_items)
|
function M.get_lines(outline_items)
|
||||||
local ret = {}
|
|
||||||
for _, value in ipairs(outline_items) do
|
|
||||||
table.insert(ret, value)
|
|
||||||
if value.children ~= nil then
|
|
||||||
local inner = M.flatten(value.children)
|
|
||||||
for _, value_inner in ipairs(inner) do
|
|
||||||
table.insert(ret, value_inner)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return ret
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.get_lines(flattened_outline_items)
|
|
||||||
local lines = {}
|
local lines = {}
|
||||||
local hl_info = {}
|
local hl_info = {}
|
||||||
|
|
||||||
for node_line, node in ipairs(flattened_outline_items) do
|
for node_line, node in ipairs(outline_items) do
|
||||||
local line = t_utils.str_to_table(string.rep(' ', node.depth))
|
local line = t_utils.str_to_table(string.rep(' ', node.depth))
|
||||||
local running_length = 1
|
local running_length = 1
|
||||||
|
|
||||||
@@ -224,9 +214,9 @@ function M.get_lines(flattened_outline_items)
|
|||||||
return lines, hl_info
|
return lines, hl_info
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_details(flattened_outline_items)
|
function M.get_details(outline_items)
|
||||||
local lines = {}
|
local lines = {}
|
||||||
for _, value in ipairs(flattened_outline_items) do
|
for _, value in ipairs(outline_items) do
|
||||||
table.insert(lines, value.detail or '')
|
table.insert(lines, value.detail or '')
|
||||||
end
|
end
|
||||||
return lines
|
return lines
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ end
|
|||||||
|
|
||||||
local function get_hovered_node()
|
local function get_hovered_node()
|
||||||
local hovered_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
|
local hovered_line = vim.api.nvim_win_get_cursor(main.state.outline_win)[1]
|
||||||
local node = main.state.flattened_outline_items[hovered_line]
|
local node = main.state.outline_items[hovered_line]
|
||||||
return node
|
return node
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ end
|
|||||||
|
|
||||||
function M.rename()
|
function M.rename()
|
||||||
local current_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1]
|
local current_line = vim.api.nvim_win_get_cursor(so.view.winnr)[1]
|
||||||
local node = so.state.flattened_outline_items[current_line]
|
local node = so.state.outline_items[current_line]
|
||||||
|
|
||||||
local params = get_rename_params(node, so.state.code_win)
|
local params = get_rename_params(node, so.state.code_win)
|
||||||
|
|
||||||
|
|||||||
@@ -62,12 +62,12 @@ end
|
|||||||
|
|
||||||
-- runs the whole writing routine where the text is cleared, new data is parsed
|
-- runs the whole writing routine where the text is cleared, new data is parsed
|
||||||
-- and then written
|
-- and then written
|
||||||
function M.parse_and_write(bufnr, flattened_outline_items)
|
function M.parse_and_write(bufnr, outline_items)
|
||||||
local lines, hl_info = parser.get_lines(flattened_outline_items)
|
local lines, hl_info = parser.get_lines(outline_items)
|
||||||
M.write_outline(bufnr, lines)
|
M.write_outline(bufnr, lines)
|
||||||
|
|
||||||
clear_virt_text(bufnr)
|
clear_virt_text(bufnr)
|
||||||
local details = parser.get_details(flattened_outline_items)
|
local details = parser.get_details(outline_items)
|
||||||
M.add_highlights(bufnr, hl_info)
|
M.add_highlights(bufnr, hl_info)
|
||||||
M.write_details(bufnr, details)
|
M.write_details(bufnr, details)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user