This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
/
AbstractLangSourceViewerConfiguration.java
233 lines (183 loc) · 9.4 KB
/
AbstractLangSourceViewerConfiguration.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
/*******************************************************************************
* Copyright (c) 2015, 2015 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.text;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertUnreachable;
import static melnorme.utilbox.core.CoreUtil.array;
import static melnorme.utilbox.core.CoreUtil.tryCast;
import java.util.Map;
import melnorme.lang.ide.ui.CodeFormatterConstants;
import melnorme.lang.ide.ui.CodeFormatterConstants.IndentMode;
import melnorme.lang.ide.ui.EditorSettings_Actual;
import melnorme.lang.ide.ui.LangUIPlugin;
import melnorme.lang.ide.ui.LangUIPlugin_Actual;
import melnorme.lang.ide.ui.editor.AbstractLangEditor;
import melnorme.lang.ide.ui.editor.LangSourceViewer;
import melnorme.lang.ide.ui.editor.structure.LangOutlineInformationControl.OutlineInformationControlCreator;
import melnorme.lang.ide.ui.editor.structure.StructureElementInformationProvider;
import melnorme.lang.ide.ui.editor.text.LangReconciler;
import melnorme.lang.ide.ui.text.completion.CompletionProposalsGrouping;
import melnorme.lang.ide.ui.text.completion.ContentAssistPreferenceHandler;
import melnorme.lang.ide.ui.text.completion.ContentAssistantExt;
import melnorme.lang.ide.ui.text.completion.LangContentAssistProcessor;
import melnorme.lang.ide.ui.text.completion.LangContentAssistProcessor.ContentAssistCategoriesBuilder;
import melnorme.lang.ide.ui.text.util.AutoEditUtils;
import melnorme.utilbox.collections.Indexable;
import org.eclipse.cdt.ui.text.IColorManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.AbstractInformationControlManager;
import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.information.IInformationProvider;
import org.eclipse.jface.text.information.InformationPresenter;
import org.eclipse.jface.text.reconciler.AbstractReconciler;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
import org.eclipse.ui.texteditor.ITextEditor;
import _org.eclipse.jdt.internal.ui.text.CompositeReconcilingStrategy;
public abstract class AbstractLangSourceViewerConfiguration extends SimpleLangSourceViewerConfiguration {
protected final AbstractDecoratedTextEditor editor;
public AbstractLangSourceViewerConfiguration(IPreferenceStore preferenceStore, IColorManager colorManager,
AbstractDecoratedTextEditor editor) {
super(preferenceStore, colorManager);
this.editor = editor;
}
public AbstractDecoratedTextEditor getEditor() {
return editor;
}
public AbstractLangEditor getEditor_asLang() {
return tryCast(editor, AbstractLangEditor.class);
}
/* ----------------- Navigation operations ----------------- */
@Override
protected Map<String, ITextEditor> getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
Map<String, ITextEditor> targets = super.getHyperlinkDetectorTargets(sourceViewer);
targets.put(EditorSettings_Actual.EDITOR_CODE_TARGET, editor);
return targets;
}
public void installOutlinePresenter(final LangSourceViewer sourceViewer) {
final InformationPresenter presenter =
new InformationPresenter(getOutlinePresenterControlCreator(sourceViewer));
presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
presenter.setAnchor(AbstractInformationControlManager.ANCHOR_GLOBAL);
IInformationProvider provider = new StructureElementInformationProvider(getEditor());
for(String contentType : getConfiguredContentTypes(sourceViewer)) {
presenter.setInformationProvider(provider, contentType);
}
presenter.setSizeConstraints(50, 20, true, false);
presenter.install(sourceViewer);
sourceViewer.setOutlinePresenter(presenter);
}
protected IInformationControlCreator getOutlinePresenterControlCreator(
@SuppressWarnings("unused") ISourceViewer sourceViewer) {
return new OutlineInformationControlCreator(this);
}
/* ----------------- Modification operations ----------------- */
@Override
public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
return new String[] { getToggleCommentPrefix(), "" };
}
protected abstract String getToggleCommentPrefix();
@Override
public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
if(IDocument.DEFAULT_CONTENT_TYPE.equals(contentType)) {
return array(LangUIPlugin_Actual.createAutoEditStrategy(sourceViewer, contentType));
} else {
return super.getAutoEditStrategies(sourceViewer, contentType);
}
}
@Override
public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
IndentMode indentMode = CodeFormatterConstants.IndentMode.fromPrefStore();
int spaceIndentationSize = CodeFormatterConstants.FORMATTER_INDENTATION_SPACES_SIZE.get();
String spaceIndent = AutoEditUtils.getNSpaces(spaceIndentationSize);
switch (indentMode) {
case TAB: return array("\t", spaceIndent); // return getIndentPrefixesForTab(spaceIndent);
case SPACES: return array(spaceIndent, "\t"); // return getIndentPrefixesForSpaces(spaceIndent);
}
throw assertUnreachable();
}
@Override
protected void updateIndentationSettings(SourceViewer sourceViewer, String property) {
super.updateIndentationSettings(sourceViewer, property);
if(
CodeFormatterConstants.FORMATTER_INDENTATION_SPACES_SIZE.keyEquals(property) ||
CodeFormatterConstants.FORMATTER_INDENT_MODE.keyEquals(property)) {
for(String contentType : getConfiguredContentTypes(sourceViewer)) {
String[] prefixes= getIndentPrefixes(sourceViewer, contentType);
sourceViewer.setIndentPrefixes(prefixes, contentType);
}
}
}
/* ----------------- Content Assist ----------------- */
@Override
public ContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
AbstractLangEditor editor = getEditor_asLang();
if(editor != null) {
final ContentAssistPreferenceHandler caPrefHelper = createContentAssistPrefHandler();
ContentAssistantExt assistant = new ContentAssistantExt(caPrefHelper);
assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
assistant.setRestoreCompletionProposalSize(LangUIPlugin.getDialogSettings("completion_proposal_size"));
assistant.setInformationControlCreator(
getInformationControl_ContentAsssist(caPrefHelper.getAdditionalInfoAffordanceString()));
assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
assistant.enableColoredLabels(true);
configureContentAssistantProcessors(assistant);
// Note: configuration must come after processors are created
assistant.configure(fPreferenceStore);
return assistant;
}
return null;
}
protected ContentAssistPreferenceHandler createContentAssistPrefHandler() {
return new ContentAssistPreferenceHandler();
}
protected void configureContentAssistantProcessors(ContentAssistant assistant) {
Indexable<CompletionProposalsGrouping> categories = getContentAssistCategoriesProvider().getCategories();
IContentAssistProcessor cap = createContentAssistProcessor(assistant, categories);
assistant.setContentAssistProcessor(cap, IDocument.DEFAULT_CONTENT_TYPE);
}
protected LangContentAssistProcessor createContentAssistProcessor(ContentAssistant assistant,
Indexable<CompletionProposalsGrouping> categories) {
return new LangContentAssistProcessor(assistant, getEditor(), categories);
}
protected abstract ContentAssistCategoriesBuilder getContentAssistCategoriesProvider();
/* ----------------- reconciler ----------------- */
@Override
public IReconciler getReconciler(ISourceViewer sourceViewer) {
ITextEditor editor = getEditor();
if(editor != null && editor.isEditable()) {
AbstractReconciler reconciler = doCreateReconciler(editor);
reconciler.setIsAllowedToModifyDocument(false);
reconciler.setDelay(500); // Can't use zero
return reconciler;
}
return null;
}
protected LangReconciler doCreateReconciler(ITextEditor editor) {
CompositeReconcilingStrategy strategy = getReconciler_createCompositeStrategy(editor);
return new LangReconciler(strategy, false, editor);
}
@SuppressWarnings("unused")
protected CompositeReconcilingStrategy getReconciler_createCompositeStrategy(ITextEditor editor) {
return new CompositeReconcilingStrategy();
}
/* ----------------- ----------------- */
@Override
public void setupCustomConfiguration(LangSourceViewer sourceViewer) {
installOutlinePresenter(sourceViewer);
}
}