Need help enabling grammars in treesitter (Python)

Yeah, tree-sitter-languages is just a simple wrapper around a deprecated API that is going away. You could easily just copy it verbatim into your source code:

import pathlib
import sys
import os
from tree_sitter import Language, Parser

treesitter_path = os.environ.get("TREESITTER_PATH")


def get_language(language):
    if sys.platform == "win32":
        filename = f"{language}.dll"
    else:
        filename = f"{language}.so"

    binary_path = str(pathlib.Path(treesitter_path) / filename)
    language = Language(binary_path, language)
    return language


def get_parser(language):
    language = get_language(language)
    parser = Parser()
    parser.set_language(language)
    return parser


if __name__ == '__main__':
    parser = get_parser("python")
    tree = parser.parse(
        bytes(
            """
def foo():
    if bar:
        baz()
""",
            "utf8",
        )
    )
    print(tree)
let
  # pkgs = import <nixpkgs> {};
  pkgs = import ../. {};
in
pkgs.mkShell {
  buildInputs = [
    (pkgs.python3.withPackages (pp: [
      pp.tree-sitter
    ]))
  ];

  env = {
    # Gotta catch 'em all.
    TREESITTER_PATH = pkgs.tree-sitter.withPlugins (gg: builtins.attrValues gg);
  };
}

The recommended way of installing is using one Python package per language. So we would need to package all of those.

But looking at the Python bindings wheel, the package is just a simple native extension that exposes a single method simply wrapping the C API so we could probably just re-create it in Nixpkgs for all tree-sitter grammars automatically:

5 Likes