default.nix 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. { lib, runCommand, fetchFromGitHub, tree-sitter }:
  2. let
  3. grammars = builtins.fromJSON (builtins.readFile ./grammars.json);
  4. overrides = {
  5. "svelte" = let
  6. nvimSvelteSrc = fetchFromGitHub {
  7. owner = "themixednuts";
  8. repo = "nvim-treesitter-svelte";
  9. rev = "a92f64aa397888975162d9296ed79fbce903fb7f";
  10. sha256 = "sha256-kcKbkPyOW19bK4tYk0ylzvZePTApr/KHG4+ae7wxf6Q=";
  11. };
  12. in {
  13. postInstall = ''
  14. rm -r $out/queries
  15. mkdir $out/queries
  16. cp -r ${nvimSvelteSrc}/queries/svelte5 $out/queries/svelte
  17. ls -la $out
  18. '';
  19. };
  20. };
  21. buildTreesitterPlugin = name: spec:
  22. let
  23. nameParts = builtins.split "/" (spec.repo or "tree-sitter/tree-sitter-${name}");
  24. owner = builtins.head nameParts;
  25. repo = builtins.elemAt nameParts 2;
  26. filetypes = spec.filetypes or [name];
  27. grammar = tree-sitter.buildGrammar ({
  28. language = name;
  29. version = "0.0.0+rev=${spec.rev}";
  30. src = fetchFromGitHub {
  31. inherit owner repo;
  32. inherit (spec) rev sha256;
  33. };
  34. } // lib.optionalAttrs (builtins.hasAttr "path" spec) {
  35. location = spec.path;
  36. } // (overrides.${name} or {}));
  37. initLua = ''
  38. vim.treesitter.language.register('${name}', { ${lib.concatMapStringsSep ", " (filetype: "'${filetype}'") filetypes} })
  39. '';
  40. in
  41. runCommand "tree-sitter-${name}" {
  42. passthru = {
  43. inherit grammar;
  44. };
  45. inherit initLua;
  46. passAsFile = [ "initLua" ];
  47. } ''
  48. mkdir -p $out/parser
  49. ln -s ${grammar}/parser $out/parser/${name}.so
  50. ${lib.optionalString (spec.external or false) ''
  51. if [ -d ${grammar}/queries ]; then
  52. ln -s ${grammar}/queries $out/queries
  53. fi
  54. ''}
  55. mkdir -p $out/plugin
  56. cp "$initLuaPath" $out/plugin/init.lua
  57. mkdir -p $out/after/ftplugin
  58. ${lib.concatMapStringsSep "\n" (filetype: ''
  59. echo 'vim.treesitter.start()' > $out/after/ftplugin/${filetype}.lua
  60. '') filetypes}
  61. '';
  62. in
  63. builtins.mapAttrs buildTreesitterPlugin grammars