Org-mode mailing list
 help / color / mirror / Atom feed
From: John Herrlin <jherrlin@gmail.com>
To: ian martins <ianxm@jhu.edu>
Cc: org-mode-email <emacs-orgmode@gnu.org>
Subject: [PATCH] ob-java, a proposal on import improvement
Date: Fri, 08 Jan 2021 17:28:33 +0100
Message-ID: <87o8hzv1e8.fsf@gmail.com> (raw)

[-- 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


             reply	other threads:[~2021-01-08 16:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-08 16:28 John Herrlin [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://orgmode.org

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87o8hzv1e8.fsf@gmail.com \
    --to=jherrlin@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=ianxm@jhu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Org-mode mailing list

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://orgmode.org/list/0 list/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 list list/ https://orgmode.org/list \
		emacs-orgmode@gnu.org
	public-inbox-index list

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.emacs.orgmode
	nntp://news.gmane.io/gmane.emacs.orgmode


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git