about summary refs log tree commit diff
path: root/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs
diff options
context:
space:
mode:
authorAndersonTorres <torres.anderson.85@protonmail.com>2022-11-12 17:29:15 -0300
committerAndersonTorres <torres.anderson.85@protonmail.com>2022-11-13 09:17:04 -0300
commit03fa9cdde9bf21ed7f518193d60f131cd254856d (patch)
tree0c2b70e69ba592a52baeb73f81baf1212eb0eb32 /pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs
parentc47cbf3acaff10179b418c216bb5e6e1cd63c2c4 (diff)
treewide: move all ad-hoc elisp packages to manual-packages/
Diffstat (limited to 'pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs')
-rw-r--r--pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/default-grammars.json33
-rw-r--r--pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/default.nix59
-rwxr-xr-xpkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/update-defaults.py74
3 files changed, 166 insertions, 0 deletions
diff --git a/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/default-grammars.json b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/default-grammars.json
new file mode 100644
index 0000000000000..1cfa52fed5b58
--- /dev/null
+++ b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/default-grammars.json
@@ -0,0 +1,33 @@
+[
+  "tree-sitter-bash",
+  "tree-sitter-c",
+  "tree-sitter-c-sharp",
+  "tree-sitter-cpp",
+  "tree-sitter-css",
+  "tree-sitter-elixir",
+  "tree-sitter-elm",
+  "tree-sitter-go",
+  "tree-sitter-haskell",
+  "tree-sitter-hcl",
+  "tree-sitter-html",
+  "tree-sitter-janet-simple",
+  "tree-sitter-java",
+  "tree-sitter-javascript",
+  "tree-sitter-jsdoc",
+  "tree-sitter-json",
+  "tree-sitter-julia",
+  "tree-sitter-nix",
+  "tree-sitter-ocaml",
+  "tree-sitter-perl",
+  "tree-sitter-pgn",
+  "tree-sitter-php",
+  "tree-sitter-prisma",
+  "tree-sitter-python",
+  "tree-sitter-ruby",
+  "tree-sitter-rust",
+  "tree-sitter-scala",
+  "tree-sitter-typescript",
+  "tree-sitter-verilog",
+  "tree-sitter-yaml",
+  "tree-sitter-zig"
+]
diff --git a/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/default.nix b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/default.nix
new file mode 100644
index 0000000000000..cb4cec172bac1
--- /dev/null
+++ b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/default.nix
@@ -0,0 +1,59 @@
+{ lib
+, pkgs
+, symlinkJoin
+, fetchzip
+, melpaBuild
+, stdenv
+, fetchFromGitHub
+, writeText
+, melpaStablePackages
+, runCommand
+, tree-sitter-grammars
+, plugins ? map (g: tree-sitter-grammars.${g}) (lib.importJSON ./default-grammars.json)
+, final
+}:
+
+let
+  inherit (melpaStablePackages) tree-sitter-langs;
+
+  libSuffix = if stdenv.isDarwin then "dylib" else "so";
+  langName = g: lib.removeSuffix "-grammar" (lib.removePrefix "tree-sitter-" g.pname);
+  soName = g: langName g + "." + libSuffix;
+
+  grammarDir = runCommand "emacs-tree-sitter-grammars" {
+    # Fake same version number as upstream language bundle to prevent triggering runtime downloads
+    inherit (tree-sitter-langs) version;
+  } (''
+    install -d $out/langs/bin
+    echo -n $version > $out/langs/bin/BUNDLE-VERSION
+  '' + lib.concatStringsSep "\n" (map (
+    g: "ln -s ${g}/parser $out/langs/bin/${soName g}") plugins
+  ));
+  siteDir = "$out/share/emacs/site-lisp/elpa/${tree-sitter-langs.pname}-${tree-sitter-langs.version}";
+
+in
+melpaStablePackages.tree-sitter-langs.overrideAttrs(old: {
+  postPatch = old.postPatch or "" + ''
+    substituteInPlace ./tree-sitter-langs-build.el \
+    --replace "tree-sitter-langs-grammar-dir tree-sitter-langs--dir"  "tree-sitter-langs-grammar-dir \"${grammarDir}/langs\""
+  '';
+
+  postInstall =
+    old.postInstall or ""
+    + lib.concatStringsSep "\n"
+      (map
+        (g: ''
+          if [[ -d "${g}/queries" ]]; then
+            mkdir -p ${siteDir}/queries/${langName g}/
+            for f in ${g}/queries/*; do
+              ln -sfn "$f" ${siteDir}/queries/${langName g}/
+            done
+          fi
+        '') plugins);
+
+  passthru = old.passthru or {} // {
+    inherit plugins;
+    withPlugins = fn: final.tree-sitter-langs.override { plugins = fn tree-sitter-grammars; };
+  };
+
+})
diff --git a/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/update-defaults.py b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/update-defaults.py
new file mode 100755
index 0000000000000..19bcb8989c30d
--- /dev/null
+++ b/pkgs/applications/editors/emacs/elisp-packages/manual-packages/tree-sitter-langs/update-defaults.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env nix-shell
+#! nix-shell ../../../../../../. -i python3 -p python3 -p nix
+from os.path import (
+    dirname,
+    abspath,
+    join,
+)
+from typing import (
+    List,
+    Any,
+)
+import subprocess
+import json
+import sys
+import os
+
+
+def fmt_grammar(grammar: str) -> str:
+    return "tree-sitter-" + grammar
+
+
+def eval_expr(nixpkgs: str, expr: str) -> Any:
+    p = subprocess.run(
+        [
+            "nix-instantiate",
+            "--json",
+            "--eval",
+            "--expr",
+            ("with import %s {}; %s" % (nixpkgs, expr)),
+        ],
+        check=True,
+        stdout=subprocess.PIPE,
+    )
+    return json.loads(p.stdout)
+
+
+def check_grammar_exists(nixpkgs: str, grammar: str) -> bool:
+    return eval_expr(
+        nixpkgs, f'lib.hasAttr "{fmt_grammar(grammar)}" tree-sitter-grammars'
+    )
+
+
+def build_attr(nixpkgs, attr: str) -> str:
+    return (
+        subprocess.run(
+            ["nix-build", "--no-out-link", nixpkgs, "-A", attr],
+            check=True,
+            stdout=subprocess.PIPE,
+        )
+        .stdout.decode()
+        .strip()
+    )
+
+
+if __name__ == "__main__":
+    cwd = dirname(abspath(__file__))
+    nixpkgs = abspath(join(cwd, "../../../../../.."))
+
+    src_dir = build_attr(nixpkgs, "emacs.pkgs.tree-sitter-langs.src")
+
+    existing: List[str] = []
+
+    grammars = os.listdir(join(src_dir, "repos"))
+    for g in grammars:
+        exists = check_grammar_exists(nixpkgs, g)
+        if exists:
+            existing.append(fmt_grammar(g))
+        else:
+            sys.stderr.write("Missing grammar: " + fmt_grammar(g) + "\n")
+            sys.stderr.flush()
+
+    with open(join(cwd, "default-grammars.json"), mode="w") as f:
+        json.dump(sorted(existing), f, indent=2)
+        f.write("\n")