Org-mode mailing list
 help / color / mirror / 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; 7+ 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	[flat|nested] 7+ messages in thread

* Re: [PATCH] ob-java, a proposal on import improvement
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Dr. Arne Babenhauserheide @ 2021-01-08 22:09 UTC (permalink / raw)
  To: John Herrlin; +Cc: emacs-orgmode

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


John Herrlin <jherrlin@gmail.com> writes:
> 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

> What do you think about it?

I can’t give an informed opinion about the patch, but the example looks
great! I did not know that Java support in org-babel is that good.

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

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

* Re: [PATCH] ob-java, a proposal on import improvement
  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:51 ` ian martins
  2021-01-10 20:55   ` John Herrlin
  1 sibling, 1 reply; 7+ messages in thread
From: ian martins @ 2021-01-09 15:51 UTC (permalink / raw)
  To: John Herrlin; +Cc: org-mode-email

On Fri, Jan 8, 2021 at 11:28 AM John Herrlin <jherrlin@gmail.com> wrote:

> I would like to combine imports from header-args with imports from a
> source block.
> ...
> I didnt get the to work so I made a patch.

John, Sorry that wasn't working. Thanks for investigating and
submitting a fix.  I think the problem was that I was missing static
imports, which you fixed in the first chunk of your patch. I don't
think the rest of the change is necessary. Could you revert the other
chunks and re-test?

This works for me using master, and your patch produces nearly the
same code (only import order differs):

    #+begin_src java :results output :imports java.util.HashMap
      import java.util.Map;

      Map<String, Integer> hash = new HashMap<>();
      hash.put("one", 1);
      hash.put("two", 2);
      System.out.println(hash);
    #+end_src


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

* Re: [PATCH] ob-java, a proposal on import improvement
  2021-01-08 22:09 ` Dr. Arne Babenhauserheide
@ 2021-01-09 15:55   ` ian martins
  0 siblings, 0 replies; 7+ messages in thread
From: ian martins @ 2021-01-09 15:55 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide; +Cc: Org-Mode mailing list, John Herrlin

> I can’t give an informed opinion about the patch, but the example looks
> great! I did not know that Java support in org-babel is that good.

Thanks. Yes, ob-java got an update with org 9.4.


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

* Re: [PATCH] ob-java, a proposal on import improvement
  2021-01-09 15:51 ` ian martins
@ 2021-01-10 20:55   ` John Herrlin
  2021-01-12 12:00     ` ian martins
  0 siblings, 1 reply; 7+ messages in thread
From: John Herrlin @ 2021-01-10 20:55 UTC (permalink / raw)
  To: ian martins; +Cc: org-mode-email

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


ian martins <ianxm@jhu.edu> writes:

> On Fri, Jan 8, 2021 at 11:28 AM John Herrlin <jherrlin@gmail.com> wrote:
>
>> I would like to combine imports from header-args with imports from a
>> source block.
>> ...
>> I didnt get the to work so I made a patch.
>
> John, Sorry that wasn't working. Thanks for investigating and
> submitting a fix.  I think the problem was that I was missing static
> imports, which you fixed in the first chunk of your patch. I don't
> think the rest of the change is necessary. Could you revert the other
> chunks and re-test?

Thats looks correct! Thanks!

Here is a patch with the regexp fix.

Take care!


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-java-Include-static-imports-in-regex.patch --]
[-- Type: text/x-patch, Size: 1037 bytes --]

From 3de5cf1c3173285a7f51ab436f02419f8c9f5ffb Mon Sep 17 00:00:00 2001
From: John Herrlin <jherrlin@gmail.com>
Date: Sun, 10 Jan 2021 21:47:26 +0100
Subject: [PATCH] ob-java: Include static imports in regex

* lisp/ob-java.el (org-babel-java--imports-re): Include static imports in Java import regex.

TINYCHANGE
---
 lisp/ob-java.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/ob-java.el b/lisp/ob-java.el
index f70a50192..05f591ee3 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.")
-- 
2.30.0


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

* Re: [PATCH] ob-java, a proposal on import improvement
  2021-01-10 20:55   ` John Herrlin
@ 2021-01-12 12:00     ` ian martins
  2021-01-16 12:56       ` ian martins
  0 siblings, 1 reply; 7+ messages in thread
From: ian martins @ 2021-01-12 12:00 UTC (permalink / raw)
  To: John Herrlin; +Cc: org-mode-email

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

 <https://orgmode.org/worg/org-contribute.html#commit-messages>On Sun, Jan
10, 2021 at 3:55 PM John Herrlin <jherrlin@gmail.com> wrote:
> ian martins <ianxm@jhu.edu> writes:
> > I think the problem was that I was missing static
> > imports, which you fixed in the first chunk of your patch. I don't
> > think the rest of the change is necessary. Could you revert the other
> > chunks and re-test?
>
> Thats looks correct! Thanks!
>
> Here is a patch with the regexp fix.

That's great. One small change, though. This only allows for a single space
between "import" and "static" so if someone were to put in two it wouldn't
work. I actually did the same thing in an earlier version and it caused a
problem. Since then I went to =(1+ space)= everywhere. Could you also move
the part that you're adding down to the next line. It's not that the line
is too long, but it keeps it to one thing per line.

The commit message is fine, but the first line shouldn't end in a period.

ref: https://orgmode.org/worg/org-contribute.html#commit-messages

[-- Attachment #2: Type: text/html, Size: 1613 bytes --]

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

* Re: [PATCH] ob-java, a proposal on import improvement
  2021-01-12 12:00     ` ian martins
@ 2021-01-16 12:56       ` ian martins
  0 siblings, 0 replies; 7+ messages in thread
From: ian martins @ 2021-01-16 12:56 UTC (permalink / raw)
  To: John Herrlin; +Cc: org-mode-email

John, would you mind if I go ahead and make the change I mentioned and
push it with you as the author?

On Tue, Jan 12, 2021 at 7:00 AM ian martins <ianxm@jhu.edu> wrote:
>
> On Sun, Jan 10, 2021 at 3:55 PM John Herrlin <jherrlin@gmail.com> wrote:
> > ian martins <ianxm@jhu.edu> writes:
> > > I think the problem was that I was missing static
> > > imports, which you fixed in the first chunk of your patch. I don't
> > > think the rest of the change is necessary. Could you revert the other
> > > chunks and re-test?
> >
> > Thats looks correct! Thanks!
> >
> > Here is a patch with the regexp fix.
>
> That's great. One small change, though. This only allows for a single space between "import" and "static" so if someone were to put in two it wouldn't work. I actually did the same thing in an earlier version and it caused a problem. Since then I went to =(1+ space)= everywhere. Could you also move the part that you're adding down to the next line. It's not that the line is too long, but it keeps it to one thing per line.
>
> The commit message is fine, but the first line shouldn't end in a period.
>
> ref: https://orgmode.org/worg/org-contribute.html#commit-messages


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

end of thread, other threads:[~2021-01-16 12:57 UTC | newest]

Thread overview: 7+ 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

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