Skip to content

Commit

Permalink
feat(lsp): exact-match support for lualine component (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Feb 2, 2022
1 parent 8e7911f commit f19e748
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ignore = {
"212", -- Unused argument
"631", -- Line is too long
"122", -- Setting a readonly global
"542", -- Empty if branch
}

read_globals = {
Expand Down
2 changes: 2 additions & 0 deletions lua/aerial/backends/lsp/callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ local function process_symbols(symbols, bufnr)
parent = parent,
lnum = range.start.line + 1,
col = range.start.character,
end_lnum = range["end"].line + 1,
end_col = range["end"].character,
}

-- Skip this symbol if it's in the same location as the last one.
Expand Down
22 changes: 21 additions & 1 deletion lua/aerial/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,18 @@ M.on_attach = function(...)
end

-- Returns a list representing the symbol path to the current location.
-- exact (bool): If true, only return symbols if we are exactly inside the
-- hierarchy. When false, will return the closest symbol.
-- Returns empty list if none found or in an invalid buffer.
-- Items have the following keys:
-- name The name of the symbol
-- kind The SymbolKind of the symbol
-- icon The icon that represents the symbol
M.get_location = function()
M.get_location = function(exact)
-- exact defaults to true
if exact == nil then
exact = true
end
if not data:has_symbols(0) then
return {}
end
Expand All @@ -113,13 +119,27 @@ M.get_location = function()
return {}
end
local item = bufdata:item(pos.lnum)
local cur = vim.api.nvim_win_get_cursor(0)
local ret = {}
while item do
if exact then
if not item.end_lnum or not item.end_col then
-- end_lnum/end_col isn't supported by all backends yet
elseif
item.lnum > cur[1]
or item.end_lnum < cur[1]
or (item.lnum == cur[1] and item.col > cur[2])
or (item.end_lnum == cur[1] and item.end_col < cur[2])
then
goto continue
end
end
table.insert(ret, 1, {
kind = item.kind,
icon = config.get_icon(item.kind),
name = item.name,
})
::continue::
item = item.parent
end
return ret
Expand Down
5 changes: 3 additions & 2 deletions lua/lualine/components/aerial.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ local default_options = {
depth = nil,
dense = false,
dense_sep = ".",
exact = true,
}

local function format_status(symbols, depth, separator, icons_enabled)
Expand Down Expand Up @@ -86,7 +87,7 @@ function M:update_status()
end

function M:get_status_normal()
local symbols = aerial.get_location()
local symbols = aerial.get_location(self.options.exact)
local status = format_status(
symbols,
self.options.depth,
Expand All @@ -97,7 +98,7 @@ function M:get_status_normal()
end

function M:get_status_dense()
local symbols = aerial.get_location()
local symbols = aerial.get_location(self.options.exact)
local status = format_status(
symbols,
self.options.depth,
Expand Down

0 comments on commit f19e748

Please sign in to comment.