implement very basic highlighting

This commit is contained in:
Colin 2023-07-07 01:47:46 +00:00
parent 27596798d6
commit c2fcc8b6ee
17 changed files with 87 additions and 20 deletions

15
Makefile Normal file
View File

@ -0,0 +1,15 @@
CACHEDIR := $(PWD)/cache
build:
cd tree-sitter-nix-shell && tree-sitter generate
test: build
cd tree-sitter-nix-shell && XDG_CACHE_HOME="${CACHEDIR}" tree-sitter test
XDG_CACHE_HOME="${CACHEDIR}" ./tests/test_highlight basic
clean:
rm -f tree-sitter-nix-shell/{Cargo.toml,binding.gyp}
rm -rf tree-sitter-nix-shell/{bindings/,src/}
rm -rf "${CACHEDIR}"
.PHONY: build test clean

View File

@ -3,6 +3,13 @@
pkgs.tree-sitter.buildGrammar {
src = ./.;
version = "0.1.0";
language = "nixshell";
language = "nix-shell";
generate = true;
location = "tree-sitter-nix-shell";
checkPhase = ''
(cd ..; make test)
'';
nativeCheckInputs = with pkgs; [ htmlq ];
doCheck = true;
}

View File

@ -20,16 +20,16 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1688389917,
"narHash": "sha256-RKiK1QeommEsjQ8fLgxt4831x9O6n2gD7wAhVZTrr8M=",
"lastModified": 1688590700,
"narHash": "sha256-ZF055rIUP89cVwiLpG5xkJzx00gEuuGFF60Bs/LM3wc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "aed4b19d312525ae7ca9bceb4e1efe3357d0e2eb",
"rev": "f292b4964cb71f9dfbbd30dc9f511d6165cd109b",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-23.05",
"ref": "nixos-unstable",
"type": "indirect"
}
},

View File

@ -1,7 +1,7 @@
{
description = "tree-sitter implementation for nix-shell scripts";
inputs = {
nixpkgs.url = "nixpkgs/nixos-23.05";
nixpkgs.url = "nixpkgs/nixos-unstable";
flake-utils.url = github:numtide/flake-utils;
};
@ -12,18 +12,9 @@
inherit system;
};
in rec {
packages.tree-sitter-nixshell = pkgs.callPackage ./default.nix { };
defaultPackage = packages.tree-sitter-nixshell;
packages.tree-sitter-nix-shell = pkgs.callPackage ./default.nix { };
defaultPackage = packages.tree-sitter-nix-shell;
devShells.default = pkgs.callPackage ./shell.nix { };
apps.clean = {
type = "app";
program = builtins.toString (pkgs.writeShellScript "clean" ''
# remove build artifacts
rm -f Cargo.toml binding.gyp
rm -rf bindings/ src/
'');
};
});
}

View File

@ -1 +0,0 @@
(nix_shell_directive) @keyword

View File

@ -1,10 +1,10 @@
{ pkgs ? import <nixpkgs> { } }:
let
tree-sitter-nixshell = pkgs.callPackage ./. { };
tree-sitter-nix-shell = pkgs.callPackage ./. { };
in
pkgs.mkShell {
inherit (tree-sitter-nixshell) nativeBuildInputs;
inherit (tree-sitter-nix-shell) nativeBuildInputs;
# to manually build, once in this shell, just run
# `tree-sitter generate`
}

View File

@ -0,0 +1,8 @@
{
"parser-directories": [
".."
],
"theme": {
"keyword": "#cccccc"
}
}

4
tests/highlight/basic Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash
true
echo "bash-string"

View File

@ -0,0 +1,16 @@
<body>
<table>
<tbody><tr><td class="line-number">1</td><td class="line">#!/usr/bin/env nix-shell
</td></tr>
<tr><td class="line-number">2</td><td class="line"><span style="color: #cccccc">#!nix-shell -i bash -p bash</span>
</td></tr>
<tr><td class="line-number">3</td><td class="line">true
</td></tr>
<tr><td class="line-number">4</td><td class="line">echo "bash-string"
</td></tr>
</tbody></table>
</body>

22
tests/test_highlight Executable file
View File

@ -0,0 +1,22 @@
#!/bin/sh
# `tree-sitter test` doesn't work to validate syntax highlighting for two reasons:
# - it identifies a file's language only by its filetype (not shebang/regex), but nix-shell has no filetype
# - it requires the language to support comments, but nix-shell doesn't have those
#
# hence this custom testing tool.
#
# usage: test_highlight <TEST_NAME>
#
# upon execution, performs this sequence:
# 1. highlights the file at `highlight/<TEST_NAME>`.
# 2. asserts the result's body against `hightlight/<TEST_NAME>.spec.html`
here=$(dirname $(realpath $0))
input_file="$here/highlight/$1"
spec_file="$here/highlight/$1.spec.html"
# cd into this directory before highlighting because paths specified in the tree-sitter config are relative to PWD
cd "$here" && XDG_CONFIG_HOME="$here/config" tree-sitter highlight --scope "source.nix_shell" --html "$input_file" | \
htmlq 'body' | \
diff -u "$spec_file" '/dev/stdin'

View File

@ -22,6 +22,7 @@
"content-regex": "^\b$nix-shell wraps other languages so intentionally fail against (and yield to) those other language matches",
"injection-regex": "^(nix[-_]?shell)$",
"scope": "source.nix_shell",
"injections": [ "queries/injections.scm" ],
"highlights": [ "queries/highlights.scm" ]
}
]

View File

@ -0,0 +1,4 @@
; these highlights will be overriden by the highlighting of the injected language.
; they might briefly be visible if the nix-shell directive is incomplete
; or if the embedded language doesn't support highlighting.
(nix_shell_directive) @keyword