1 package org.simantics.document.ui;
3 import org.eclipse.jface.preference.IPreferenceStore;
4 import org.eclipse.jface.text.DefaultInformationControl;
5 import org.eclipse.jface.text.IDocument;
6 import org.eclipse.jface.text.IInformationControl;
7 import org.eclipse.jface.text.IInformationControlCreator;
8 import org.eclipse.jface.text.ITextHover;
9 import org.eclipse.jface.text.TextAttribute;
10 import org.eclipse.jface.text.contentassist.ContentAssistant;
11 import org.eclipse.jface.text.contentassist.IContentAssistant;
12 import org.eclipse.jface.text.presentation.IPresentationReconciler;
13 import org.eclipse.jface.text.presentation.PresentationReconciler;
14 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
15 import org.eclipse.jface.text.rules.EndOfLineRule;
16 import org.eclipse.jface.text.rules.IRule;
17 import org.eclipse.jface.text.rules.ITokenScanner;
18 import org.eclipse.jface.text.rules.IWordDetector;
19 import org.eclipse.jface.text.rules.MultiLineRule;
20 import org.eclipse.jface.text.rules.PatternRule;
21 import org.eclipse.jface.text.rules.RuleBasedScanner;
22 import org.eclipse.jface.text.rules.Token;
23 import org.eclipse.jface.text.rules.WordRule;
24 import org.eclipse.jface.text.source.DefaultAnnotationHover;
25 import org.eclipse.jface.text.source.IAnnotationHover;
26 import org.eclipse.jface.text.source.ISharedTextColors;
27 import org.eclipse.jface.text.source.ISourceViewer;
28 import org.eclipse.jface.text.source.SourceViewerConfiguration;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Device;
31 import org.eclipse.swt.graphics.Font;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.editors.text.EditorsUI;
35 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
37 public class CSSSourceViewerConfiguration extends SourceViewerConfiguration {
39 public static final char[] CONTENT_ASSIST_AUTO_CHARS = new char[] { '.' };
42 ISharedTextColors sharedTextColors;
43 IPreferenceStore preferenceStore;
45 private CSSTextEditorEnvironment editorEnvironment;
47 public CSSSourceViewerConfiguration(Device device,
48 ISharedTextColors sharedTextColors) {
50 this.sharedTextColors = sharedTextColors;
51 this.preferenceStore = EditorsUI.getPreferenceStore();
53 this.editorEnvironment = new CSSTextEditorEnvironment();
56 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
58 IDocument.DEFAULT_CONTENT_TYPE
63 public int getTabWidth(ISourceViewer sourceViewer) {
64 if (preferenceStore == null)
65 return super.getTabWidth(sourceViewer);
66 return preferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
70 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
71 String[] indentPrefixes = getIndentPrefixesForTab(getTabWidth(sourceViewer));
72 if (indentPrefixes == null)
75 int length = indentPrefixes.length;
77 // Swap first with second last
78 String first = indentPrefixes[0];
79 indentPrefixes[0] = indentPrefixes[length - 2];
80 indentPrefixes[length - 2] = first;
83 return indentPrefixes;
86 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
87 PresentationReconciler reconciler = new PresentationReconciler();
89 DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());
91 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
92 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
97 ITokenScanner getSclTokenScanner() {
98 RuleBasedScanner scanner = new RuleBasedScanner();
100 Font font = new Font(device, "Courier New", 10, SWT.NORMAL); //$NON-NLS-1$
101 Font boldFont = new Font(device, "Courier New", 10, SWT.BOLD); //$NON-NLS-1$
103 Token defaultToken = new Token(
105 sharedTextColors.getColor(new RGB(0, 0, 0)),
110 Token string = new Token(new TextAttribute(
111 sharedTextColors.getColor(new RGB(42, 0, 255)),
116 Token reserved = new Token(
118 sharedTextColors.getColor(new RGB(127, 0, 85)),
123 Token comment = new Token(new TextAttribute(
124 sharedTextColors.getColor(new RGB(63, 127, 95)),
130 WordRule reservedWord = new WordRule(new IWordDetector() {
132 public boolean isWordStart(char c) {
133 return Character.isJavaIdentifierStart(c);
137 public boolean isWordPart(char c) {
138 return Character.isJavaIdentifierPart(c) || c=='.';
142 reservedWord.addWord("if", reserved); //$NON-NLS-1$
143 reservedWord.addWord("then", reserved); //$NON-NLS-1$
144 reservedWord.addWord("else", reserved); //$NON-NLS-1$
145 reservedWord.addWord("match", reserved); //$NON-NLS-1$
146 reservedWord.addWord("with", reserved); //$NON-NLS-1$
147 reservedWord.addWord("data", reserved); //$NON-NLS-1$
148 reservedWord.addWord("type", reserved); //$NON-NLS-1$
149 reservedWord.addWord("class", reserved); //$NON-NLS-1$
151 IRule[] rules = new IRule[] {
152 //new MultiLineRule("\"\"\"", "\"\"\"", string),
153 new PatternRule("\"", "\"", string, '\\', true), //$NON-NLS-1$ //$NON-NLS-2$
154 new MultiLineRule("/*", "*/", comment), //$NON-NLS-1$ //$NON-NLS-2$
155 new EndOfLineRule("//", comment), //$NON-NLS-1$
158 scanner.setRules(rules);
159 scanner.setDefaultReturnToken(defaultToken);
165 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
166 return new CSSEditorTextHover(sourceViewer, editorEnvironment);
170 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
171 return new DefaultAnnotationHover();
175 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
176 ContentAssistant assistant = new ContentAssistant();
177 assistant.enableColoredLabels(true);
178 assistant.setStatusLineVisible(true);
180 assistant.enableAutoActivation(true);
181 assistant.enableAutoInsert(true);
182 assistant.setContentAssistProcessor(new CSSCompletionAssistProcessor(editorEnvironment), IDocument.DEFAULT_CONTENT_TYPE);
183 assistant.setInformationControlCreator(CREATOR);
184 assistant.setRestoreCompletionProposalSize(Activator.getDefault().getDialogSettings());
188 private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {
191 public IInformationControl createInformationControl(Shell parent) {
192 return new DefaultInformationControl(parent);
197 // public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
198 // return new ContentFormatter();