Subject: [bug#49946] [PATCH v5 24/27] gnu: Add rust-tree-sitter-for-emacs.

From: Pierre Langlois <[email-masked]>
Date: Tue, 29 Mar 2022 20:43:15 +0100

* gnu/packages/tree-sitter.scm (rust-tree-sitter-for-emacs): New variable.
* gnu/packages/patches/rust-tree-sitter-text-provider-fix.patch: New file.
* gnu/local.mk (dist_patch_DATA): Register it.
---
 gnu/local.mk                                  |  1 +
 .../rust-tree-sitter-text-provider-fix.patch  | 98 +++++++++++++++++++
 gnu/packages/tree-sitter.scm                  | 14 +++
 3 files changed, 113 insertions(+)
 create mode 100644 gnu/packages/patches/rust-tree-sitter-text-provider-fix.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 03972441e6..aa353a6f68 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1780,6 +1780,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/rust-nettle-sys-disable-vendor.patch	 \
   %D%/packages/patches/rust-openssl-sys-no-vendor.patch	\
   %D%/packages/patches/rust-shell2batch-lint-fix.patch		\
+  %D%/packages/patches/rust-tree-sitter-text-provider-fix.patch	\
   %D%/packages/patches/rust-wl-clipboard-rs-newer-wl.patch      \
   %D%/packages/patches/sbc-fix-build-non-x86.patch		\
   %D%/packages/patches/sbcl-aserve-add-HTML-5-elements.patch	\
diff --git a/gnu/packages/patches/rust-tree-sitter-text-provider-fix.patch b/gnu/packages/patches/rust-tree-sitter-text-provider-fix.patch
new file mode 100644
index 0000000000..215e7fc18b
--- /dev/null
+++ b/gnu/packages/patches/rust-tree-sitter-text-provider-fix.patch
@@ -0,0 +1,98 @@
+From e3576b180488b8231e1fc0ca130748577579d129 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Tu=E1=BA=A5n-Anh=20Nguy=E1=BB=85n?= <[email-masked]>
+Date: Sun, 25 Jul 2021 13:11:52 +0700
+Subject: [PATCH] Allow TextProvider's iterators to generate owned text
+
+---
+ binding_rust/lib.rs | 33 ++++++++++++++++++---------------
+ 1 file changed, 18 insertions(+), 15 deletions(-)
+
+diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs
+index e88a411c..cf214d92 100644
+--- a/binding_rust/lib.rs
++++ b/binding_rust/lib.rs
+@@ -5,6 +5,7 @@ mod util;
+ use std::os::unix::io::AsRawFd;
+
+ use std::{
++    borrow::Cow,
+     char, error,
+     ffi::CStr,
+     fmt, hash, iter,
+@@ -183,7 +184,8 @@ pub struct QueryCaptures<'a, 'tree: 'a, T: TextProvider<'a>> {
+ }
+
+ pub trait TextProvider<'a> {
+-    type I: Iterator<Item = &'a [u8]> + 'a;
++    type I: Iterator<Item = Cow<'a, [u8]>>;
++
+     fn text(&mut self, node: Node) -> Self::I;
+ }
+
+@@ -1840,19 +1842,19 @@ impl<'a, 'tree> QueryMatch<'a, 'tree> {
+         buffer2: &mut Vec<u8>,
+         text_provider: &mut impl TextProvider<'a>,
+     ) -> bool {
+-        fn get_text<'a, 'b: 'a, I: Iterator<Item = &'b [u8]>>(
++        fn get_text<'a, 'b: 'a, I: Iterator<Item = Cow<'b, [u8]>>>(
+             buffer: &'a mut Vec<u8>,
+             mut chunks: I,
+-        ) -> &'a [u8] {
+-            let first_chunk = chunks.next().unwrap_or(&[]);
++        ) -> Cow<'a, [u8]> {
++            let first_chunk = chunks.next().unwrap_or(Cow::Owned(vec![0u8; 0]));
+             if let Some(next_chunk) = chunks.next() {
+                 buffer.clear();
+-                buffer.extend_from_slice(first_chunk);
+-                buffer.extend_from_slice(next_chunk);
++                buffer.extend_from_slice(&first_chunk);
++                buffer.extend_from_slice(&next_chunk);
+                 for chunk in chunks {
+-                    buffer.extend_from_slice(chunk);
++                    buffer.extend_from_slice(&chunk);
+                 }
+-                buffer.as_slice()
++                Cow::Borrowed(buffer.as_slice())
+             } else {
+                 first_chunk
+             }
+@@ -1888,7 +1890,7 @@ impl<'a, 'tree> QueryMatch<'a, 'tree> {
+                     match node {
+                         Some(node) => {
+                             let text = get_text(buffer1, text_provider.text(node));
+-                            r.is_match(text) == *is_positive
++                            r.is_match(&text) == *is_positive
+                         }
+                         None => true,
+                     }
+@@ -2002,23 +2004,24 @@ impl<'cursor, 'tree> fmt::Debug for QueryMatch<'cursor, 'tree> {
+     }
+ }
+
+-impl<'a, F, I> TextProvider<'a> for F
++impl<'a, F, I, T> TextProvider<'a> for F
+ where
+     F: FnMut(Node) -> I,
+-    I: Iterator<Item = &'a [u8]> + 'a,
++    T: Into<Cow<'a, [u8]>>,
++    I: Iterator<Item = T>,
+ {
+-    type I = I;
++    type I = iter::Map<I, fn(T) -> Cow<'a, [u8]>>;
+
+     fn text(&mut self, node: Node) -> Self::I {
+-        (self)(node)
++        (self)(node).map(T::into)
+     }
+ }
+
+ impl<'a> TextProvider<'a> for &'a [u8] {
+-    type I = iter::Once<&'a [u8]>;
++    type I = iter::Once<Cow<'a, [u8]>>;
+
+     fn text(&mut self, node: Node) -> Self::I {
+-        iter::once(&self[node.byte_range()])
++        iter::once(Cow::Borrowed(&self[node.byte_range()]))
+     }
+ }
+
diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index 389cb944b9..5209876431 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -27,6 +27,7 @@ (define-module (gnu packages tree-sitter)
   #:use-module (guix git-download)
   #:use-module (guix packages)
   #:use-module (guix utils)
+  #:use-module (gnu packages)
   #:use-module (gnu packages crates-graphics)
   #:use-module (gnu packages crates-io)
   #:use-module (gnu packages icu4c))
@@ -196,6 +197,19 @@ (define-public rust-tree-sitter
 parsing library.")
     (license license:expat)))

+;; We need to apply a patch in order to compile the rust bindings against the
+;; emacs tree-sitter module.
+;; See https://github.com/tree-sitter/tree-sitter/pull/1294
+(define-public rust-tree-sitter-for-emacs
+  (package (inherit rust-tree-sitter)
+    (source (origin
+              (inherit (package-source rust-tree-sitter))
+              (patches (search-patches
+                        "rust-tree-sitter-text-provider-fix.patch"))))
+    ;; Do not show this package in the UI as it's only meant to be used for
+    ;; emacs's tree-sitter module.
+    (properties '((hidden? . #t)))))
+
 (define tree-sitter-delete-generated-files
   '(begin
      (delete-file "binding.gyp")
--
2.34.0