Skip to content

hedyhli/tree-sitter-cognate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tree-sitter-cognate

Checks

What is Cognate?

The official regex-based highlighting plugins are great, but for a wider reach of editor support a tree-sitter grammar is needed which gives way to a more unified approach to supporting indents and code-folding rules.

Editor setup

Nvim

Note that the built-in tree-sitter support requires Nvim 0.9+.

  • Highlights
    • Identifiers fallback as variables
    • Function name after Def
    • Field name after .
  • Toggle line comments
  • Toggle block comments (for Comment.nvim and kommentary)
  • Injection language for comments
  • Indents
  • Folds
  • Locals
  • Text objects
    • Function
    • Block
    • Assignment
    • Statement
    • Comment
    • Number
  • Context

Ensure nvim-treesitter is installed, then install this repo as a plugin using your favorite plugin manager (hedyhli/tree-sitter-cognate);

Lazy.nvim example:

{
  "hedyhli/tree-sitter-cognate",
  dependencies = { "nvim-treesitter/nvim-treesitter" },
  enabled = vim.fn.has('nvim-0.9') == 1,
},

If you use Comment.nvim or kommentary, they will automatically be set up to be usable for commenting for both line and blocks in Cognate in addition to commentstring.

Parsers and queries will be automatically set up. Finally either put cognate into your ensure_installed, or run :TSInstall cognate.

Emacs

  • Highlights
    • Identifiers fallback as variables
    • Function name after Def
    • Field name after .
  • Toggle line comments
  • Toggle block comments
  • Indents
  • Imenu

A library for cognate-ts-mode is included in this repository, it supports the built-in tree-sitter feature for Emacs 29 and above. The major mode supports a few other features using tree-sitter in addition to highlighting, as listed above.

For other versions, consider using the official cognate-mode package for emacs (which uses regex-based syntax highlighting), or consult the documentation on how to add a new grammar for the tree-sitter package you are using.

Details on how to set up tree-sitter-cognate for Emacs 29+ is as follows.

Step 1

Install the grammar using M-x treesit-install-language-grammar, enter cognate, the URL https://github.com/hedyhli/tree-sitter-cognate, and accept default options.

Step 2

Install this repository as a package using your package manager.

Elpaca example:

(use-package cognate-ts-mode
  :ensure (:host "github" :repo "hedyhli/tree-sitter-cognate"))

Note that this package is not currently published on ELPA/MELPA.

Step 3

A restart of emacs might be required after installation. Finally, test the syntax highlighting on a Cognate file. All *.cog files are set up to automatically use cognate-ts-mode.

Helix

  • Highlights
    • Identifiers fallback as variables
    • Function name after Def
    • Field name after .
  • Toggle line comments
  • Toggle block comments
  • Injection language for comments
  • Indents
  • Text objects
    • Functions
    • Comments
    • Blocks (note that you can simply use mi) to select parenthesis)

Add a new language by appending the contents of helix/language.toml in this repository.

Next, download and install the grammar:

hx -g fetch
hx -g build

Then copy (or link) queries files from helix/ into the correct runtime directory. For instance, helix/queries/cognate (of this repo) to ~/.config/helix/runtime/queries/cognate. Note that Helix should use queries under the helix/ directory in this repository.

VSCode

  • Highlights

First, ensure the tree-sitter extension for VS Code is installed.

Next download the tree-sitter-cognate.vsix file from this repository to add support for detecting Cognate files, and install it as an extension.

Clone this repository locally somewhere, then add these lines to your settings.json:

"tree-sitter-vscode.languageConfigs": [{
    "lang": "cognate",
    "parser": "[PATH_TO_THIS_REPOSITORY]/tree-sitter-cognate.wasm",
    "highlights": "[PATH_TO_THIS_REPOSITORY]/queries/cognate/highlights.scm"
}]

Vim

Consider using the official vim plugin for vim's regex-based syntax highlighting: https://github.com/cognate-lang/cognate-vim.

TODO

  • Support symbols.
  • Support block comments.
  • Don't enforce "function" statements -- there's no such thing.
  • Floating point numbers.
  • String escapes.
  • Case insensitivity for indentifiers (booleans in grammar, keywords everywhere else)
  • Use cognac's builtins.c rather than hardcoding builtins
  • Support functions and fields for emacs

References