The web content provides a comprehensive guide on advanced usage of Neovim Tree-sitter for improved coding efficiency, including practical tips on text object manipulation, syntax highlighting for embedded languages, and semantic highlighting.
Abstract
The article delves into the practical applications of Tree-sitter within Neovim, expanding on previous discussions about Tree-sitter basics. It covers how to extend text object manipulation for swapping classes, functions, and parameters, and demonstrates this with examples and key mappings. The guide also explains how to use Tree-sitter queries in Lua for syntax highlighting of embedded languages like GraphQL within JavaScript or TypeScript files. It includes instructions on defining custom queries and highlights the use of plugins like hlargs.nvim for semantic highlighting to distinguish between argument definitions and usages. Additionally, the article touches on using Tree-sitter for code folding and concealment, offering a more organized and cleaner coding environment. The content encourages readers to explore further Tree-sitter tips and provides references to external resources and related articles for a deeper understanding of Neovim's capabilities.
Opinions
The author emphasizes the benefits of using Tree-sitter for a better coding experience in Neovim, suggesting that it enhances productivity.
Customization is a key aspect, with the article providing examples of how to tailor Tree-sitter features to personal preferences.
The use of plugins like hlargs.nvim and markid is recommended for semantic highlighting, indicating a preference for community-driven tools that complement Neovim's functionality.
The article promotes the idea that learning Tree-sitter query syntax is essential for leveraging its full potential in Neovim.
By showcasing the capabilities of Tree-sitter through practical examples, the author conveys enthusiasm for the tool and its impact on the coding workflow.
The inclusion of a cost-effective AI service recommendation at the end suggests the author's support for accessible technology solutions.
Neovim 101 —Tree-sitter Usage
Practical usage of Neovim Tree-sitter.
Previously, we went through the basics of Tree-sitter. In this article, let’s go through Tree-sitter usage to help us in coding.
We already went through some of the practical usages of Tree-sitter in the Neovim Tips for a Betting Coding Experience series. This article covers additional tips not mentioned in the series.
For a Lua file that contains embedded tree-sitter queries, we can have syntax highlighting now if it starts with ;;query.
In the screenshot below, we can see the difference. For the same query, the one starts with ;; query the syntax is highlighted correctly.
Embedded Tree-sitter Queries
GraphQL in JavaScript and TypeScript
Similarly, for GraphQL queries embedded within JavaScript or TypeScript files, the built-in queries highlight the syntax if the gql identifier or the /* GraphQL */ comment is found.
From the screenshot below, for the same GraphQL query, only the first two constants are highlighted with the correct syntax.
If you are not familiar with Tree-sitter query syntax, check out the Tree-sitter article.
Semantic Highlighting
From the screenshot below, we can see the passed-in parameters are not highlighted in the same color.
Semantic Highlighting Issue
Rather than defining our highlight module, we can use the hlargs.nvim plugin to highlight arguments’ definitions and usages, asynchronously, using Treesitter.
If you are not a Medium member yet and want to become one, click here. You will gain unlimited access to all Medium articles and support my work directly.