Java Imenu

My patch to CC-mode, which I talk about here, was finally accepted and submitted. The final regex turns out to be:

 
(defvar cc-imenu-java-generic-expression
  `((nil
     ,(concat
       "[" c-alpha "_][\]\[." c-alnum "_<> ]+[ \t\n\r]+" ; type spec
       "\\([" c-alpha "_][" c-alnum "_]*\\)" ; method name
       "[ \t\n\r]*"
       ;; An argument list that is either empty or contains any number
       ;; of arguments.  An argument is any number of annotations
 
       ;; followed by a type spec followed by a word.  A word is an
       ;; identifier.  A type spec is an identifier, possibly followed
       ;; by < typespec > possibly followed by [].
       (concat "("
               "\\("
                 "[ \t\n\r]*"
		 "\\("
		   "@"
		   "[" c-alpha "_]"
		   "[" c-alnum "._]*"
		   "[ \t\n\r]+"
		 "\\)*"
		 "\\("
		   "[" c-alpha "_]"
		   "[\]\[" c-alnum "_.]*"
		   "\\("
		     "<"
		     "[ \t\n\r]*"
		     "[\]\[.," c-alnum "_<> \t\n\r]*"
		     ">"
		   "\\)?"
                   "\\(\\[\\]\\)?"
                   "[ \t\n\r]+"
                 "\\)"
                 "[" c-alpha "_]"
                 "[" c-alnum "_]*"
                 "[ \t\n\r,]*"
               "\\)*"
               ")"
               "[.," c-alnum " \t\n\r]*"
               "{"
               )) 1))
  "Imenu generic expression for Java mode.  See `imenu-generic-expression'.")

You can install this in your emacs initializations for now; it will be in the 23.2 release. I’m glad it finally made it in - it will make my personal customizations slightly shorter.

As a result of this patch, I was also asked to join the cc-mode project in order to bring their Java support up to 1.6 - it’s unfortunately lagged behind, this patch being one example. If you’ve had any similar issue, please let me know!

Tags:

3 Responses to “Java Imenu”

  1. sean says:

    Hopefully the “htat” in the comments was fixed ;-).

  2. Joel says:

    Well you got enums, annotations, generics and the keywords transient, strictfp and volatile. A quick look through lisp/progmodes/cc-langs.el makes me believe that at least enum is missing.

    Big up for working on this!

  3. Joel says:

    In this, Imenu only finds the class Test and the method main:

    public class Test {
    enum moreEnums { MONKEY, BANANA };

    enum inClassEnum { FOO, BAR, BAZ }

    // look Ma no semicolon ^^
    // blame Mark Reinholds!

    enum moreEnums2 { THE, CAKE, IS, A, LIE };

    public static void main(String []args) {
    System.out.println(Animals.CAT.CAT.CAMEL.performFunkyMove());
    System.out.println(Animals.CAT.performFunkyMove());
    System.out.println(Animals.CAT.CAT.CAMEL.getSound());
    System.out.println(Animals.CAT.DOG.CAMEL.CAT.DOG.getSound());
    }
    }

    enum Animals {
    CAT(”Meow”), DOG(”Woof”), CAMEL(”Bää”) {
    public String performFunkyMove() {
    return “shake camel hump”;
    }
    };

    private String sound;

    public String getSound() { return sound; }

    Animals(String sound) {
    this.sound = sound;
    }

    public String performFunkyMove() {
    return “boring animal”;
    }
    }

Leave a Reply