1 package org.simantics.scl.ui.editor;
\r
3 import org.eclipse.jface.preference.IPreferenceStore;
\r
4 import org.eclipse.jface.resource.FontDescriptor;
\r
5 import org.eclipse.jface.resource.ResourceManager;
\r
6 import org.eclipse.jface.text.DefaultInformationControl;
\r
7 import org.eclipse.jface.text.IDocument;
\r
8 import org.eclipse.jface.text.IInformationControl;
\r
9 import org.eclipse.jface.text.IInformationControlCreator;
\r
10 import org.eclipse.jface.text.ITextHover;
\r
11 import org.eclipse.jface.text.TextAttribute;
\r
12 import org.eclipse.jface.text.contentassist.ContentAssistant;
\r
13 import org.eclipse.jface.text.contentassist.IContentAssistant;
\r
14 import org.eclipse.jface.text.presentation.IPresentationReconciler;
\r
15 import org.eclipse.jface.text.presentation.PresentationReconciler;
\r
16 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
\r
17 import org.eclipse.jface.text.rules.EndOfLineRule;
\r
18 import org.eclipse.jface.text.rules.IRule;
\r
19 import org.eclipse.jface.text.rules.ITokenScanner;
\r
20 import org.eclipse.jface.text.rules.IWordDetector;
\r
21 import org.eclipse.jface.text.rules.MultiLineRule;
\r
22 import org.eclipse.jface.text.rules.PatternRule;
\r
23 import org.eclipse.jface.text.rules.RuleBasedScanner;
\r
24 import org.eclipse.jface.text.rules.Token;
\r
25 import org.eclipse.jface.text.rules.WordRule;
\r
26 import org.eclipse.jface.text.source.DefaultAnnotationHover;
\r
27 import org.eclipse.jface.text.source.IAnnotationHover;
\r
28 import org.eclipse.jface.text.source.ISourceViewer;
\r
29 import org.eclipse.jface.text.source.SourceViewerConfiguration;
\r
30 import org.eclipse.swt.SWT;
\r
31 import org.eclipse.swt.graphics.Font;
\r
32 import org.eclipse.swt.graphics.RGB;
\r
33 import org.eclipse.swt.widgets.Shell;
\r
34 import org.eclipse.ui.editors.text.EditorsUI;
\r
35 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
\r
36 import org.simantics.scl.ui.Activator;
\r
37 import org.simantics.scl.ui.editor.completion.SCLCompletionAssistProcessor;
\r
38 import org.simantics.scl.ui.editor.completion.SCLEditorTextHover;
\r
39 import org.simantics.scl.ui.editor.completion.SCLTextEditorEnvironment;
\r
40 import org.simantics.scl.ui.info.SCLInfo;
\r
42 public class SCLSourceViewerConfigurationNew extends SourceViewerConfiguration {
\r
44 public static final char[] CONTENT_ASSIST_AUTO_CHARS = new char[] { '.' };
\r
46 ResourceManager resourceManager;
\r
47 IPreferenceStore preferenceStore;
\r
49 private SCLTextEditorEnvironment sclTextEditorEnvironment;
\r
51 public SCLSourceViewerConfigurationNew(ResourceManager resourceManager) {
\r
52 this.resourceManager = resourceManager;
\r
53 this.preferenceStore = EditorsUI.getPreferenceStore();
\r
55 this.sclTextEditorEnvironment = new SCLTextEditorEnvironment("");
\r
58 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
\r
59 return new String[] {
\r
60 IDocument.DEFAULT_CONTENT_TYPE
\r
65 public int getTabWidth(ISourceViewer sourceViewer) {
\r
66 if (preferenceStore == null)
\r
67 return super.getTabWidth(sourceViewer);
\r
68 return preferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
\r
72 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
\r
73 String[] indentPrefixes = getIndentPrefixesForTab(getTabWidth(sourceViewer));
\r
74 if (indentPrefixes == null)
\r
77 int length = indentPrefixes.length;
\r
79 // Swap first with second last
\r
80 String first = indentPrefixes[0];
\r
81 indentPrefixes[0] = indentPrefixes[length - 2];
\r
82 indentPrefixes[length - 2] = first;
\r
85 return indentPrefixes;
\r
88 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
\r
89 PresentationReconciler reconciler = new PresentationReconciler();
\r
91 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());
\r
93 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
\r
94 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
\r
99 private static final FontDescriptor FONT_NORMAL = FontDescriptor.createFrom("Consolas", 10, SWT.NORMAL);
\r
100 private static final FontDescriptor FONT_BOLD = FontDescriptor.createFrom("Consolas", 10, SWT.BOLD);
\r
102 ITokenScanner getSclTokenScanner() {
\r
103 RuleBasedScanner scanner = new RuleBasedScanner();
\r
105 Font font = resourceManager.createFont(FONT_NORMAL);
\r
106 Font boldFont = resourceManager.createFont(FONT_BOLD);
\r
108 Token defaultToken = new Token(
\r
110 resourceManager.createColor(new RGB(0, 0, 0)),
\r
115 Token string = new Token(new TextAttribute(
\r
116 resourceManager.createColor(new RGB(42, 0, 255)),
\r
121 Token reserved = new Token(
\r
123 resourceManager.createColor(new RGB(127, 0, 85)),
\r
128 Token comment = new Token(new TextAttribute(
\r
129 resourceManager.createColor(new RGB(63, 127, 95)),
\r
135 WordRule reservedWord = new WordRule(new IWordDetector() {
\r
137 public boolean isWordStart(char c) {
\r
138 return Character.isJavaIdentifierStart(c);
\r
142 public boolean isWordPart(char c) {
\r
143 return Character.isJavaIdentifierPart(c) || c=='.';
\r
147 for(String word : SCLInfo.RESERVED_WORDS)
\r
148 reservedWord.addWord(word, reserved);
\r
150 IRule[] rules = new IRule[] {
\r
151 //new MultiLineRule("\"\"\"", "\"\"\"", string),
\r
152 new PatternRule("\"", "\"", string, '\\', true),
\r
153 new MultiLineRule("/*", "*/", comment),
\r
154 new EndOfLineRule("//", comment),
\r
157 scanner.setRules(rules);
\r
158 scanner.setDefaultReturnToken(defaultToken);
\r
164 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
\r
165 return new SCLEditorTextHover(sourceViewer, sclTextEditorEnvironment);
\r
169 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
\r
170 return new DefaultAnnotationHover();
\r
174 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
\r
175 ContentAssistant assistant = new ContentAssistant();
\r
176 assistant.enableColoredLabels(true);
\r
177 assistant.setStatusLineVisible(true);
\r
179 assistant.enableAutoActivation(true);
\r
180 assistant.enableAutoInsert(true);
\r
181 assistant.setContentAssistProcessor(new SCLCompletionAssistProcessor(sclTextEditorEnvironment), IDocument.DEFAULT_CONTENT_TYPE);
\r
182 assistant.setInformationControlCreator(CREATOR);
\r
183 assistant.setRestoreCompletionProposalSize(Activator.getInstance().getDialogSettings());
\r
187 private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {
\r
190 public IInformationControl createInformationControl(Shell parent) {
\r
191 return new DefaultInformationControl(parent);
\r
195 public void updateCompletionAssistModuleName(String moduleName) {
\r
196 sclTextEditorEnvironment.updateModuleName(moduleName);
\r
200 // public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
\r
201 // return new ContentFormatter();
\r
204 public SCLTextEditorEnvironment getSclTextEditorEnvironment() {
\r
205 return sclTextEditorEnvironment;
\r