Re: Java Generics Syntax highlighting

James Ahlborn <[email protected]> Thu, 26 Jul 2007 14:55:42 -0400
Newsgroups gmane.emacs.jdee
Message-ID <[email protected]>
hey,
i used the c++ highlighting (using emacs 21.3) to get java 1.5
highlighting pretty much working.  just add this to your emacs init
files somewhere.

(require 'font-lock)

;; FIXME temp hack to get a little better java 1.5 support
(let* ((java-keywords
  (eval-when-compile
    (regexp-opt
     '("catch" "do" "else" "super" "this" "finally" "for" "if"
       ;; Anders Lindgren <****> says these have gone.
       ;; "cast" "byvalue" "future" "generic" "operator" "var"
       ;; "inner" "outer" "rest"
       "implements" "extends" "throws" "instanceof" "new"
       "interface" "return" "switch" "throw" "try" "while"))))
       ;;
       ;; Classes immediately followed by an object name.
       (java-type-names
  `(mapconcat 'identity
    (cons
     ,(eval-when-compile
        (regexp-opt '("boolean" "char" "byte" "short" "int" "long"
          "float" "double" "void")))
     java-font-lock-extra-types)
    "\\|"))
       (java-type-names-depth `(regexp-opt-depth ,java-type-names))
       ;;
       ;; These are eventually followed by an object name.
       (java-type-specs
  (eval-when-compile
    (regexp-opt
     '("abstract" "const" "final" "synchronized" "transient" "static"
       ;; Anders Lindgren <****> says this has gone.
       ;; "threadsafe"
       "volatile" "public" "private" "protected" "native"
       ;; Carl Manning <[email protected]> says this is new.
       "strictfp"))))
       )

 (setq java-font-lock-keywords-3
  (append

   (list
    ;; support static import statements
    '("\\<\\(import\\)\\>\\s-+\\(static\\)\\s-+\\(\\sw+\\)"
      (1 font-lock-keyword-face)
      (2 font-lock-keyword-face)
      (3 (if (equal (char-after (match-end 0)) ?\.)
             'jde-java-font-lock-package-face
           'font-lock-type-face))
      ("\\=\\.\\(\\sw+\\)" nil nil
       (1 (if (and (equal (char-after (match-end 0)) ?\.)
                   (not (equal (char-after (+ (match-end 0) 1)) ?\*)))
              'jde-java-font-lock-package-face
            'font-lock-type-face))))
    )

   java-font-lock-keywords-2

   ;;
   ;; More complicated regexps for more complete highlighting for types.
   ;; We still have to fontify type specifiers individually, as Java is
hairy.
   (list
    ;;
    ;; Fontify class names with ellipses
    `(eval .
      (cons (concat "\\<\\(" ,java-type-names "\\)\\>\\.\\.\\.[^.]")
      '(1 font-lock-type-face)))
    ;;
    ;; Fontify random types immediately followed by an item or items.
    `(eval .
      (list (concat "\\<\\(\\(?:" ,java-type-names "\\)"
                    "\\(?:\\(?:<.*>\\)\\|\\>\\)\\(?:\\.\\.\\.\\)?\\)"
        "\\([ \t]*\\[[ \t]*\\]\\)*"
        "\\([ \t]*\\sw\\)")
      ;; Fontify each declaration item.
      (list 'font-lock-match-c-style-declaration-item-and-skip-to-next
      ;; Start and finish with point after the type specifier.
      (list 'goto-char (list 'match-beginning
           (+ ,java-type-names-depth 3)))
      (list 'goto-char (list 'match-beginning
           (+ ,java-type-names-depth 3)))
      ;; Fontify as a variable or function name.
      '(1 (if (match-beginning 2)
        font-lock-function-name-face
      font-lock-variable-name-face)))))
    ;;
    ;; Fontify those that are eventually followed by an item or items.
    (list (concat "\\<\\(" java-type-specs "\\)\\>"
      "\\([ \t]+\\sw+\\>"
      "\\([ \t]*\\[[ \t]*\\]\\)*"
      "\\)*")
    ;; Fontify each declaration item.
    '(font-lock-match-c-style-declaration-item-and-skip-to-next
      ;; Start with point after all type specifiers.
      (goto-char (or (match-beginning 5) (match-end 1)))
      ;; Finish with point after first type specifier.
      (goto-char (match-end 1))
      ;; Fontify as a variable or function name.
      (1 (if (match-beginning 2)
       font-lock-function-name-face
     font-lock-variable-name-face))))

      )))
 )



On Tue, 2007-07-24 at 16:57 -0400, Daniel Guilderson wrote:

> Is anyone working on syntax highlighting for Java Generics in JDE?  The 
> current syntax highlighting doesn't work too well with Java Generics.  
> Attached is an example of what I'm talking about (in html).
> 
> Dan
> 
> 
> HTML page attachment (test.html)
> 
> Before Java Generics:
> 
> Vector messages = new Vector(); 
> 
> Currently with Java Generics:
> 
> Vector<Object> messages = new Vector<Object>(); 
> 
> Something like this would be better:
> 
> Vector<Object> messages = new Vector<Object>(); 
> 
> Or this:
> 
> Vector<Object> messages = new Vector<Object>(); 
> 
> Or this:
> 
> Vector<Object> messages = new Vector<Object>(); 
> 
> Or maybe this would make the most sense:
> 
> Vector<Object> messages = new Vector<Object>(); 
>