/
ClojureSourceViewer.java
381 lines (312 loc) · 12.6 KB
/
ClojureSourceViewer.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
/*******************************************************************************
* Copyright (c) 2009 Stephan Muehlstrasser.
* 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:
* Stephan Muehlstrasser - initial implementation
*******************************************************************************/
package ccw.editors.antlrbased;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextInputListener;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IOverviewRuler;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import ccw.CCWPlugin;
import ccw.ClojureCore;
import ccw.repl.REPLView;
import ccw.util.DisplayUtil;
public abstract class ClojureSourceViewer extends ProjectionViewer implements
IClojureEditor, IPropertyChangeListener {
/**
* The preference store.
*/
private IPreferenceStore fPreferenceStore;
/**
* This viewer's foreground color.
*/
private Color fForegroundColor;
/**
* The viewer's background color.
*/
private Color fBackgroundColor;
/**
* This viewer's selection foreground color.
*/
private Color fSelectionForegroundColor;
/**
* The viewer's selection background color.
*/
private Color fSelectionBackgroundColor;
/**
* The source viewer configuration. Needed for property change events
* for reconfiguring.
*/
private ClojureSourceViewerConfiguration fConfiguration;
/**
* Is this source viewer configured?
*/
private boolean fIsConfigured;
private boolean inEscapeSequence;
public ClojureSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles, IPreferenceStore store) {
super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
setPreferenceStore(store);
getTextWidget().addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.character == SWT.ESC) {
inEscapeSequence = true;
updateTabsToSpacesConverter();
}
}
public void keyReleased(KeyEvent e) {
if (inEscapeSequence && !(e.character == SWT.ESC)) {
inEscapeSequence = false;
updateTabsToSpacesConverter();
}
}
});
addTextInputListener(new ITextInputListener() {
public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
if (newInput != null) {
newInput.addDocumentListener(parseTreeConstructorDocumentListener);
updateParseRef(newInput.get());
}
}
public void inputDocumentAboutToBeChanged(IDocument oldInput,
IDocument newInput) {
if (oldInput != null)
oldInput.removeDocumentListener(parseTreeConstructorDocumentListener);
}
});
}
public void propertyChange(PropertyChangeEvent event) {
if (fConfiguration != null) {
ClojureSourceViewerConfiguration tmp = fConfiguration;
unconfigure();
initializeViewerColors();
tmp.initTokenScanner();
configure(tmp);
}
}
/**
* Sets the preference store on this viewer.
*
* @param store the preference store
*
* @since 3.0
*/
public void setPreferenceStore(IPreferenceStore store) {
if (fIsConfigured && fPreferenceStore != null)
fPreferenceStore.removePropertyChangeListener(this);
fPreferenceStore= store;
if (fIsConfigured && fPreferenceStore != null) {
fPreferenceStore.addPropertyChangeListener(this);
initializeViewerColors();
}
}
public void configure(SourceViewerConfiguration configuration) {
super.configure(configuration);
if (fPreferenceStore != null) {
fPreferenceStore.addPropertyChangeListener(this);
initializeViewerColors();
}
if (configuration instanceof ClojureSourceViewerConfiguration)
fConfiguration = (ClojureSourceViewerConfiguration) configuration;
fIsConfigured= true;
}
/**
* Creates a color from the information stored in the given preference store.
* Returns <code>null</code> if there is no such information available.
*
* @param store the store to read from
* @param key the key used for the lookup in the preference store
* @param display the display used create the color
* @return the created color according to the specification in the preference store
*/
static public Color createColor(IPreferenceStore store, String key, Display display) {
RGB rgb = null;
if (store.contains(key)) {
if (store.isDefault(key))
rgb = PreferenceConverter.getDefaultColor(store, key);
else
rgb = PreferenceConverter.getColor(store, key);
if (rgb != null)
return new Color(display, rgb);
}
return null;
}
public void initializeViewerColors() {
if (fPreferenceStore != null) {
StyledText styledText= getTextWidget();
// ----------- foreground color --------------------
Color color= fPreferenceStore.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND_SYSTEM_DEFAULT)
? null
: createColor(fPreferenceStore, AbstractTextEditor.PREFERENCE_COLOR_FOREGROUND, styledText.getDisplay());
styledText.setForeground(color);
if (fForegroundColor != null)
fForegroundColor.dispose();
fForegroundColor= color;
// ---------- background color ----------------------
color= fPreferenceStore.getBoolean(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)
? null
: createColor(fPreferenceStore, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND, styledText.getDisplay());
styledText.setBackground(color);
if (fBackgroundColor != null)
fBackgroundColor.dispose();
fBackgroundColor= color;
// ----------- selection foreground color --------------------
color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR)
? null
: createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, styledText.getDisplay());
styledText.setSelectionForeground(color);
if (fSelectionForegroundColor != null)
fSelectionForegroundColor.dispose();
fSelectionForegroundColor= color;
// ---------- selection background color ----------------------
color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR)
? null
: createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, styledText.getDisplay());
styledText.setSelectionBackground(color);
if (fSelectionBackgroundColor != null)
fSelectionBackgroundColor.dispose();
fSelectionBackgroundColor= color;
CCWPlugin.registerEditorColors(fPreferenceStore, styledText.getForeground().getRGB());
}
}
/*
* @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
* @since 3.0
*/
public void unconfigure() {
if (fForegroundColor != null) {
fForegroundColor.dispose();
fForegroundColor= null;
}
if (fBackgroundColor != null) {
fBackgroundColor.dispose();
fBackgroundColor= null;
}
if (fSelectionForegroundColor != null) {
fSelectionForegroundColor.dispose();
fSelectionForegroundColor= null;
}
if (fSelectionBackgroundColor != null) {
fSelectionBackgroundColor.dispose();
fSelectionBackgroundColor= null;
}
if (fPreferenceStore != null)
fPreferenceStore.removePropertyChangeListener(this);
super.unconfigure();
fIsConfigured= false;
fConfiguration = null;
}
/** This is manipulated by clojure functions.
* It's a ref, holding a map {:text "the raw text file" :parser parser}
* where state is a future holding the parser's state
*/
private Object parseRef;
private IDocumentListener parseTreeConstructorDocumentListener = new IDocumentListener() {
public void documentAboutToBeChanged(DocumentEvent event) { }
public void documentChanged(DocumentEvent event) {
updateParseRef(event.getDocument().get());
}
};
private void updateParseRef (String text) {
boolean firstTime = (parseRef == null);
parseRef = EditorSupport.updateParseRef(text, parseRef);
if (firstTime) {
EditorSupport.startWatchParseRef(parseRef, this);
}
}
public Object getParsed () {
if (parseRef == null) {
updateParseRef(getDocument().get());
}
return EditorSupport.getParser(getDocument().get(), parseRef);
}
private boolean structuralEditionPossible = true;
public void setStructuralEditionPossible(final boolean state) {
structuralEditionPossible = state;
syncWithStructuralEditionPossibleState();
}
private void syncWithStructuralEditionPossibleState() {
DisplayUtil.asyncExec(new Runnable() {
public void run() {
getTextWidget().setBackground(
structuralEditionPossible ? null : Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
getTextWidget().setToolTipText(structuralEditionPossible ? null : "Unparseable source code. Structural Edition temporarily disabled.");
}
});
}
public IRegion getSignedSelection () {
StyledText text = getTextWidget();
Point selection = text.getSelectionRange();
if (text.getCaretOffset() == selection.x) {
selection.x = selection.x + selection.y;
selection.y = -selection.y;
}
selection.x = widgetOffset2ModelOffset(selection.x);
return new Region(selection.x, selection.y);
}
public IRegion getUnSignedSelection () {
StyledText text = getTextWidget();
Point selection = text.getSelectionRange();
selection.x = widgetOffset2ModelOffset(selection.x);
return new Region(selection.x, selection.y);
}
public void selectAndReveal(int start, int length) {
setSelection(new TextSelection(start, length), true);
}
public boolean isStructuralEditingEnabled() {
return true;
}
public boolean isInEscapeSequence () {
return inEscapeSequence;
}
public String getDeclaringNamespace() {
return ClojureCore.getDeclaringNamespace(getDocument().get());
}
public IJavaProject getAssociatedProject() {
return null;
}
public REPLView getCorrespondingREPL () {
// this gets overridden in REPLView as appropriate so that the toolConnection there gets returned
return null;
}
public void updateTabsToSpacesConverter () {}
// TODO get rid of this way of handling document initialization
@Override
public void setDocument(IDocument document,
IAnnotationModel annotationModel, int modelRangeOffset,
int modelRangeLength) {
super.setDocument(document, annotationModel, modelRangeOffset, modelRangeLength);
if (document != null) {
updateParseRef(document.get());
}
}
}