emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-java, a proposal on import improvement
@ 2021-01-08 16:28 John Herrlin
  2021-01-08 22:09 ` Dr. Arne Babenhauserheide
  2021-01-09 15:51 ` ian martins
  0 siblings, 2 replies; 10+ messages in thread
From: John Herrlin @ 2021-01-08 16:28 UTC (permalink / raw)
  To: ian martins; +Cc: org-mode-email

[-- Attachment #1: Type: text/plain, Size: 805 bytes --]


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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-ob-java.el-imports-in-source-block-improvement.patch --]
[-- Type: text/x-patch, Size: 2874 bytes --]

From a602a86e42c2da5cb531ada6c13184d3c307a0db Mon Sep 17 00:00:00 2001
From: John Herrlin <jherrlin@gmail.com>
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


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-04-25  3:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-01-08 16:28 [PATCH] ob-java, a proposal on import improvement John Herrlin
2021-01-08 22:09 ` Dr. Arne Babenhauserheide
2021-01-09 15:55   ` ian martins
2021-01-09 15:51 ` ian martins
2021-01-10 20:55   ` John Herrlin
2021-01-12 12:00     ` ian martins
2021-01-16 12:56       ` ian martins
2021-01-16 15:32         ` John Herrlin
2021-01-16 20:49           ` ian martins
2021-04-25  3:42             ` Timothy

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).