Neovim 101 — Contextual Semantic Highlighting

Context-aware semantic highlighting using LSP and Tree-sitter.

alpha2phi
5 min readDec 28, 2022
Neovim 101 — Contextual Semantic Highlighting

We configured semantic highlighting using Tree-sitter and LSP in a previous article. As semantic tokens support is now part of Neovim, we will use the LSP semantic tokens if it is supported, and fall back to Tree-sitter if the capability is unavailable.

This article is part of the Neovim 101 series.

The Neovim configuration files are available in this repository.

Getting Started

Starting Neovim release 0.9, semantic tokens are supported (:h lsp-semantic-tokens). However, not all language servers provide this capability.

We will check out Lua APIs and user commands that help us inspect all the items at a given buffer location, including semantic tokens. With this information, we will configure semantic highlighting using LSP or Tree-sitter.

User Command and Lua APIs

Neovim provides us with user commands and Lua APIs (:h lua-inspector) to get information related to Tree-sitter, semantic tokens, extmarks, syntax groups, and other information at a given buffer location.

Inspect and vim.inspect_pos()

--

--

alpha2phi

Software engineer, Data Science and ML practitioner.