Subject: bug#18378: [Patch] wrong fontification of Java code

From: Alan Mackenzie <[email-masked]>
Date: Wed, 22 Jan 2020 20:59:06 +0000

Hello, Stefan.

On Mon, Jan 20, 2020 at 22:16:46 +0100, Stefan Kangas wrote:
> Hi Alan,

> Could you please help review the below patch for cc-fonts.el?

I missed this bug report back in 2014.  The code has moved around so
much since then that Jordon's patch no longer applies.  But a simple
patch with the same idea appears to fix this bug:

diff -r 3d883b66c245 cc-engine.el
--- a/cc-engine.el	Tue Jan 07 19:24:00 2020 +0000
+++ b/cc-engine.el	Wed Jan 22 20:49:53 2020 +0000
@@ -9025,7 +9025,7 @@
 			   (if (looking-at c-:-op-cont-regexp)
 			       (progn (goto-char (match-end 0)) t)
 			     (not
-			      (and (c-major-mode-is 'c++-mode)
+			      (and (c-major-mode-is '(c++-mode java-mode))
 				   (save-excursion
 				     (and
 				      (c-go-up-list-backward)

This patch should apply cleanly to the emacs-27 branch, and most
probably to master, too.

> Thanks in advance.

> Best regards,
> Stefan Kangas

-- 
Alan Mackenzie (Nuremberg, Germany).

> Paul Pogonyshev <[email-masked]> writes:

> > class X
> > {
> >     public void x ()
> >     {
> >         for (int this_is_not_a_function_name : getX ()) {
> >         }
> >     }
> > }
> >
> > In current trunk (compiled five minutes ago)
> > 'this_is_not_a_function_name' above is fontified as a function
> > name. This seems to depend on whether I have '()' after 'getX' or
> > not.
> >
> > Paul

> Jordon Biondo <[email-masked]> writes:

> > I have a potential solution for this bug, as it has impacted me as well.
> >
> > Not sure what the best solution would be but I have included a patch that does
> > solve the problem. From what I can tell there are no tests for java
> > fortification but I have not seen any issues arising in my own java
> > projects. The issue was that in java, a “:” can mark the end of a declaration in
> > a foreach, not just a “;” or “,”.
> >
> > === modified file 'lisp/ChangeLog'
> > *** lisp/ChangeLog	2014-09-01 15:03:45 +0000
> > --- lisp/ChangeLog	2014-09-01 19:11:46 +0000
> > ***************
> > *** 1,3 ****
> > --- 1,8 ----
> > + 2014-09-01  Jordon Biondo  <[email-masked]>
> > + 
> > + 	* progmodes/cc-fonts.el (c-font-lock-declarators): Special
> > + 	case for declarations inside Java foreach loops (Bug#18378)
> > + 
> >   2014-09-01  Eli Zaretskii  <[email-masked]>
> >   
> >   	* ls-lisp.el (ls-lisp-use-string-collate)
> >
> > === modified file 'lisp/progmodes/cc-fonts.el'
> > *** lisp/progmodes/cc-fonts.el	2014-08-24 20:50:11 +0000
> > --- lisp/progmodes/cc-fonts.el	2014-09-01 19:11:59 +0000
> > *************** casts and declarations are fontified.  U
> > *** 1116,1123 ****
> >   	    ;; initializing brace lists.
> >   	    (let (found)
> >   	      (while
> > ! 		  (and (setq found (c-syntactic-re-search-forward
> > ! 			     "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
> >   		       (eq (char-before) ?\[)
> >   		       (c-go-up-list-forward))
> >   		     (setq brackets-after-id t))
> > --- 1116,1128 ----
> >   	    ;; initializing brace lists.
> >   	    (let (found)
> >   	      (while
> > !                   (and (setq found (c-syntactic-re-search-forward
> > !                                     (concat "[;,]\\|\\s)\\|\\'\\|\\(=\\|"
> > !                                             (if (c-major-mode-is 'java-mode)
> > !                                                 ":\\|"
> > !                                               "")
> > !                                             "\\s(\\)")
> > !                                     limit t t))
> >   		       (eq (char-before) ?\[)
> >   		       (c-go-up-list-forward))
> >   		     (setq brackets-after-id t))