]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLSourceViewerConfigurationNew.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor / SCLSourceViewerConfigurationNew.java
1 package org.simantics.scl.ui.editor;\r
2 \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
41 \r
42 public class SCLSourceViewerConfigurationNew extends SourceViewerConfiguration {\r
43 \r
44     public static final char[] CONTENT_ASSIST_AUTO_CHARS = new char[] { '.' };\r
45     \r
46     ResourceManager resourceManager;\r
47     IPreferenceStore preferenceStore;\r
48     \r
49     private SCLTextEditorEnvironment sclTextEditorEnvironment;\r
50     \r
51     public SCLSourceViewerConfigurationNew(ResourceManager resourceManager) {\r
52         this.resourceManager = resourceManager;\r
53         this.preferenceStore = EditorsUI.getPreferenceStore();\r
54         \r
55         this.sclTextEditorEnvironment = new SCLTextEditorEnvironment("");\r
56     }\r
57 \r
58     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {\r
59         return new String[] {\r
60             IDocument.DEFAULT_CONTENT_TYPE\r
61         };\r
62     }\r
63     \r
64         @Override\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
69         }\r
70 \r
71         @Override\r
72         public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {\r
73                 String[] indentPrefixes = getIndentPrefixesForTab(getTabWidth(sourceViewer));\r
74                 if (indentPrefixes == null)\r
75                         return null;\r
76 \r
77                 int length = indentPrefixes.length;\r
78                 if (length > 2) {\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
83                 }\r
84 \r
85                 return indentPrefixes;\r
86         }\r
87     \r
88     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {\r
89         PresentationReconciler reconciler = new PresentationReconciler();\r
90         \r
91         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getSclTokenScanner());\r
92         \r
93         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
94         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);\r
95         \r
96         return reconciler;\r
97     }\r
98     \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
101     \r
102     ITokenScanner getSclTokenScanner() {\r
103         RuleBasedScanner scanner = new RuleBasedScanner();\r
104         \r
105         Font font = resourceManager.createFont(FONT_NORMAL);\r
106         Font boldFont = resourceManager.createFont(FONT_BOLD);\r
107         \r
108         Token defaultToken = new Token(\r
109                 new TextAttribute(\r
110                         resourceManager.createColor(new RGB(0, 0, 0)),\r
111                         null,\r
112                         0,\r
113                         font\r
114                 ));\r
115         Token string = new Token(new TextAttribute(\r
116                 resourceManager.createColor(new RGB(42, 0, 255)),\r
117                 null,\r
118                 0,\r
119                 font\r
120                 ));\r
121         Token reserved = new Token(\r
122                 new TextAttribute(\r
123                         resourceManager.createColor(new RGB(127, 0, 85)),\r
124                         null,\r
125                         SWT.BOLD,\r
126                         boldFont\r
127                 ));\r
128         Token comment = new Token(new TextAttribute(\r
129                 resourceManager.createColor(new RGB(63, 127, 95)),\r
130                 null,\r
131                 0,\r
132                 font\r
133                 ));\r
134 \r
135         WordRule reservedWord = new WordRule(new IWordDetector() {          \r
136             @Override\r
137             public boolean isWordStart(char c) {\r
138                 return Character.isJavaIdentifierStart(c);\r
139             }\r
140             \r
141             @Override\r
142             public boolean isWordPart(char c) {\r
143                 return Character.isJavaIdentifierPart(c) || c=='.';\r
144             }\r
145         });\r
146 \r
147         for(String word : SCLInfo.RESERVED_WORDS)\r
148             reservedWord.addWord(word, reserved);\r
149         \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
155             reservedWord\r
156         };\r
157         scanner.setRules(rules);\r
158         scanner.setDefaultReturnToken(defaultToken);\r
159         \r
160         return scanner;\r
161     }\r
162     \r
163     @Override\r
164     public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {\r
165         return new SCLEditorTextHover(sourceViewer, sclTextEditorEnvironment);\r
166     }\r
167     \r
168     @Override\r
169     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {\r
170         return new DefaultAnnotationHover();\r
171     }\r
172     \r
173     @Override\r
174     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {\r
175         ContentAssistant assistant = new ContentAssistant();\r
176         assistant.enableColoredLabels(true);\r
177         assistant.setStatusLineVisible(true);\r
178 \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
184         return assistant;\r
185     }\r
186     \r
187     private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {\r
188         \r
189         @Override\r
190         public IInformationControl createInformationControl(Shell parent) {\r
191             return new DefaultInformationControl(parent);\r
192         }\r
193     };\r
194     \r
195     public void updateCompletionAssistModuleName(String moduleName) {\r
196         sclTextEditorEnvironment.updateModuleName(moduleName);\r
197     }\r
198     \r
199 //    @Override\r
200 //    public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {\r
201 //        return new ContentFormatter();\r
202 //    }\r
203     \r
204     public SCLTextEditorEnvironment getSclTextEditorEnvironment() {\r
205         return sclTextEditorEnvironment;\r
206     }\r
207 }\r