diff mbox series

[bug#49946,v4,31/31] gnu: Add emacs-tree-sitter-langs.

Message ID 20220218143948.28989-31-pierre.langlois@gmx.com
State New
Headers show
Series gnu: Add tree-sitter for emacs. | expand

Commit Message

Pierre Langlois Feb. 18, 2022, 2:39 p.m. UTC
* gnu/packages/tree-sitter.scm
(tree-sitter-langs-grammar-bundle, emacs-tree-sitter-langs): New variables.
---
 gnu/packages/tree-sitter.scm | 136 +++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)

--
2.34.0
diff mbox series

Patch

diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index 9a427bed7f..d1341a2ddd 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -23,6 +23,7 @@  (define-module (gnu packages tree-sitter)
   #:use-module (guix build-system emacs)
   #:use-module (guix build-system gnu)
   #:use-module (guix build-system node)
+  #:use-module (guix build-system trivial)
   #:use-module (guix download)
   #:use-module (guix git-download)
   #:use-module (guix packages)
@@ -950,3 +951,138 @@  (define-public emacs-tree-sitter
 @item More informative indexing for imenu.
 @end enumerate")
     (license license:expat)))
+
+(define tree-sitter-langs-grammar-bundle
+  (package
+    (name "tree-sitter-langs-grammar-bundle")
+    (source #f)
+    (version "0.11.3")
+    (build-system trivial-build-system)
+    (inputs
+     ;; FIXME: Support for some languages is still left to package.
+     (list 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-html
+           tree-sitter-java
+           tree-sitter-javascript
+           tree-sitter-json
+           tree-sitter-julia
+           tree-sitter-ocaml
+           tree-sitter-php
+           tree-sitter-python
+           tree-sitter-rust
+           tree-sitter-ruby
+           tree-sitter-typescript))
+    (arguments
+     (list #:builder
+           (with-imported-modules '((guix build union)
+                                    (guix build utils))
+             #~(begin
+                 (use-modules (ice-9 match)
+                              (guix build union)
+                              (guix build utils))
+                 (union-build
+                  #$output
+                  (filter directory-exists?
+                          (map (match-lambda
+                                 ((name directory)
+                                  (string-append directory "/lib/tree-sitter")))
+                               '#$(package-inputs this-package))))
+                  ;; The BUNDLE-VERSION file prevents emacs-tree-sitter-langs
+                  ;; from downloading libraries at load time.
+                  (call-with-output-file (string-append #$output "/BUNDLE-VERSION")
+                    (lambda (port) (display #$version port)))))))
+    (synopsis #f)
+    (description #f)
+    (home-page #f)
+    (license #f)))
+
+(define-public emacs-tree-sitter-langs
+  (package
+    (name "emacs-tree-sitter-langs")
+    (version "0.11.3")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/emacs-tree-sitter/tree-sitter-langs")
+                    (commit version)))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "1sgpsjd3037pbvwk7lgw70gb4j0fyqikv4lwxwa6l5dwnqvqxgq6"))))
+    (build-system emacs-build-system)
+    (inputs
+     (list tree-sitter-langs-grammar-bundle))
+    (propagated-inputs
+     (list emacs-tree-sitter))
+    (arguments
+     (list
+      #:tests? #t
+      #:test-command ''("script/test")
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-after 'unpack 'remove-cask
+            (lambda _
+              (substitute* "script/test"
+                (("cask") ""))))
+          (add-before 'check 'bundle-for-testing
+            (lambda* (#:key inputs #:allow-other-keys)
+              (delete-file-recursively "bin")
+              (symlink #$tree-sitter-langs-grammar-bundle "bin")))
+          (add-before 'check 'patch-tree-sitter-require-test
+            (lambda _
+              (use-modules (ice-9 regex))
+              ;; This test needs a git repositories with submodules for
+              ;; each languages in order to map all repositories.  We patch
+              ;; the mapping function with one that invokes the tests for each
+              ;; packaged language.
+              (let ((supported-languages
+                     (map (lambda (lib)
+                            (match:substring
+                             (string-match "(.*)\\.so$" (basename lib))
+                             1))
+                          (find-files "bin" "\\.so$"))))
+                (substitute* "tree-sitter-langs-tests.el"
+                  (("tree-sitter-langs--map-repos")
+                   (call-with-output-string
+                     (lambda (port)
+                       (write `(lambda (fn)
+                                 (dolist (lang ',supported-languages)
+                                         (funcall fn lang)))
+                              port))))))))
+          ;; Tests for queries will fail given those languages are not
+          ;; packages yet.
+          (add-before 'check 'remove-unused-highlight-queries
+            (lambda _
+              (delete-file-recursively "queries/hcl")
+              (delete-file-recursively "queries/pgn")))
+          (add-before 'install 'install-bundle
+            (lambda _
+              (let ((elpa (elpa-directory #$output)))
+                (mkdir-p elpa)
+                (symlink #$tree-sitter-langs-grammar-bundle
+                         (string-append elpa "/bin")))))
+          (add-after 'install 'install-queries
+            (lambda* (#:key outputs #:allow-other-keys)
+              (let ((elpa (elpa-directory (assoc-ref outputs "out"))))
+                (copy-recursively "queries" (string-append elpa "/queries"))))))))
+    (home-page "https://ubolonton.github.io/emacs-tree-sitter/languages/")
+    (synopsis "Language support bundle for Tree-sitter")
+    (description "This package is a convenient language bundle for
+Tree-sitter.  For each supported language, this package provides:
+
+@enumerate
+@item Pre-compiled grammar binaries.
+@item An optional highlights.scm file that provides highlighting patterns.
+This is mainly intended for major modes that are not aware of tree-sitter.
+@item Optional query patterns for other minor modes that provide high-level
+functionalities on top of tree-sitter, such as code folding, evil text
+objects, ...etc.
+@end enumerate")
+    (license license:expat)))