Member-only story

Neovim for Beginners — Code Context

Show code context with line number using the window bar.

--

Neovim for Beginners — Code Context

In the previous articles, we learned how to customize the window bar, and use the lualine.nvim plugin to show the current code context together with other information like diagnostics.

In this article, we will customize the code context further to add the absolute or relative line number to ease navigation.

This article is part of the Neovim for Beginners series.

The Neovim configuration files are available in this repository.

Check out the related articles if you are unfamiliar with the window bar.

Getting Started

Code Context using Tree-sitter

The nvim-treesitter-context plugin shows the current code context using Tree-sitter.

For this plugin, depending on the relativenumber option, it shows the line number relative to the current cursor position, or the actual line number.

nvim-treesitter-context

This feature makes it easy for us to jump to the enclosing code context.

Code Context using LSP

We configured the window bar using the nvim-navic plugin. This plugin uses LSP to show the current code context.

Specifically, this plugin requires a language server that supports the document symbol feature (:h vim.lsp.buf.document_symbol()).

One specific feature missing from this plugin is that we are unable to display the line number.

Update:

  • nvim-navic get_data API now provides the scope information that we can use to calculate the absolute or relative position. Check out this article for details.

--

--

alpha2phi
alpha2phi

Written by alpha2phi

Software engineer, Data Science and ML practitioner.

No responses yet