neovim tree-sitter knowledge-management
I know what you’re probably thinking, “Another post on Fern?! Aish…clearly this blogger is just simping for internet points and milking their tool’s latest release,”. Not true! While I wouldn’t mind some internet points 😸, this post is not really about Fern, the CLI tool. It’s really about NeoVim!
I enjoy using Fern daily, but there was something missing with the recent switch to a more structured log file experience in my journalling. Where before I had a lovely markdown file, replete with highlights, folds, and more, now it was a drab log file with plain text.
“No worries,” I thought to myself, “I’m a moderately competent developer, I am sure can figure this all out and make the expereience better”. Two weeks later, with some help from another plugin called Tree-Sitter, Fern.nvim was born!
Some quick Tree-Sitter background
Tree-Sitter, is a separate plugin that is agnostic to any one editor and works with many editors (probably the one you use as well). It works by building trees from parsed file buffers in your editor. Tree-Sitter is very performant because it only checks and rebuilds the tree in the immediate surrounding area that is being edited, and not the whole file.
Tree-Sitter language parsers use “grammar” files that define important syntax and other components of a language that a given file might be written in. With a parsed tree of components in place, we can query and annotated an open file to affect how it appears and its intractability.
For example the following text: “Trans rights, are human rights!” could be defined as a set of grammar components that give rise to the following tree structure:
(sentence
(noun)
(punctuation
type: comma)
(verb)
(noun)
(punctuation
type: exclamation)
)
Using that tree, we can define things like highlights/styling and fold points (to collapse/expand sections of text). Maybe we want all nouns to have purple background. Maybe all verbs should be bold and pink text. The world is our oyster!
Fern.nvim Plugin
Fern.nvim is a NeoVim plugin that strives to add back some of the functionality and life to Fern journal Log files lost in the switch and then go further and make using it even better. The NeoVim plugin first sets up and activates a custom Tree-Sitter parser, for a language I’m calling “Fernlog”, that defines grammar and builds trees on our Fern Log journal files.
With this custom Tree-Sitter grammar tree accessible, we hook into some of the custom highlights that have been setup (ex. call-outs for all Entries marked with !
and headers colored & bold). This also enables text folding for headers, allowing for easier reading and working with files that are longer length. The plugin folds all headers exceeding given cutoff level in depth by default when you open a log file. This can be customised in the plugin user configs. See the Installation section of the plugin README.md for more details.
We set up some custom annotations which to allow you to quickly create new entries on the fly using only a few keystrokes. This helps ensure that you don’t break your flow when rapidly writing down notes and tasks in your log during the course of the day. Annotations also help us have consistent Unicode symbols in all our log files effortlessly.
The plugin also creates a function to allow users to switch a Task/ToDo Entry line’s status. Lastly, it adds some custom keyshortcuts to all open Fern log file buffers:
<TAB>
- open and close a folded Header quickly<leader>eb
- calls the aforementioned custom function to toggle a Task/ToDo Entry’s status, changing the Unicode marker
What’s Next?
The biggest things I will be looking to tackle next for the plugin are:
- more user config customization options
- custom highlight colors
- custom keybinding shortcuts for commands
- fast way to make a new Entry from Normal mode (skipping the need to enter Insert mode)
- Vim commands for common actions for those who prefer them (ie.
:Fern<Action>
so something like:FernToggle
)
Fern.nvim is still very new and I have lots of ideas about how to take it further. I would love to hear any feedback you have! If you use NeoVim please consider trying it out! 💖 Thank you so much for reading! :)