-
Notifications
You must be signed in to change notification settings - Fork 55
/
jdee-javadoc.el
1935 lines (1763 loc) · 71.2 KB
/
jdee-javadoc.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; jdee-javadoc.el --- JDEE javadoc autodoc
;; Author: David Ponce <david@dponce.com>
;; Maintainer: David Ponce
;; Paul Landes <landes <at> mailc dt net>
;; Copyright (C) 1998-2004 by David Ponce
;; Copyright (C) 2009 by Paul Landes
;; Keywords: java, tools
;;
;; 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 provides a javadoc comment checker and generator to
;; help handling of Java source documentation.
;;; Code:
(require 'semantic/java)
(require 'jdee-backend)
(require 'jdee-javadoc-gen)
(require 'jdee-parse)
(require 'jdee-project-file)
(require 'regexp-opt)
(require 'tempo)
;; working-XXX are from CEDET/working library, which is not included
;; in Emacs, so need to workaround that with this code (also from
;; the library).
(eval-and-compile
(or (require 'working nil t)
(progn
(defvar working-message nil
"Message stored when in a status loop.")
(defmacro working-status-forms (message donestr &rest forms)
"Contain a block of code during which a working status is shown."
(list 'let (list (list 'msg message) (list 'dstr donestr)
'(ref1 0))
(cons 'progn forms)))
(defun working-status (&optional percent &rest args)
"Called within the macro `working-status-forms', show the status."
(message "%s%s" (apply 'format msg args)
(if (eq percent t) (concat "... " dstr)
(format "... %3d%%"
(or percent
(floor (* 100.0 (/ (float (point))
(point-max)))))))))
(defun working-dynamic-status (&optional number &rest args)
"Called within the macro `working-status-forms', show the status."
(message "%s%s" (apply 'format msg args)
(format "... %c" (aref [ ?- ?/ ?| ?\\ ] (% ref1 4))))
(setq ref1 (1+ ref1)))
(put 'working-status-forms 'lisp-indent-function 2))))
;;;; Customization
;;;; -------------
(defgroup jdee-javadoc nil
"JDEE javadoc utilities"
:group 'jdee
:prefix "jdee-javadoc-")
;; IMPORTANT: This function must be defined before the following
;; defcustoms because it is used in their :set clause.
(defun jdee-javadoc-define-template (sym val)
"Define a template (see `tempo-define-template').
The template name is the `symbol-name' of SYM from which the
'-template' suffix has been removed, prefixed by 'tempo-template-'.
VAL is the template value. If VAL is a string it is converted to a
list of template elements."
(let* ((name (symbol-name sym))
(template-name
(if (string-match "\\(.*\\)-template$" name)
(match-string 1 name)
(error "Invalid template variable name: %S" name)))
(template-val
(if (stringp val)
(car (read-from-string (format "(%s)" val)))
val)))
(tempo-define-template template-name template-val nil name)
(set-default sym val)))
(defcustom jdee-javadoc-describe-class-template
"\"* Describe class \" (jdee-javadoc-code name) \" here.\""
"*Line template used to describe a class.
If nil the line is not inserted.
The variable 'name' is set to the class name.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-describe-class'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-describe-interface-template
"\"* Describe interface \" (jdee-javadoc-code name) \" here.\""
"*Line template used to describe an interface.
If nil the line is not inserted.
The variable 'name' is set to the interface name.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-describe-interface'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-describe-constructor-template
"\"* Creates a new \" (jdee-javadoc-code name) \" instance.\""
"*Line template used to describe a constructor.
If nil the line is not inserted.
The variable 'name' is set to the constructor name (that is the class
name). See `jdee-javadoc-autodoc-at-line' for usage. Define the
template variable `tempo-template-jdee-javadoc-describe-constructor'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-describe-method-template
"\"* Describe \" (jdee-javadoc-code name) \" method here.\""
"*Line template used to describe a method.
If nil the line is not inserted.
The variable 'name' is set to the method name.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-describe-method'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-describe-field-template
"\"* Describe \" (jdee-javadoc-field-type modifiers)
\" \" (jdee-javadoc-code name) \" here.\""
"*Line template used to describe a method.
If nil the line is not inserted.
The variable 'name' is set to the field name.
The variable 'type' is set to the field type.
The variable 'modifiers' is set to the field modifiers.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-describe-field'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-param-tag-template
"\"* @param \" name \" \" (jdee-javadoc-a type)
\" \" (jdee-javadoc-code type) \" value\""
"*Line template used to describe a parameter.
If nil the line is not inserted.
The variable 'name' is set to the parameter name.
The variable 'type' is set to the parameter type.
A line is inserted for each parameter.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-param-tag'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-return-tag-template
"\"* @return \" (jdee-javadoc-a type)
\" \" (jdee-javadoc-code type) \" value\""
"*Line template used to describe a returned value.
If nil the line is not inserted.
The variable 'type' is set to the returned type.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-return-tag'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-exception-tag-template
"\"* @exception \" type \" if an error occurs\""
"*Line template used to describe an exception.
If nil the line is not inserted.
The variable 'type' is set to the exception type.
A line is inserted for each exception in the 'throws' clause.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-exception-tag'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-author-tag-template
"\"* @author <a href=\\\"mailto:\" user-mail-address
\"\\\">\" user-full-name \"</a>\""
"*Line template used to give an author.
If nil the line is not inserted.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-author-tag'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-version-tag-template
"\"* @version 1.0\""
"*Line template used to give a version.
If nil the line is not inserted.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-version-tag'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
;; (defcustom jdee-javadoc-see-tag-template
;; '("* @see " ref)
;; "*Line template used to give a reference.
;; If nil the line is not inserted.
;; The variable 'ref' is set to the class or interface name.
;; A line is inserted for each name in the 'extends' then 'implements'
;; clauses. See `jdee-javadoc-autodoc-at-line' for usage. Define the
;; template variable `tempo-template-jdee-javadoc-see-tag'."
;; :group 'jdee-javadoc
;; :type '(choice :tag "Template form"
;; (text :format "%t\n%v" :tag "String")
;; (repeat :tag "Lisp Expressions" (sexp :tag "")))
;; :set 'jdee-javadoc-define-template)
;; (defcustom jdee-javadoc-since-tag-template
;; '("* @since 1.0")
;; "*Line template used to give a since reference.
;; If nil the line is not inserted.
;; See `jdee-javadoc-autodoc-at-line' for usage. Define the template
;; variable `tempo-template-jdee-javadoc-since-tag'."
;; :group 'jdee-javadoc
;; :type '(choice :tag "Template form"
;; (text :format "%t\n%v" :tag "String")
;; (repeat :tag "Lisp Expressions" (sexp :tag "")))
;; :set 'jdee-javadoc-define-template)
(defcustom jdee-javadoc-end-block-template
nil
"*Javadoc end comment block characters.
If nil \"*/\" is inserted.
See `jdee-javadoc-autodoc-at-line' for usage. Define the template
variable `tempo-template-jdee-javadoc-end-block'."
:group 'jdee-javadoc
:type '(choice :tag "Template form"
(text :format "%t\n%v" :tag "String")
(repeat :tag "Lisp Expressions" (sexp :tag "")))
:set 'jdee-javadoc-define-template)
;;;; Utilities
;;;; ---------
(defmacro jdee-javadoc-status-forms (message donestr &rest forms)
"Wrapper for `working-status-forms'.
See `working-status-forms' for details on MESSAGE, DONESTR and FORMS
arguments. Does not override an outer `working-status-forms'
MESSAGE."
`(working-status-forms (or working-message ,message) ,donestr
,@forms))
(defun jdee-javadoc-dynamic-status (&rest args)
"Wrapper for `working-dynamic-status'.
Does nothing if not called within the macro `working-status-forms'.
See `working-dynamic-status' for meaning of ARGS."
(and working-message
(apply #'working-dynamic-status args)))
(defalias 'jdee-javadoc-skip-spaces-forward
'semantic-java-skip-spaces-forward)
(defalias 'jdee-javadoc-indent-line 'c-indent-line)
(defun jdee-javadoc-indent-region (start end)
"Indent region between START and END."
(save-excursion
(goto-char end)
(setq end (point-marker))
(goto-char start)
(while (< (point) end)
(jdee-javadoc-dynamic-status)
(or (and (bolp) (eolp))
(jdee-javadoc-indent-line))
(forward-line 1))
(move-marker end nil)))
(defun jdee-javadoc-map (f l &rest args)
"Apply F to each element of L.
F receives optional ARGS after the current element of L."
(while l
(apply f (car l) args)
(setq l (cdr l))))
(defun jdee-javadoc-window-lines ()
"Return the number of lines of the selected window.
This number may be greater than the number of actual lines in the
buffer if any wrap on the display due to their length."
(let ((start (point-min))
(end (point-max)))
(if (= start end)
0
(save-excursion
(save-restriction
(widen)
(narrow-to-region start end)
(goto-char start)
(vertical-motion (buffer-size)))))))
(defun jdee-javadoc-adjust-window (window)
"Adjust WINDOW height to fit its buffer contents."
(save-selected-window
(select-window window)
(let ((height (window-height))
(lines (+ 3 (jdee-javadoc-window-lines))))
;; ensure window will not be deleted if too small
(if (< lines window-min-height)
(setq lines window-min-height))
(enlarge-window (- lines height)))))
(defsubst jdee-javadoc-variable-name (name)
"Return canonical variable name from NAME.
That is strip any array brackets from NAME."
(if (string-match "\\`\\([^[]+\\)[[]" name)
(match-string 1 name)
name))
(defcustom jdee-javadoc-check-undeclared-exception-flag nil
"*non-nil means to check for undeclared exceptions.
When an exception is implicitly declared (inherits from
RuntimeException) any associated @exception/@throws tag that already
exists will be just kept without issuing a warning. Other extra tags
are automatically removed.
If nil extra @exception/@throws tags are never removed but warnings
are issued for (maybe) undeclared exceptions."
:group 'jdee-javadoc
:type 'boolean)
(defun jdee-javadoc-implicit-exception-p (type)
"Return non-nil if TYPE is an implicitly declared exception.
That is if TYPE inherits from java.lang.RuntimeException or if Java
reflection failed to process TYPE."
(condition-case nil
(jdee-backend-is-ancestor-of "java.lang.RuntimeException"
(jdee-parse-get-qualified-name type))
(error t)))
;;;; Text helpers
;;;; ------------
(defun jdee-javadoc-field-type (modifiers)
"Return field category.
That is \"constant\" if field MODIFIERS contains \"static\" and
\"final\" or \"variable\" otherwise. Useful to generate field
description."
(if (and (member "static" modifiers) (member "final" modifiers))
"constant"
"variable"))
(defun jdee-javadoc-a (word)
"Return \"an\" if WORD begin with a vowel or \"a\" otherwise.
Useful to generate description like \"an int value\" or \"a long value\"."
(if (string-match "^[aeiouyAEIOUY]" word)
"an" "a"))
(defun jdee-javadoc-code (text)
"Return \"<code>TEXT</code>\".
Useful to generate HTML code style."
(concat "<code>" text "</code>"))
;;;; Javadoc comment parser
;;;; ----------------------
;; tags and matchers (many are provided by Semantic)
;;
(defconst jdee-javadoc-desc-tag
"*DESCRIPTION*"
"Special internal tag associated to descriptions.")
(defconst jdee-javadoc-start-tag-regexp
"[\r\n][ \t]*\\(/\\*\\*\\|\\*?\\)[ \t]*@"
"Regexp matching the beginning of a tag.")
(defconst jdee-javadoc-end-tag-regexp
(concat "\\(" jdee-javadoc-start-tag-regexp
"\\|[ \n\r\t]*\\*/\\)")
"Regexp matching the end of a tag or description.")
;; Core comment parser
;;
(defun jdee-javadoc-normalize-description (desc)
"Ensure DESC text begins with '\\n* ' and ends with '\\n*\\n'."
(let ((i (string-match "[^ *\n\r\t]" desc)))
(if i
(setq desc (concat "\n* " (substring desc i))))
;; TODO: ensure empty line at end
desc))
(defun jdee-javadoc-normalize-ref (val)
"Strip any [* \\n\\r\\t] from VAL."
(let* ((keep "[^* \n\r\t]+")
(ref "")
(i (string-match keep val))
j)
(while i
(jdee-javadoc-dynamic-status)
(setq j (match-end 0)
ref (concat ref (substring val i j))
i (string-match keep val j)))
ref))
(defun jdee-javadoc-parse-description (docstring)
"Return the description from DOCSTRING or nil if not found.
The returned value has the form ((DESC)). See also
`jdee-javadoc-parse-tag-values'."
(let ((matcher "/\\*\\*")
(i 0)
j tag-val)
(when (string-match matcher docstring)
(jdee-javadoc-dynamic-status)
(setq j (match-end 0))
(setq i (string-match jdee-javadoc-end-tag-regexp docstring j))
(setq tag-val (if i
(substring docstring j i)
(substring docstring j)))
;; Ensure that a valid description exists
(if (not (string-equal ""
(jdee-javadoc-normalize-ref tag-val)))
(list (list tag-val))))))
(defun jdee-javadoc-parse-tag-values (docstring tag &optional with-key)
"Return from DOCSTRING the list of TAG values or nil if not found.
Each value is a pair (VALUE-STRING . VALUE-KEY). If optional WITH-KEY
is 'name VALUE-KEY is the first word of VALUE-STRING. If optional
WITH-KEY is 'ref VALUE-KEY is a normalized VALUE-STRING reference (see
`jdee-javadoc-normalize-ref'). Otherwise VALUE-KEY is nil."
(let ((matcher (concat jdee-javadoc-start-tag-regexp tag))
(i 0)
j tag-val key tag-list)
(while (string-match matcher docstring i)
(jdee-javadoc-dynamic-status)
(setq j (match-end 0))
(setq i (or (string-match
jdee-javadoc-end-tag-regexp docstring j)
(length docstring)))
(setq tag-val (substring docstring j i))
(cond ((eq with-key 'name)
(setq key (and (string-match
"[* \n\r\t]*\\([^ \n\r\t]+\\)" tag-val)
(jdee-javadoc-variable-name
(match-string 1 tag-val)))))
((eq with-key 'ref)
(setq key (jdee-javadoc-normalize-ref tag-val))))
(setq tag-list (cons (cons tag-val key) tag-list)))
(nreverse tag-list)))
(defun jdee-javadoc-parse-tag (tag docstring)
"Return the TAG documentation from DOCSTRING or nil if not found.
Documentation has the form (TAG VALUE-LIST). See also
`jdee-javadoc-parse-tag-values'."
(cond ((string-equal tag jdee-javadoc-desc-tag)
(jdee-javadoc-parse-description docstring))
((member tag semantic-java-doc-with-name-tags)
(jdee-javadoc-parse-tag-values docstring tag 'name))
((member tag semantic-java-doc-with-ref-tags)
(jdee-javadoc-parse-tag-values docstring tag 'ref))
(t
(jdee-javadoc-parse-tag-values docstring tag))
))
(defun jdee-javadoc-parse-tag-list (docstring)
"Return the list of tag found in DOCSTRING."
(let* ((matcher (concat jdee-javadoc-start-tag-regexp
"\\([^ \n\r\t]+\\)"))
(depth (regexp-opt-depth matcher))
(i (string-match matcher docstring))
j tag-list)
(while i
(jdee-javadoc-dynamic-status)
(setq tag-list (cons (match-string depth docstring) tag-list))
(setq i (string-match matcher docstring (match-end depth))))
(nreverse tag-list)))
(defun jdee-javadoc-parse-docstring (docstring)
"Return the parsed documentation tree from DOCSTRING.
Result has the following form: (DOCSTRING TAG-LIST TAG-VALUE-ALIST)."
(if docstring
(let (tag-list
tag-alist l tag
throws-assoc except-assoc merged-values)
(jdee-javadoc-status-forms "Parsing" "done"
(jdee-javadoc-dynamic-status)
(setq tag-list (jdee-javadoc-parse-tag-list docstring))
(setq l (cons jdee-javadoc-desc-tag tag-list))
(while l
(jdee-javadoc-dynamic-status)
(setq tag (car l))
(if (assoc tag tag-alist)
nil ; tag already processed
(setq tag-alist
(cons (cons tag
(jdee-javadoc-parse-tag tag docstring))
tag-alist)))
(setq l (cdr l)))
;; The 'throws' and 'exception' tags are equivalent, so
;; their values are merged to allow access to 'exception'
;; tag using 'throws' and vice versa.
(jdee-javadoc-dynamic-status)
(setq throws-assoc (assoc "throws" tag-alist))
(jdee-javadoc-dynamic-status)
(setq except-assoc (assoc "exception" tag-alist))
(when (or throws-assoc except-assoc)
(jdee-javadoc-dynamic-status)
(setq merged-values (append (cdr throws-assoc)
(cdr except-assoc)))
(jdee-javadoc-dynamic-status)
(if throws-assoc
(setcdr throws-assoc merged-values)
(setq tag-alist (cons (cons "throws" merged-values)
tag-alist)))
(jdee-javadoc-dynamic-status)
(if except-assoc
(setcdr except-assoc merged-values)
(setq tag-alist (cons (cons "exception" merged-values)
tag-alist))))
(jdee-javadoc-dynamic-status t))
(list docstring tag-list tag-alist))))
;; Handling of javadoc comment parsed tree
;;
(defmacro jdee-javadoc-doctree-docstring (doctree)
"Return the docstring part of DOCTREE."
`(car ,doctree))
(defmacro jdee-javadoc-doctree-tag-list (doctree)
"Return the tag-list part of DOCTREE."
`(car (cdr ,doctree)))
(defmacro jdee-javadoc-doctree-tag-value-alist (doctree)
"Return the tag-value-alist part of DOCTREE."
`(car (cdr (cdr ,doctree))))
(defun jdee-javadoc-doctree-tag (doctree tag &optional name)
"Return from DOCTREE the list of TAG values.
If optional NAME is non-nil return its specific value."
(let ((doc (cdr
(assoc
tag
(jdee-javadoc-doctree-tag-value-alist doctree)))))
(and doc
name
(setq doc (rassoc name doc))
(setq doc (list doc)))
doc))
(defun jdee-javadoc-doctree-known-tag-list (doctree)
"Return the list of known tags in DOCTREE .
That is tags in `semantic-java-doc-line-tags'."
(delq nil
(mapcar (function
(lambda (tag)
(and (member tag semantic-java-doc-line-tags)
tag)))
(jdee-javadoc-doctree-tag-list doctree))))
(defun jdee-javadoc-doctree-unknown-tag-list (doctree)
"Return the list of unknown tags in DOCTREE .
That is tags not in `semantic-java-doc-line-tags'."
(delq nil
(mapcar (function
(lambda (tag)
(and (not (member tag semantic-java-doc-line-tags))
tag)))
(jdee-javadoc-doctree-tag-list doctree))))
;;;; semantic tags stuff
;;;; ---------------------
(defun jdee-javadoc-tag-doctree (tag)
"Return the parsed documentation tree from TAG."
(jdee-javadoc-parse-docstring
(semantic-documentation-for-tag tag t)))
(defun jdee-javadoc-replace-documentation (tag &optional docstring)
"Replace TAG documentation with DOCSTRING.
If DOCSTRING is nil just delete the existing documentation."
(let* ((comment (semantic-documentation-for-tag tag 'flex))
start end)
(when comment
(set-buffer (semantic-tag-buffer tag))
(setq start (semantic-lex-token-start comment))
(setq end (semantic-lex-token-end comment))
(goto-char start)
(save-excursion
(goto-char end)
(jdee-javadoc-skip-spaces-forward)
(delete-region start (point))
(when docstring
(insert docstring)
(jdee-javadoc-indent-documentation tag))))))
(defun jdee-javadoc-delete-documentation (tag &optional noconfirm)
"Delete TAG documentation.
Require confirmation if optional NOCONFIRM is non-nil. Return non-nil
if done."
(if (or noconfirm
(y-or-n-p (format "Delete '%s' previous documentation? "
(semantic-tag-name tag))))
(progn
(jdee-javadoc-replace-documentation tag)
t)))
(defun jdee-javadoc-recenter-documentation (tag &optional arg)
"Center TAG documentation in window and redisplay frame.
With ARG, put point on line ARG. See also `recenter'."
(let ((comment (semantic-documentation-for-tag tag 'flex))
start)
(if (not comment)
(setq start (semantic-tag-start tag))
(set-buffer (semantic-tag-buffer tag))
(setq start (semantic-lex-token-start comment)))
(goto-char start)
(recenter arg)))
(defun jdee-javadoc-indent-documentation (tag)
"Indent TAG documentation."
(save-excursion
(let ((comment (semantic-documentation-for-tag tag 'flex))
start end)
(when comment
(set-buffer (semantic-tag-buffer tag))
(setq start (semantic-lex-token-start comment))
(setq end (semantic-lex-token-end comment))
(goto-char end)
(jdee-javadoc-skip-spaces-forward)
(jdee-javadoc-indent-region start (point))))))
;;;; Doc checker
;;;; -----------
(defconst jdee-javadoc-checker-report-buffer "*jdee-javadoc-checker*"
"Name of the checker report buffer.")
(defvar jdee-javadoc-checker-tag nil
"Current checked tag.
Local to checker report buffer.")
(defvar jdee-javadoc-checker-buffer nil
"Current checked buffer.
Local to checker report buffer.")
(condition-case nil
(require 'jdee-font-lock)
(error nil))
(defvar jdee-javadoc-checker-report-font-lock-keywords
(list
;; References
(list "`\\(.*\\)'"
1 'font-lock-warning-face)
;; Javadoc tags
(list "\\(@[^ \n\r\t]+\\)"
1 (cond ((boundp 'jdee-font-lock-doc-tag-face)
'jdee-font-lock-doc-tag-face)
(t
'font-lock-constant-face)))
;; Misc.
(list "\\[\\([fnpq]\\)\\]"
1 'font-lock-keyword-face)
)
"Keywords used to highlight the checker report buffer.")
(defvar jdee-javadoc-checker-report-mode-map nil
"Keymap used in `jdee-javadoc-checker-report-mode'.")
(if jdee-javadoc-checker-report-mode-map
()
(let ((keymap (make-sparse-keymap)))
(define-key keymap "q" 'jdee-javadoc-checker-quit)
(define-key keymap "p" 'jdee-javadoc-checker-previous)
(define-key keymap "n" 'jdee-javadoc-checker-next)
(setq jdee-javadoc-checker-report-mode-map keymap)))
(defun jdee-javadoc-checker-report-mode ()
"Mode used in checker report buffer.
\\{jdee-javadoc-checker-report-mode-map}"
(kill-all-local-variables)
(setq major-mode 'jdee-javadoc-checker-report-mode)
(setq mode-name "jdee-javadoc-checker")
(set (make-local-variable 'paragraph-start)
"[ \t]*$")
(set (make-local-variable 'paragraph-separate)
paragraph-start)
(set (make-local-variable 'font-lock-defaults)
'((jdee-javadoc-checker-report-font-lock-keywords)
t t ((?_ . "w"))))
(use-local-map jdee-javadoc-checker-report-mode-map)
(turn-on-font-lock))
(defun jdee-javadoc-checker-show-report (report tag)
"Show the `jdee-javadoc-checker' REPORT for TAG."
(let ((buffer (semantic-tag-buffer tag)))
(with-current-buffer
(get-buffer-create jdee-javadoc-checker-report-buffer)
(setq buffer-read-only nil)
(erase-buffer)
(jdee-javadoc-checker-report-mode)
(set (make-local-variable 'jdee-javadoc-checker-buffer) buffer)
(set (make-local-variable 'jdee-javadoc-checker-tag) tag)
(cond
(report
(define-key (current-local-map) "f" 'jdee-javadoc-checker-fix)
(insert (car report))
(newline 2)
(mapc (function
(lambda (line)
(let* ((from (point))
(to (progn
(fill-region
(point)
(progn
(insert " " line)
(newline)
(point)))
(point))))
(goto-char from)
(delete-char 1)
(insert-char ?\* 1)
(goto-char to))))
(cdr report))
(newline)
(insert "[f]-try to fix ")
)
(t
(define-key (current-local-map) "f" nil)
(insert "Documentation is up-to-date")
(newline 2)))
(insert "[p]-check previous [n]-check next [q]-quit")
(goto-char (point-min))
(setq buffer-read-only t)
(pop-to-buffer (current-buffer))
(jdee-javadoc-adjust-window
(get-buffer-window (current-buffer)))
(sit-for 0)
(save-selected-window
(let ((window (get-buffer-window buffer)))
(if (not window)
nil
(select-window window)
(goto-char (semantic-tag-start tag))
(jdee-javadoc-skip-spaces-forward)
(when (looking-at "/\\*\\*")
(forward-comment 1)
(jdee-javadoc-skip-spaces-forward))
(recenter -4))))
(semantic-momentary-highlight-tag tag))))
(defun jdee-javadoc-check-add-summary (report type name)
"Add a summary to REPORT error list, using tag TYPE and NAME."
(and (setq report (delq nil report)) ;; Clear empty entries
(let* ((count (length report))
(eword (if (= count 1) "error" "errors")))
(cons (format "%s `%s' has %d documentation %s:"
type name count eword)
(nreverse report)))))
;; Basic doc checkers
;;
(defun jdee-javadoc-check-description (doctree)
"Return a message if DOCTREE does not contain a description."
(if (jdee-javadoc-doctree-tag doctree jdee-javadoc-desc-tag)
nil
"Missing description"))
(defun jdee-javadoc-check-required-tags (doctree allowed extra
&rest nocheck)
"Return a message if DOCTREE is missing a required tag.
ALLOWED and EXTRA are respectively the listes of allowed tags and
optional ones. Optional arguments NOCHECK can be a listes of tags
that are not checked."
(let (tag missing)
(while allowed
(setq tag (car allowed)
allowed (cdr allowed))
(or (member tag extra)
(member tag nocheck)
(let ((ignored nocheck) ignore)
(while (and (not ignore) ignored)
(setq ignore (member tag (car ignored))
ignored (cdr ignored)))
ignore)
;; (member tag semantic-java-doc-with-name-tags)
(jdee-javadoc-doctree-tag doctree tag)
(setq missing (cons tag missing))))
(if missing
(concat "Missing tag"
(if (> (length missing) 1) "s @" " @")
(mapconcat 'identity missing ", @")))))
(defun jdee-javadoc-check-suggest-tag-order (tag-list reference)
"Return a list of tags in suggested order from TAG-LIST.
REFERENCE is the list of tags allowed."
(let (otl utl)
(while tag-list
(if (member (car tag-list) semantic-java-doc-line-tags)
(and (member (car tag-list) reference)
(add-to-list 'otl (car tag-list)))
(add-to-list 'utl (car tag-list)))
(setq tag-list (cdr tag-list)))
(append (sort otl #'semantic-java-doc-keyword-before-p)
(nreverse utl))))
(defun jdee-javadoc-check-tag-ordered (doctree reference)
"Return a message if tags in DOCTREE are not correctly ordered.
REFERENCE is the list of allowed tags in correct order. See variable
`semantic-java-doc-line-tags'."
(let* ((tag-list (jdee-javadoc-doctree-tag-list doctree))
(tag (car tag-list))
(l (cdr tag-list))
(ok t))
(while (and l ok)
(jdee-javadoc-dynamic-status)
(setq ok (semantic-java-doc-keyword-before-p tag (car l))
tag (car l)
l (cdr l)))
(if ok
nil
(concat "Recommended tag order is @"
(mapconcat 'identity
(jdee-javadoc-check-suggest-tag-order
tag-list reference)
", @")))))
(defun jdee-javadoc-check-tag-allowed (doctree allowed)
"Return a message if some tags in DOCTREE are not in ALLOWED.
Third party tags (not in `semantic-java-doc-line-tags') are allways
allowed."
(let ((invalids
(delq nil
(mapcar
(function
(lambda (tag)
(jdee-javadoc-dynamic-status)
(and (member tag semantic-java-doc-line-tags)
(not (member tag allowed))
tag)))
(jdee-javadoc-doctree-tag-list doctree)))))
(if (not invalids)
nil
(concat "Invalid tag"
(if (> (length invalids) 1) "s @" " @")
(mapconcat 'identity invalids ", @")))))
;; Tag based doc checkers
;;
(defun jdee-javadoc-check-type (tag doctree)
"Check doc of 'type' (class or interface) TAG.
DOCTREE is the current doctree of TAG. Return a non-nil report if
errors were found."
(let ((name (semantic-tag-name tag))
(type (semantic-tag-type tag))
(main (jdee-javadoc-checker-main-type-p tag))
report)
;; Check for missing description
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-description doctree)
report))
;; Check for missing tags
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-required-tags
doctree semantic-java-doc-type-tags
semantic-java-doc-extra-type-tags
(if main
nil
;; Don't check these tags if internal type.
'("author" "version" "since")))
report))
;; Check for incorrect tag order
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-tag-ordered
doctree semantic-java-doc-type-tags)
report))
;; Check for invalid tags
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-tag-allowed
doctree semantic-java-doc-type-tags)
report))
;; Setup the error summary
(jdee-javadoc-dynamic-status)
(jdee-javadoc-check-add-summary report type name)))
(defun jdee-javadoc-check-variable (tag doctree)
"Check doc of 'variable' (field) TAG.
DOCTREE is the current doctree of TAG. Return a non-nil report if
errors were found."
(let ((name (semantic-tag-name tag))
report)
;; Check for missing description
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-description doctree)
report))
;; Check for missing tags
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-required-tags
doctree semantic-java-doc-variable-tags
semantic-java-doc-extra-variable-tags)
report))
;; Check for incorrect tag order
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-tag-ordered
doctree semantic-java-doc-variable-tags)
report))
;; Check for invalid tags
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-tag-allowed
doctree semantic-java-doc-variable-tags)
report))
;; Setup the error summary
(jdee-javadoc-dynamic-status)
(jdee-javadoc-check-add-summary report "variable" name)))
(defun jdee-javadoc-check-function (tag doctree)
"Check doc of 'function' (method or constructor) TAG.
DOCTREE is the current doctree of TAG. Return a non-nil report if
errors were found."
(let ((name (semantic-tag-name tag))
(type (semantic-tag-type tag))
(args (semantic-tag-function-arguments tag))
(throws (semantic-tag-function-throws tag))
report items item)
;; Check for missing description
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-description doctree)
report))
;; Check for missing tags
(jdee-javadoc-dynamic-status)
(setq report
(cons
(jdee-javadoc-check-required-tags
doctree semantic-java-doc-function-tags
semantic-java-doc-extra-function-tags
;; Don't check return, param and exception/throws tags.
'("return") semantic-java-doc-with-name-tags)
report))
;; Check for missing @param tags
(setq items nil)
(while args
(jdee-javadoc-dynamic-status)
(if (semantic-tag-p (car args))
(progn
(setq item (jdee-javadoc-variable-name
(semantic-tag-name (car args))))
(setq items (cons item items))
(or (jdee-javadoc-doctree-tag doctree "param" item)
(setq report
(cons