-
Notifications
You must be signed in to change notification settings - Fork 82
/
AbstractJavaCompletionProposal.java
1351 lines (1185 loc) · 45.1 KB
/
AbstractJavaCompletionProposal.java
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
/*******************************************************************************
* Copyright (c) 2005, 2020 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Christian Georgi<christian.georgi@sap.com> - Bug 462770: Use OS symbol for 'Ctrl'
* Gábor Kövesdán - Contribution for Bug 350000 - [content assist] Include non-prefix matches in auto-complete suggestions
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text.java;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.osgi.framework.Bundle;
import org.eclipse.osgi.util.TextProcessor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.internal.text.html.BrowserInformationControl;
import org.eclipse.jface.internal.text.html.HTMLPrinter;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.DocumentCommand;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IPositionUpdater;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextPresentationListener;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITextViewerExtension2;
import org.eclipse.jface.text.ITextViewerExtension4;
import org.eclipse.jface.text.ITextViewerExtension5;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.BoldStylerProvider;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension6;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension7;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.link.ILinkedModeListener;
import org.eclipse.jface.text.link.LinkedModeModel;
import org.eclipse.jface.text.link.LinkedModeUI;
import org.eclipse.jface.text.link.LinkedModeUI.ExitFlags;
import org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy;
import org.eclipse.jface.text.link.LinkedPosition;
import org.eclipse.jface.text.link.LinkedPositionGroup;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.texteditor.link.EditorLinkedModeUI;
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.SourceRange;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
import org.eclipse.jdt.internal.corext.javadoc.JavaDocLocations;
import org.eclipse.jdt.internal.corext.util.Strings;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.text.IJavaPartitions;
import org.eclipse.jdt.ui.text.JavaTextTools;
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.text.java.hover.JavadocBrowserInformationControlInput;
import org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover;
import org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2;
/**
*
* @since 3.2
*/
public abstract class AbstractJavaCompletionProposal implements IJavaCompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2, ICompletionProposalExtension3,
ICompletionProposalExtension4, ICompletionProposalExtension5, ICompletionProposalExtension6, ICompletionProposalExtension7 {
/**
* The key modifier that toggles whether to insert or overwrite.
*/
public static final int MODIFIER_TOGGLE_COMPLETION_MODE= SWT.CTRL;
/**
* A class to simplify tracking a reference position in a document.
*/
static final class ReferenceTracker {
/** The reference position category name. */
private static final String CATEGORY= "reference_position"; //$NON-NLS-1$
/** The position updater of the reference position. */
private final IPositionUpdater fPositionUpdater= new DefaultPositionUpdater(CATEGORY);
/** The reference position. */
private final Position fPosition= new Position(0);
/**
* Called before document changes occur. It must be followed by a call to postReplace().
*
* @param document the document on which to track the reference position.
* @param offset the offset
* @throws BadLocationException if the offset describes an invalid range in this document
*/
public void preReplace(IDocument document, int offset) throws BadLocationException {
fPosition.setOffset(offset);
try {
document.addPositionCategory(CATEGORY);
document.addPositionUpdater(fPositionUpdater);
document.addPosition(CATEGORY, fPosition);
} catch (BadPositionCategoryException e) {
// should not happen
JavaPlugin.log(e);
}
}
/**
* Called after the document changed occurred. It must be preceded by a call to preReplace().
*
* @param document the document on which to track the reference position.
* @return offset after the replace
*/
public int postReplace(IDocument document) {
try {
document.removePosition(CATEGORY, fPosition);
document.removePositionUpdater(fPositionUpdater);
document.removePositionCategory(CATEGORY);
} catch (BadPositionCategoryException e) {
// should not happen
JavaPlugin.log(e);
}
return fPosition.getOffset();
}
}
protected static class ExitPolicy implements IExitPolicy {
final char fExitCharacter;
private final IDocument fDocument;
public ExitPolicy(char exitCharacter, IDocument document) {
fExitCharacter= exitCharacter;
fDocument= document;
}
@Override
public ExitFlags doExit(LinkedModeModel environment, VerifyEvent event, int offset, int length) {
if (event.character == fExitCharacter) {
if (environment.anyPositionContains(offset))
return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
else
return new ExitFlags(ILinkedModeListener.UPDATE_CARET, true);
}
switch (event.character) {
case ';':
return new ExitFlags(ILinkedModeListener.NONE, true);
case SWT.CR:
// 1) when entering an anonymous class as a parameter, we don't want
// to jump after the parenthesis when return is pressed
// 2) after auto completion of methods without parameters, exit from linked mode when return is pressed
if (offset > 0) {
try {
char prevOffsetChar= fDocument.getChar(offset - 1);
if (prevOffsetChar == '{' || prevOffsetChar == ';')
return new ExitFlags(ILinkedModeListener.EXIT_ALL, true);
} catch (BadLocationException e) {
}
}
return null;
default:
return null;
}
}
}
private StyledString fDisplayString;
private String fReplacementString;
private int fReplacementOffset;
private int fReplacementLength;
private int fCursorPosition;
private Image fImage;
private IContextInformation fContextInformation;
private ProposalInfo fProposalInfo;
private char[] fTriggerCharacters;
private String fSortString;
private int fRelevance;
private boolean fIsInJavadoc;
private int fPatternMatchRule= -1;
private StyleRange fRememberedStyleRange;
private boolean fToggleEating;
private ITextViewer fTextViewer;
/**
* The control creator.
*
* @since 3.2
*/
private IInformationControlCreator fCreator;
/**
* The CSS used to format javadoc information.
* @since 3.3
*/
private static String fgCSSStyles;
/**
* The invocation context of this completion proposal. Can be <code>null</code>.
*/
protected final JavaContentAssistInvocationContext fInvocationContext;
/**
* Cache to store last validation state.
* @since 3.5
*/
private boolean fIsValidated= true;
/**
* The text presentation listener.
* @since 3.6
*/
private ITextPresentationListener fTextPresentationListener;
protected AbstractJavaCompletionProposal() {
fInvocationContext= null;
}
protected AbstractJavaCompletionProposal(JavaContentAssistInvocationContext context) {
fInvocationContext= context;
}
@Override
public char[] getTriggerCharacters() {
return fTriggerCharacters;
}
/**
* Sets the trigger characters.
*
* @param triggerCharacters The set of characters which can trigger the application of this
* completion proposal
*/
public void setTriggerCharacters(char[] triggerCharacters) {
fTriggerCharacters= triggerCharacters;
}
/**
* Sets the proposal info.
*
* @param proposalInfo The additional information associated with this proposal or
* <code>null</code>
*/
public void setProposalInfo(ProposalInfo proposalInfo) {
fProposalInfo= proposalInfo;
}
/**
* Returns the additional proposal info, or <code>null</code> if none exists.
*
* @return the additional proposal info, or <code>null</code> if none exists
*/
protected ProposalInfo getProposalInfo() {
return fProposalInfo;
}
/**
* Sets the cursor position relative to the insertion offset. By default this is the length of
* the completion string (Cursor positioned after the completion)
*
* @param cursorPosition The cursorPosition to set
*/
public void setCursorPosition(int cursorPosition) {
Assert.isTrue(cursorPosition >= 0);
fCursorPosition= cursorPosition;
}
protected int getCursorPosition() {
return fCursorPosition;
}
@Override
public final void apply(IDocument document) {
// not used any longer
apply(document, (char) 0, getReplacementOffset() + getReplacementLength());
}
@Override
public void apply(IDocument document, char trigger, int offset) {
if (isSupportingRequiredProposals()) {
CompletionProposal coreProposal= ((MemberProposalInfo)getProposalInfo()).fProposal;
CompletionProposal[] requiredProposals= coreProposal.getRequiredProposals();
for (int i= 0; requiredProposals != null && i < requiredProposals.length; i++) {
int oldLen= document.getLength();
if (requiredProposals[i].getKind() == CompletionProposal.TYPE_REF) {
LazyJavaCompletionProposal proposal= createRequiredTypeCompletionProposal(requiredProposals[i], fInvocationContext);
proposal.apply(document);
setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen);
} else if (requiredProposals[i].getKind() == CompletionProposal.TYPE_IMPORT
|| requiredProposals[i].getKind() == CompletionProposal.METHOD_IMPORT
|| requiredProposals[i].getKind() == CompletionProposal.FIELD_IMPORT) {
ImportCompletionProposal proposal= new ImportCompletionProposal(requiredProposals[i], fInvocationContext, coreProposal.getKind());
proposal.setReplacementOffset(getReplacementOffset());
proposal.apply(document);
setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen - proposal.getLengthOfImportsAddedBehindReplacementOffset());
} else {
/*
* In 3.3 we only support the above required proposals, see
* CompletionProposal#getRequiredProposals()
*/
Assert.isTrue(false);
}
}
}
try {
boolean isSmartTrigger= isSmartTrigger(trigger);
String replacement;
if (isSmartTrigger || trigger == (char) 0) {
int referenceOffset= getReplacementOffset() + getReplacementLength();
replacement= getReplacementString();
//add ; to the replacement string if replacement string do not end with a semicolon and the document do not already have a ; at the reference offset.
if (trigger == ';' && replacement.charAt(replacement.length() - 1) != ';' && (referenceOffset >= document.getLength() || document.getChar(referenceOffset) != ';')) {
replacement= replacement + ";"; //$NON-NLS-1$
setReplacementString(replacement);
}
} else {
StringBuilder buffer= new StringBuilder(getReplacementString());
// fix for PR #5533. Assumes that no eating takes place.
if ((getCursorPosition() > 0 && getCursorPosition() <= buffer.length() && buffer.charAt(getCursorPosition() - 1) != trigger)) {
// insert trigger ';' for methods with parameter at the end of the replacement string and not at the cursor position.
int length= getReplacementString().length();
if (trigger == ';' && getCursorPosition() != length) {
if (buffer.charAt(length - 1) != trigger) {
buffer.insert(length, trigger);
}
} else {
buffer.insert(getCursorPosition(), trigger);
setCursorPosition(getCursorPosition() + 1);
}
}
replacement= buffer.toString();
setReplacementString(replacement);
}
// reference position just at the end of the document change.
int referenceOffset= getReplacementOffset() + getReplacementLength();
final ReferenceTracker referenceTracker= new ReferenceTracker();
referenceTracker.preReplace(document, referenceOffset);
replace(document, getReplacementOffset(), getReplacementLength(), replacement);
referenceOffset= referenceTracker.postReplace(document);
setReplacementOffset(referenceOffset - replacement.length());
// PR 47097
if (isSmartTrigger) {
// avoid inserting redundant semicolon when smart insert is enabled.
if ((trigger != ';') || (!replacement.endsWith(";") && (document.getChar(referenceOffset) != ';'))) { //$NON-NLS-1$
handleSmartTrigger(document, trigger, referenceOffset);
}
}
} catch (BadLocationException x) {
// ignore
}
}
/**
* Creates the required type proposal.
*
* @param completionProposal the core completion proposal
* @param invocationContext invocation context
* @return the required type completion proposal
* @since 3.5
*/
protected LazyJavaCompletionProposal createRequiredTypeCompletionProposal(CompletionProposal completionProposal, JavaContentAssistInvocationContext invocationContext) {
if (PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES))
return (LazyJavaCompletionProposal)new FillArgumentNamesCompletionProposalCollector(invocationContext).createJavaCompletionProposal(completionProposal);
else
return new LazyJavaTypeCompletionProposal(completionProposal, invocationContext);
}
private boolean isSmartTrigger(char trigger) {
return trigger == ';' && JavaPlugin.getDefault().getCombinedPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_SEMICOLON)
|| trigger == '{' && JavaPlugin.getDefault().getCombinedPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_OPENING_BRACE);
}
private void handleSmartTrigger(IDocument document, char trigger, int referenceOffset) throws BadLocationException {
DocumentCommand cmd= new DocumentCommand() {
};
cmd.offset= referenceOffset;
cmd.length= 0;
cmd.text= Character.toString(trigger);
cmd.doit= true;
cmd.shiftsCaret= true;
cmd.caretOffset= getReplacementOffset() + getCursorPosition();
SmartSemicolonAutoEditStrategy strategy= new SmartSemicolonAutoEditStrategy(IJavaPartitions.JAVA_PARTITIONING);
strategy.customizeDocumentCommand(document, cmd);
replace(document, cmd.offset, cmd.length, cmd.text);
setCursorPosition(cmd.caretOffset - getReplacementOffset() + cmd.text.length());
}
protected final void replace(IDocument document, int offset, int length, String string) throws BadLocationException {
if (!document.get(offset, length).equals(string))
document.replace(offset, length, string);
}
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
IDocument document= viewer.getDocument();
if (fTextViewer == null)
fTextViewer= viewer;
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96059
// don't apply the proposal if for some reason we're not valid any longer
if (!isInJavadoc() && !validate(document, offset, null)) {
setCursorPosition(offset);
if (trigger != '\0') {
try {
document.replace(offset, 0, String.valueOf(trigger));
setCursorPosition(getCursorPosition() + 1);
if (trigger == '(' && autocloseBrackets()) {
document.replace(getReplacementOffset() + getCursorPosition(), 0, ")"); //$NON-NLS-1$
setUpLinkedMode(document, ')');
}
} catch (BadLocationException x) {
// ignore
}
}
return;
}
// don't eat if not in preferences, XOR with Ctrl
// but: if there is a selection, replace it!
Point selection= viewer.getSelectedRange();
fToggleEating= (stateMask & MODIFIER_TOGGLE_COMPLETION_MODE) != 0;
int newLength= selection.x + selection.y - getReplacementOffset();
if ((insertCompletion() ^ fToggleEating) && newLength >= 0)
setReplacementLength(newLength);
apply(document, trigger, offset);
fToggleEating= false;
}
/**
* Tells whether the user toggled the insert mode by pressing the 'Ctrl' key.
*
* @return <code>true</code> if the insert mode is toggled, <code>false</code> otherwise
* @since 3.5
*/
protected boolean isInsertModeToggled() {
return fToggleEating;
}
/**
* Returns <code>true</code> if the proposal is within javadoc, <code>false</code> otherwise.
*
* @return <code>true</code> if the proposal is within javadoc, <code>false</code> otherwise
*/
protected boolean isInJavadoc() {
return fIsInJavadoc;
}
/**
* Sets the javadoc attribute.
*
* @param isInJavadoc <code>true</code> if the proposal is within javadoc
*/
protected void setInJavadoc(boolean isInJavadoc) {
fIsInJavadoc= isInJavadoc;
}
@Override
public Point getSelection(IDocument document) {
if (!fIsValidated)
return null;
return new Point(getReplacementOffset() + getCursorPosition(), 0);
}
@Override
public IContextInformation getContextInformation() {
return fContextInformation;
}
/**
* Sets the context information.
* @param contextInformation The context information associated with this proposal
*/
public void setContextInformation(IContextInformation contextInformation) {
fContextInformation= contextInformation;
}
@Override
public String getDisplayString() {
if (fDisplayString != null)
return fDisplayString.getString();
return ""; //$NON-NLS-1$
}
@Override
public String getAdditionalProposalInfo() {
Object info= getAdditionalProposalInfo(new NullProgressMonitor());
return info == null ? null : info.toString();
}
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
ProposalInfo proposalInfo= getProposalInfo();
if (proposalInfo != null) {
try {
StringBuilder buffer= new StringBuilder();
IJavaElement element= proposalInfo.getJavaElement();
if (element != null) {
String base= null;
String info= proposalInfo.getInfo(monitor);
if (info != null && info.length() > 0) {
buffer.append(info);
base= JavadocContentAccess2.extractBaseURL(info);
}
if (base == null && element instanceof IMember) {
base= JavaDocLocations.getBaseURL(element, ((IMember) element).isBinary());
}
addConstantOrDefaultValue(buffer, element);
if (buffer.length() > 0) {
ColorRegistry registry= JFaceResources.getColorRegistry();
RGB fgRGB= registry.getRGB("org.eclipse.jdt.ui.Javadoc.foregroundColor"); //$NON-NLS-1$
RGB bgRGB= registry.getRGB("org.eclipse.jdt.ui.Javadoc.backgroundColor"); //$NON-NLS-1$
HTMLPrinter.insertPageProlog(buffer, 0, fgRGB, bgRGB, getCSSStyles());
if (base != null) {
int endHeadIdx= buffer.indexOf("</head>"); //$NON-NLS-1$
buffer.insert(endHeadIdx, "\n<base href='" + base + "'>\n"); //$NON-NLS-1$ //$NON-NLS-2$
}
HTMLPrinter.addPageEpilog(buffer);
return new JavadocBrowserInformationControlInput(null, element, buffer.toString(), 0);
}
}
} catch (JavaModelException e) {
JavaPlugin.log(e);
}
}
return null;
}
private void addConstantOrDefaultValue(StringBuilder buffer, IJavaElement element) throws JavaModelException {
int elementType= element.getElementType();
if ((elementType != IJavaElement.FIELD)
&& (elementType != IJavaElement.METHOD)) {
return;
}
ITypeRoot typeRoot= null;
Region nameRegion= null;
if (element instanceof IMember) {
// typeRoot and nameRegion - used to retrieve the constant/default value of the element.
// Useful only when the element is declared in the active CU as JavadocHover#getHoveredASTNode
// uses SharedASTProvider.WAIT_ACTIVE_ONLY. Otherwise, bindings are created.
typeRoot= ((IMember) element).getTypeRoot();
ISourceRange nameRange= ((ISourceReference) element).getNameRange();
if (SourceRange.isAvailable(nameRange)) {
nameRegion= new Region(nameRange.getOffset(), nameRange.getLength());
}
}
if (elementType == IJavaElement.FIELD) {
String constantValue= JavadocHover.getConstantValue((IField) element, typeRoot, nameRegion);
if (constantValue != null) {
constantValue= HTMLPrinter.convertToHTMLContentWithWhitespace(constantValue);
buffer.append(JavadocContentAccess2.BlOCK_TAG_TITLE_START);
buffer.append(JavaTextMessages.AbstractJavaCompletionProposal_value);
buffer.append(JavadocContentAccess2.BlOCK_TAG_TITLE_END);
buffer.append(JavadocContentAccess2.BlOCK_TAG_ENTRY_START);
buffer.append(constantValue);
buffer.append(JavadocContentAccess2.BlOCK_TAG_ENTRY_END);
}
} else if (elementType == IJavaElement.METHOD) {
String defaultValue;
try {
defaultValue= JavadocHover.getAnnotationMemberDefaultValue((IMethod) element, typeRoot, nameRegion);
} catch (JavaModelException e) {
defaultValue= null;
}
if (defaultValue != null) {
defaultValue= HTMLPrinter.convertToHTMLContentWithWhitespace(defaultValue);
buffer.append(JavadocContentAccess2.BlOCK_TAG_TITLE_START);
buffer.append(JavaTextMessages.AbstractJavaCompletionProposal_default);
buffer.append(JavadocContentAccess2.BlOCK_TAG_TITLE_END);
buffer.append(JavadocContentAccess2.BlOCK_TAG_ENTRY_START);
buffer.append(defaultValue);
buffer.append(JavadocContentAccess2.BlOCK_TAG_ENTRY_END);
}
}
}
/**
* Returns the style information for displaying HTML (Javadoc) content.
*
* @return the CSS styles
* @since 3.3
*/
protected String getCSSStyles() {
if (fgCSSStyles == null) {
Bundle bundle= Platform.getBundle(JavaPlugin.getPluginId());
URL url= bundle.getEntry("/JavadocHoverStyleSheet.css"); //$NON-NLS-1$
if (url != null) {
BufferedReader reader= null;
try {
url= FileLocator.toFileURL(url);
reader= new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder buffer= new StringBuilder(200);
String line= reader.readLine();
while (line != null) {
buffer.append(line);
buffer.append('\n');
line= reader.readLine();
}
fgCSSStyles= buffer.toString();
} catch (IOException ex) {
JavaPlugin.log(ex);
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
}
}
}
}
String css= fgCSSStyles;
if (css != null) {
FontData fontData= JFaceResources.getFontRegistry().getFontData(PreferenceConstants.APPEARANCE_JAVADOC_FONT)[0];
css= HTMLPrinter.convertTopLevelFont(css, fontData);
}
return css;
}
@Override
public int getContextInformationPosition() {
if (getContextInformation() == null)
return getReplacementOffset() - 1;
return getReplacementOffset() + getCursorPosition();
}
/**
* Gets the replacement offset.
* @return Returns a int
*/
public int getReplacementOffset() {
return fReplacementOffset;
}
/**
* Sets the replacement offset.
* @param replacementOffset The replacement offset to set
*/
public void setReplacementOffset(int replacementOffset) {
Assert.isTrue(replacementOffset >= 0);
fReplacementOffset= replacementOffset;
}
@Override
public int getPrefixCompletionStart(IDocument document, int completionOffset) {
return getReplacementOffset();
}
/**
* Gets the replacement length.
* @return Returns a int
*/
public int getReplacementLength() {
return fReplacementLength;
}
/**
* Sets the replacement length.
* @param replacementLength The replacementLength to set
*/
public void setReplacementLength(int replacementLength) {
Assert.isTrue(replacementLength >= 0);
fReplacementLength= replacementLength;
}
/**
* Gets the replacement string.
* @return Returns a String
*/
public String getReplacementString() {
return fReplacementString;
}
/**
* Sets the replacement string.
* @param replacementString The replacement string to set
*/
public void setReplacementString(String replacementString) {
Assert.isNotNull(replacementString);
fReplacementString= replacementString;
}
@Override
public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {
if (!isCamelCaseMatching())
return getReplacementString();
String prefix= getPrefix(document, completionOffset);
return getCamelCaseCompound(prefix, getReplacementString());
}
@Override
public Image getImage() {
return fImage;
}
/**
* Sets the image.
* @param image The image to set
*/
public void setImage(Image image) {
fImage= image;
}
@Override
public boolean isValidFor(IDocument document, int offset) {
return validate(document, offset, null);
}
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
if (!isOffsetValid(offset))
return fIsValidated= false;
fIsValidated= isValidPrefix(getPrefix(document, offset));
if (fIsValidated && event != null) {
// adapt replacement range to document change
int delta= (event.fText == null ? 0 : event.fText.length()) - event.fLength;
final int newLength= Math.max(getReplacementLength() + delta, 0);
setReplacementLength(newLength);
}
return fIsValidated;
}
/**
* Checks whether the given offset is valid for this proposal.
*
* @param offset the caret offset
* @return <code>true</code> if the offset is valid for this proposal
* @since 3.5
*/
protected boolean isOffsetValid(int offset) {
return getReplacementOffset() <= offset;
}
/**
* Checks whether <code>pattern</code> is a valid pattern for this proposal. Usually, while code
* completion is in progress, the user types and edits the pattern in the document in order to
* filter the proposal list. From {@link #validate(IDocument, int, DocumentEvent) }, the current
* pattern in the document is extracted and this method is called to find out whether the
* proposal is still valid.
* <p>
* The default implementation checks if <code>pattern</code> matches the proposal's
* {@link #getDisplayString() display string} using the {@link #isPrefix(String, String) }
* method.
* </p>
*
* @param pattern the current pattern in the document
* @return <code>true</code> if <code>pattern</code> is a valid match for this proposal
*/
protected boolean isValidPrefix(String pattern) {
/*
* See http://dev.eclipse.org/bugs/show_bug.cgi?id=17667
* why we do not use the replacement string.
* String word= fReplacementString;
*
* Besides that bug we also use the display string
* for performance reasons, as computing the
* replacement string can be expensive.
*/
return isPrefix(pattern, TextProcessor.deprocess(getDisplayString()));
}
/**
* Gets the proposal's relevance.
* @return Returns a int
*/
@Override
public int getRelevance() {
if (fPatternMatchRule == SearchPattern.R_SUBSTRING_MATCH) {
return fRelevance - 500;
}
if (fPatternMatchRule == SearchPattern.R_SUBWORD_MATCH) {
return fRelevance - 1000;
}
return fRelevance;
}
/**
* Sets the proposal's relevance.
* @param relevance The relevance to set
*/
public void setRelevance(int relevance) {
fRelevance= relevance;
}
/**
* Returns the text in <code>document</code> from {@link #getReplacementOffset()} to
* <code>offset</code>. Returns the empty string if <code>offset</code> is before the
* replacement offset or if an exception occurs when accessing the document.
*
* @param document the document
* @param offset the offset
* @return the prefix
* @since 3.2
*/
protected String getPrefix(IDocument document, int offset) {
try {
int length= offset - getReplacementOffset();
if (length > 0)
return document.get(getReplacementOffset(), length);
} catch (BadLocationException x) {
}
return ""; //$NON-NLS-1$
}
/**
* Case insensitive matching of the <code>pattern</code> within the given <code>string</code>.
*
* @param pattern the pattern
* @param string the string to look for the pattern
* @return <code>true</code> if the given pattern matches the string as a prefix, as a CamelCase
* match, or as a substring pattern and <code>false</code> if <code>pattern</code> is
* longer than <code>string</code> or if the pattern doesn't match the string based on
* any of these rules
* @since 3.2
*/
protected boolean isPrefix(String pattern, String string) {
if (pattern == null || string == null || pattern.length() > string.length())
return false;
fPatternMatchRule= getPatternMatchRule(pattern, string);
return fPatternMatchRule != -1;
}
/**
* Matches the given <code>pattern</code> in <code>string</code> and returns the match rule.
*
* @param pattern the pattern to match
* @param string the string to look for the pattern
* @return the match rule used to match the given <code>pattern</code> in <code>string</code>,
* or -1 if the <code>pattern</code> doesn't match the <code>string</code> based on any
* rule
* @since 3.12
*/
protected int getPatternMatchRule(String pattern, String string) {
String start;
try {
start= string.substring(0, pattern.length());
} catch (StringIndexOutOfBoundsException e) {
String message= "Error retrieving proposal text.\nDisplay string:\n" + string + "\nPattern:\n" + pattern; //$NON-NLS-1$//$NON-NLS-2$
JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, message, e));
return -1;
}
if (start.equalsIgnoreCase(pattern)) {
return SearchPattern.R_PREFIX_MATCH;
} else if (isCamelCaseMatching() && CharOperation.camelCaseMatch(pattern.toCharArray(), string.toCharArray())) {
return SearchPattern.R_CAMELCASE_MATCH;
} else if (isSubstringMatching() && CharOperation.substringMatch(pattern.toCharArray(), string.toCharArray())) {
return SearchPattern.R_SUBSTRING_MATCH;
} else if (isSubwordMatching() && CharOperation.subWordMatch(pattern.toCharArray(), string.toCharArray())) {
return SearchPattern.R_SUBWORD_MATCH;
} else {
return -1;
}
}
/**
* Matches <code>prefix</code> against <code>string</code> and replaces the matched region
* by prefix. Case is preserved as much as possible. This method returns <code>string</code> if camel case completion
* is disabled. Examples when camel case completion is enabled:
* <ul>
* <li>getCamelCompound("NuPo", "NullPointerException") -> "NuPointerException"</li>
* <li>getCamelCompound("NuPoE", "NullPointerException") -> "NuPoException"</li>
* <li>getCamelCompound("hasCod", "hashCode") -> "hasCode"</li>
* </ul>
*
* @param prefix the prefix to match against
* @param string the string to match
* @return a compound of prefix and any postfix taken from <code>string</code>
* @since 3.2
*/
protected final String getCamelCaseCompound(String prefix, String string) {
if (prefix.length() > string.length())
return string;
// a normal prefix - no camel case logic at all
String start= string.substring(0, prefix.length());
if (start.equalsIgnoreCase(prefix))
return string;
final char[] patternChars= prefix.toCharArray();
final char[] stringChars= string.toCharArray();
for (int i= 1; i <= stringChars.length; i++)
if (CharOperation.camelCaseMatch(patternChars, 0, patternChars.length, stringChars, 0, i))
return prefix + string.substring(i);
// Not a camel case match at all.
// This should not happen -> stay with the default behavior
return string;
}
/**
* Returns true if camel case matching is enabled.
*
* @return <code>true</code> if camel case matching is enabled
* @since 3.2
*/
protected boolean isCamelCaseMatching() {
String value= JavaCore.getOption(JavaCore.CODEASSIST_CAMEL_CASE_MATCH);
return JavaCore.ENABLED.equals(value);
}
/**
* Returns true if substring matching is enabled.
*
* @return <code>true</code> if substring matching is enabled
* @since 3.12
*/
protected boolean isSubstringMatching() {