MAJOR: Project rename and preparation for v1.0.0
I hope I haven't missed any for the renames!
This commit is contained in:
126
lua/outline/utils/init.lua
Normal file
126
lua/outline/utils/init.lua
Normal file
@@ -0,0 +1,126 @@
|
||||
local M = {}
|
||||
|
||||
---maps the table|string of keys to the action
|
||||
---@param keys table|string
|
||||
---@param action function|string
|
||||
function M.nmap(bufnr, keys, action)
|
||||
if type(keys) == 'string' then
|
||||
keys = { keys }
|
||||
end
|
||||
|
||||
for _, lhs in ipairs(keys) do
|
||||
vim.keymap.set(
|
||||
'n',
|
||||
lhs,
|
||||
action,
|
||||
{ silent = true, noremap = true, buffer = bufnr }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param f function
|
||||
--- @param delay number
|
||||
--- @return function
|
||||
function M.debounce(f, delay)
|
||||
local timer = vim.loop.new_timer()
|
||||
|
||||
return function(...)
|
||||
local args = { ... }
|
||||
|
||||
timer:start(
|
||||
delay,
|
||||
0,
|
||||
vim.schedule_wrap(function()
|
||||
timer:stop()
|
||||
f(unpack(args))
|
||||
end)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
function M.items_dfs(callback, children)
|
||||
for _, val in ipairs(children) do
|
||||
callback(val)
|
||||
|
||||
if val.children then
|
||||
M.items_dfs(callback, val.children)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Merges a symbol tree recursively, only replacing nodes
|
||||
---which have changed. This will maintain the folding
|
||||
---status of any unchanged nodes.
|
||||
---@param new_node table New node
|
||||
---@param old_node table Old node
|
||||
---@param index? number Index of old_item in parent
|
||||
---@param parent? table Parent of old_item
|
||||
M.merge_items_rec = function(new_node, old_node, index, parent)
|
||||
local failed = false
|
||||
|
||||
if not new_node or not old_node then
|
||||
failed = true
|
||||
else
|
||||
for key, _ in pairs(new_node) do
|
||||
if
|
||||
vim.tbl_contains({
|
||||
'parent',
|
||||
'children',
|
||||
'folded',
|
||||
'hovered',
|
||||
'line_in_outline',
|
||||
'hierarchy',
|
||||
}, key)
|
||||
then
|
||||
goto continue
|
||||
end
|
||||
|
||||
if key == 'name' then
|
||||
-- in the case of a rename, just rename the existing node
|
||||
old_node['name'] = new_node['name']
|
||||
else
|
||||
if not vim.deep_equal(new_node[key], old_node[key]) then
|
||||
failed = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
if failed then
|
||||
if parent and index then
|
||||
parent[index] = new_node
|
||||
end
|
||||
else
|
||||
local next_new_item = new_node.children or {}
|
||||
|
||||
-- in case new children are created on a node which
|
||||
-- previously had no children
|
||||
if #next_new_item > 0 and not old_node.children then
|
||||
old_node.children = {}
|
||||
end
|
||||
|
||||
local next_old_item = old_node.children or {}
|
||||
|
||||
for i = 1, math.max(#next_new_item, #next_old_item) do
|
||||
M.merge_items_rec(next_new_item[i], next_old_item[i], i, next_old_item)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
M.flash_highlight = function(winnr, lnum, durationMs, hl_group)
|
||||
hl_group = hl_group or "Visual"
|
||||
if durationMs == true or durationMs == 1 then
|
||||
durationMs = 300
|
||||
end
|
||||
local bufnr = vim.api.nvim_win_get_buf(winnr)
|
||||
local ns = vim.api.nvim_buf_add_highlight(bufnr, 0, hl_group, lnum - 1, 0, -1)
|
||||
local remove_highlight = function()
|
||||
pcall(vim.api.nvim_buf_clear_namespace, bufnr, ns, 0, -1)
|
||||
end
|
||||
vim.defer_fn(remove_highlight, durationMs)
|
||||
end
|
||||
|
||||
return M
|
||||
128
lua/outline/utils/jsx.lua
Normal file
128
lua/outline/utils/jsx.lua
Normal file
@@ -0,0 +1,128 @@
|
||||
local M = {}
|
||||
|
||||
local SYMBOL_COMPONENT = 27
|
||||
local SYMBOL_FRAGMENT = 28
|
||||
|
||||
local function get_open_tag(node)
|
||||
if node:type() == 'jsx_element' then
|
||||
for _, outer in ipairs(node:field 'open_tag') do
|
||||
if outer:type() == 'jsx_opening_element' then
|
||||
return outer
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
local function jsx_node_detail(node, buf)
|
||||
node = get_open_tag(node) or node
|
||||
|
||||
local param_nodes = node:field 'attribute'
|
||||
if #param_nodes == 0 then
|
||||
return nil
|
||||
end
|
||||
|
||||
local res = '{ '
|
||||
.. table.concat(
|
||||
vim.tbl_map(function(el)
|
||||
local a, b, c, d = el:range()
|
||||
local text = vim.api.nvim_buf_get_text(buf, a, b, c, d, {})
|
||||
return text[1]
|
||||
end, param_nodes),
|
||||
' '
|
||||
)
|
||||
.. ' }'
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
local function jsx_node_tagname(node, buf)
|
||||
local tagnode = get_open_tag(node) or node
|
||||
|
||||
local identifier = nil
|
||||
|
||||
for _, val in ipairs(tagnode:field 'name') do
|
||||
if val:type() == 'identifier' then
|
||||
identifier = val
|
||||
end
|
||||
end
|
||||
|
||||
if identifier then
|
||||
local a, b, c, d = identifier:range()
|
||||
local text = vim.api.nvim_buf_get_text(buf, a, b, c, d, {})
|
||||
local name = table.concat(text)
|
||||
return name
|
||||
end
|
||||
end
|
||||
|
||||
local function convert_ts(child, children, bufnr)
|
||||
local is_frag = (child:type() == 'jsx_fragment')
|
||||
|
||||
local a, b, c, d = child:range()
|
||||
local range = {
|
||||
start = { line = a, character = b },
|
||||
['end'] = { line = c, character = d },
|
||||
}
|
||||
|
||||
local converted = {
|
||||
name = (not is_frag and (jsx_node_tagname(child, bufnr) or '<unknown>'))
|
||||
or 'fragment',
|
||||
children = (#children > 0 and children) or nil,
|
||||
kind = (is_frag and SYMBOL_FRAGMENT) or SYMBOL_COMPONENT,
|
||||
detail = jsx_node_detail(child, bufnr),
|
||||
range = range,
|
||||
selectionRange = range,
|
||||
}
|
||||
|
||||
return converted
|
||||
end
|
||||
|
||||
function M.parse_ts(root, children, bufnr)
|
||||
children = children or {}
|
||||
|
||||
for child in root:iter_children() do
|
||||
if
|
||||
vim.tbl_contains(
|
||||
{ 'jsx_element', 'jsx_self_closing_element' },
|
||||
child:type()
|
||||
)
|
||||
then
|
||||
local new_children = {}
|
||||
|
||||
M.parse_ts(child, new_children, bufnr)
|
||||
|
||||
table.insert(children, convert_ts(child, new_children, bufnr))
|
||||
else
|
||||
M.parse_ts(child, children, bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
return children
|
||||
end
|
||||
|
||||
function M.get_symbols()
|
||||
local status, parsers = pcall(require, 'nvim-treesitter.parsers')
|
||||
|
||||
if not status then
|
||||
return {}
|
||||
end
|
||||
|
||||
local bufnr = 0
|
||||
|
||||
local parser = parsers.get_parser(bufnr)
|
||||
|
||||
if parser == nil then
|
||||
return {}
|
||||
end
|
||||
|
||||
local root = parser:parse()[1]:root()
|
||||
|
||||
if root == nil then
|
||||
return {}
|
||||
end
|
||||
|
||||
return M.parse_ts(root, nil, bufnr)
|
||||
end
|
||||
|
||||
return M
|
||||
185
lua/outline/utils/lsp_utils.lua
Normal file
185
lua/outline/utils/lsp_utils.lua
Normal file
@@ -0,0 +1,185 @@
|
||||
local config = require 'outline.config'
|
||||
local tbl_utils = require 'outline.utils.table'
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.is_buf_attached_to_lsp(bufnr)
|
||||
local clients = vim.lsp.buf_get_clients(bufnr or 0)
|
||||
return clients ~= nil and #clients > 0
|
||||
end
|
||||
|
||||
function M.is_buf_markdown(bufnr)
|
||||
return vim.api.nvim_buf_get_option(bufnr, 'ft') == 'markdown'
|
||||
end
|
||||
|
||||
--- Merge all client token lists in an LSP response
|
||||
function M.flatten_response(response)
|
||||
local all_results = {}
|
||||
|
||||
-- flatten results to one giant table of symbols
|
||||
for client_id, client_response in pairs(response) do
|
||||
if config.is_client_blacklisted(client_id) then
|
||||
print('skipping client ' .. client_id)
|
||||
goto continue
|
||||
end
|
||||
|
||||
local result = client_response['result']
|
||||
if result == nil or type(result) ~= 'table' then
|
||||
goto continue
|
||||
end
|
||||
|
||||
for _, value in pairs(result) do
|
||||
table.insert(all_results, value)
|
||||
end
|
||||
|
||||
::continue::
|
||||
end
|
||||
|
||||
return all_results
|
||||
end
|
||||
|
||||
function M.get_selection_range(token)
|
||||
-- support symbolinformation[]
|
||||
-- https://microsoft.github.io/language-server-protocol/specification#textdocument_documentsymbol
|
||||
if token.selectionRange == nil then
|
||||
return token.location.range
|
||||
end
|
||||
|
||||
return token.selectionRange
|
||||
end
|
||||
|
||||
function M.get_range(token)
|
||||
if token == nil then
|
||||
return {
|
||||
start={ line=math.huge, character=math.huge },
|
||||
['end']={ line=-math.huge, character=-math.huge },
|
||||
}
|
||||
end
|
||||
|
||||
-- support symbolinformation[]
|
||||
-- https://microsoft.github.io/language-server-protocol/specification#textdocument_documentsymbol
|
||||
if token.range == nil then
|
||||
return token.location.range
|
||||
end
|
||||
|
||||
return token.range
|
||||
end
|
||||
|
||||
--- lexicographically strict compare Ranges, line first
|
||||
--- https://microsoft.github.io/language-server-protocol/specification/#range
|
||||
local function range_compare(a, b)
|
||||
if a == nil and b == nil then
|
||||
return true
|
||||
end
|
||||
|
||||
if a == nil then
|
||||
return true
|
||||
end
|
||||
|
||||
if b == nil then
|
||||
return false
|
||||
end
|
||||
|
||||
return (a.line < b.line) or (a.line == b.line and a.character < b.character)
|
||||
end
|
||||
|
||||
--- Sorts the result from LSP by where the symbols start.
|
||||
function M.sort_symbols(symbols)
|
||||
table.sort(symbols, function(a, b)
|
||||
return range_compare(
|
||||
M.get_range(a).start,
|
||||
M.get_range(b).start
|
||||
)
|
||||
end)
|
||||
|
||||
for _, child in ipairs(symbols) do
|
||||
if child.children ~= nil then
|
||||
M.sort_symbols(child.children)
|
||||
end
|
||||
end
|
||||
|
||||
return symbols
|
||||
end
|
||||
|
||||
--- Preorder DFS iterator on the symbol tree
|
||||
function M.symbol_preorder_iter(symbols)
|
||||
local stk = {}
|
||||
|
||||
local function push_stk(symbols_list)
|
||||
for i = #symbols_list, 1, -1 do
|
||||
table.insert(stk, symbols_list[i])
|
||||
end
|
||||
end
|
||||
|
||||
push_stk(symbols)
|
||||
|
||||
local function is_empty()
|
||||
return #stk == 0
|
||||
end
|
||||
|
||||
local function next()
|
||||
if not is_empty() then
|
||||
local top = table.remove(stk)
|
||||
|
||||
push_stk(top and top.children or {})
|
||||
|
||||
return top
|
||||
end
|
||||
end
|
||||
|
||||
local function peek()
|
||||
return stk[#stk]
|
||||
end
|
||||
|
||||
return { next=next, is_empty=is_empty, peek=peek }
|
||||
end
|
||||
|
||||
local function merge_symbols_rec(iter1, iter2, ub)
|
||||
local res = {}
|
||||
|
||||
while not (iter1.is_empty() and iter2.is_empty()) do
|
||||
local bv1 = ((not iter1.is_empty()) and M.get_range(iter1.peek()).start) or { line=math.huge, character=math.huge }
|
||||
local bv2 = ((not iter2.is_empty()) and M.get_range(iter2.peek()).start) or { line=math.huge, character=math.huge }
|
||||
|
||||
local iter = (range_compare(bv1, bv2) and iter1) or iter2
|
||||
|
||||
if ub ~= nil and range_compare(ub, M.get_range(iter.peek()).start) then
|
||||
break
|
||||
end
|
||||
|
||||
local node = iter.next()
|
||||
|
||||
node.new_children = merge_symbols_rec(iter1, iter2, M.get_range(node)['end'])
|
||||
|
||||
table.insert(res, node)
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
--- Merge symbols from two symbol trees
|
||||
--- NOTE: Symbols are mutated!
|
||||
function M.merge_symbols(symbols1, symbols2)
|
||||
M.sort_symbols(symbols1)
|
||||
M.sort_symbols(symbols2)
|
||||
|
||||
local iter1 = M.symbol_preorder_iter(symbols1)
|
||||
local iter2 = M.symbol_preorder_iter(symbols2)
|
||||
|
||||
local symbols = merge_symbols_rec(iter1, iter2)
|
||||
|
||||
local function dfs(nodes)
|
||||
for _, node in ipairs(nodes) do
|
||||
dfs(node.new_children or {})
|
||||
|
||||
node.children = node.new_children
|
||||
node.new_children = nil
|
||||
end
|
||||
end
|
||||
|
||||
dfs(symbols)
|
||||
|
||||
return symbols
|
||||
end
|
||||
|
||||
return M
|
||||
49
lua/outline/utils/table.lua
Normal file
49
lua/outline/utils/table.lua
Normal file
@@ -0,0 +1,49 @@
|
||||
local M = {}
|
||||
|
||||
function M.table_to_str(t)
|
||||
local ret = ''
|
||||
for _, value in ipairs(t) do
|
||||
ret = ret .. tostring(value)
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
function M.str_to_table(str)
|
||||
local t = {}
|
||||
for i = 1, #str do
|
||||
t[i] = str:sub(i, i)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
--- Copies an array and returns it because lua usually does references
|
||||
---@generic T
|
||||
---@param t T[]
|
||||
---@return T[]
|
||||
function M.array_copy(t)
|
||||
local ret = {}
|
||||
for _, value in ipairs(t) do
|
||||
table.insert(ret, value)
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
|
||||
--- Deep copy a table, deeply excluding certain keys
|
||||
function M.deepcopy_excluding(t, keys)
|
||||
local res = {}
|
||||
|
||||
for key, value in pairs(t) do
|
||||
if not vim.tbl_contains(keys, key) then
|
||||
if type(value) == "table" then
|
||||
res[key] = M.deepcopy_excluding(value, keys)
|
||||
else
|
||||
res[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user