Skip to content

Commit

Permalink
feat: build.queries for passing in embedded query files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Mar 17, 2024
1 parent f3a8d4d commit 39b7998
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 24 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,28 @@ build = {
---@type boolean? (optional) Must the sources be generated using the tree-sitter CLI?
generate_from_grammar = true,

---@type boolean? (optional) Is npm required to generate the sources?
--- Ignored if generate_from_grammar is false.
---@type boolean? (optional) Is npm required to generate the sources?
generate_requires_npm = false,

---@type string? (optional) tree-sitter grammar's location (relative to the source root).
location = "libs/tree-sitter-LANG",

--- Overwrites any existing queries with the embedded queries.
--- Will add 'queries' to the rockspec's 'copy_directories' if set.
---@type table<string, string>
queries = {
-- Will create a `queries/<lang>/highlights.scm`
-- Note that the content should not be indented.
["highlights.scm"] = [[
(signature
name: (variable) @function)
(function
name: (variable) @function)
]],
},

}
```

Expand Down
5 changes: 5 additions & 0 deletions fixtures/tree-sitter-rust-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ build = {
type = "treesitter-parser",
lang = "rust",
sources = { "src/parser.c", "src/scanner.c" },
queries = {
["highlights.scm"] = [[
;;
]],
},
}
1 change: 1 addition & 0 deletions luarocks-build-treesitter-parser-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = _MODREV .. _SPECREV

dependencies = {
"lua >= 5.1",
"luafilesystem ~> 1",
}

test_dependencies = {
Expand Down
54 changes: 34 additions & 20 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,51 @@
luarocks-build-treesitter-parser = luaself.callPackage ({
buildLuarocksPackage,
luaOlder,
luafilesystem,
}:
buildLuarocksPackage {
pname = "luarocks-build-treesitter-parser";
version = "scm-1";
knownRockspec = "${self}/luarocks-build-treesitter-parser-scm-1.rockspec";
src = self;
disabled = luaOlder "5.1";
}) {};

tree-sitter-rust = luaself.callPackage ({
buildLuarocksPackage,
fetchFromGitHub,
luaOlder,
luarocks-build-treesitter-parser,
}:
buildLuarocksPackage {
pname = "tree-sitter-rust";
version = "scm-1";
knownRockspec = "${self}/fixtures/tree-sitter-rust-scm-1.rockspec";
src = fetchFromGitHub {
owner = "tree-sitter";
repo = "tree-sitter-rust";
rev = "3a56481f8d13b6874a28752502a58520b9139dc7";
hash = "sha256-6ROXeKuPehtIOtaI1OJuTtyPfQmZyLzCxv3ZS04yAIk=";
};
propagatedBuildInputs = [
luarocks-build-treesitter-parser
luafilesystem
];
disabled = luaOlder "5.1";
}) {};

tree-sitter-rust =
(luaself.callPackage ({
buildLuarocksPackage,
fetchFromGitHub,
luaOlder,
luarocks-build-treesitter-parser,
}:
buildLuarocksPackage {
pname = "tree-sitter-rust";
version = "scm-1";
knownRockspec = "${self}/fixtures/tree-sitter-rust-scm-1.rockspec";
src = fetchFromGitHub {
owner = "tree-sitter";
repo = "tree-sitter-rust";
rev = "3a56481f8d13b6874a28752502a58520b9139dc7";
hash = "sha256-6ROXeKuPehtIOtaI1OJuTtyPfQmZyLzCxv3ZS04yAIk=";
};
propagatedBuildInputs = [
luarocks-build-treesitter-parser
];
disabled = luaOlder "5.1";
}) {})
.overrideAttrs (oa: {
fixupPhase = ''
grep -q ';;' $out/tree-sitter-rust-scm-1-rocks/tree-sitter-rust/scm-1/queries/rust/highlights.scm
if [ $? -ne 0 ]; then
echo "Build did not create highlights.scm file with the expected content"
exit 1
fi
'';
});

tree-sitter-ocamllex = luaself.callPackage ({
buildLuarocksPackage,
fetchFromGitHub,
Expand Down
26 changes: 23 additions & 3 deletions src/luarocks/build/treesitter-parser.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---@diagnostic disable: inject-field
local fs = require("luarocks.fs")
local dir = require("luarocks.dir")
local builtin = require("luarocks.build.builtin")
local util = require("luarocks.util")

Expand All @@ -22,6 +23,7 @@ local treesitter_parser = {}
---@field generate_requires_npm? boolean
---@field generate_from_grammar? boolean
---@field location? string
---@field queries? table<string, string>

---@param rockspec table
---@param no_install boolean
Expand Down Expand Up @@ -81,11 +83,29 @@ function treesitter_parser.run(rockspec, no_install)
end
local incdirs = {}
for _, source in ipairs(build.sources) do
local dir = source:match("(.-)%/")
if dir then
table.insert(incdirs, dir)
local source_dir = source:match("(.-)%/")
if source_dir then
table.insert(incdirs, source_dir)
end
end
if build.queries then
if fs.is_dir("queries") then
pcall(fs.delete, "queries")
end
local queries_dir = dir.path("queries", build.lang)
fs.make_dir(queries_dir)
for name, content in pairs(build.queries) do
local queries_file = dir.path(queries_dir, name)
local fd = io.open(queries_file, "w+")
if not fd then
return nil, "Could not open " .. queries_file .. " for writing"
end
fd:write(content)
fd:close()
end
rockspec.build.copy_directories = rockspec.build.copy_directories or {}
table.insert(rockspec.build.copy_directories, "queries")
end
rockspec.build.modules = {
["parser." .. build.lang] = {
sources = build.sources,
Expand Down

0 comments on commit 39b7998

Please sign in to comment.