-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsemantic-complete.el
2125 lines (1952 loc) · 76.3 KB
/
semantic-complete.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
;;; semantic-complete.el --- Routines for performing tag completion
;;; Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009 Eric M. Ludlam
;; Author: Eric M. Ludlam <[email protected]>
;; Keywords: syntax
;; X-RCS: $Id: semantic-complete.el,v 1.59 2009/02/14 18:05:10 zappo Exp $
;; This file is not part of GNU Emacs.
;; Semantic 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 software 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., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Completion of tags by name using tables of semantic generated tags.
;;
;; While it would be a simple matter of flattening all tag known
;; tables to perform completion across them using `all-completions',
;; or `try-completion', that process would be slow. In particular,
;; when a system database is included in the mix, the potential for a
;; ludicrous number of options becomes apparent.
;;
;; As such, dynamically searching across tables using a prefix,
;; regular expression, or other feature is needed to help find symbols
;; quickly without resorting to "show me every possible option now".
;;
;; In addition, some symbol names will appear in multiple locations.
;; If it is important to distiguish, then a way to provide a choice
;; over these locations is important as well.
;;
;; Beyond brute force offers for completion of plain strings,
;; using the smarts of semantic-analyze to provide reduced lists of
;; symbols, or fancy tabbing to zoom into files to show multiple hits
;; of the same name can be provided.
;;
;;; How it works:
;;
;; There are several parts of any completion engine. They are:
;;
;; A. Collection of possible hits
;; B. Typing or selecting an option
;; C. Displaying possible unique completions
;; D. Using the result
;;
;; Here, we will treat each section separately (excluding D)
;; They can then be strung together in user-visible commands to
;; fullfill specific needs.
;;
;; COLLECTORS:
;;
;; A collector is an object which represents the means by which tags
;; to complete on are collected. It's first job is to find all the
;; tags which are to be completed against. It can also rename
;; some tags if needed so long as `semantic-tag-clone' is used.
;;
;; Some collectors will gather all tags to complete against first
;; (for in buffer queries, or other small list situations). It may
;; choose to do a broad search on each completion request. Built in
;; functionality automatically focuses the cache in as the user types.
;;
;; A collector choosing to create and rename tags could choose a
;; plain name format, a postfix name such as method:class, or a
;; prefix name such as class.method.
;;
;; DISPLAYORS
;;
;; A displayor is in charge if showing the user interesting things
;; about available completions, and can optionally provide a focus.
;; The simplest display just lists all available names in a separate
;; window. It may even choose to show short names when there are
;; many to choose from, or long names when there are fewer.
;;
;; A complex displayor could opt to help the user 'focus' on some
;; range. For example, if 4 tags all have the same name, subsequent
;; calls to the displayor may opt to show each tag one at a time in
;; the buffer. When the user likes one, selection would cause the
;; 'focus' item to be selected.
;;
;; CACHE FORMAT
;;
;; The format of the tag lists used to perform the completions are in
;; semanticdb "find" format, like this:
;;
;; ( ( DBTABLE1 TAG1 TAG2 ...)
;; ( DBTABLE2 TAG1 TAG2 ...)
;; ... )
;;
;; INLINE vs MINIBUFFER
;;
;; Two major ways completion is used in Emacs is either through a
;; minibuffer query, or via completion in a normal editing buffer,
;; encompassing some small range of characters.
;;
;; Structure for both types of completion are provided here.
;; `semantic-complete-read-tag-engine' will use the minibuffer.
;; `semantic-complete-inline-tag-engine' will complete text in
;; a buffer.
(require 'eieio)
(require 'semantic-tag)
(require 'semantic-find)
(require 'semantic-analyze)
(require 'semantic-format)
(require 'semantic-ctxt)
;; Keep semanticdb optional.
(eval-when-compile
(require 'semanticdb)
(require 'semanticdb-find))
(eval-when-compile
(condition-case nil
;; Tooltip not available in older emacsen.
(require 'tooltip)
(error nil))
)
;;; Code:
;;; Compatibility
;;
(if (fboundp 'minibuffer-contents)
(eval-and-compile (defalias 'semantic-minibuffer-contents 'minibuffer-contents))
(eval-and-compile (defalias 'semantic-minibuffer-contents 'buffer-string)))
(if (fboundp 'delete-minibuffer-contents)
(eval-and-compile (defalias 'semantic-delete-minibuffer-contents 'delete-minibuffer-contents))
(eval-and-compile (defalias 'semantic-delete-minibuffer-contents 'erase-buffer)))
(defvar semantic-complete-inline-overlay nil
"The overlay currently active while completing inline.")
;;;###autoload
(defun semantic-completion-inline-active-p ()
"Non-nil if inline completion is active."
(when (and semantic-complete-inline-overlay
(not (semantic-overlay-live-p semantic-complete-inline-overlay)))
(semantic-overlay-delete semantic-complete-inline-overlay)
(setq semantic-complete-inline-overlay nil))
semantic-complete-inline-overlay)
;;; ------------------------------------------------------------
;;; MINIBUFFER or INLINE utils
;;
(defun semantic-completion-text ()
"Return the text that is currently in the completion buffer.
For a minibuffer prompt, this is the minibuffer text.
For inline completion, this is the text wrapped in the inline completion
overlay."
(if semantic-complete-inline-overlay
(semantic-complete-inline-text)
(semantic-minibuffer-contents)))
(defun semantic-completion-delete-text ()
"Delete the text that is actively being completed.
Presumably if you call this you will insert something new there."
(if semantic-complete-inline-overlay
(semantic-complete-inline-delete-text)
(semantic-delete-minibuffer-contents)))
(defun semantic-completion-message (fmt &rest args)
"Display the string FMT formatted with ARGS at the end of the minibuffer."
(if semantic-complete-inline-overlay
(apply 'message fmt args)
(message (concat (buffer-string) (apply 'format fmt args)))))
;;; ------------------------------------------------------------
;;; MINIBUFFER: Option Selection harnesses
;;
(defvar semantic-completion-collector-engine nil
"The tag collector for the current completion operation.
Value should be an object of a subclass of
`semantic-completion-engine-abstract'.")
(defvar semantic-completion-display-engine nil
"The tag display engine for the current completion operation.
Value should be a ... what?")
(defvar semantic-complete-key-map
(let ((km (make-sparse-keymap)))
(define-key km " " 'semantic-complete-complete-space)
(define-key km "\t" 'semantic-complete-complete-tab)
(define-key km "\C-m" 'semantic-complete-done)
(define-key km "\C-g" 'abort-recursive-edit)
(define-key km "\M-n" 'next-history-element)
(define-key km "\M-p" 'previous-history-element)
(define-key km "\C-n" 'next-history-element)
(define-key km "\C-p" 'previous-history-element)
;; Add history navigation
km)
"Keymap used while completing across a list of tags.")
(defvar semantic-completion-default-history nil
"Default history variable for any unhistoried prompt.
Keeps STRINGS only in the history.")
;;;###autoload
(defun semantic-complete-read-tag-engine (collector displayor prompt
default-tag initial-input
history)
"Read a semantic tag, and return a tag for the selection.
Argument COLLECTOR is an object which can be used to to calculate
a list of possible hits. See `semantic-completion-collector-engine'
for details on COLLECTOR.
Argumeng DISPLAYOR is an object used to display a list of possible
completions for a given prefix. See`semantic-completion-display-engine'
for details on DISPLAYOR.
PROMPT is a string to prompt with.
DEFAULT-TAG is a semantic tag or string to use as the default value.
If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
HISTORY is a symbol representing a variable to story the history in."
(let* ((semantic-completion-collector-engine collector)
(semantic-completion-display-engine displayor)
(semantic-complete-active-default nil)
(semantic-complete-current-matched-tag nil)
(default-as-tag (semantic-complete-default-to-tag default-tag))
(default-as-string (when (semantic-tag-p default-as-tag)
(semantic-tag-name default-as-tag)))
)
(when default-as-string
;; Add this to the prompt.
;;
;; I really want to add a lookup of the symbol in those
;; tags available to the collector and only add it if it
;; is available as a possibility, but I'm too lazy right
;; now.
;;
;; @todo - move from () to into the editable area
(if (string-match ":" prompt)
(setq prompt (concat
(substring prompt 0 (match-beginning 0))
" (" default-as-string ")"
(substring prompt (match-beginning 0))))
(setq prompt (concat prompt " (" default-as-string "): "))))
;;
;; Perform the Completion
;;
(unwind-protect
(read-from-minibuffer prompt
initial-input
semantic-complete-key-map
nil
(or history
'semantic-completion-default-history)
default-tag)
(semantic-collector-cleanup semantic-completion-collector-engine)
(semantic-displayor-cleanup semantic-completion-display-engine)
)
;;
;; Extract the tag from the completion machinery.
;;
semantic-complete-current-matched-tag
))
;;; Util for basic completion prompts
;;
(defvar semantic-complete-active-default nil
"The current default tag calculated for this prompt.")
(defun semantic-complete-default-to-tag (default)
"Convert a calculated or passed in DEFAULT into a tag."
(if (semantic-tag-p default)
;; Just return what was passed in.
(setq semantic-complete-active-default default)
;; If none was passed in, guess.
(if (null default)
(setq default (semantic-ctxt-current-thing)))
(if (null default)
;; Do nothing
nil
;; Turn default into something useful.
(let ((str
(cond
;; Semantic-ctxt-current-symbol will return a list of
;; strings. Technically, we should use the analyzer to
;; fully extract what we need, but for now, just grab the
;; first string
((and (listp default) (stringp (car default)))
(car default))
((stringp default)
default)
((symbolp default)
(symbol-name default))
(t
(signal 'wrong-type-argument
(list default 'semantic-tag-p)))))
(tag nil))
;; Now that we have that symbol string, look it up using the active
;; collector. If we get a match, use it.
(save-excursion
(semantic-collector-calculate-completions
semantic-completion-collector-engine
str nil))
;; Do we have the perfect match???
(let ((ml (semantic-collector-current-exact-match
semantic-completion-collector-engine)))
(when ml
;; We don't care about uniqueness. Just guess for convenience
(setq tag (semanticdb-find-result-nth-in-buffer ml 0))))
;; save it
(setq semantic-complete-active-default tag)
;; Return it.. .whatever it may be
tag))))
;;; Prompt Return Value
;;
;; Getting a return value out of this completion prompt is a bit
;; challenging. The read command returns the string typed in.
;; We need to convert this into a valid tag. We can exit the minibuffer
;; for different reasons. If we purposely exit, we must make sure
;; the focused tag is calculated... preferably once.
(defvar semantic-complete-current-matched-tag nil
"Variable used to pass the tags being matched to the prompt.")
(defun semantic-complete-current-match ()
"Calculate a match from the current completion environment.
Save this in our completion variable. Make sure that variable
is cleared if any other keypress is made.
Return value can be:
tag - a single tag that has been matched.
string - a message to show in the minibuffer."
;; Query the environment for an active completion.
(let ((collector semantic-completion-collector-engine)
(displayor semantic-completion-display-engine)
(contents (semantic-completion-text))
matchlist
answer)
(if (string= contents "")
;; The user wants the defaults!
(setq answer semantic-complete-active-default)
;; This forces a full calculation of completion on CR.
(save-excursion
(semantic-collector-calculate-completions collector contents nil))
(semantic-complete-try-completion)
(cond
;; Input match displayor focus entry
((setq answer (semantic-displayor-current-focus displayor))
;; We have answer, continue
)
;; One match from the collector
((setq matchlist (semantic-collector-current-exact-match collector))
(if (= (semanticdb-find-result-length matchlist) 1)
(setq answer (semanticdb-find-result-nth-in-buffer matchlist 0))
(if (semantic-displayor-focus-abstract-child-p displayor)
;; For focusing displayors, we can claim this is
;; not unique. Multiple focuses can choose the correct
;; one.
(setq answer "Not Unique")
;; If we don't have a focusing displayor, we need to do something
;; graceful. First, see if all the matches have the same name.
(let ((allsame t)
(firstname (semantic-tag-name
(car
(semanticdb-find-result-nth matchlist 0)))
)
(cnt 1)
(max (semanticdb-find-result-length matchlist)))
(while (and allsame (< cnt max))
(if (not (string=
firstname
(semantic-tag-name
(car
(semanticdb-find-result-nth matchlist cnt)))))
(setq allsame nil))
(setq cnt (1+ cnt))
)
;; Now we know if they are all the same. If they are, just
;; accept the first, otherwise complain.
(if allsame
(setq answer (semanticdb-find-result-nth-in-buffer
matchlist 0))
(setq answer "Not Unique"))
))))
;; No match
(t
(setq answer "No Match")))
)
;; Set it into our completion target.
(when (semantic-tag-p answer)
(setq semantic-complete-current-matched-tag answer)
;; Make sure it is up to date by clearing it if the user dares
;; to touch the keyboard.
(add-hook 'pre-command-hook
(lambda () (setq semantic-complete-current-matched-tag nil)))
)
;; Return it
answer
))
;;; Keybindings
;;
;; Keys are bound to to perform completion using our mechanisms.
;; Do that work here.
(defun semantic-complete-done ()
"Accept the current input."
(interactive)
(let ((ans (semantic-complete-current-match)))
(if (stringp ans)
(semantic-completion-message (concat " [" ans "]"))
(exit-minibuffer)))
)
(defun semantic-complete-complete-space ()
"Complete the partial input in the minibuffer."
(interactive)
(semantic-complete-do-completion t))
(defun semantic-complete-complete-tab ()
"Complete the partial input in the minibuffer as far as possible."
(interactive)
(semantic-complete-do-completion))
;;; Completion Functions
;;
;; Thees routines are functional entry points to performing completion.
;;
(defun semantic-complete-hack-word-boundaries (original new)
"Return a string to use for completion.
ORIGINAL is the text in the minibuffer.
NEW is the new text to insert into the minibuffer.
Within the difference bounds of ORIGINAL and NEW, shorten NEW
to the nearest word boundary, and return that."
(save-match-data
(let* ((diff (substring new (length original)))
(end (string-match "\\>" diff))
(start (string-match "\\<" diff)))
(cond
((and start (> start 0))
;; If start is greater than 0, include only the new
;; white-space stuff
(concat original (substring diff 0 start)))
(end
(concat original (substring diff 0 end)))
(t new)))))
(defun semantic-complete-try-completion (&optional partial)
"Try a completion for the current minibuffer.
If PARTIAL, do partial completion stopping at spaces."
(let ((comp (semantic-collector-try-completion
semantic-completion-collector-engine
(semantic-completion-text))))
(cond
((null comp)
(semantic-completion-message " [No Match]")
(ding)
)
((stringp comp)
(if (string= (semantic-completion-text) comp)
(when partial
;; Minibuffer isn't changing AND the text is not unique.
;; Test for partial completion over a word separator character.
;; If there is one available, use that so that SPC can
;; act like a SPC insert key.
(let ((newcomp (semantic-collector-current-whitespace-completion
semantic-completion-collector-engine)))
(when newcomp
(semantic-completion-delete-text)
(insert newcomp))
))
(when partial
(let ((orig (semantic-completion-text)))
;; For partial completion, we stop and step over
;; word boundaries. Use this nifty function to do
;; that calculation for us.
(setq comp
(semantic-complete-hack-word-boundaries orig comp))))
;; Do the replacement.
(semantic-completion-delete-text)
(insert comp))
)
((and (listp comp) (semantic-tag-p (car comp)))
(unless (string= (semantic-completion-text)
(semantic-tag-name (car comp)))
;; A fully unique completion was available.
(semantic-completion-delete-text)
(insert (semantic-tag-name (car comp))))
;; The match is complete
(if (= (length comp) 1)
(semantic-completion-message " [Complete]")
(semantic-completion-message " [Complete, but not unique]"))
)
(t nil))))
(defun semantic-complete-do-completion (&optional partial inline)
"Do a completion for the current minibuffer.
If PARTIAL, do partial completion stopping at spaces.
if INLINE, then completion is happening inline in a buffer."
(let* ((collector semantic-completion-collector-engine)
(displayor semantic-completion-display-engine)
(contents (semantic-completion-text))
(ans nil))
(save-excursion
(semantic-collector-calculate-completions collector contents partial))
(let* ((na (semantic-complete-next-action partial)))
(cond
;; We're all done, but only from a very specific
;; area of completion.
((eq na 'done)
(semantic-completion-message " [Complete]")
(setq ans 'done))
;; Perform completion
((or (eq na 'complete)
(eq na 'complete-whitespace))
(semantic-complete-try-completion partial)
(setq ans 'complete))
;; We need to display the completions.
;; Set the completions into the display engine
((or (eq na 'display) (eq na 'displayend))
(semantic-displayor-set-completions
displayor
(or
(and (not (eq na 'displayend))
(semantic-collector-current-exact-match collector))
(semantic-collector-all-completions collector contents))
contents)
;; Ask the displayor to display them.
(semantic-displayor-show-request displayor))
((eq na 'scroll)
(semantic-displayor-scroll-request displayor)
)
((eq na 'focus)
(semantic-displayor-focus-next displayor)
(semantic-displayor-focus-request displayor)
)
((eq na 'empty)
(semantic-completion-message " [No Match]"))
(t nil)))
ans))
;;; ------------------------------------------------------------
;;; INLINE: tag completion harness
;;
;; Unlike the minibuffer, there is no mode nor other traditional
;; means of reading user commands in completion mode. Instead
;; we use a pre-command-hook to inset in our commands, and to
;; push ourselves out of this mode on alternate keypresses.
(defvar semantic-complete-inline-map
(let ((km (make-sparse-keymap)))
(define-key km "\C-i" 'semantic-complete-inline-TAB)
(define-key km "\M-p" 'semantic-complete-inline-up)
(define-key km "\M-n" 'semantic-complete-inline-down)
(define-key km "\C-m" 'semantic-complete-inline-done)
(define-key km "\C-\M-c" 'semantic-complete-inline-exit)
(define-key km "\C-g" 'semantic-complete-inline-quit)
(define-key km "?"
(lambda () (interactive)
(describe-variable 'semantic-complete-inline-map)))
km)
"Keymap used while performing Semantic inline completion.
\\{semantic-complete-inline-map}")
(defface semantic-complete-inline-face
'((((class color) (background dark))
(:underline "yellow"))
(((class color) (background light))
(:underline "brown")))
"*Face used to show the region being completed inline.
The face is used in `semantic-complete-inline-tag-engine'."
:group 'semantic-faces)
(defun semantic-complete-inline-text ()
"Return the text that is being completed inline.
Similar to `minibuffer-contents' when completing in the minibuffer."
(let ((s (semantic-overlay-start semantic-complete-inline-overlay))
(e (semantic-overlay-end semantic-complete-inline-overlay)))
(if (= s e)
""
(buffer-substring-no-properties s e ))))
(defun semantic-complete-inline-delete-text ()
"Delete the text currently being completed in the current buffer."
(delete-region
(semantic-overlay-start semantic-complete-inline-overlay)
(semantic-overlay-end semantic-complete-inline-overlay)))
(defun semantic-complete-inline-done ()
"This completion thing is DONE, OR, insert a newline."
(interactive)
(let* ((displayor semantic-completion-display-engine)
(tag (semantic-displayor-current-focus displayor)))
(if tag
(let ((txt (semantic-completion-text)))
(insert (substring (semantic-tag-name tag)
(length txt)))
(semantic-complete-inline-exit))
;; Get whatever binding RET usually has.
(let ((fcn
(condition-case nil
(lookup-key (current-active-maps) (this-command-keys))
(error
;; I don't know why, but for some reason the above
;; throws an error sometimes.
(lookup-key (current-global-map) (this-command-keys))
))))
(when fcn
(funcall fcn)))
)))
(defun semantic-complete-inline-quit ()
"Quit an inline edit."
(interactive)
(semantic-complete-inline-exit)
(keyboard-quit))
(defun semantic-complete-inline-exit ()
"Exit inline completion mode."
(interactive)
;; Remove this hook FIRST!
(remove-hook 'pre-command-hook 'semantic-complete-pre-command-hook)
(condition-case nil
(progn
(when semantic-completion-collector-engine
(semantic-collector-cleanup semantic-completion-collector-engine))
(when semantic-completion-display-engine
(semantic-displayor-cleanup semantic-completion-display-engine))
(when semantic-complete-inline-overlay
(let ((wc (semantic-overlay-get semantic-complete-inline-overlay
'window-config-start))
(buf (semantic-overlay-buffer semantic-complete-inline-overlay))
)
(semantic-overlay-delete semantic-complete-inline-overlay)
(setq semantic-complete-inline-overlay nil)
;; DONT restore the window configuration if we just
;; switched windows!
(when (eq buf (current-buffer))
(set-window-configuration wc))
))
(setq semantic-completion-collector-engine nil
semantic-completion-display-engine nil))
(error nil))
;; Remove this hook LAST!!!
;; This will force us back through this function if there was
;; some sort of error above.
(remove-hook 'post-command-hook 'semantic-complete-post-command-hook)
;;(message "Exiting inline completion.")
)
(defun semantic-complete-pre-command-hook ()
"Used to redefine what commands are being run while completing.
When installed as a `pre-command-hook' the special keymap
`semantic-complete-inline-map' is queried to replace commands normally run.
Commands which edit what is in the region of interest operate normally.
Commands which would take us out of the region of interest, or our
quit hook, will exit this completion mode."
(let ((fcn (lookup-key semantic-complete-inline-map
(this-command-keys) nil)))
(cond ((commandp fcn)
(setq this-command fcn))
(t nil)))
)
(defun semantic-complete-post-command-hook ()
"Used to determine if we need to exit inline completion mode.
If completion mode is active, check to see if we are within
the bounds of `semantic-complete-inline-overlay', or within
a reasonable distance."
(condition-case nil
;; Exit if something bad happened.
(if (not semantic-complete-inline-overlay)
(progn
;;(message "Inline Hook installed, but overlay deleted.")
(semantic-complete-inline-exit))
;; Exit if commands caused us to exit the area of interest
(let ((s (semantic-overlay-start semantic-complete-inline-overlay))
(e (semantic-overlay-end semantic-complete-inline-overlay))
(b (semantic-overlay-buffer semantic-complete-inline-overlay))
(txt nil)
)
(cond
;; EXIT when we are no longer in a good place.
((or (not (eq b (current-buffer)))
(< (point) s)
(> (point) e))
;;(message "Exit: %S %S %S" s e (point))
(semantic-complete-inline-exit)
)
;; Exit if the user typed in a character that is not part
;; of the symbol being completed.
((and (setq txt (semantic-completion-text))
(not (string= txt ""))
(and (/= (point) s)
(save-excursion
(forward-char -1)
(not (looking-at "\\(\\w\\|\\s_\\)")))))
;;(message "Non symbol character.")
(semantic-complete-inline-exit))
((lookup-key semantic-complete-inline-map
(this-command-keys) nil)
;; If the last command was one of our completion commands,
;; then do nothing.
nil
)
(t
;; Else, show completions now
(semantic-complete-inline-force-display)
))))
;; If something goes terribly wrong, clean up after ourselves.
(error (semantic-complete-inline-exit))))
;;;###autoload
(defun semantic-complete-inline-force-display ()
"Force the display of whatever the current completions are.
DO NOT CALL THIS IF THE INLINE COMPLETION ENGINE IS NOT ACTIVE."
(condition-case e
(save-excursion
(let ((collector semantic-completion-collector-engine)
(displayor semantic-completion-display-engine)
(contents (semantic-completion-text)))
(when collector
(semantic-collector-calculate-completions
collector contents nil)
(semantic-displayor-set-completions
displayor
(semantic-collector-all-completions collector contents)
contents)
;; Ask the displayor to display them.
(semantic-displayor-show-request displayor))
))
(error (message "Bug Showing Completions: %S" e))))
(defun semantic-complete-inline-tag-engine
(collector displayor buffer start end)
"Perform completion based on semantic tags in a buffer.
Argument COLLECTOR is an object which can be used to to calculate
a list of possible hits. See `semantic-completion-collector-engine'
for details on COLLECTOR.
Argumeng DISPLAYOR is an object used to display a list of possible
completions for a given prefix. See`semantic-completion-display-engine'
for details on DISPLAYOR.
BUFFER is the buffer in which completion will take place.
START is a location for the start of the full symbol.
If the symbol being completed is \"foo.ba\", then START
is on the \"f\" character.
END is at the end of the current symbol being completed."
;; Set us up for doing completion
(setq semantic-completion-collector-engine collector
semantic-completion-display-engine displayor)
;; Create an overlay
(setq semantic-complete-inline-overlay
(semantic-make-overlay start end buffer nil t))
(semantic-overlay-put semantic-complete-inline-overlay
'face
'semantic-complete-inline-face)
(semantic-overlay-put semantic-complete-inline-overlay
'window-config-start
(current-window-configuration))
;; Install our command hooks
(add-hook 'pre-command-hook 'semantic-complete-pre-command-hook)
(add-hook 'post-command-hook 'semantic-complete-post-command-hook)
;; Go!
(semantic-complete-inline-force-display)
)
;;; Inline Completion Keymap Functions
;;
(defun semantic-complete-inline-TAB ()
"Perform inline completion."
(interactive)
(let ((cmpl (semantic-complete-do-completion nil t)))
(cond
((eq cmpl 'complete)
(semantic-complete-inline-force-display))
((eq cmpl 'done)
(semantic-complete-inline-done))
))
)
(defun semantic-complete-inline-down()
"Focus forwards through the displayor."
(interactive)
(let ((displayor semantic-completion-display-engine))
(semantic-displayor-focus-next displayor)
(semantic-displayor-focus-request displayor)
))
(defun semantic-complete-inline-up ()
"Focus backwards through the displayor."
(interactive)
(let ((displayor semantic-completion-display-engine))
(semantic-displayor-focus-previous displayor)
(semantic-displayor-focus-request displayor)
))
;;; ------------------------------------------------------------
;;; Interactions between collection and displaying
;;
;; Functional routines used to help collectors communicate with
;; the current displayor, or for the previous section.
(defun semantic-complete-next-action (partial)
"Determine what the next completion action should be.
PARTIAL is non-nil if we are doing partial completion.
First, the collector can determine if we should perform a completion or not.
If there is nothing to complete, then the displayor determines if we are
to show a completion list, scroll, or perhaps do a focus (if it is capable.)
Expected return values are:
done -> We have a singular match
empty -> There are no matches to the current text
complete -> Perform a completion action
complete-whitespace -> Complete next whitespace type character.
display -> Show the list of completions
scroll -> The completions have been shown, and the user keeps hitting
the complete button. If possible, scroll the completions
focus -> The displayor knows how to shift focus among possible completions.
Let it do that.
displayend -> Whatever options the displayor had for repeating options, there
are none left. Try something new."
(let ((ans1 (semantic-collector-next-action
semantic-completion-collector-engine
partial))
(ans2 (semantic-displayor-next-action
semantic-completion-display-engine))
)
(cond
;; No collector answer, use displayor answer.
((not ans1)
ans2)
;; Displayor selection of 'scroll, 'display, or 'focus trumps
;; 'done
((and (eq ans1 'done) ans2)
ans2)
;; Use ans1 when we have it.
(t
ans1))))
;;; ------------------------------------------------------------
;;; Collection Engines
;;
;; Collection engines can scan tags from the current environment and
;; provide lists of possible completions.
;;
;; General features of the abstract collector:
;; * Cache completion lists between uses
;; * Cache itself per buffer. Handle reparse hooks
;;
;; Key Interface Functions to implement:
;; * semantic-collector-next-action
;; * semantic-collector-calculate-completions
;; * semantic-collector-try-completion
;; * semantic-collector-all-completions
(defvar semantic-collector-per-buffer-list nil
"List of collectors active in this buffer.")
(make-variable-buffer-local 'semantic-collector-per-buffer-list)
(defvar semantic-collector-list nil
"List of global collectors active this session.")
(defclass semantic-collector-abstract ()
((buffer :initarg :buffer
:type buffer
:documentation "Originating buffer for this collector.
Some collectors use a given buffer as a starting place while looking up
tags.")
(cache :initform nil
:type (or null semanticdb-find-result-with-nil)
:documentation "Cache of tags.
These tags are re-used during a completion session.
Sometimes these tags are cached between completion sessions.")
(last-all-completions :initarg nil
:type semanticdb-find-result-with-nil
:documentation "Last result of `all-completions'.
This result can be used for refined completions as `last-prefix' gets
closer to a specific result.")
(last-prefix :type string
:protection :protected
:documentation "The last queried prefix.
This prefix can be used to cache intermediate completion offers.
making the action of homing in on a token faster.")
(last-completion :type (or null string)
:documentation "The last calculated completion.
This completion is calculated and saved for future use.")
(last-whitespace-completion :type (or null string)
:documentation "The last whitespace completion.
For partial completion, SPC will disabiguate over whitespace type
characters. This is the last calculated version.")
(current-exact-match :type list
:protection :protected
:documentation "The list of matched tags.
When tokens are matched, they are added to this list.")
)
"Root class for completion engines.
The baseclass provides basic functionality for interacting with
a completion displayor object, and tracking the current progress
of a completion."
:abstract t)
(defmethod semantic-collector-cleanup ((obj semantic-collector-abstract))
"Clean up any mess this collector may have."
nil)
(defmethod semantic-collector-next-action
((obj semantic-collector-abstract) partial)
"What should we do next? OBJ can predict a next good action.
PARTIAL indicates if we are doing a partial completion."
(if (and (slot-boundp obj 'last-completion)
(string= (semantic-completion-text) (oref obj last-completion)))
(let* ((cem (semantic-collector-current-exact-match obj))
(cemlen (semanticdb-find-result-length cem))
(cac (semantic-collector-all-completions
obj (semantic-completion-text)))
(caclen (semanticdb-find-result-length cac)))
(cond ((and cem (= cemlen 1)
cac (> caclen 1)
(eq last-command this-command))
;; Defer to the displayor...
nil)
((and cem (= cemlen 1))
'done)
((and (not cem) (not cac))
'empty)
((and partial (semantic-collector-try-completion-whitespace
obj (semantic-completion-text)))
'complete-whitespace)))
'complete))
(defmethod semantic-collector-last-prefix= ((obj semantic-collector-abstract)
last-prefix)
"Return non-nil if OBJ's prefix matches PREFIX."
(and (slot-boundp obj 'last-prefix)
(string= (oref obj last-prefix) last-prefix)))
(defmethod semantic-collector-get-cache ((obj semantic-collector-abstract))
"Get the raw cache of tags for completion.
Calculate the cache if there isn't one."
(or (oref obj cache)
(semantic-collector-calculate-cache obj)))
(defmethod semantic-collector-calculate-completions-raw
((obj semantic-collector-abstract) prefix completionlist)
"Calculate the completions for prefix from completionlist.
Output must be in semanticdb Find result format."
;; Must output in semanticdb format
(let ((table (save-excursion
(set-buffer (oref obj buffer))
semanticdb-current-table))
(result (semantic-find-tags-for-completion
prefix
;; To do this kind of search with a pre-built completion
;; list, we need to strip it first.
(semanticdb-strip-find-results completionlist)))
)
(if result
(list (cons table result)))))
(defmethod semantic-collector-calculate-completions
((obj semantic-collector-abstract) prefix partial)
"Calculate completions for prefix as setup for other queries."
(let* ((case-fold-search semantic-case-fold)
(same-prefix-p (semantic-collector-last-prefix= obj prefix))
(completionlist
(if (or same-prefix-p
(and (slot-boundp obj 'last-prefix)
(eq (compare-strings (oref obj last-prefix) 0 nil
prefix 0 (length prefix))
t)))
;; New prefix is subset of old prefix
(oref obj last-all-completions)
(semantic-collector-get-cache obj)))
;; Get the result
(answer (if same-prefix-p
completionlist
(semantic-collector-calculate-completions-raw
obj prefix completionlist))
)
(completion nil)
(complete-not-uniq nil)
)
;;(semanticdb-find-result-test answer)
(when (not same-prefix-p)