feat: Line numbers next to symbols for quick jumping

See reasoning in:
simrat39/symbols-outline.nvim#212

This feature is turned off by default to avoid clutter, and many users
might not want this feature on by default.
This commit is contained in:
hedy
2023-11-08 08:02:42 +08:00
parent 03859e9ab5
commit 0e785762f7
4 changed files with 138 additions and 8 deletions

View File

@@ -80,7 +80,7 @@ features:
## Features
[Skip this section](#symbols-outlinenvim)
[Skip to plugin readme](#symbols-outlinenvim)
Below is a list of features I've included in this fork which, at the time of
writing, has not been included upstream (in the original repo). I try my best to
@@ -99,6 +99,11 @@ Features/Changes:
- simrat39/symbols-outline.nvim#174
- simrat39/symbols-outline.nvim#207
- Show line number of each symbol in outline window (see [recipes](#recipes)
for a screenshot)
- Fixed issues:
- simrat39/symbols-outline.nvim#212
- Cursorline option for the outline window.
- Added function and command to show provider and outline window status,
@@ -120,7 +125,7 @@ Screen recordings of some of the features is shown at the bottom of the readme.
## PRs
[Skip this section](#symbols-outlinenvim)
[Skip to plugin readme](#symbols-outlinenvim)
Key:
```
@@ -128,6 +133,9 @@ Key:
📮 = Planned for merge
```
- 📮 center view on goto symbol
(#239 by skomposzczet)
- ✅ Open handler checks if view is not already open
(#235 by eyalz800)
@@ -206,7 +214,7 @@ Key:
## TODO
[Skip this section](#symbols-outlinenvim)
[Skip to plugin readme](#symbols-outlinenvim)
Key:
```
@@ -215,8 +223,6 @@ Key:
- : Idea
```
Items will be moved to above list when complete.
- Folds
- `[ ]` Org-like <kbd>shift+tab</kbd> behavior: Open folds level-by-level
- `[ ]` Optionally not opening all child nodes when opening parent node
@@ -238,7 +244,8 @@ Items will be moved to above list when complete.
- View
- `[/]` Outline window customizations (simrat39/symbols-outline#137)
- `[/]` Option to show line number next to symbols
- Option to show line number next to symbols (simrat39/symbols-outline#212)
- `[/]` Option to hide cursor in outline window if cursorline enabled
## Related plugins
@@ -259,6 +266,24 @@ Supports all your favourite languages.**
![demo](https://github.com/simrat39/rust-tools-demos/raw/master/symbols-demo.gif)
Table of contents
<!-- mtoc start -->
* [Prerequisites](#prerequisites)
* [Installation](#installation)
* [Setup](#setup)
* [Configuration](#configuration)
* [Terminology](#terminology)
* [Options](#options)
* [Commands](#commands)
* [Lua API](#lua-api)
* [Default keymaps](#default-keymaps)
* [Highlights](#highlights)
* [Recipes](#recipes)
<!-- mtoc end -->
## Prerequisites
- `neovim 0.7+`
@@ -388,6 +413,11 @@ local opts = {
show_cursorline = true, -- Only in this fork
-- Show extra details with the symbols (lsp dependent)
show_symbol_details = true,
-- Only in this fork.
-- Show line numbers of each symbol next to them.
-- Why? See this comment:
-- https://github.com/simrat39/symbols-outline.nvim/issues/212#issuecomment-1793503563
show_symbol_lineno = false,
-- Highlight group for the preview background
preview_bg_highlight = 'Pmenu',
-- Depth past which nodes will be folded by default
@@ -665,3 +695,21 @@ This feature was added by @stickperson in an upstream PR 🙌
https://github.com/hedyhli/symbols-outline.nvim/assets/50042066/3d06e342-97ac-400c-8598-97a9235de66c
**Hide the extra details after each symbol name**
```lua
show_symbol_details = false,
```
**Show line numbers next to each symbol to jump to that symbol quickly**
```lua
show_symbol_lineno = true,
```
The default highlight group is `LineNr`.
<img width="900" alt="image" src="https://github.com/hedyhli/symbols-outline.nvim/assets/50042066/2bbb5833-f40b-4c53-8338-407252d61443">

View File

@@ -25,6 +25,7 @@ M.defaults = {
show_relative_numbers = false,
show_cursorline = true,
show_symbol_details = true,
show_symbol_lineno = false,
preview_bg_highlight = 'Pmenu',
winblend = 0,
autofold_depth = nil,

View File

@@ -94,6 +94,8 @@ end
function M.get_lines(flattened_outline_items)
local lines = {}
local hl_info = {}
local guide_hl_info = {}
local lineno_max = 0
for node_line, node in ipairs(flattened_outline_items) do
local depth = node.depth
@@ -102,8 +104,12 @@ function M.get_lines(flattened_outline_items)
local line = t_utils.str_to_table(string.rep(' ', depth + marker_space))
local running_length = 1
if node.range_start+1 > lineno_max then
lineno_max = node.range_start+1
end
local function add_guide_hl(from, to)
table.insert(hl_info, {
table.insert(guide_hl_info, {
node_line,
from,
to,
@@ -184,7 +190,46 @@ function M.get_lines(flattened_outline_items)
node.prefix_length = #string_prefix + #node.icon + 1
end
return lines, hl_info
local final_hl = {}
if config.options.show_symbol_lineno then
-- Width of the highest lineno value
local max_width = #tostring(lineno_max)
-- Padded prefix to the right of lineno for better readability if linenos
-- get more than 2 digits.
local prefix = string.rep(' ', math.max(2, max_width) - 1)
-- Offset to hl_info due to adding lineno on the left of each symbol line
local total_offset = #prefix
for i, node in ipairs(flattened_outline_items) do
lines[i] = prefix .. lines[i]
table.insert(final_hl, {
hl_info[i][1], -- node_line
hl_info[i][2] + total_offset, -- start
hl_info[i][3] + total_offset, -- end
hl_info[i][4] -- type
})
node.prefix_length = node.prefix_length + total_offset
end
if config.options.guides.enabled then
for _, hl in ipairs(guide_hl_info) do
table.insert(final_hl, {
hl[1],
hl[2] + total_offset,
hl[3] + total_offset,
hl[4]
})
end
end
else
-- Merge lists hl_info and guide_hl_info
final_hl = hl_info
if config.options.guides.enabled then
for _, hl in ipairs(guide_hl_info) do
table.insert(final_hl, hl)
end
end
end
return lines, final_hl
end
function M.get_details(flattened_outline_items)
@@ -195,4 +240,18 @@ function M.get_details(flattened_outline_items)
return lines
end
function M.get_lineno(flattened_outline_items)
local lines = {}
local max = 0
for _, value in ipairs(flattened_outline_items) do
local line = value.range_start+1
if line > max then
max = line
end
-- Not padded here
table.insert(lines, tostring(line))
end
return lines, max
end
return M

View File

@@ -65,6 +65,26 @@ function M.write_details(bufnr, lines)
end
end
function M.write_lineno(bufnr, lines, max)
if not is_buffer_outline(bufnr) then
return
end
if not config.options.show_symbol_lineno then
return
end
local maxwidth = #tostring(max)
for index, value in ipairs(lines) do
local leftpad = string.rep(' ', maxwidth-#value)
vim.api.nvim_buf_set_extmark(bufnr, ns, index - 1, -1, {
virt_text = { {leftpad..value, 'LineNr' } },
virt_text_pos = 'overlay',
virt_text_win_col = 0,
hl_mode = 'combine',
})
end
end
local function clear_virt_text(bufnr)
vim.api.nvim_buf_clear_namespace(bufnr, -1, 0, -1)
end
@@ -101,8 +121,10 @@ function M.parse_and_write(bufnr, flattened_outline_items)
clear_virt_text(bufnr)
local details = parser.get_details(flattened_outline_items)
local lineno, lineno_max = parser.get_lineno(flattened_outline_items)
M.add_highlights(bufnr, hl_info, flattened_outline_items)
M.write_details(bufnr, details)
M.write_lineno(bufnr, lineno, lineno_max)
end
return M