-
Notifications
You must be signed in to change notification settings - Fork 55
/
jdee-annotations.el
148 lines (119 loc) · 4.85 KB
/
jdee-annotations.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;;; jdee-annotations.el --- Indentation for Java 5 annotations.
;; $Id$
;; Copyright (C) 2006 by Suraj Acharya
;; Copyright (C) 2009 by Paul Landes
;; Author: Suraj Acharya <[email protected]>
;; Maintainer: Suraj Acharya <[email protected]>
;; Created: 22 Feb 2006
;; Keywords: cc-mode java annotations indentation
;; This file is not part of Emacs
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; This library tries to indent java annotations
;; (http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html)
;; like the code examples listed in the webpage.
;;; Code:
(eval-when-compile
(require 'cc-mode))
(defun c-preprend-offset (symbol offset)
"Find the offset entry for SYMBOL and add OFFSET at the front of the list.
See `c-set-offset' for a description of OFFSET and SYMBOL."
(let ((old-offset (cdr-safe (or (assq symbol c-offsets-alist)
(assq symbol (get 'c-offsets-alist
'c-stylevar-fallback))))))
(if old-offset
(if (listp old-offset)
(c-set-offset symbol (cons offset old-offset))
(c-set-offset symbol (list offset old-offset)))
(c-set-offset symbol offset))))
(defun jdee-annotations-setup ()
"Set up java mode indent function to handle java 1.5 annotations.
The setup function adds one of the custom indentation functions
`c-single-indent-after-java-annotations' or `c-no-indent-after-java-annotations'
to the offset lists of the symbols `arglist-intro', `topmost-intro-cont', `arglist-intro',
`arglist-close', `statement-cont' and `func-decl-cont'.
This function should be called after any calls to `c-set-style'."
(c-preprend-offset 'arglist-intro 'c-single-indent-after-java-annotations)
(c-preprend-offset 'topmost-intro-cont 'c-no-indent-after-java-annotations)
(c-preprend-offset 'arglist-close 'c-no-indent-after-java-annotations)
(c-preprend-offset 'statement-cont 'c-no-indent-after-java-annotations)
(c-preprend-offset 'func-decl-cont 'c-no-indent-after-java-annotations))
(defun c-only-java-annotations-p (langelem)
"Check if there are only java annotations before the current line.
It does this by moving across the region from the start of
LANGELEM to the beginning of this line one sexp at a time. If
during this traversal, this function only sees whitespaces
followed by either a '@' or a '(' then it returns t."
(save-excursion
(condition-case err ;; return nil if any errors are thrown by forward-sexp
(let* ((lim (1- (c-point 'bol)))
(throws (catch 'notAnno
(goto-char (cdr langelem))
(while (< (point) lim)
(if (not (looking-at "\\(\\s \\|\n\\)*\\(@\\|(\\)"))
(throw 'notAnno t))
(forward-sexp 1)))))
(if (not throws)
t)))))
(defun c-no-indent-after-java-annotations (langelem)
"If preceeded by java annotations, indent this line the same as the previous.
Works with topmost-intro-cont, statement-cont, arglist-close and func-decl-cont.
With topmost-intro-cont, indents as
@Id
Long foo;
instead of
@Id
<-->Long foo;
Also for method and class declarations instead of the field foo.
With statement-cont, indents local variables with annotations as above.
With statement-cont, indents as
@Id(
arg=\"value\"
)
Long foo;
instead of
@Id(
arg=\"value\"
)
<-->Long foo;
With arglist-close, indents as
@RequestForEnhancement(
...
)
Instead of
@RequestForEnhancement(
...
<-->)
Argument LANGELEM The language element being indented."
(if (c-only-java-annotations-p langelem)
0))
(defun c-single-indent-after-java-annotations (langelem)
"If preceeded by java annotations, indent this line more than previous.
This function indents this line by `c-basic-offset' columns more
than the previous line.
Works with arglist-intro.
Indents as
@RequestForEnhancement(
id = 2868724,
...
instead of
@RequestForEnhancement(
id = 2868724,
...
Argument LANGELEM The language element being indented."
(if (c-only-java-annotations-p langelem)
c-basic-offset))
(provide 'jdee-annotations)
;;; jdee-annotations.el ends here