Skip to main content
summaryrefslogtreecommitdiffstats
blob: 38055949e718732b69c27892e75df4071483b525 (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
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
/*******************************************************************************
 * Copyright (c) 2000, 2013 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
 *     Tom Eicher (Avaloq Evolution AG) - block selection mode
 *     Tom Hofmann (Perspectix AG) - bug 297572
 *******************************************************************************/
package org.eclipse.fx.text.ui.source;

import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.eclipse.fx.text.hover.HoverInfo;
import org.eclipse.fx.text.ui.Feature;
import org.eclipse.fx.text.ui.ITextViewerExtension2;
import org.eclipse.fx.text.ui.TextViewer;
import org.eclipse.fx.text.ui.contentassist.ContentAssistant;
import org.eclipse.fx.text.ui.contentassist.IContentAssistant;
import org.eclipse.fx.text.ui.internal.AnnotationModelSupport;
import org.eclipse.fx.text.ui.internal.SimpleSmartIndent;
import org.eclipse.fx.text.ui.internal.WrappedLineRulerAnnotationPresenter;
import org.eclipse.fx.text.ui.internal.WrappedTextAnnotationPresenter;
import org.eclipse.fx.text.ui.presentation.IPresentationReconciler;
import org.eclipse.fx.text.ui.reconciler.IReconciler;
import org.eclipse.fx.ui.controls.styledtext.VerifyEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ISynchronizable;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelExtension;

public class SourceViewer extends TextViewer implements ISourceViewer, ISourceViewerExtension, ISourceViewerExtension2, ISourceViewerExtension3, ISourceViewerExtension4 {

	private IPresentationReconciler fPresentationReconciler;
	private IReconciler fReconciler;
	private IAnnotationModel fVisualAnnotationModel;
	protected final static Object MODEL_ANNOTATION_MODEL= new Object();

	private Annotation fRangeIndicator;


	private BiFunction<IDocument, Integer, Set<HoverInfo>> documentHoverInfoLookup = null;
	private Function<Annotation, Set<HoverInfo>> annotationHoverInfoLookup = null;

	@Override
	public void configure(SourceViewerConfiguration configuration) {
		if (getTextWidget() == null)
			return;

//		installPlugin(new InvisibleCharacterPlugin());

		if (configuration.getFeatures() != null) {
			getFeatures().bind(configuration.getFeatures());
		}

		setUndoManager(configuration.getUndoManager(this));

		getTextWidget().getStyleClass().add(configuration.getStyleclassName());

		this.documentHoverInfoLookup = configuration::getDocumentHoverInfo;
		this.annotationHoverInfoLookup = configuration::getAnnotationHoverInfo;

//		if( configuration.getDefaultStylesheet().getValue() != null ) {
//			getTextWidget().getStylesheets().add(configuration.getDefaultStylesheet().getValue().toExternalForm());
//		}
//
//		configuration.getDefaultStylesheet().addListener((obs,oldVal,newVal) -> {
//			if( oldVal != null ) {
//				getTextWidget().getStylesheets().remove(oldVal.toExternalForm());
//			}
//			if( newVal != null ) {
//				getTextWidget().getStylesheets().add(newVal.toExternalForm());
//			}
//		});

		if (configuration.getContentAssist() instanceof ContentAssistant) {
			((ContentAssistant)configuration.getContentAssist()).setAutoTriggers(configuration.getContentAssistAutoTriggers());
		}

		setDocumentPartitioning(configuration.getConfiguredDocumentPartitioning(this));

		// install content type independent plug-ins
		fPresentationReconciler= configuration.getPresentationReconciler(this);
		if (fPresentationReconciler != null)
			fPresentationReconciler.install(this);

		fReconciler= configuration.getReconciler(this);
		if (fReconciler != null)
			fReconciler.install(this);

		IContentAssistant assist = configuration.getContentAssist();
		if( assist != null ) {
			assist.install(this);
		}

		//TODO This is complete different to JFace-Text
		IAnnotationModel annotationModel = configuration.getAnnotationModel();
		if( annotationModel != null ) {
//			getTextWidget().setLineRulerGraphicNodeFactory(this::annotationFactory);

			// NFO: the line ruler is updated by changes to the annotation model

//			annotationModel.addAnnotationModelListener(new IAnnotationModelListener() {
//
//				private boolean scheduled = false;
//
//				@Override
//				public void modelChanged(IAnnotationModel model) {
//					if( ! this.scheduled ) {
//						this.scheduled = true;
//						Platform.runLater(() -> {
//							this.scheduled = false;
//							getTextWidget().refreshLineRuler();
//						});
//					}
//				}
//
//			});
		}

		if( configuration.getAnnotationPresenter() != null ) {
			// install presenters
			configuration.getAnnotationPresenter().getLineRulerAnnotationPresenter().forEach(p -> {
				System.err.println("adding line ruler presenter " + p);
				getTextWidget().getAnnotationPresenter().add(new WrappedLineRulerAnnotationPresenter(p));
			});
			configuration.getAnnotationPresenter().getTextAnnotationPresenter().forEach(p -> {
				getTextWidget().getAnnotationPresenter().add(new WrappedTextAnnotationPresenter(p));
			});
		}

//		presenterMap.putAll(configuration.getAnnotationPresenters().stream().collect(Collectors.toMap(p -> p.getType(), p -> p)));

		AnnotationPainter annotationPainter = configuration.getAnnotationPainter(this);
		if( annotationModel != null && annotationPainter != null ) {
			annotationModel.addAnnotationModelListener(annotationPainter);
			addTextPresentationListener(annotationPainter);
		}

		String[] types= configuration.getConfiguredContentTypes(this);
		for (int i= 0; i < types.length; i++) {
			String t = types[i];
			setTextHover(configuration.getTextHover(this, t), t, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
		}

		// register annotation model with text widget
		if (annotationModel != null) {

			AnnotationModelSupport support = new AnnotationModelSupport(annotationModel, getTextWidget());
			support.install();

//			MarkerAnnotationPlugin markerAnnotationPlugin = new MarkerAnnotationPlugin(annotationModel, getTextWidget(), presenterMap);
//			markerAnnotationPlugin.install();

//			new AnnotationIconLineAnnotationPlugin(annotationModel, getTextWidget(),
//
//
//					(a)->presenterMap.get(a.getType()).getPresentation(a));

//			installPlugin(new AnnotationPlugin(annotationModel));
//			annotationModel.addAnnotationModelListener(new IAnnotationModelListener() {
//				@Override
//				public void modelChanged(IAnnotationModel model) {
//					System.err.println("Annotation model change!");
//
//					Map<String, StyledTextAnnotation> toAdd = new HashMap<>();
//					Map<String, StyledTextAnnotation> toRemove = new HashMap<>();
//
//
//					Map<String, StyledTextAnnotation> current = new HashMap<>();
//					for (StyledTextAnnotation n : getTextWidget().getAnnotations()) {
//						current.put(n.getId(), n);
//						toRemove.put(n.getId(), n);
//					}
//
//					Iterator it = model.getAnnotationIterator();
//					while (it.hasNext()) {
//						Annotation next = (Annotation) it.next();
//						Position pos = model.getPosition(next);
//						String id = Integer.toHexString(next.hashCode());
//						if (pos != null) {
//
//							if (current.containsKey(id)) {
//								toRemove.remove(id);
//							}
//							else {
//								StyledTextAnnotation s = new StyledTextAnnotation() {
//									@Override
//									public String getId() {
//										return id;
//									}
//
//									@Override
//									public String getType() {
//										return next.getType();
//									}
//
//									@Override
//									public String getText() {
//										return next.getText();
//									}
//
//									@Override
//									public int getStartOffset() {
//										return pos.offset;
//									}
//
//									@Override
//									public int getLength() {
//										return pos.length;
//									}
//								};
//								toAdd.put(id,  s);
//							}
//
//						}
//					}
//					getTextWidget().getAnnotations().removeAll(toRemove.values());
//					getTextWidget().getAnnotations().addAll(toAdd.values());
//				}
//
//			});
		}


		new SimpleSmartIndent(this);

	}

//	private Node annotationFactory(StyledTextLine l) {
//		//TODO Should use IAnnotationExtension2
//		@SuppressWarnings("unchecked")
//		Iterator<Annotation> annotationIterator = this.fVisualAnnotationModel.getAnnotationIterator();
//		while( annotationIterator.hasNext() ) {
//			Annotation a = annotationIterator.next();
//			Position position = this.fVisualAnnotationModel.getPosition(a);
//
//			if( l.getLineIndex() == getTextWidget().getContent().getLineAtOffset(position.offset) ) {
//				AnnotationPresenter annotationPresenter = this.presenterMap.get(a.getType());
//				if( annotationPresenter != null ) {
//					return annotationPresenter.getPresentation(a);
//				}
//				return null;
//			}
//		}
//		return null;
//	}

	@Override
	public Set<HoverInfo> getHoverInfo(int offset) {
		if (this.documentHoverInfoLookup != null) {
			return this.documentHoverInfoLookup.apply(getDocument(), offset);
		}
		return super.getHoverInfo(offset);
	}

	@Override
	public Set<HoverInfo> getHoverInfo(Annotation annotation) {
		if (this.annotationHoverInfoLookup != null) {
			return this.annotationHoverInfoLookup.apply(annotation);
		}
		return super.getHoverInfo(annotation);
	}

	/**
	 * Disposes the visual annotation model.
	 *
	 * @since 3.1
	 */
	protected void disposeVisualAnnotationModel() {
		if (this.fVisualAnnotationModel != null) {
			if (getDocument() != null)
				this.fVisualAnnotationModel.disconnect(getDocument());

			if ( this.fVisualAnnotationModel instanceof IAnnotationModelExtension)
				((IAnnotationModelExtension)this.fVisualAnnotationModel).removeAnnotationModel(MODEL_ANNOTATION_MODEL);

			this.fVisualAnnotationModel= null;
		}
	}

	/**
	 * Creates the visual annotation model on top of the given annotation model.
	 *
	 * @param annotationModel the wrapped annotation model
	 * @return the visual annotation model on top of the given annotation model
	 * @since 3.0
	 */
	@SuppressWarnings("static-method")
	protected IAnnotationModel createVisualAnnotationModel(IAnnotationModel annotationModel) {
		IAnnotationModelExtension model= new AnnotationModel();
		model.addAnnotationModel(MODEL_ANNOTATION_MODEL, annotationModel);
		return (IAnnotationModel) model;
	}

	public void setDocument(IDocument document, IAnnotationModel annotationModel) {
		setDocument(document, annotationModel, -1, -1);
	}

	@Override
	public void setDocument(IDocument document, IAnnotationModel annotationModel, int modelRangeOffset, int modelRangeLength) {
		disposeVisualAnnotationModel();

		if (annotationModel != null && document != null) {
			this.fVisualAnnotationModel= createVisualAnnotationModel(annotationModel);

			// Make sure the visual model uses the same lock as the underlying model
			if (annotationModel instanceof ISynchronizable && this.fVisualAnnotationModel instanceof ISynchronizable) {
				ISynchronizable sync= (ISynchronizable)this.fVisualAnnotationModel;
				sync.setLockObject(((ISynchronizable)annotationModel).getLockObject());
			}

			this.fVisualAnnotationModel.connect(document);
		}

		if (modelRangeOffset == -1 && modelRangeLength == -1)
			super.setDocument(document);
		else
			super.setDocument(document, modelRangeOffset, modelRangeLength);
	}

	@Override
	public IAnnotationModel getAnnotationModel() {
		if (this.fVisualAnnotationModel instanceof IAnnotationModelExtension) {
			IAnnotationModelExtension extension= (IAnnotationModelExtension) this.fVisualAnnotationModel;
			return extension.getAnnotationModel(MODEL_ANNOTATION_MODEL);
		}
		return null;
	}

	public IAnnotationModel getVisualAnnotationModel() {
		return this.fVisualAnnotationModel;
	}

	public void setRangeIndicator(Annotation rangeIndicator) {
		this.fRangeIndicator= rangeIndicator;
	}

	public IRegion getRangeIndication() {
		if (this.fRangeIndicator != null && this.fVisualAnnotationModel != null) {
			Position position= this.fVisualAnnotationModel.getPosition(this.fRangeIndicator);
			if (position != null)
				return new Region(position.getOffset(), position.getLength());
		}

		return null;
	}

}

Back to the top