Hey,
Happy new year and a good continuation!
I would like to combine imports from header-args with imports from a
source block.
Here is an example:
* RxJava
:PROPERTIES:
:header-args: :dir src :results output code
:header-args:java: :cmdline -classpath ./rxjava-1.3.8.jar:src:. :cmpflag
-classpath ./rxjava-1.3.8.jar:src:. :imports rx.Observable rx.Subscriber
:END:
#+BEGIN_SRC java
import static rx.Observable.empty;
import static rx.Observable.just;
import static rx.Observable.range;
Observable
.range(5, 5)
.flatMap(x -> just(x * 2))
.flatMap(x -> (x != 10) ? just(x) : empty())
.subscribe(System.out::println);
#+END_SRC
I didnt get the to work so I made a patch.
What do you think about it?
Stay safe!
Best regards
John
>From a602a86e42c2da5cb531ada6c13184d3c307a0db Mon Sep 17 00:00:00 2001
From: John Herrlin <[email protected]>
Date: Fri, 8 Jan 2021 17:11:56 +0100
Subject: [PATCH] lisp/ob-java.el: imports in source block improvement
* lisp/ob-java.el (re-seq): Improve imports when expanding
---
lisp/ob-java.el | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/lisp/ob-java.el b/lisp/ob-java.el
index f70a50192..a5b6649ca 100644
--- a/lisp/ob-java.el
+++ b/lisp/ob-java.el
@@ -86,7 +86,7 @@ like javac -verbose."
(1+ space) (group (1+ (in alnum ?_ ?.))) ; capture the package name
(0+ space) ?\; line-end)
"Regexp for the package statement.")
-(defconst org-babel-java--imports-re (rx line-start (0+ space) "import"
+(defconst org-babel-java--imports-re (rx line-start (0+ space) "import" (opt space "static")
(1+ space) (group (1+ (in alnum ?_ ?.))) ; capture the fully qualified class name
(0+ space) ?\; line-end)
"Regexp for import statements.")
@@ -308,6 +308,15 @@ RESULT-FILE is the temp file to write the result."
(org-babel-java--move-past org-babel-java--package-re)
(insert (concat "import " package "." class ";\n")))))
+(defun re-seq (regexp string)
+ "Get a list of all regexp matches in a string"
+ (save-match-data
+ (let ((pos 0) matches)
+ (while (string-match regexp string pos)
+ (push (match-string 0 string) matches)
+ (setq pos (match-end 0)))
+ matches)))
+
(defun org-babel-expand-body:java (body params)
"Expand BODY with PARAMS.
BODY could be a few statements, or could include a full class
@@ -323,7 +332,9 @@ is simplest to expand the code block from the inside out."
(imports-val (assq :imports params))
(imports (if imports-val
(split-string (org-babel-read (cdr imports-val) nil) " ")
- nil)))
+ nil))
+ (imports-in-body (re-seq org-babel-java--imports-re body))
+ (body (string-trim (replace-regexp-in-string org-babel-java--imports-re "" body))))
(with-temp-buffer
(insert body)
@@ -364,6 +375,12 @@ is simplest to expand the code block from the inside out."
(org-babel-java--move-past org-babel-java--package-re) ; if package is defined, move past it
(insert (mapconcat (lambda (package) (concat "import " package ";")) imports "\n") "\n"))
+ ;; add imports from source block
+ (when imports-in-body
+ (goto-char (point-min))
+ (org-babel-java--move-past org-babel-java--package-re) ; if package is defined, move past it
+ (insert (mapconcat (lambda (import) import) imports-in-body "\n") "\n"))
+
;; add package at the top
(goto-char (point-min))
(when (and packagename (not (re-search-forward org-babel-java--package-re nil t)))
--
2.30.0