-
Notifications
You must be signed in to change notification settings - Fork 78
/
CompilationUnitDocumentProvider.java
1671 lines (1448 loc) · 52.8 KB
/
CompilationUnitDocumentProvider.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) 2000, 2016 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
*******************************************************************************/
package org.eclipse.jdt.internal.ui.javaeditor;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.resources.IEncodedStorage;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFileState;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.filebuffers.IAnnotationModelFactory;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DefaultLineTracker;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ILineTracker;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.quickassist.IQuickFixableAnnotation;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.AnnotationModelEvent;
import org.eclipse.jface.text.source.IAnnotationAccessExtension;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelListener;
import org.eclipse.jface.text.source.IAnnotationModelListenerExtension;
import org.eclipse.jface.text.source.IAnnotationPresentation;
import org.eclipse.jface.text.source.ImageUtilities;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE.SharedImages;
import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
import org.eclipse.ui.texteditor.AnnotationPreference;
import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.MarkerAnnotation;
import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.ForwardingDocumentProvider;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IProblemRequestor;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.text.IJavaPartitions;
import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.IPostSaveListener;
import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.SaveParticipantRegistry;
import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
import org.eclipse.jdt.internal.ui.text.java.IProblemRequestorExtension;
import org.eclipse.jdt.internal.ui.text.spelling.JavaSpellingReconcileStrategy;
import org.eclipse.jdt.internal.ui.util.Progress;
public class CompilationUnitDocumentProvider extends TextFileDocumentProvider implements ICompilationUnitDocumentProvider, IAnnotationModelFactory {
/**
* Bundle of all required informations to allow working copy management.
*/
static protected class CompilationUnitInfo extends FileInfo {
public ICompilationUnit fCopy;
}
/**
* Annotation representing an <code>IProblem</code>.
*/
static public class ProblemAnnotation extends Annotation implements IJavaAnnotation, IAnnotationPresentation, IQuickFixableAnnotation {
public static final String SPELLING_ANNOTATION_TYPE= "org.eclipse.ui.workbench.texteditor.spelling"; //$NON-NLS-1$
//XXX: To be fully correct these constants should be non-static
/**
* The layer in which task problem annotations are located.
*/
private static final int TASK_LAYER;
/**
* The layer in which info problem annotations are located.
*/
private static final int INFO_LAYER;
/**
* The layer in which warning problem annotations representing are located.
*/
private static final int WARNING_LAYER;
/**
* The layer in which error problem annotations representing are located.
*/
private static final int ERROR_LAYER;
static {
AnnotationPreferenceLookup lookup= EditorsUI.getAnnotationPreferenceLookup();
TASK_LAYER= computeLayer("org.eclipse.ui.workbench.texteditor.task", lookup); //$NON-NLS-1$
INFO_LAYER= computeLayer("org.eclipse.jdt.ui.info", lookup); //$NON-NLS-1$
WARNING_LAYER= computeLayer("org.eclipse.jdt.ui.warning", lookup); //$NON-NLS-1$
ERROR_LAYER= computeLayer("org.eclipse.jdt.ui.error", lookup); //$NON-NLS-1$
}
private static int computeLayer(String annotationType, AnnotationPreferenceLookup lookup) {
Annotation annotation= new Annotation(annotationType, false, null);
AnnotationPreference preference= lookup.getAnnotationPreference(annotation);
if (preference != null)
return preference.getPresentationLayer() + 1;
else
return IAnnotationAccessExtension.DEFAULT_LAYER + 1;
}
private static Image fgQuickFixWarningImage;
private static Image fgQuickFixErrorImage;
private static Image fgQuickFixInfoImage;
private static Image fgTaskImage;
private static Image fgInfoImage;
private static Image fgWarningImage;
private static Image fgErrorImage;
private static boolean fgImagesInitialized= false;
private ICompilationUnit fCompilationUnit;
private List<IJavaAnnotation> fOverlaids;
private IProblem fProblem;
private Image fImage;
private boolean fImageInitialized= false;
private int fLayer= IAnnotationAccessExtension.DEFAULT_LAYER;
private boolean fIsQuickFixable;
private boolean fIsQuickFixableStateSet= false;
public ProblemAnnotation(IProblem problem, ICompilationUnit cu) {
fProblem= problem;
fCompilationUnit= cu;
if (JavaSpellingReconcileStrategy.SPELLING_PROBLEM_ID == fProblem.getID()) {
setType(SPELLING_ANNOTATION_TYPE);
fLayer= WARNING_LAYER;
} else if (IProblem.Task == fProblem.getID()) {
setType(JavaMarkerAnnotation.TASK_ANNOTATION_TYPE);
fLayer= TASK_LAYER;
} else if (fProblem.isWarning()) {
setType(JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE);
fLayer= WARNING_LAYER;
} else if (fProblem.isError()) {
setType(JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE);
fLayer= ERROR_LAYER;
} else {
setType(JavaMarkerAnnotation.INFO_ANNOTATION_TYPE);
fLayer= INFO_LAYER;
}
}
/*
* @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
*/
@Override
public int getLayer() {
return fLayer;
}
private void initializeImage() {
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=18936
if (!fImageInitialized) {
initializeImages();
if (!isQuickFixableStateSet())
setQuickFixable(isProblem() && indicateQuixFixableProblems() && JavaCorrectionProcessor.hasCorrections(this)); // no light bulb for tasks
if (isQuickFixable()) {
if (JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(getType()))
fImage= fgQuickFixErrorImage;
else if (JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(getType()))
fImage= fgQuickFixWarningImage;
else
fImage= fgQuickFixInfoImage;
} else {
String type= getType();
if (null != type)
switch (type) {
case JavaMarkerAnnotation.TASK_ANNOTATION_TYPE:
fImage= fgTaskImage;
break;
case JavaMarkerAnnotation.INFO_ANNOTATION_TYPE:
fImage= fgInfoImage;
break;
case JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE:
fImage= fgWarningImage;
break;
case JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE:
fImage= fgErrorImage;
break;
default:
break;
}
}
fImageInitialized= true;
}
}
private void initializeImages() {
if (fgImagesInitialized)
return;
fgQuickFixWarningImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_WARNING);
fgQuickFixErrorImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_ERROR);
fgQuickFixInfoImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_FIXABLE_INFO);
ISharedImages sharedImages= PlatformUI.getWorkbench().getSharedImages();
fgTaskImage= sharedImages.getImage(SharedImages.IMG_OBJS_TASK_TSK);
fgInfoImage= sharedImages.getImage(ISharedImages.IMG_OBJS_INFO_TSK);
fgWarningImage= sharedImages.getImage(ISharedImages.IMG_OBJS_WARN_TSK);
fgErrorImage= sharedImages.getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
fgImagesInitialized= true;
}
private boolean indicateQuixFixableProblems() {
return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
}
/*
* @see Annotation#paint
*/
@Override
public void paint(GC gc, Canvas canvas, Rectangle r) {
initializeImage();
if (fImage != null)
ImageUtilities.drawImage(fImage, gc, canvas, r, SWT.CENTER, SWT.TOP);
}
/*
* @see IJavaAnnotation#getMessage()
*/
@Override
public String getText() {
return fProblem.getMessage();
}
/*
* @see IJavaAnnotation#getArguments()
*/
@Override
public String[] getArguments() {
return isProblem() ? fProblem.getArguments() : null;
}
/*
* @see IJavaAnnotation#getId()
*/
@Override
public int getId() {
return fProblem.getID();
}
/*
* @see IJavaAnnotation#isProblem()
*/
@Override
public boolean isProblem() {
String type= getType();
return JavaMarkerAnnotation.INFO_ANNOTATION_TYPE.equals(type)
|| JavaMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(type)
|| JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(type)
|| SPELLING_ANNOTATION_TYPE.equals(type);
}
/*
* @see IJavaAnnotation#hasOverlay()
*/
@Override
public boolean hasOverlay() {
return false;
}
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getOverlay()
*/
@Override
public IJavaAnnotation getOverlay() {
return null;
}
/*
* @see IJavaAnnotation#addOverlaid(IJavaAnnotation)
*/
@Override
public void addOverlaid(IJavaAnnotation annotation) {
if (fOverlaids == null)
fOverlaids= new ArrayList<>(1);
fOverlaids.add(annotation);
}
/*
* @see IJavaAnnotation#removeOverlaid(IJavaAnnotation)
*/
@Override
public void removeOverlaid(IJavaAnnotation annotation) {
if (fOverlaids != null) {
fOverlaids.remove(annotation);
if (fOverlaids.isEmpty())
fOverlaids= null;
}
}
/*
* @see IJavaAnnotation#getOverlaidIterator()
*/
@Override
public Iterator<IJavaAnnotation> getOverlaidIterator() {
if (fOverlaids != null)
return fOverlaids.iterator();
return null;
}
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getCompilationUnit()
*/
@Override
public ICompilationUnit getCompilationUnit() {
return fCompilationUnit;
}
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation#getMarkerType()
*/
@Override
public String getMarkerType() {
if (fProblem instanceof CategorizedProblem)
return ((CategorizedProblem) fProblem).getMarkerType();
return null;
}
/*
* @see org.eclipse.jface.text.quickassist.IQuickFixableAnnotation#setQuickFixable(boolean)
* @since 3.2
*/
@Override
public void setQuickFixable(boolean state) {
fIsQuickFixable= state;
fIsQuickFixableStateSet= true;
}
/*
* @see org.eclipse.jface.text.quickassist.IQuickFixableAnnotation#isQuickFixableStateSet()
* @since 3.2
*/
@Override
public boolean isQuickFixableStateSet() {
return fIsQuickFixableStateSet;
}
/*
* @see org.eclipse.jface.text.quickassist.IQuickFixableAnnotation#isQuickFixable()
* @since 3.2
*/
@Override
public boolean isQuickFixable() {
Assert.isTrue(isQuickFixableStateSet());
return fIsQuickFixable;
}
}
/**
* Internal structure for mapping positions to JavaMarkerAnnotations.
* The reason for this specific structure is that positions can
* change over time. Thus a lookup is based on value and not
* on hash value.
*/
private static class ReverseJavaMarkerAnnotationsMap {
static class Entry {
private final Position position;
final List<JavaMarkerAnnotation> annotations= new ArrayList<>();
public Entry(Position position) {
this.position= position;
}
public boolean isFor(Position positionToCheck) {
return position.equals(positionToCheck);
}
}
private List<Entry> entries= new ArrayList<>(2);
private int anchorIndex= 0;
public Iterable<JavaMarkerAnnotation> get(Position position) {
// Start behind and store anchor because usually retrieval is performed sequentially over positions
Entry entry= getEntry(position, true);
if (entry != null) {
return entry.annotations;
} else {
return Collections.emptyList();
}
}
private Entry getEntry(Position position, boolean useAndStoreAnchor) {
int startIndex= useAndStoreAnchor ? anchorIndex : 0;
int numberOfIndices= entries.size();
for (int i= 0; i < numberOfIndices; i++) {
int indexToCheck= (i + startIndex) % numberOfIndices;
Entry entry= entries.get(indexToCheck);
if (entry.isFor(position)) {
if (useAndStoreAnchor) {
anchorIndex= indexToCheck;
}
return entry;
}
}
return null;
}
public void put(Position position, JavaMarkerAnnotation annotation) {
Entry entry= getOrCreateEntry(position);
entry.annotations.add(annotation);
}
private Entry getOrCreateEntry(Position position) {
Entry entry= getEntry(position, false);
if (entry == null) {
entry= new Entry(position);
entries.add(entry);
}
return entry;
}
public void remove(Position position, JavaMarkerAnnotation annotation) {
Entry entry= getEntry(position, false);
if (entry != null) {
if (entry.annotations.size() == 1) {
entries.remove(entry);
} else {
entry.annotations.remove(annotation);
}
}
}
public void clear() {
entries.clear();
}
}
/**
* Annotation model dealing with java marker annotations and temporary problems.
* Also acts as problem requester for its compilation unit. Initially inactive. Must explicitly be
* activated.
*/
protected static class CompilationUnitAnnotationModel extends ResourceMarkerAnnotationModel implements IProblemRequestor, IProblemRequestorExtension {
private static class ProblemRequestorState {
boolean fInsideReportingSequence= false;
List<IProblem> fReportedProblems;
}
private ThreadLocal<ProblemRequestorState> fProblemRequestorState= new ThreadLocal<>();
private int fStateCount= 0;
private ICompilationUnit fCompilationUnit;
private final List<ProblemAnnotation> fGeneratedAnnotations= new ArrayList<>();
private IProgressMonitor fProgressMonitor;
private boolean fIsActive= false;
private boolean fIsHandlingTemporaryProblems;
private ReverseJavaMarkerAnnotationsMap fReverseMap= new ReverseJavaMarkerAnnotationsMap();
private List<JavaMarkerAnnotation> fPreviouslyOverlaid= null;
private List<JavaMarkerAnnotation> fCurrentlyOverlaid= new ArrayList<>();
private Thread fActiveThread;
public CompilationUnitAnnotationModel(IResource resource) {
super(resource);
}
public void setCompilationUnit(ICompilationUnit unit) {
fCompilationUnit= unit;
}
@Override
protected MarkerAnnotation createMarkerAnnotation(IMarker marker) {
if (JavaMarkerAnnotation.isJavaAnnotation(marker))
return new JavaMarkerAnnotation(marker);
return super.createMarkerAnnotation(marker);
}
/*
* @see org.eclipse.jface.text.source.AnnotationModel#createAnnotationModelEvent()
*/
@Override
protected AnnotationModelEvent createAnnotationModelEvent() {
return new CompilationUnitAnnotationModelEvent(this, getResource());
}
protected Position createPositionFromProblem(IProblem problem) {
int start= problem.getSourceStart();
int end= problem.getSourceEnd();
if (start == -1 && end == -1)
return new Position(0);
if (start == -1)
return new Position(end);
if (end == -1)
return new Position(start);
int length= end - start + 1;
if (length < 0)
return null;
return new Position(start, length);
}
/*
* @see IProblemRequestor#beginReporting()
*/
@Override
public void beginReporting() {
ProblemRequestorState state= fProblemRequestorState.get();
if (state == null)
internalBeginReporting(false);
}
/*
* @see org.eclipse.jdt.internal.ui.text.java.IProblemRequestorExtension#beginReportingSequence()
*/
@Override
public void beginReportingSequence() {
ProblemRequestorState state= fProblemRequestorState.get();
if (state == null)
internalBeginReporting(true);
}
/**
* Sets up the infrastructure necessary for problem reporting.
*
* @param insideReportingSequence <code>true</code> if this method
* call is issued from inside a reporting sequence
*/
private void internalBeginReporting(boolean insideReportingSequence) {
if (fCompilationUnit != null && fCompilationUnit.getJavaProject().isOnClasspath(fCompilationUnit)) {
ProblemRequestorState state= new ProblemRequestorState();
state.fInsideReportingSequence= insideReportingSequence;
state.fReportedProblems= new ArrayList<>();
synchronized (getLockObject()) {
fProblemRequestorState.set(state);
++fStateCount;
}
}
}
/*
* @see IProblemRequestor#acceptProblem(IProblem)
*/
@Override
public void acceptProblem(IProblem problem) {
if (fIsHandlingTemporaryProblems || problem.getID() == JavaSpellingReconcileStrategy.SPELLING_PROBLEM_ID) {
ProblemRequestorState state= fProblemRequestorState.get();
if (state != null)
state.fReportedProblems.add(problem);
}
}
/*
* @see IProblemRequestor#endReporting()
*/
@Override
public void endReporting() {
ProblemRequestorState state= fProblemRequestorState.get();
if (state != null && !state.fInsideReportingSequence)
internalEndReporting(state);
}
/*
* @see org.eclipse.jdt.internal.ui.text.java.IProblemRequestorExtension#endReportingSequence()
*/
@Override
public void endReportingSequence() {
ProblemRequestorState state= fProblemRequestorState.get();
if (state != null && state.fInsideReportingSequence)
internalEndReporting(state);
}
private void internalEndReporting(ProblemRequestorState state) {
int stateCount= 0;
synchronized(getLockObject()) {
-- fStateCount;
stateCount= fStateCount;
fProblemRequestorState.remove();
}
if (stateCount == 0)
reportProblems(state.fReportedProblems);
}
/**
* Signals the end of problem reporting.
*
* @param reportedProblems the problems to report
*/
private void reportProblems(List<IProblem> reportedProblems) {
if (fProgressMonitor != null && fProgressMonitor.isCanceled())
return;
boolean temporaryProblemsChanged= false;
synchronized (getLockObject()) {
boolean isCanceled= false;
fPreviouslyOverlaid= fCurrentlyOverlaid;
fCurrentlyOverlaid= new ArrayList<>();
if (fGeneratedAnnotations.size() > 0) {
temporaryProblemsChanged= true;
removeAnnotations(fGeneratedAnnotations, false, true);
fGeneratedAnnotations.clear();
}
if (reportedProblems != null && reportedProblems.size() > 0) {
Iterator<IProblem> e= reportedProblems.iterator();
while (e.hasNext()) {
if (fProgressMonitor != null && fProgressMonitor.isCanceled()) {
isCanceled= true;
break;
}
IProblem problem= e.next();
Position position= createPositionFromProblem(problem);
if (position != null) {
try {
ProblemAnnotation annotation= new ProblemAnnotation(problem, fCompilationUnit);
overlayMarkers(position, annotation);
addAnnotation(annotation, position, false);
fGeneratedAnnotations.add(annotation);
temporaryProblemsChanged= true;
} catch (BadLocationException x) {
// ignore invalid position
}
}
}
}
removeMarkerOverlays(isCanceled);
fPreviouslyOverlaid= null;
}
if (temporaryProblemsChanged)
fireModelChanged();
}
private void removeMarkerOverlays(boolean isCanceled) {
if (isCanceled) {
fCurrentlyOverlaid.addAll(fPreviouslyOverlaid);
} else if (fPreviouslyOverlaid != null) {
Iterator<JavaMarkerAnnotation> e= fPreviouslyOverlaid.iterator();
while (e.hasNext()) {
JavaMarkerAnnotation annotation= e.next();
annotation.setOverlay(null);
}
}
}
/**
* Overlays Java marker annotation with problem annotation.
*
* @param annotation the Java marker annotation to attach a problem annotation to
* @param problemAnnotation the problem annotation
*/
private void setOverlay(JavaMarkerAnnotation annotation, ProblemAnnotation problemAnnotation) {
if (annotation.isProblem()) {
annotation.setOverlay(problemAnnotation);
fPreviouslyOverlaid.remove(annotation);
fCurrentlyOverlaid.add(annotation);
}
}
private void overlayMarkers(Position position, ProblemAnnotation problemAnnotation) {
for (JavaMarkerAnnotation annotation : getJavaMarkerAnnotations(position)) {
setOverlay(annotation, problemAnnotation);
}
}
/**
* Tells this annotation model to collect temporary problems from now on.
*/
private void startCollectingProblems() {
fGeneratedAnnotations.clear();
}
/**
* Tells this annotation model to no longer collect temporary problems.
*/
private void stopCollectingProblems() {
removeAnnotations(fGeneratedAnnotations, true, true);
fGeneratedAnnotations.clear();
}
/*
* @see IProblemRequestor#isActive()
*/
@Override
public synchronized boolean isActive() {
return fIsActive && fActiveThread == Thread.currentThread();
}
/*
* @see IProblemRequestorExtension#setProgressMonitor(IProgressMonitor)
*/
@Override
public void setProgressMonitor(IProgressMonitor monitor) {
fProgressMonitor= monitor;
}
/*
* @see IProblemRequestorExtension#setIsActive(boolean)
*/
@Override
public synchronized void setIsActive(boolean isActive) {
Assert.isLegal(!isActive || Display.getCurrent() == null); // must not be enabled from UI threads
fIsActive= isActive;
if (fIsActive)
fActiveThread= Thread.currentThread();
else
fActiveThread= null;
}
/*
* @see IProblemRequestorExtension#setIsHandlingTemporaryProblems(boolean)
* @since 3.1
*/
@Override
public void setIsHandlingTemporaryProblems(boolean enable) {
if (fIsHandlingTemporaryProblems != enable) {
fIsHandlingTemporaryProblems= enable;
if (fIsHandlingTemporaryProblems)
startCollectingProblems();
else
stopCollectingProblems();
}
}
private Iterable<JavaMarkerAnnotation> getJavaMarkerAnnotations(Position position) {
synchronized (getLockObject()) {
return fReverseMap.get(position);
}
}
/*
* @see AnnotationModel#addAnnotation(Annotation, Position, boolean)
*/
@Override
protected void addAnnotation(Annotation annotation, Position position, boolean fireModelChanged) throws BadLocationException {
super.addAnnotation(annotation, position, fireModelChanged);
if (annotation instanceof JavaMarkerAnnotation javaAnnotation) {
synchronized (getLockObject()) {
fReverseMap.put(position, javaAnnotation);
}
}
}
/*
* @see AnnotationModel#removeAllAnnotations(boolean)
*/
@Override
protected void removeAllAnnotations(boolean fireModelChanged) {
super.removeAllAnnotations(fireModelChanged);
synchronized (getLockObject()) {
fReverseMap.clear();
}
}
/*
* @see AnnotationModel#removeAnnotation(Annotation, boolean)
*/
@Override
protected void removeAnnotation(Annotation annotation, boolean fireModelChanged) {
Position position= getPosition(annotation);
if (annotation instanceof JavaMarkerAnnotation javaAnnotation) {
synchronized (getLockObject()) {
fReverseMap.remove(position, javaAnnotation);
}
}
super.removeAnnotation(annotation, fireModelChanged);
}
}
protected static class GlobalAnnotationModelListener implements IAnnotationModelListener, IAnnotationModelListenerExtension {
private ListenerList<IAnnotationModelListener> fListenerList;
public GlobalAnnotationModelListener() {
fListenerList= new ListenerList<>(ListenerList.IDENTITY);
}
/**
* @see IAnnotationModelListener#modelChanged(IAnnotationModel)
*/
@Override
public void modelChanged(IAnnotationModel model) {
for (IAnnotationModelListener listener : fListenerList) {
listener.modelChanged(model);
}
}
/**
* @see IAnnotationModelListenerExtension#modelChanged(AnnotationModelEvent)
*/
@Override
public void modelChanged(AnnotationModelEvent event) {
for (IAnnotationModelListener curr : fListenerList) {
if (curr instanceof IAnnotationModelListenerExtension) {
((IAnnotationModelListenerExtension) curr).modelChanged(event);
}
}
}
public void addListener(IAnnotationModelListener listener) {
fListenerList.add(listener);
}
public void removeListener(IAnnotationModelListener listener) {
fListenerList.remove(listener);
}
}
/** Preference key for temporary problems */
private final static String HANDLE_TEMPORARY_PROBLEMS= PreferenceConstants.EDITOR_EVALUTE_TEMPORARY_PROBLEMS;
/** Indicates whether the save has been initialized by this provider */
private boolean fIsAboutToSave= false;
/** The save policy used by this provider */
private ISavePolicy fSavePolicy;
/** Internal property changed listener */
private IPropertyChangeListener fPropertyListener;
/** Annotation model listener added to all created CU annotation models */
private GlobalAnnotationModelListener fGlobalAnnotationModelListener;
/**
* Element information of all connected elements with a fake CU but no file info.
* @since 3.2
*/
private final Map<Object, CompilationUnitInfo> fFakeCUMapForMissingInfo= new HashMap<>();
/**
* Constructor
*/
public CompilationUnitDocumentProvider() {
IDocumentProvider provider= new TextFileDocumentProvider();
provider= new ForwardingDocumentProvider(IJavaPartitions.JAVA_PARTITIONING, new JavaDocumentSetupParticipant(), provider);
setParentDocumentProvider(provider);
fGlobalAnnotationModelListener= new GlobalAnnotationModelListener();
fPropertyListener= event -> {
if (HANDLE_TEMPORARY_PROBLEMS.equals(event.getProperty()))
enableHandlingTemporaryProblems();
};
JavaPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(fPropertyListener);
}
/**
* Creates a compilation unit from the given file.
*
* @param file the file from which to create the compilation unit
* @return the fake compilation unit
*/
protected ICompilationUnit createCompilationUnit(IFile file) {
Object element= JavaCore.create(file);
if (element instanceof ICompilationUnit)
return (ICompilationUnit) element;
return null;
}
/*
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createEmptyFileInfo()
*/
@Override
protected FileInfo createEmptyFileInfo() {
return new CompilationUnitInfo();
}
/*
* @see org.eclipse.core.filebuffers.IAnnotationModelFactory#createAnnotationModel(org.eclipse.core.runtime.IPath)
* @since 3.4
*/
@Override
public IAnnotationModel createAnnotationModel(IPath path) {
IResource file= ResourcesPlugin.getWorkspace().getRoot().findMember(path);
if (file instanceof IFile)
return new CompilationUnitAnnotationModel(file);
return new AnnotationModel();
}
/*
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createFileInfo(java.lang.Object)
*/
@Override
protected FileInfo createFileInfo(Object element) throws CoreException {
ICompilationUnit original= null;
if (element instanceof IFileEditorInput) {
IFileEditorInput input= (IFileEditorInput) element;
original= createCompilationUnit(input.getFile());
if (original == null)
return null;
}
FileInfo info= super.createFileInfo(element);
if (!(info instanceof CompilationUnitInfo))
return null;
if (original == null)
original= createFakeCompiltationUnit(element, false);
if (original == null)
return null;