This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
/
AbstractLangEditor.java
258 lines (202 loc) · 9.03 KB
/
AbstractLangEditor.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
/*******************************************************************************
* Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.editor;
import static melnorme.utilbox.core.CoreUtil.array;
import static melnorme.utilbox.core.CoreUtil.assertInstance;
import java.util.ArrayList;
import java.util.List;
import melnorme.lang.ide.core.TextSettings_Actual;
import melnorme.lang.ide.ui.EditorSettings_Actual;
import melnorme.lang.ide.ui.EditorSettings_Actual.EditorPrefConstants;
import melnorme.lang.ide.ui.LangUIPlugin;
import melnorme.lang.ide.ui.LangUIPlugin_Actual;
import melnorme.lang.ide.ui.editor.actions.GotoMatchingBracketManager;
import melnorme.lang.ide.ui.editor.text.LangPairMatcher;
import melnorme.lang.ide.ui.text.AbstractLangSourceViewerConfiguration;
import melnorme.utilbox.misc.ArrayUtil;
import org.eclipse.cdt.internal.ui.editor.EclipsePreferencesAdapter;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.texteditor.ChainedPreferenceStore;
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
public abstract class AbstractLangEditor extends TextEditorExt {
public AbstractLangEditor() {
super();
}
@Override
protected void initializeEditor() {
super.initializeEditor();
initialize_setContextMenuIds();
}
/* ----------------- actions init ----------------- */
@Override
protected void initializeKeyBindingScopes() {
setKeyBindingScopes(array(EditorSettings_Actual.EDITOR_CONTEXT_ID));
}
protected void initialize_setContextMenuIds() {
setEditorContextMenuId(LangUIPlugin_Actual.EDITOR_CONTEXT);
setRulerContextMenuId(LangUIPlugin_Actual.RULER_CONTEXT);
}
/* ----------------- input ----------------- */
@Override
protected void doSetInput(IEditorInput input) throws CoreException {
super.doSetInput(input);
SourceViewer sourceViewer = getSourceViewer_();
if(sourceViewer == null) {
changePreferenceStore(createCombinedPreferenceStore(input));
} else {
getSourceViewerDecorationSupport(sourceViewer).uninstall();
sourceViewer.unconfigure();
changePreferenceStore(createCombinedPreferenceStore(input));
sourceViewer.configure(getSourceViewerConfiguration());
getSourceViewerDecorationSupport(sourceViewer).install(getPreferenceStore());
}
IDocument doc = getDocumentProvider().getDocument(input);
// Setup up partitioning if not set. It can happen if opening non-language files in the language editor.
TextSettings_Actual.createDocumentSetupHelper().setupPartitioningIfNotSet(doc);
internalDoSetInput(input);
}
protected void changePreferenceStore(IPreferenceStore store) {
super.setPreferenceStore(store);
setSourceViewerConfiguration(createSourceViewerConfiguration());
}
@SuppressWarnings("unused")
protected void internalDoSetInput(IEditorInput input) {
}
protected abstract AbstractLangSourceViewerConfiguration createSourceViewerConfiguration();
public AbstractLangSourceViewerConfiguration getSourceViewerConfiguration_asLang() {
SourceViewerConfiguration svc = getSourceViewerConfiguration();
if(svc instanceof AbstractLangSourceViewerConfiguration) {
return (AbstractLangSourceViewerConfiguration) svc;
}
return null;
}
protected IPreferenceStore createCombinedPreferenceStore(IEditorInput input) {
List<IPreferenceStore> stores = new ArrayList<IPreferenceStore>(4);
IProject project = EditorUtils.getAssociatedProject(input);
if (project != null) {
stores.add(new EclipsePreferencesAdapter(new ProjectScope(project), LangUIPlugin.PLUGIN_ID));
}
stores.add(LangUIPlugin.getInstance().getPreferenceStore());
stores.add(LangUIPlugin.getInstance().getCorePreferenceStore());
alterCombinedPreferenceStores_beforeEditorsUI(stores);
stores.add(EditorsUI.getPreferenceStore());
return new ChainedPreferenceStore(ArrayUtil.createFrom(stores, IPreferenceStore.class));
}
@SuppressWarnings("unused")
protected void alterCombinedPreferenceStores_beforeEditorsUI(List<IPreferenceStore> stores) {
}
@Override
protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
AbstractLangSourceViewerConfiguration langSVC = getSourceViewerConfiguration_asLang();
if(langSVC != null) {
langSVC.handlePropertyChangeEvent(event);
}
ISourceViewerExt sourceViewer = getSourceViewer_asExt();
if(sourceViewer != null) {
sourceViewer.handlePropertyChangeEvent_2(event, getPreferenceStore());
}
super.handlePreferenceStoreChanged(event);
}
@Override
protected boolean affectsTextPresentation(PropertyChangeEvent event) {
AbstractLangSourceViewerConfiguration langSVC = getSourceViewerConfiguration_asLang();
if(langSVC != null && langSVC.affectsTextPresentation(event)) {
return true;
}
return super.affectsTextPresentation(event);
}
/* ----------------- create controls ----------------- */
@Override
public int getOrientation() {
return SWT.LEFT_TO_RIGHT; // Always left to right,
// also, this fixes bug super.getOrientation(), which can return SWT.NONE (but shouldn't)
}
@Override
protected final ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
SourceViewer viewer = doCreateSourceViewer(parent, ruler, styles);
assertInstance(viewer, SourceViewer.class);
assertInstance(viewer, ISourceViewerExt.class);
return viewer;
}
public SourceViewer getSourceViewer_() {
return (SourceViewer) getSourceViewer();
}
public ISourceViewerExt getSourceViewer_asExt() {
return (ISourceViewerExt) getSourceViewer();
}
protected SourceViewer doCreateSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
IPreferenceStore store = getPreferenceStore();
SourceViewer viewer =
new LangSourceViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles, store);
// ensure decoration support has been created and configured.
getSourceViewerDecorationSupport(viewer);
return viewer;
}
@Override
public void createPartControl(Composite parent) {
super.createPartControl(parent);
editorSelChangedListener.install(getSelectionProvider());
}
/* ----------------- other ----------------- */
@Override
protected boolean isTabsToSpacesConversionEnabled() {
return false;
}
protected final AbstractSelectionChangedListener editorSelChangedListener = new AbstractSelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
handleEditorSelectionChanged(event);
}
};
@SuppressWarnings("unused")
protected void handleEditorSelectionChanged(SelectionChangedEvent event) {
}
/* ----------------- Bracket matcher ----------------- */
protected final LangPairMatcher fBracketMatcher = addOwned(init_createBracketMatcher());
protected final GotoMatchingBracketManager gotoMatchingBracketManager = init_createGoToMatchingBracketManager();
protected abstract LangPairMatcher init_createBracketMatcher();
protected GotoMatchingBracketManager init_createGoToMatchingBracketManager() {
return new GotoMatchingBracketManager(this);
}
public final LangPairMatcher getBracketMatcher() {
return fBracketMatcher;
}
public final GotoMatchingBracketManager getGotoMatchingBracketManager() {
return gotoMatchingBracketManager;
}
@Override
protected void configureSourceViewerDecorationSupport(SourceViewerDecorationSupport support) {
configureBracketMatcher(support);
super.configureSourceViewerDecorationSupport(support);
}
protected void configureBracketMatcher(SourceViewerDecorationSupport support) {
support.setCharacterPairMatcher(fBracketMatcher);
support.setMatchingCharacterPainterPreferenceKeys(
EditorPrefConstants.MATCHING_BRACKETS,
EditorPrefConstants.MATCHING_BRACKETS_COLOR,
EditorPrefConstants.HIGHLIGHT_BRACKET_AT_CARET_LOCATION,
EditorPrefConstants.ENCLOSING_BRACKETS);
}
}