-
Notifications
You must be signed in to change notification settings - Fork 55
/
jdee-parse.el
1794 lines (1585 loc) · 73.1 KB
/
jdee-parse.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-parse.el --- Class parsing mechanisms
;; Author: Paul Kinnucan <[email protected]>
;; Maintainer: Paul Landes <landes <at> mailc dt net>
;; Keywords: java, tools
;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004 Paul Kinnucan.
;; Copyright (C) 2009 Paul Landes
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;; Code:
(require 'cl-lib)
(require 'efc)
(require 'eieio)
(require 'etags)
(require 'jdee-avl-tree)
(require 'jdee-imenu)
(require 'rx)
(require 'semantic/ctxt)
(require 'semantic/sb)
(require 'thingatpt)
(require 'jdee-backend)
;; FIXME: refactor
(defvar jdee-complete-private)
(defvar jdee-complete-current-list)
(declare-function jdee-complete-find-completion-for-pair "jdee-complete" (pair &optional exact-completion access-level))
(declare-function jdee-import-find-and-import "jdee-import" (class &optional no-errors no-exclude qualifiedp))
;; FIXME: (require 'cc-engine) doesn't work in Emacs 24.3
(declare-function c-parse-state "cc-engine" ())
(defcustom jdee-auto-parse-enable t
"Enables automatic reparsing of a Java source buffer.
After you makes changes to the buffer, but only if the buffer is less
than `jdee-auto-parse-max-buffer-size'."
:group 'jdee-project
:type 'boolean)
(defcustom jdee-auto-parse-buffer-interval 180
"Time in seconds between buffer change and reparse.
That is between the time between you change a Java source buffer and
the time the JDE reparses the buffer."
:group 'jdee-project
:type 'number)
(defcustom jdee-auto-parse-max-buffer-size 50000
"Maximum size in bytes of buffers automatically reparsed.
Used when `jdee-auto-parse-enable' is non-nil. Setting the threshold
to 0 causes the JDE to parse a buffer automatically regardless of its
size."
:group 'jdee-project
:type 'number)
(defvar jdee-parse-buffer-needs-reparse-p nil
"non-nil if buffer changed since last parse.")
(make-variable-buffer-local 'jdee-parse-buffer-needs-reparse-p)
(defvar jdee-auto-parse-buffer-timer nil
"Timer used to schedule automatic reparse.")
(make-variable-buffer-local 'jdee-auto-parse-buffer-timer)
(defvar jdee-parse-casting nil
"Variable use to determined when the variable was casted")
(defvar jdee-parse-current-beginning (make-marker)
"The beginning of the region where the last completion was inserted.")
(defvar jdee-parse-current-end (make-marker)
"The end of the region where the last completion was inserted.")
(defvar jdee-parse-primitive-types '("byte" "char" "double" "float"
"int" "long" "short" "boolean")
"Primitive Java types.")
(defvar jdee-parse-attempted-to-import nil
"Variable use to avoid looping in jdee-parse-eval-type-of when
the type of a class could not be found an it tried to import it")
;; (makunbound 'jdee-parse-java-symbol-re)
(defvar jdee-parse-java-symbol-re
(rx
(1+ ;; A Java symbol comprises one or more of the following:
(char (?A . ?Z) ;; - upper case characters
(?a . ?z) ;; - lower case characters
(?0 . ?9) ;; - digits
"[]" ;; - square brackets
"?" ;; - question mark
"_" ;; - underscore
"." ;; - period
(160 . 255) ;; - accented characters
)))
"Regular expression that matches any Java symbol.")
(defun jdee-parse-java-name-parts-re (&optional sep)
" Create a regular expression that will identify java name parts.
Name parts are things like java.util.Map or Map or java/util/Map
Create a match group.
SEP defaults to ."
(format "\\([[:alpha:]_$][[:alnum:]_$%s]*\\)" (or sep ".")))
(defun jdee-parse-java-name-part-re ()
"Set one match region on the name"
(jdee-parse-java-name-parts-re ""))
(defun jdee-parse-java-fqn-re ()
"
Set 3 match regions
1 - FQN
2 - package name
3 - unqualified class name
"
(format "\\(%s[.]%s\\)" (jdee-parse-java-name-parts-re) (jdee-parse-java-name-part-re)))
(defun jdee-parse-after-buffer-changed ()
"Reparse the current buffer after any change.
Called after `jdee-auto-parse-buffer-interval' seconds following a
buffer change if `jdee-auto-parse-enable' is non-nil and buffer size
match `jdee-auto-parse-max-buffer-size' threshold."
;; This function should be called only in JDE buffers
;; but for some reason it is called in every buffer
;; in some versions of XEmacs. Hence the following
;; guard.
(if (eq major-mode 'jdee-mode)
(semantic-fetch-tags)))
(defsubst jdee-parse-should-auto-parse-buffer-p ()
"Return non-nil if the JDE should automatically reparse the buffer."
(and jdee-auto-parse-enable
(or
(<= jdee-auto-parse-max-buffer-size 0)
(< (buffer-size) jdee-auto-parse-max-buffer-size))))
(eval-when-compile
(defsubst jdee-auto-parse-delay ()
"Return the time in seconds before auto-parse triggering."
(- (timer-until jdee-auto-parse-buffer-timer (current-time)))))
(defun jdee-parse-buffer-changed-hook (begin end length)
"Hook run when Semantic detects a change in the current buffer.
BEGIN and END are respectively the beginning and end of the range of
changed text. LENGTH is the length in bytes of the pre-change text
replaced by that range. See also `semantic-change-hooks'."
;; This function should be called only in JDE buffers
;; but for some reason it is called in every buffer
;; in some versions of XEmacs. Hence the following
;; guard.
(when (eq major-mode 'jdee-mode)
(setq jdee-parse-buffer-needs-reparse-p t)
(when (jdee-parse-should-auto-parse-buffer-p)
(if (timerp jdee-auto-parse-buffer-timer)
(let ((rem (jdee-auto-parse-delay)))
(cond
((< rem 0)
;; Timer has expired, re-schedule a new auto-parse.
(cancel-timer jdee-auto-parse-buffer-timer)
(setq jdee-auto-parse-buffer-timer nil))
((< rem 2) ;; less that 2 secs. before auto-parse
;; The auto-parse task is about to be triggered when
;; this change occurs, so it is delayed to let finish
;; typing before re-parsing.
(timer-inc-time jdee-auto-parse-buffer-timer
10) ;; wait 10 secs. more.
(message "Auto parse delayed...")))))
;; Schedule a new auto-parse task.
(or (timerp jdee-auto-parse-buffer-timer)
(setq jdee-auto-parse-buffer-timer
(run-with-timer
jdee-auto-parse-buffer-interval
nil
#'jdee-parse-after-buffer-changed))))))
(defun jdee-parse-buffer-contains-multiple-classes-p ()
"Return non-nil if buffer contains multiple class definitions."
(let* ((top-level-classes
(semantic-brute-find-tag-by-class
'type
(semantic-fetch-tags)))
(top-level-class-count (length top-level-classes)))
(or
(> top-level-class-count 1)
(and
(= top-level-class-count 1)
(let* ((inner-class-parts (semantic-tag-type-members (car top-level-classes)))
(inner-classes
(semantic-brute-find-tag-by-class
'type inner-class-parts)))
(>= (length inner-classes) 1))))))
(defvar jdee-parse-buffer-contains-multiple-classes-p nil
"non-nil if buffer contains more than one class definition.")
(make-variable-buffer-local 'jdee-parse-buffer-contains-multiple-classes-p)
(defun jdee-parse-update-after-parse (tokens)
"Hook run after Semantic changed the token cache.
TOKENS is the list of tokens, new value of the cache. It can be nil
when the cache is cleared.
See also `semantic-after-toplevel-cache-change-hook'."
(if (jdee-parse-should-auto-parse-buffer-p)
(setq jdee-parse-buffer-needs-reparse-p nil
jdee-auto-parse-buffer-timer
(and (timerp jdee-auto-parse-buffer-timer)
(cancel-timer jdee-auto-parse-buffer-timer)
nil)))
(if (car tokens)
(setq jdee-parse-buffer-contains-multiple-classes-p
(jdee-parse-buffer-contains-multiple-classes-p)
jdee-parse-the-method-map
(jdee-parse-method-map "Method map"))))
(defun jdee-parse-update-after-partial-parse (tokens)
"Hook run after Semantic updated the token cache.
TOKENS is the list of updated tokens.
See also `semantic-after-partial-cache-change-hook'."
(jdee-parse-update-after-parse (semantic-fetch-tags)))
(defun jdee-parse-get-top-of-class (&optional class-regexp no-move-point)
"Return the position and optionally go to where the class prototype ends and its
class definition (contents) begin.
CLASS-REGEXP the class regular expression to use. This is useful for when the
source Java files has more than one class. If this the symbol `first' then
the first class from the top of the file is used. If this is nil then the
class that the point it in is used but errors out if not in a class.
NO-MOVE-POINT if non-nil just the position is returned, otherwise the point is
moved also."
(interactive "sClass name (or enter for first): ")
(if (eq class-regexp 'first)
(setq class-regexp ".*")
(if (or (null class-regexp) (= 0 (length class-regexp)))
(let* ((class-name (car (jdee-parse-get-innermost-class-at-point))))
(if (null class-name) (error "point is not in a class definition"))
(setq class-regexp (regexp-quote class-name)))))
(let* ((tokens (semantic-fetch-tags))
(classes (semantic-brute-find-tag-by-class 'type tokens))
class-parts pos)
(setq pos
(save-excursion
(save-match-data)
(block outter
(dolist (class classes)
;; Commented out this form because the variable is never
;; used, causes a compile error, and I don't know it's purpose.
;; (setq token-class-regexp (semantic-tag-name class))
(when (or (null class-regexp)
(string-match class-regexp
(semantic-tag-name class)))
(let ((class-parts (semantic-tag-type-members class)))
(setq pos (semantic-tag-start class))
(if (not (numberp pos))
(error (concat "invalid token start, probably because "
"not semantic bovinated top level")))
(goto-char pos)
(if (not (search-forward "{" nil t))
(error "invalid class, can't find next `{'"))
(return-from outter (point))))))))
(if (and (not no-move-point) pos) (goto-char pos))
pos))
(defun jdee-parse-get-nth-member (&optional class-name modifiers
member-name-regexp
elt goto-start-p
compare-method)
"Return the point at the Nth class member (field) with given criteria. The
first match that is a subset of the modifiers give is choosen.
CLASS-NAME the class that has the member.
MODIFIERS a list of Java string modifiers (i.e. \"private\").
MEMBER-NAME-REGEXP regular expresion of the name of the member.
ELT integer indicating the sequential member to get or ELT from the end if
negative.
GOTO-START-P if non-nil return the position at the beginning of the line,
otherwise return the position at the end of the line.
COMPARE-METHOD is either the symbols `equal' or `subset'. This matches the
MODIFIERS creteria as an exact match or subset. This defaults to `subset'."
(interactive)
(if (null member-name-regexp) (setq member-name-regexp ".*"))
(let* ((tokens (semantic-fetch-tags))
(classes (semantic-brute-find-tag-by-class 'type tokens))
cmp-fn vars var-parts var-name var-modifiers i)
(setq cmp-fn
(cond ((or (null compare-method)
(eq 'subset compare-method))
#'(lambda (modifiers var-modifiers)
(cl-subsetp modifiers var-modifiers :test 'equal)
))
((eq 'equal compare-method)
#'(lambda (modifiers var-modifiers)
(let ((m (copy-tree modifiers))
(v (copy-tree var-modifiers)))
(setq m (sort m 'string<)
v (sort v 'string<))
(equal m v)
)))
(t (error "compare-method `%S' not supported"
compare-method))))
(save-excursion
(save-match-data
(block outter
(dolist (class classes)
;; commented out this line because the variable is never bound
;; nor used and I don't understand it's purpose. Paul Kinnucan.
;; (setq token-class-name (semantic-tag-name class))
(when (or (null class-name)
(string-equal class-name (semantic-tag-name class)))
(setq var-parts (semantic-tag-type-members class)
vars (semantic-brute-find-tag-by-class
'variable var-parts)
i 0)
(setq vars
(mapcar
#'(lambda (variable)
(setq var-modifiers
(if modifiers
(semantic-tag-modifiers variable)))
(if (or (and (null var-modifiers) (null modifiers))
(funcall cmp-fn modifiers var-modifiers))
variable)) vars))
(setq vars (remove nil vars))
(setq elt (cond ((not elt) 0)
((< elt 0) (+ (length vars) elt))
(t elt)))
(dolist (variable vars)
(setq var-name (semantic-tag-name variable))
(when (and (= elt i)
(string-match member-name-regexp var-name))
(goto-char (if goto-start-p (semantic-tag-start variable)
(semantic-tag-end variable)))
(return-from outter (point)) )
(setq i (1+ i))))))))))
(defun jdee-parse-get-member-variables (&optional tag)
"Get all member variables of the current class as a semantic tag list.
The optional parameter `tag' can be a semantic tag, which is
then used as the current class instead of the result of `semantic-current-tag'."
(let ((curtag (or tag (semantic-current-tag))))
(remove nil
(mapcar
(lambda (member)
(if (semantic-tag-of-class-p member 'variable)
member
nil))
(semantic-tag-type-members curtag)))))
(defun jdee-parse-get-member-functions (&optional tag)
"Get all member functions of the current class as a semantic tag list.
The optional parameter `tag' can be a semantic tag, which is
then used as the current class instead of the result of `semantic-current-tag'."
(let ((curtag (or tag (semantic-current-tag))))
(remove nil
(mapcar
(lambda (member) (if (semantic-tag-of-class-p member 'function) member nil))
(semantic-tag-type-members curtag)))))
(defun jdee-parse-get-serializable-members (&optional tag)
"Get all serializable member variables of the current class as a semantic tag list.
The optional parameter `tag' can be a semantic tag, which is
then used as the current class instead of the result of `semantic-current-tag'."
(let ((curtag (or tag (semantic-current-tag))))
(remove nil
(mapcar
(lambda (member)
(let ((is-variable (semantic-tag-of-class-p member 'variable))
(modifiers (semantic-tag-modifiers member)))
(if (and is-variable (not (or (member "static" modifiers)
(member "transient" modifiers))))
member
nil)))
(semantic-tag-type-members curtag)))))
(defun jdee-parse-member-is-scalar (tag)
"Check if tag is of a scalar type"
(not (or (string-match "\\[.*\\]" (semantic-tag-name tag))
(string-match "\\[.*\\]" (semantic-tag-type tag)))))
(defun jdee-parse-member-is-primitive (tag)
"Check if tag is of primitive type"
(and (or (semantic-tag-of-type-p tag "byte")
(semantic-tag-of-type-p tag "char")
(semantic-tag-of-type-p tag "short")
(semantic-tag-of-type-p tag "boolean")
(semantic-tag-of-type-p tag "int")
(semantic-tag-of-type-p tag "long"))
(jdee-parse-member-is-scalar tag)))
(defun jdee-parse-member-is-float (tag)
"Check if tag is of a floating point type"
(and (or (semantic-tag-of-type-p tag "float")
(semantic-tag-of-type-p tag "double")))
(jdee-parse-member-is-scalar tag))
(defun jdee-parse-compare-member-types (a b)
"List sorter for a class' member list. Primitive types will be considered
lower, so that they are returned at the head of the list. This ensures they will be
processed first and the more costly complex types later."
(or (and (jdee-parse-member-is-primitive a)
(not (jdee-parse-member-is-primitive b)))
(and (jdee-parse-member-is-float a)
(not (or (jdee-parse-member-is-primitive b)
(jdee-parse-member-is-float b))))
(and (jdee-parse-member-is-scalar a)
(not (or (jdee-parse-member-is-primitive b)
(jdee-parse-member-is-float b)
(jdee-parse-member-is-scalar b))))))
(defun jdee-parse-get-package-name ()
"Gets the name of the package in which the Java source file in the
current buffer resides."
(let ((packages (semantic-brute-find-tag-by-class 'package (current-buffer))))
(if (and (listp packages) (eq (length packages) 1))
(semantic-tag-name (car packages)))))
(defun jdee-parse-get-package-from-name (class-name)
"Gets the package portion of a qualified class name."
(substring
class-name 0
(let ((pos (cl-position ?. class-name :from-end t)))
(if pos
pos
0))))
;;FIXME: remove in favor of jdee-parse-get-package-name
(defun jdee-parse-get-package ()
"Return the package of the class whose source file resides in the current buffer."
(save-excursion
(goto-char (point-min))
(if (re-search-forward "^[ \t]*\\<\\(package\\) +\\([^ \t\n]*\\) *;" (point-max) t)
(concat (buffer-substring-no-properties (match-beginning 2) (match-end 2))
"."))))
(defun jdee-parse-get-unqualified-name (name)
"Gets the last name in a qualified name."
(let ((unqualified-name (substring name (string-match "[^.]+$" name))))
(if unqualified-name unqualified-name name)))
(defun jdee-parse-get-super-class-at-point ()
(condition-case err
(let ((superClass "Object")
(class-re "extends[ \t]+\\([a-zA-z]+[a-zA-Z0-9._]*\\).*[ \n]*"))
(save-excursion
(let ((open-brace-pos
(scan-lists (point) -1 1)))
(when open-brace-pos
(goto-char open-brace-pos)
(when (re-search-backward class-re (point-min) t)
(looking-at class-re)
(setq superClass (buffer-substring-no-properties
(match-beginning 1)
(match-end 1)))))))
superClass)
(error)))
(defconst jdee-parse-class-mod-re
"public\\|abstract\\|final\\|static\\|strictfp\\|protected"
"Regular expression matching class modifiers.")
(defconst jdee-parse-java-comment-re
"/\\*\\(?:[*][^/]\\|[^*][/]\\|[^*/]\\)*[*/]?\\*/\\|//.*$"
"Regular expression matching java comments.")
(defconst jdee-parse-java-comment-or-ws-re
(concat jdee-parse-java-comment-re "\\|[ \t\n]")
"Regular expression matching java comments and whitespaces.")
(defun jdee-parse-get-class-modifiers ()
"Get modifiers of the innermost class containing point.
Returns a list constisting of the result of
`jdee-parse-get-innermost-class-at-point' followed by
all modifieres of the class.
Returns nil, if no class could be found."
(let ((class (jdee-parse-get-innermost-class-at-point))
(mod-or-ws-re (concat "\\(" jdee-parse-class-mod-re
"\\|" jdee-parse-java-comment-or-ws-re
"\\)\\="))
(case-fold-search)
(modifiers))
(if class
(save-excursion
(goto-char (cdr class))
(while (re-search-backward mod-or-ws-re (point-min) t)
(progn
(if (looking-at jdee-parse-class-mod-re)
(setq modifiers
(cons (match-string-no-properties 0) modifiers)))))
(setq modifiers (cons class modifiers))))
modifiers))
(defconst jdee-parse-class-decl-re
(concat "^"
;; comments, string literals, keywords, identifier,
;; assignment operator or open parenthese:
"\\(?:" jdee-parse-java-comment-or-ws-re "\\|[a-zA-Z0-9_.=(]\\)*"
;; keyword before classname:
"\\<\\(class\\|enum\\|interface\\|new\\)"
"\\(?:" jdee-parse-java-comment-or-ws-re "\\)+"
;; package part of superclass of anonymous class:
"\\(?:[a-zA-Z0-9_]+\\.\\)*"
;; classname:
"\\([a-zA-Z0-9_]+\\)"
;; everything between classname and curly brace:
"\\(?:" jdee-parse-java-comment-or-ws-re "\\|[a-zA-Z0-9_.,()<>]\\)*"
"\\=")
"Regular expression matching class declarations before point.
Point must be at opening curly brace of class.
It matches interfaces, named and anonymous classes.")
(defun jdee-parse-get-innermost-class-at-point ()
"Get the innermost class containing point.
If point is in a class, this function returns
\(CLASS_NAME . CLASS_POSITION). CLASS_NAME is the
name of the class. For anonymous classes it is
the unqualified name of the superclass. CLASS_POSITION
is the position of the first character of the class
or interface keyword or the first character
of the new keyword in case of anonymous classes.
Returns nil, if point is not in a class."
(semantic-refresh-tags-safe)
(let ((left-paren-pos (c-parse-state)))
(if left-paren-pos
(save-excursion
(catch 'class-found
(let ((left-paren-index 0)
(left-paren-count (length left-paren-pos)))
(while (< left-paren-index left-paren-count)
(let ((paren-pos (nth left-paren-index left-paren-pos)))
(unless (consp paren-pos)
(goto-char paren-pos)
(when (looking-at "{")
(let* ((search-end-pos
(if (< left-paren-index (1- left-paren-count))
(let ((pos (nth (1+ left-paren-index) left-paren-pos)))
(if (consp pos)
(cdr pos)
pos))
(point-min)))
(case-fold-search nil)
(class-pos (re-search-backward jdee-parse-class-decl-re search-end-pos t)))
(if class-pos
(throw
'class-found
(cons (match-string-no-properties 2)
(match-beginning 1))))))))
(setq left-paren-index (1+ left-paren-index)))))))))
(defun jdee-parse-get-class-at-point ()
(let ((class-info (jdee-parse-get-innermost-class-at-point))
class-name)
(while class-info
(let ((name (car class-info))
(pos (cdr class-info)))
(if (not class-name)
(setq class-name name)
(setq class-name (concat name "." class-name)))
(save-excursion
(goto-char pos)
(setq class-info (jdee-parse-get-innermost-class-at-point)))))
class-name))
(defun jdee-parse-get-classes-at-point ()
(interactive)
(let ((class (jdee-parse-get-innermost-class-at-point)))
(if class (message "%s %s" (car class) (cdr class) ) (message "no class")))
;; (goto-char (aref (c-search-uplist-for-classkey (c-parse-state)) 0))
)
;;TODO: check if it duplicates something in this file
(defun jdee-parse-get-class ()
"Lookups and return fully qualified class name, e.g. A$B if point
is in inner class B of A."
(interactive)
(let ((class-info (jdee-parse-get-innermost-class-at-point)))
(if class-info
(save-excursion
(goto-char (cdr class-info))
(let ((parent (jdee-parse-get-class)))
(if (not parent)
(car class-info)
(concat parent "$" (car class-info))))))))
(defun jdee-parse-select-qualified-class-name (class &optional prompt)
"PROMPT the user to select the fully qualified name for CLASS.
Return the selection."
(condition-case err
(let ((names (jdee-backend-get-qualified-name class)))
(if names
(if (> (length names) 1)
(efc-query-options
names
(or prompt "Select class.")
"Class Name Dialog")
(car names))
(error "Cannot find class %s on the current classpath." class)))
(error
(message "%s" (error-message-string err)))))
(defun jdee-parse-qualified-name-at-point ()
"Returns (cons QUALIFIER NAME) where NAME is the symbol at point and
QUALIFIER is the symbol's qualifier. For example, suppose the name at
point is
int i = error.msg.length()
^
In this case, this function returns (cons \"error.msg\" \"length\").
This function works only for qualified names that do not contain
white space. It returns null if there is no qualified name at point."
(let ((symbol-at-point (thing-at-point 'symbol)))
(when symbol-at-point
(thing-at-point-looking-at "[^ \n\t();,:+<]+") ;; add < to prevent like "Map<String"
(let ((qualified-name
(buffer-substring-no-properties
(match-beginning 0)
(match-end 0))))
(string-match "\\(.+[.]\\)*\\([^.]+\\)" qualified-name)
(let ((qualifier (if (match-beginning 1)
(substring qualified-name
(match-beginning 1) (match-end 1))))
(name (substring qualified-name
(match-beginning 2) (match-end 2))))
(if qualifier
(setq qualifier (substring qualifier 0 (1- (length qualifier)))))
(cons qualifier name))))))
;;;###autoload
(defun jdee-parse-get-buffer-class (&optional no-package-p)
"Get the fully qualified name of the class of this buffer.
NO-PACKAGE-P, if non-`nil', return only the class name (sans
package name), otherwise, include the package name.
If called interactively, add the name in the mini-buffer."
(interactive (list (not current-prefix-arg)))
(if (eq major-mode 'jdee-mode)
(let ((package-name (jdee-parse-get-package-name))
(class-name (file-name-sans-extension
(file-name-nondirectory (buffer-file-name)))))
(if (and (not no-package-p) package-name)
(setq class-name (concat package-name "." class-name)))
(when (called-interactively-p 'interactive)
(kill-new class-name)
(message (format "Copied `%s'" class-name)))
class-name)
(error "Not a Java source buffer.")))
(defun jdee-parse-get-buffer-unqualified-class ()
"Get the class name from the buffer filename"
(file-name-sans-extension (file-name-nondirectory
(or (buffer-file-name) "Object.java"))))
;;;TODO: is the same as jdee-parse-get-buffer-class?
(defun jdee-parse-fqn ()
"Return the fully qualified class name at point.
If not in a class, use the buffer name."
(interactive)
(let* ((pkg (jdee-parse-get-package))
(class (or (jdee-parse-get-class)
(caar (semantic-find-tags-by-type "class" (current-buffer)))))
(rtnval (if pkg
(format "%s%s" pkg class)
class)))
rtnval))
;;;TODO: move to UI package
(defun jdee-parse-fqn-to-kill-ring ()
"Copy the qualified class name of class containing point to the kill ring.
Return the fully qualified name."
(interactive)
(let* ((fqn (jdee-parse-fqn)))
(kill-new fqn)
(when (called-interactively-p 'any)
(message "%s added to kill ring" fqn))
fqn))
(defun jdee-parse-double-backslashes (name)
(mapconcat (lambda (x) (if (eq x ?\\)
"\\\\"
(string x)))
name ""))
(defvar jdee-parse-java-symbol-declare-re
(rx
(1+ ;; A Java symbol comprises one or more of the following:
(char (?A . ?Z) ;; - upper case characters
(?a . ?z) ;; - lower case characters
(?0 . ?9) ;; - digits
"[]" ;; - square brackets
"?" ;; - question mark
"<,> \t\n\r";; - java1.5 generic support
"_" ;; - underscore
"." ;; - period
(160 . 255) ;; - accented characters
)))
"Regular expression that matches any Java symbol declare.")
(defun jdee-parse-valid-declaration-at (point varname)
"Verify that a POINT starts a valid java declaration
for the VARNAME variable."
(save-excursion
(goto-char point)
(let ((case-fold-search nil)) ;; Why case-insensitive?
(if (or
(looking-at (concat "\\(" jdee-parse-java-symbol-declare-re "\\)[ \t\n\r]+"
(jdee-parse-double-backslashes varname)
"[]?[ \t\n\r]*[),;=]"))
;; Handle case where varname is part of a list declaration, e.g.,
;;
;; String a, b, c;
;;
(looking-at (concat "\\(" jdee-parse-java-symbol-declare-re "\\)[ \t\n\r]+"
"\\(" jdee-parse-java-symbol-re "[ \t\n\r]*,[ \t\n\r]*\\)*"
(jdee-parse-double-backslashes varname)
"[]?[ \t\n\r]*[,;]"))
;; Parse jdk1.5 for (Type val : collection) {
(looking-at (concat "\\(" jdee-parse-java-symbol-declare-re "\\)[ \t\n\r]+"
varname "[ \t\n\r]*:"))) ;[ \t\n\r]*"
;"\\(" jdee-parse-java-symbol-re "[,{} \t\n\r]*\\)+" ")")))
(let ((type (match-string 1))
(type-pos (match-beginning 1)))
(goto-char type-pos)
;; Check for following case.
;; Object table
;; //representing objects after all updates.
;; table = new Truc();
;; table.
;; Avoid false hit on updates.
(if (not (or
(jdee-parse-comment-or-quoted-p)
(string= type "instanceof")
(string= type "return")))
type))
nil))))
(defun jdee-parse-declared-type-of (name)
"Find in the current buffer the java type of the variable NAME. The
function returns a cons of two strings. The first is a string containing
the name of the type, or nil if it cannot be found. The second is a string
containing any qualifying text that precedes the class name, which is nil
if no text that looks like an identifier precedes the type name. This
function does not give the fully-qualified java class name, it just returns
the type as it is declared, and a qualifier that might be the package or
the containing/outer class of the declared type."
(save-excursion
(let (found res foundpt qualifier)
(setq foundpt (jdee-parse-find-declaration-of name))
(setq found (not (null foundpt)))
;; now check for qualifying identifier. Note: reuses
;; jdee-parse-qualified-name-at-point for simplicity, not efficiency
(if found
(let (qualname)
(goto-char foundpt)
(setq qualname (jdee-parse-qualified-name-at-point))
(setq qualifier (car qualname))
(setq res (cdr qualname))))
(cons res qualifier))))
(defun jdee-parse-find-declaration-of (name)
"Find in the current buffer the location where NAME is declared.
Returns the character position in the buffer, or nil if no declaration
could be found."
(save-excursion
(let ((symbol-list-entry-re
(concat jdee-parse-java-symbol-re "[ \t\n\r]*,[ \t\n\r]*"))
(orgpt (point))
(case-fold-search nil)
found pos resname foundpt lastpos)
;; Search backward in the buffer.
(while (and (not found)
(search-backward name nil t))
(setq pos (point))
(setq lastpos (point))
;; Position point at the start of the type
;; symbol in the declaration, e.g.,
;;
;; String s;
;; ^
(backward-word 1)
;; Handle case where declaration declares
;; a list of symbols, e.g.,
;;
;; String a, b, c;
;;
;; In this case, back over any entries in
;; the list ahead of name.
(while (looking-at symbol-list-entry-re)
(setq lastpos (point))
(backward-word 1))
;; List<String, List<String>> a;
;; ^
;; In this case, back over any entries between < and >
(let ((try-count 0)
(max-try-count 20))
(while (and
(< try-count max-try-count)
(not
(= (cl-count ?< (buffer-substring (point) lastpos))
(cl-count ?> (buffer-substring (point) lastpos)))))
(setq try-count (1+ try-count))
(backward-word 1)))
(setq resname (jdee-parse-valid-declaration-at (point) name))
(setq foundpt (point))
(goto-char pos)
(forward-char -1)
(if (and resname
(not (jdee-parse-keywordp resname)))
(setq found t)))
(unless found
;; Not found backward in buffer. Try looking forward.
(goto-char orgpt)
(while (and (not found)
(search-forward name nil t))
(setq pos (point))
(setq lastpos (point))
(backward-word 2)
(while (looking-at symbol-list-entry-re)
(setq lastpos (point))
(backward-word 1))
;; List<String, List<String>> a;
;; ^
;; In this case, back over any entries between < and >
(let ((try-count 0)
(max-try-count 20))
(while (and
(< try-count max-try-count)
(not
(= (cl-count ?< (buffer-substring (point) lastpos))
(cl-count ?> (buffer-substring (point) lastpos)))))
(setq try-count (1+ try-count))
(backward-word 1)))
(setq resname (jdee-parse-valid-declaration-at (point) name))
(setq foundpt (point))
(goto-char pos)
(forward-char 1)
(if (and resname
(not (jdee-parse-keywordp resname)))
(setq found t))))
(if found foundpt nil))))
(defun jdee-display-parse-error (error)
(let* ((parser-buffer-name "*Java Parser*")
(buf (get-buffer parser-buffer-name)))
(if (not buf)
(setq buf (get-buffer-create parser-buffer-name)))
(set-buffer buf)
(erase-buffer)
(insert error)
(pop-to-buffer buf)))
(defun jdee-parse ()
"*Parses the Java source file displayed in the current buffer.
If the source file parses successfully, this command displays
a success message in the minibuffer. Otherwise, it displays an error
message in the Java Parser buffer. If the Java Parser buffer does
not exist, this command creates it.
Note. This command uses an external Java parser implemented in
Java to parse Java source files. This command uses the JDE's integrated
Java source interpreter, the BeanShell, to invoke the parser. If the
BeanShell is not running, this command starts the BeanShell. Thus,
the first time you invoke the parser you may notice a slight delay
before getting a response. Thereafter, the response should be very
fast."
(interactive)
(save-some-buffers (not compilation-ask-about-save) nil)
(let ((parse-error
(jdee-backend-parse-file (buffer-file-name))))
(if parse-error
(jdee-display-parse-error parse-error)
(message "Parsed %s successfully" (buffer-name)))))
(defun jdee-parse-comment-or-quoted-p ()
"Returns t if point is in a comment or a quoted string,
otherwise nil."
(let* ((state (save-excursion
(parse-partial-sexp (point-min) (point)))))
(if (or (nth 3 state)
(nth 4 state))
t)))
(defun jdee-parse--search-class (class pos)
;; Define an internal function that recursively searches a class
;; and its subclasses for a method containing point.
(let* ((class-name (semantic-tag-name class))
(class-parts (semantic-tag-type-members class))
(class-subclasses (semantic-brute-find-tag-by-class 'type class-parts))
(class-methods (semantic-brute-find-tag-by-class 'function class-parts)))
;; Is point in a method of a subclass of this class?
(loop for subclass in class-subclasses do
(jdee-parse--search-class subclass pos))
;; Is point in any of the methods of this class?
(loop for method in class-methods do
(let* ((method-name (semantic-tag-name method))
(method-start (semantic-tag-start method))
(method-end (semantic-tag-end method)))
(when (and (>= pos method-start)
(<= pos method-end))
(throw 'found (cons (cons class-name method-name)
(cons method-start method-end))))))))
(defun jdee-parse-get-method-at-point (&optional position)
"Gets the method at POSITION, if specified, otherwise at point.
Returns (CLASS_NAME . METHOD_NAME) if the specified position is
in a method; otherwise, nil."
(let* ((pos (if position position (point)))
(tokens (semantic-fetch-tags))
(classes (semantic-brute-find-tag-by-class 'type tokens)))
(catch 'found
(loop for class in classes
do (jdee-parse--search-class class pos)))))
(defclass jdee-parse-method-map (jdee-avl-tree)
()
"Map of the methods in the current buffer.")
(defun jdee-parse-method-map-compare-fcn (m1 m2)
(and
(< (car (cdr m1)) (car (cdr m2)))
(< (cdr (cdr m1)) (car (cdr m2)))))
(defun jdee-parse--add-methods (method-map class)
(let* ((class-name (semantic-tag-name class))
(class-parts (semantic-tag-type-members class))
(class-subclasses (semantic-brute-find-tag-by-class 'type class-parts))
(class-methods (semantic-brute-find-tag-by-class 'function class-parts)))
;; Add methods of subclasses
(loop for subclass in class-subclasses do
(jdee-parse--add-methods method-map subclass))