guix-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug#49946] [PATCH v8 7/7] gnu: Add emacs-tree-sitter-langs.


From: Pierre Langlois
Subject: [bug#49946] [PATCH v8 7/7] gnu: Add emacs-tree-sitter-langs.
Date: Fri, 17 Feb 2023 12:37:22 +0000

* gnu/packages/tree-sitter.scm (emacs-tree-sitter-langs-grammar-bundle): New
variable.
(emacs-tree-sitter-langs): New variable.
---
 gnu/packages/tree-sitter.scm | 200 +++++++++++++++++++++++++++++++++++
 1 file changed, 200 insertions(+)

diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index f8be967e21..2a05179bb4 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -32,6 +32,7 @@ (define-module (gnu packages tree-sitter)
   #:use-module (guix build-system emacs)
   #:use-module (guix build-system gnu)
   #:use-module (guix build-system tree-sitter)
+  #:use-module (guix build-system trivial)
   #:use-module (guix download)
   #:use-module (guix gexp)
   #:use-module (guix git-download)
@@ -651,3 +652,202 @@ (define-public emacs-tree-sitter
 @item More informative indexing for imenu.
 @end enumerate")
     (license license:expat)))
+
+;; tree-sitter-langs expects all supported grammars to be bundled in a single
+;; directory. Doing this statically also allows us to check that grammars are
+;; compatible.
+(define emacs-tree-sitter-langs-grammar-bundle
+  (package
+    (name "emacs-tree-sitter-langs-grammar-bundle")
+    (source #f)
+    (version (package-version tree-sitter))
+    (build-system trivial-build-system)
+    (inputs
+     ;; When adding support for new languages, also add their names into the
+     ;; list in the package emacs-tree-sitter-langs package description.
+     ;; 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-clojure
+           tree-sitter-css
+           tree-sitter-elixir
+           tree-sitter-elm
+           tree-sitter-go
+           tree-sitter-haskell
+           tree-sitter-html
+           tree-sitter-java
+           tree-sitter-javascript
+           tree-sitter-json
+           tree-sitter-julia
+           tree-sitter-lua
+           tree-sitter-markdown-gfm
+           tree-sitter-ocaml
+           tree-sitter-php
+           tree-sitter-python
+           tree-sitter-r
+           tree-sitter-ruby
+           tree-sitter-rust-0.20.2
+           tree-sitter-typescript))
+    (arguments
+     (list
+      #:builder
+      (with-imported-modules '((guix build union)
+                               (guix build utils))
+        #~(begin
+            (use-modules (ice-9 match)
+                         (srfi srfi-1)
+                         (guix build union)
+                         (guix build utils))
+            (mkdir-p #$output)
+            (for-each
+             (lambda (lib)
+               ;; tree-sitter-langs expects grammars to be as "<lang>.so"
+               ;; rather than "libtree-sitter-<lang>.so".
+               (symlink lib
+                        (string-append
+                         #$output
+                         "/"
+                         (substring (basename lib)
+                                    (string-length "libtree-sitter-")))))
+             (append-map (match-lambda
+                           ((name directory)
+                            (find-files directory "libtree-sitter-.*\\.so$")))
+                         '#$(package-inputs this-package)))))))
+    (synopsis #f)
+    (description #f)
+    (home-page #f)
+    (license #f)))
+
+(define-public emacs-tree-sitter-langs
+  (package
+    (name "emacs-tree-sitter-langs")
+    (version "0.12.16")
+    (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
+                "1i8lhv404ijj52jjxrmljnrhby9jlrdgpymn2jfs4kml0x7qhsna"))))
+    (build-system emacs-build-system)
+    (inputs
+     (list emacs-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 'disable-downloader
+            (lambda _
+              (call-with-output-file "tree-sitter-langs-build.el"
+                (lambda (port)
+                  (let ((on-load-message
+                         (string-append
+                          "tree-sitter-langs: Grammar bundle already installed 
"
+                          "via Guix.  Installing external grammars via this "
+                          "function isn't supported, if a language you need is 
"
+                          "missing please report a bug at bug-guix@gnu.org.")))
+                    (format
+                     port
+                     ";;;###autoload
+                      (defun tree-sitter-langs-install-grammars
+                             (&optional skip-if-installed version os
+                                        keep-bundle)
+                        (interactive)
+                        (message \"~a\"))
+                      (defconst tree-sitter-langs--queries-dir
+                        (file-name-as-directory
+                          (concat (file-name-directory
+                                   (locate-library \"tree-sitter-langs.el\"))
+                                  \"queries\")))
+                      (defun tree-sitter-langs--bin-dir () \"~a\")
+                      (provide 'tree-sitter-langs-build)"
+                     on-load-message
+                     #$emacs-tree-sitter-langs-grammar-bundle))))))
+          (add-after 'unpack 'remove-cask
+            (lambda _
+              (substitute* "script/test"
+                (("cask") ""))))
+          (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
+          ;; packaged yet.
+          (add-before 'check 'remove-unused-highlight-queries
+            (lambda _
+              (delete-file-recursively "queries/hcl")
+              (delete-file-recursively "queries/pgn")
+              (delete-file-recursively "queries/yaml")))
+          (add-after 'install 'install-queries
+            (lambda _
+              (copy-recursively
+               "queries"
+               (string-append (elpa-directory #$output) "/queries")))))))
+    (home-page "https://emacs-tree-sitter.github.io/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
+
+The following languages are currently supported in Guix:
+
+@itemize
+@item Bash
+@item C
+@item C#
+@item C++
+@item Clojure
+@item CSS
+@item Elixir
+@item Elm
+@item Go
+@item Haskell
+@item HTML
+@item Java
+@item JavaScript
+@item JSON
+@item Julia
+@item Lua
+@item Markdown
+@item Ocaml
+@item PHP
+@item Python
+@item R
+@item Ruby
+@item Rust
+@item Typescript
+@end itemize")
+    (license license:expat)))
-- 
2.39.1






reply via email to

[Prev in Thread] Current Thread [Next in Thread]