]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSSourceViewerConfiguration.java
Several Wiki documentation view improvements.
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / CSSSourceViewerConfiguration.java
1 package org.simantics.document.ui;\r
2 \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
36 \r
37 public class CSSSourceViewerConfiguration extends SourceViewerConfiguration {\r
38 \r
39     public static final char[] CONTENT_ASSIST_AUTO_CHARS = new char[] { '.' };\r
40     Device device;\r
41     \r
42     ISharedTextColors sharedTextColors;\r
43     IPreferenceStore preferenceStore;\r
44     \r
45     private CSSTextEditorEnvironment editorEnvironment;\r
46     \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
52         \r
53         this.editorEnvironment = new CSSTextEditorEnvironment();\r
54     }\r
55 \r
56     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {\r
57         return new String[] {\r
58             IDocument.DEFAULT_CONTENT_TYPE\r
59         };\r
60     }\r
61     \r
62         @Override\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
67         }\r
68 \r
69         @Override\r
70         public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {\r
71                 String[] indentPrefixes = getIndentPrefixesForTab(getTabWidth(sourceViewer));\r
72                 if (indentPrefixes == null)\r
73                         return null;\r
74 \r
75                 int length = indentPrefixes.length;\r
76                 if (length > 2) {\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
81                 }\r
82 \r
83                 return indentPrefixes;\r
84         }\r
85     \r
86     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
87         PresentationReconciler reconciler = new PresentationReconciler();\r
88         \r
89         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());\r
90         \r
91         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
92         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
93         \r
94         return reconciler;\r
95     }\r
96     \r
97     ITokenScanner getSclTokenScanner() {\r
98         RuleBasedScanner scanner = new RuleBasedScanner();\r
99         \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
102 \r
103         Token defaultToken = new Token(\r
104                 new TextAttribute(\r
105                         sharedTextColors.getColor(new RGB(0, 0, 0)),\r
106                         null,\r
107                         0,\r
108                         font\r
109                 ));\r
110         Token string = new Token(new TextAttribute(\r
111                 sharedTextColors.getColor(new RGB(42, 0, 255)),\r
112                 null,\r
113                 0,\r
114                 font\r
115                 ));\r
116         Token reserved = new Token(\r
117                 new TextAttribute(\r
118                         sharedTextColors.getColor(new RGB(127, 0, 85)),\r
119                         null,\r
120                         SWT.BOLD,\r
121                         boldFont\r
122                 ));\r
123         Token comment = new Token(new TextAttribute(\r
124                 sharedTextColors.getColor(new RGB(63, 127, 95)),\r
125                 null,\r
126                 0,\r
127                 font\r
128                 ));\r
129 \r
130         WordRule reservedWord = new WordRule(new IWordDetector() {          \r
131             @Override\r
132             public boolean isWordStart(char c) {\r
133                 return Character.isJavaIdentifierStart(c);\r
134             }\r
135             \r
136             @Override\r
137             public boolean isWordPart(char c) {\r
138                 return Character.isJavaIdentifierPart(c) || c=='.';\r
139             }\r
140         });\r
141 \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
150         \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
156             reservedWord\r
157         };\r
158         scanner.setRules(rules);\r
159         scanner.setDefaultReturnToken(defaultToken);\r
160         \r
161         return scanner;\r
162     }\r
163     \r
164     @Override\r
165     public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {\r
166         return new CSSEditorTextHover(sourceViewer, editorEnvironment);\r
167     }\r
168     \r
169     @Override\r
170     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {\r
171         return new DefaultAnnotationHover();\r
172     }\r
173     \r
174     @Override\r
175     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {\r
176         ContentAssistant assistant = new ContentAssistant();\r
177         assistant.enableColoredLabels(true);\r
178         assistant.setStatusLineVisible(true);\r
179 \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
185         return assistant;\r
186     }\r
187     \r
188     private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {\r
189         \r
190         @Override\r
191         public IInformationControl createInformationControl(Shell parent) {\r
192             return new DefaultInformationControl(parent);\r
193         }\r
194     };\r
195 \r
196 //    @Override\r
197 //    public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {\r
198 //        return new ContentFormatter();\r
199 //    }\r
200 }\r