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