Compare commits
3 Commits
wip/highli
...
master
Author | SHA1 | Date | |
---|---|---|---|
41849dc40d | |||
9ccb867150 | |||
c2fcc8b6ee |
15
Makefile
Normal file
15
Makefile
Normal 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
|
@@ -19,6 +19,6 @@ roughly:
|
|||||||
2. `tree-sitter generate`
|
2. `tree-sitter generate`
|
||||||
3. `tree-sitter test`
|
3. `tree-sitter test`
|
||||||
|
|
||||||
run `nix run '.#clean'` before committing to avoid checking in build artifacts.
|
run `make clean` from within the `nix develop` shell before committing to avoid checking in build artifacts.
|
||||||
|
|
||||||
the parser is defined in `grammar.js`, and tests live in `corpus/`.
|
the parser is defined in `grammar.js`, and tests live in `corpus/`.
|
||||||
|
25
default.nix
25
default.nix
@@ -2,7 +2,28 @@
|
|||||||
|
|
||||||
pkgs.tree-sitter.buildGrammar {
|
pkgs.tree-sitter.buildGrammar {
|
||||||
src = ./.;
|
src = ./.;
|
||||||
version = "0.1.0";
|
version = "0.2.0";
|
||||||
language = "nixshell";
|
language = "nix-shell";
|
||||||
generate = true;
|
generate = true;
|
||||||
|
location = "tree-sitter-nix-shell";
|
||||||
|
|
||||||
|
nativeCheckInputs = with pkgs; [ htmlq ];
|
||||||
|
checkPhase = ''
|
||||||
|
runHook preCheck
|
||||||
|
(cd ..; make test)
|
||||||
|
runHook postCheck
|
||||||
|
'';
|
||||||
|
doCheck = true;
|
||||||
|
|
||||||
|
nativeInstallCheckInputs = with pkgs; [ tree ];
|
||||||
|
installCheckPhase = ''
|
||||||
|
runHook preInstallCheck
|
||||||
|
|
||||||
|
# make sure that all files i expect to be installed are actually installed
|
||||||
|
(test -x $out/parser && test -f $out/queries/highlights.scm && test -f $out/queries/injections.scm) || \
|
||||||
|
(tree $out; echo "expected output to contain /parser and /queries/"; false)
|
||||||
|
|
||||||
|
runHook postInstallCheck
|
||||||
|
'';
|
||||||
|
doInstallCheck = true;
|
||||||
}
|
}
|
||||||
|
14
flake.lock
generated
14
flake.lock
generated
@@ -5,11 +5,11 @@
|
|||||||
"systems": "systems"
|
"systems": "systems"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1687709756,
|
"lastModified": 1731533236,
|
||||||
"narHash": "sha256-Y5wKlQSkgEK2weWdOu4J3riRd+kV/VCgHsqLNTTWQ/0=",
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "flake-utils",
|
"repo": "flake-utils",
|
||||||
"rev": "dbabf0ca0c0c4bce6ea5eaf65af5cb694d2082c7",
|
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -20,16 +20,16 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1688389917,
|
"lastModified": 1741851582,
|
||||||
"narHash": "sha256-RKiK1QeommEsjQ8fLgxt4831x9O6n2gD7wAhVZTrr8M=",
|
"narHash": "sha256-cPfs8qMccim2RBgtKGF+x9IBCduRvd/N5F4nYpU0TVE=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "aed4b19d312525ae7ca9bceb4e1efe3357d0e2eb",
|
"rev": "6607cf789e541e7873d40d3a8f7815ea92204f32",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"id": "nixpkgs",
|
"id": "nixpkgs",
|
||||||
"ref": "nixos-23.05",
|
"ref": "nixos-unstable",
|
||||||
"type": "indirect"
|
"type": "indirect"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
15
flake.nix
15
flake.nix
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
description = "tree-sitter implementation for nix-shell scripts";
|
description = "tree-sitter implementation for nix-shell scripts";
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "nixpkgs/nixos-23.05";
|
nixpkgs.url = "nixpkgs/nixos-unstable";
|
||||||
flake-utils.url = github:numtide/flake-utils;
|
flake-utils.url = github:numtide/flake-utils;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -12,18 +12,9 @@
|
|||||||
inherit system;
|
inherit system;
|
||||||
};
|
};
|
||||||
in rec {
|
in rec {
|
||||||
packages.tree-sitter-nixshell = pkgs.callPackage ./default.nix { };
|
packages.tree-sitter-nix-shell = pkgs.callPackage ./default.nix { };
|
||||||
defaultPackage = packages.tree-sitter-nixshell;
|
defaultPackage = packages.tree-sitter-nix-shell;
|
||||||
|
|
||||||
devShells.default = pkgs.callPackage ./shell.nix { };
|
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/
|
|
||||||
'');
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
28
package.json
28
package.json
@@ -1,28 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "tree-sitter-nix-shell",
|
|
||||||
"version": "0.0.1",
|
|
||||||
"description": "nix-shell grammar for tree-sitter",
|
|
||||||
"main": "bindings/node",
|
|
||||||
"keywords": [
|
|
||||||
"parsing",
|
|
||||||
"incremental"
|
|
||||||
],
|
|
||||||
"dependencies": {
|
|
||||||
"nan": "^2.12.1"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"tree-sitter-cli": "^0.20.8"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"test": "tree-sitter test"
|
|
||||||
},
|
|
||||||
"tree-sitter": [
|
|
||||||
{
|
|
||||||
"first-line-regex": "^#!.*\\bnix-shell\\b.*$",
|
|
||||||
"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",
|
|
||||||
"highlights": [ "queries/highlights.scm" ]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
@@ -1 +0,0 @@
|
|||||||
(nix_shell_directive) @keyword
|
|
@@ -1,10 +1,10 @@
|
|||||||
{ pkgs ? import <nixpkgs> { } }:
|
{ pkgs ? import <nixpkgs> { } }:
|
||||||
|
|
||||||
let
|
let
|
||||||
tree-sitter-nixshell = pkgs.callPackage ./. { };
|
tree-sitter-nix-shell = pkgs.callPackage ./. { };
|
||||||
in
|
in
|
||||||
pkgs.mkShell {
|
pkgs.mkShell {
|
||||||
inherit (tree-sitter-nixshell) nativeBuildInputs;
|
inherit (tree-sitter-nix-shell) nativeBuildInputs;
|
||||||
# to manually build, once in this shell, just run
|
# to manually build, once in this shell, just run
|
||||||
# `tree-sitter generate`
|
# `tree-sitter generate`
|
||||||
}
|
}
|
||||||
|
8
tests/config/tree-sitter/config.json
Normal file
8
tests/config/tree-sitter/config.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"parser-directories": [
|
||||||
|
".."
|
||||||
|
],
|
||||||
|
"theme": {
|
||||||
|
"keyword": "#cccccc"
|
||||||
|
}
|
||||||
|
}
|
4
tests/highlight/basic
Normal file
4
tests/highlight/basic
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env nix-shell
|
||||||
|
#!nix-shell -i bash -p bash
|
||||||
|
true
|
||||||
|
echo "bash-string"
|
16
tests/highlight/basic.spec.html
Normal file
16
tests/highlight/basic.spec.html
Normal 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
22
tests/test_highlight
Executable 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'
|
4
tree-sitter-nix-shell/queries/highlights.scm
Normal file
4
tree-sitter-nix-shell/queries/highlights.scm
Normal 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
|
41
tree-sitter-nix-shell/tree-sitter.json
Normal file
41
tree-sitter-nix-shell/tree-sitter.json
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json",
|
||||||
|
"grammars": [
|
||||||
|
{
|
||||||
|
"name": "nix_shell",
|
||||||
|
"camelcase": "NixShell",
|
||||||
|
"title": "NixShell",
|
||||||
|
"scope": "source.nix_shell",
|
||||||
|
"class-name": "TreeSitterNixShell",
|
||||||
|
"file-types": [ ],
|
||||||
|
"first-line-regex": "^#!.*\\bnix-shell\\b.*$",
|
||||||
|
"content-regex": "^\b$nix-shell wraps other languages so intentionally fail against (and yield to) those other language matches",
|
||||||
|
"injection-regex": "^(nix[-_]?shell)$",
|
||||||
|
"injections": [ "queries/injections.scm" ],
|
||||||
|
"highlights": [ "queries/highlights.scm" ]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"version": "0.2.0",
|
||||||
|
"description": "understands languages encoded in #!nix-shell shebangs",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Colin",
|
||||||
|
"email": "colin@uninsane.org",
|
||||||
|
"url": "https://uninsane.org/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"links": {
|
||||||
|
"repository": "https://git.uninsane.org/colin/tree-sitter-nix-shell"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"bindings": {
|
||||||
|
"c": true,
|
||||||
|
"go": true,
|
||||||
|
"node": true,
|
||||||
|
"python": true,
|
||||||
|
"rust": true,
|
||||||
|
"swift": true,
|
||||||
|
"zig": false
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user