Skip to main content
summaryrefslogtreecommitdiffstats
blob: e4f662f85759357510eaf4f6b280c1be8b4c4505 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.search2.internal.ui.text;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;

import org.eclipse.core.resources.IFile;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.IAnnotationModel;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

import org.eclipse.search.ui.ISearchResult;
import org.eclipse.search.ui.ISearchResultListener;
import org.eclipse.search.ui.SearchResultEvent;
import org.eclipse.search.ui.text.AbstractTextSearchResult;
import org.eclipse.search.ui.text.FilterUpdateEvent;
import org.eclipse.search.ui.text.IEditorMatchAdapter;
import org.eclipse.search.ui.text.ISearchEditorAccess;
import org.eclipse.search.ui.text.Match;
import org.eclipse.search.ui.text.MatchEvent;
import org.eclipse.search.ui.text.RemoveAllEvent;

public class EditorAnnotationManager implements ISearchResultListener {

	private ArrayList<AbstractTextSearchResult> fResults;
	private IEditorPart fEditor;
	private Highlighter fHighlighter; // initialized lazy

	public static final int HIGHLLIGHTER_ANY= 0;
	public static final int HIGHLIGHTER_MARKER= 1;
	public static final int HIGHLIGHTER_ANNOTATION= 2;
	public static final int HIGHLIGHTER_EDITOR_ACCESS= 3;
	private static int fgHighlighterType= HIGHLLIGHTER_ANY;


	public EditorAnnotationManager(IEditorPart editorPart) {
		Assert.isNotNull(editorPart);
		fEditor= editorPart;
		fHighlighter= null; // lazy initialization
		fResults= new ArrayList<>(3);
	}


	public static final void debugSetHighlighterType(int type) {
		fgHighlighterType= type;
	}


	void dispose() {
		removeAllAnnotations();
		if (fHighlighter != null)
			fHighlighter.dispose();

		for (int i= 0; i < fResults.size(); i++) {
			fResults.get(i).removeListener(this);
		}
		fResults.clear();
	}

	public synchronized void doEditorInputChanged() {
		removeAllAnnotations();

		if (fHighlighter != null) {
			fHighlighter.dispose();
			fHighlighter= null;
		}

		for (int i= 0; i < fResults.size(); i++) {
			AbstractTextSearchResult curr= fResults.get(i);
			addAnnotations(curr);
		}
	}

	public synchronized void setSearchResults(List<AbstractTextSearchResult> results) {
		removeAllAnnotations();
		for (int i= 0; i < fResults.size(); i++) {
			fResults.get(i).removeListener(this);
		}
		fResults.clear();

		for (int i= 0; i < results.size(); i++) {
			addSearchResult(results.get(i));
		}
	}

	public synchronized void addSearchResult(AbstractTextSearchResult result) {
		fResults.add(result);
		result.addListener(this);
		addAnnotations(result);
	}

	public synchronized void removeSearchResult(AbstractTextSearchResult result) {
		fResults.remove(result);
		result.removeListener(this);
		removeAnnotations(result);
	}


	@Override
	public synchronized void searchResultChanged(SearchResultEvent e) {
		ISearchResult searchResult= e.getSearchResult();
		if (searchResult instanceof AbstractTextSearchResult) {
			AbstractTextSearchResult result= (AbstractTextSearchResult) searchResult;
			if (e instanceof MatchEvent) {
				MatchEvent me= (MatchEvent) e;
				Match[] matchesInEditor= getMatchesInEditor(me.getMatches(), result);
				if (matchesInEditor != null) {
					if (me.getKind() == MatchEvent.ADDED) {
						addAnnotations(matchesInEditor);
					} else {
						removeAnnotations(matchesInEditor);
					}
				}
			} else if (e instanceof RemoveAllEvent) {
				removeAnnotations(result);
			} else if (e instanceof FilterUpdateEvent) {
				Match[] matchesInEditor= getMatchesInEditor(((FilterUpdateEvent) e).getUpdatedMatches(), result);
				if (matchesInEditor != null) {
					removeAnnotations(matchesInEditor);
					addAnnotations(matchesInEditor);
				}
			}
		}
	}

	private Match[] getMatchesInEditor(Match[] matches, AbstractTextSearchResult result) {
		IEditorMatchAdapter adapter= result.getEditorMatchAdapter();
		if (adapter == null) {
			return null;
		}

		// optimize the array-length == 1 case (most common)
		if (matches.length == 1) {
			return adapter.isShownInEditor(matches[0], fEditor) ? matches : null;
		}

		ArrayList<Match> matchesInEditor= null; // lazy initialization
		for (Match curr : matches) {
			if (adapter.isShownInEditor(curr, fEditor)) {
				if (matchesInEditor == null) {
					matchesInEditor= new ArrayList<>();
				}
				matchesInEditor.add(curr);
			}
		}
		if (matchesInEditor != null) {
			return matchesInEditor.toArray(new Match[matchesInEditor.size()]);
		}
		return null;
	}

	private void removeAllAnnotations() {
		if (fHighlighter != null)
			fHighlighter.removeAll();
	}

	private Highlighter createHighlighter() {
		IEditorPart editor= fEditor;
		if (fgHighlighterType != HIGHLLIGHTER_ANY) {
			return debugCreateHighlighter(editor);
		}
		ISearchEditorAccess access= editor.getAdapter(ISearchEditorAccess.class);
		if (access != null)
			return new EditorAccessHighlighter(access);
		IAnnotationModel model= getAnnotationModel(editor);
		if (model != null)
			return new AnnotationHighlighter(model, getDocument(editor));
		IEditorInput input= editor.getEditorInput();
		if (input instanceof IFileEditorInput) {
			IFile file= ((IFileEditorInput)input).getFile();
			if (file != null)
				return new MarkerHighlighter(file);
		}
		return new Highlighter(); // does nothing
	}

	private static Highlighter debugCreateHighlighter(IEditorPart editor) {
		if (fgHighlighterType == HIGHLIGHTER_ANNOTATION) {
			IAnnotationModel model= getAnnotationModel(editor);
			if (model != null)
				return new AnnotationHighlighter(model, getDocument(editor));
		} else if (fgHighlighterType == HIGHLIGHTER_MARKER) {
			IEditorInput input= editor.getEditorInput();
			if (input instanceof IFileEditorInput) {
				IFile file= ((IFileEditorInput)input).getFile();
				if (file != null)
					return new MarkerHighlighter(file);
			}

		} else if (fgHighlighterType == HIGHLIGHTER_EDITOR_ACCESS) {
			ISearchEditorAccess access= editor.getAdapter(ISearchEditorAccess.class);
			if (access != null)
				return new EditorAccessHighlighter(access);
		}
		return null;
	}

	private void addAnnotations(AbstractTextSearchResult result) {
		IEditorMatchAdapter matchAdapter= result.getEditorMatchAdapter();
		if (matchAdapter == null)
			return;
		Match[] matches= matchAdapter.computeContainedMatches(result, fEditor);
		if (matches == null || matches.length == 0)
			return;
		addAnnotations(matches);
	}

	private void removeAnnotations(AbstractTextSearchResult result) {
		removeAllAnnotations();

		for (int i= 0; i < fResults.size(); i++) {
			AbstractTextSearchResult curr= fResults.get(i);
			if (curr != result) {
				addAnnotations(curr);
			}
		}
	}

	private void addAnnotations(Match[] matches) {
		if (fHighlighter == null) {
			fHighlighter= createHighlighter();
		}
		fHighlighter.addHighlights(matches);
	}

	private void removeAnnotations(Match[] matches) {
		if (fHighlighter != null)
			fHighlighter.removeHighlights(matches);
	}

	private static IAnnotationModel getAnnotationModel(IWorkbenchPart part) {
		IAnnotationModel model= null;
		model= part.getAdapter(IAnnotationModel.class);
		if (model == null) {
			ITextEditor textEditor= null;
			if (part instanceof ITextEditor) {
				textEditor= (ITextEditor) part;
			}
			if (textEditor != null) {
				IDocumentProvider dp= textEditor.getDocumentProvider();
				if (dp != null)
					model= dp.getAnnotationModel(textEditor.getEditorInput());
			}
		}
		return model;
	}

	private static IDocument getDocument(IWorkbenchPart part) {
		IDocument doc= null;
		doc= part.getAdapter(IDocument.class);
		if (doc == null) {
			ITextEditor textEditor= null;
			if (part instanceof ITextEditor) {
				textEditor= (ITextEditor) part;
			}
			if (textEditor != null) {
				IDocumentProvider dp= textEditor.getDocumentProvider();
				if (dp != null)
					doc= dp.getDocument(textEditor.getEditorInput());
			}
		}
		return doc;
	}
}

Back to the top