1 | package com.framsticks.framclipse.editors.configuration; |
---|
2 | |
---|
3 | import java.util.HashMap; |
---|
4 | import java.util.HashSet; |
---|
5 | import java.util.List; |
---|
6 | import java.util.Map; |
---|
7 | import java.util.Set; |
---|
8 | |
---|
9 | import org.eclipse.jface.text.IDocument; |
---|
10 | import org.eclipse.jface.text.ITextHover; |
---|
11 | import org.eclipse.jface.text.TextAttribute; |
---|
12 | import org.eclipse.jface.text.contentassist.ContentAssistant; |
---|
13 | import org.eclipse.jface.text.contentassist.IContentAssistant; |
---|
14 | import org.eclipse.jface.text.presentation.IPresentationReconciler; |
---|
15 | import org.eclipse.jface.text.presentation.PresentationReconciler; |
---|
16 | import org.eclipse.jface.text.reconciler.IReconciler; |
---|
17 | import org.eclipse.jface.text.reconciler.MonoReconciler; |
---|
18 | import org.eclipse.jface.text.rules.BufferedRuleBasedScanner; |
---|
19 | import org.eclipse.jface.text.rules.DefaultDamagerRepairer; |
---|
20 | import org.eclipse.jface.text.rules.ITokenScanner; |
---|
21 | import org.eclipse.jface.text.rules.Token; |
---|
22 | import org.eclipse.jface.text.source.ISourceViewer; |
---|
23 | import org.eclipse.jface.text.source.SourceViewerConfiguration; |
---|
24 | import org.eclipse.swt.graphics.Color; |
---|
25 | import org.jdom.Attribute; |
---|
26 | import org.jdom.Document; |
---|
27 | import org.jdom.Element; |
---|
28 | import org.jdom.xpath.XPath; |
---|
29 | |
---|
30 | import com.framsticks.framclipse.Framclipse; |
---|
31 | import com.framsticks.framclipse.editors.EditorType; |
---|
32 | import com.framsticks.framclipse.editors.FramclipseEditor; |
---|
33 | import com.framsticks.framclipse.editors.codeCompletion.FramscriptCompletionProcessor; |
---|
34 | import com.framsticks.framclipse.editors.codeCompletion.PropertyCompletionProcessor; |
---|
35 | import com.framsticks.framclipse.editors.codeCompletion.SyntaxUtils; |
---|
36 | import com.framsticks.framclipse.editors.hover.FramscriptTextHover; |
---|
37 | import com.framsticks.framclipse.syntaxColoring.ColorManager; |
---|
38 | import com.framsticks.framclipse.syntaxColoring.FramclipseColors; |
---|
39 | import com.framsticks.framclipse.syntaxColoring.FramclipseCommentScanner; |
---|
40 | import com.framsticks.framclipse.syntaxColoring.FramscriptCodeScanner; |
---|
41 | |
---|
42 | |
---|
43 | public class FramscriptSourceViewerConfiguration extends SourceViewerConfiguration { |
---|
44 | |
---|
45 | private FramscriptCodeScanner codeScanner; |
---|
46 | private final EditorType editorType; |
---|
47 | private FramclipseEditor editor; |
---|
48 | |
---|
49 | public FramscriptSourceViewerConfiguration(EditorType editorType, FramclipseEditor editor) { |
---|
50 | System.out.println("creating sourceViewerConfiguration for type: " + editorType); |
---|
51 | this.editorType = editorType; |
---|
52 | this.editor = editor; |
---|
53 | } |
---|
54 | |
---|
55 | // ///////////////////////////////////////////////////////////////////////// |
---|
56 | // code highlighting |
---|
57 | // /////////////////////////////////////////////////////////////////////// |
---|
58 | |
---|
59 | @Override |
---|
60 | public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) { |
---|
61 | return new String[] { IDocument.DEFAULT_CONTENT_TYPE, FramscriptPartitionScanner.CODE }; |
---|
62 | } |
---|
63 | |
---|
64 | protected ITokenScanner getFramscriptCodeScanner() { |
---|
65 | ColorManager cm = Framclipse.getDefault().getColorManager(); |
---|
66 | if (codeScanner == null) { |
---|
67 | codeScanner = new FramscriptCodeScanner(); |
---|
68 | Color defaultColor = cm.getColor(FramclipseColors.DEFAULT); |
---|
69 | TextAttribute textAttribute = new TextAttribute(defaultColor); |
---|
70 | Token defaultToken = new Token(textAttribute); |
---|
71 | codeScanner.setDefaultReturnToken(defaultToken); |
---|
72 | } |
---|
73 | return codeScanner; |
---|
74 | } |
---|
75 | |
---|
76 | private ITokenScanner commentScanner = null; |
---|
77 | |
---|
78 | protected ITokenScanner getCommentScanner() |
---|
79 | { |
---|
80 | if(commentScanner == null) |
---|
81 | commentScanner = new FramclipseCommentScanner(); |
---|
82 | |
---|
83 | return commentScanner; |
---|
84 | } |
---|
85 | |
---|
86 | @Override |
---|
87 | public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) { |
---|
88 | return Framclipse.FRAMSCRIPT_PARTITIONING; |
---|
89 | } |
---|
90 | |
---|
91 | @Override |
---|
92 | public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) { |
---|
93 | PresentationReconciler reconciler = new PresentationReconciler(); |
---|
94 | String partitioning = getConfiguredDocumentPartitioning(sourceViewer); |
---|
95 | reconciler.setDocumentPartitioning(partitioning); |
---|
96 | |
---|
97 | DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getFramscriptCodeScanner()); |
---|
98 | reconciler.setDamager(dr, FramscriptPartitionScanner.CODE); |
---|
99 | reconciler.setRepairer(dr, FramscriptPartitionScanner.CODE); |
---|
100 | |
---|
101 | ColorManager colorManager = Framclipse.getDefault().getColorManager(); |
---|
102 | Color color = colorManager.getColor(FramclipseColors.DESCRIPTION); |
---|
103 | dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(color))); |
---|
104 | reconciler.setDamager(dr, FramscriptPartitionScanner.DESCRIPTION); |
---|
105 | reconciler.setRepairer(dr, FramscriptPartitionScanner.DESCRIPTION); |
---|
106 | |
---|
107 | color = colorManager.getColor(FramclipseColors.DEFAULT); |
---|
108 | dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(color))); |
---|
109 | reconciler.setDamager(dr, FramscriptPartitionScanner.UNDEFINED); |
---|
110 | reconciler.setRepairer(dr, FramscriptPartitionScanner.UNDEFINED); |
---|
111 | reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); |
---|
112 | reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); |
---|
113 | |
---|
114 | //colorManager = Framclipse.getDefault().getColorManager(); |
---|
115 | //color = colorManager.getColor(FramclipseColors.COMMENT); |
---|
116 | //dr = new DefaultDamagerRepairer(new SingleTokenScanner(new TextAttribute(color))); |
---|
117 | dr = new DefaultDamagerRepairer(getCommentScanner()); |
---|
118 | reconciler.setDamager(dr, FramscriptPartitionScanner.COMMENT); |
---|
119 | reconciler.setRepairer(dr, FramscriptPartitionScanner.COMMENT); |
---|
120 | |
---|
121 | return reconciler; |
---|
122 | } |
---|
123 | |
---|
124 | static class SingleTokenScanner extends BufferedRuleBasedScanner { |
---|
125 | public SingleTokenScanner(TextAttribute attribute) { |
---|
126 | setDefaultReturnToken(new Token(attribute)); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | // ///////////////////////////////////////////////////////////////////////// |
---|
131 | // content assist |
---|
132 | // /////////////////////////////////////////////////////////////////////// |
---|
133 | |
---|
134 | @SuppressWarnings("unchecked") |
---|
135 | @Override |
---|
136 | public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { |
---|
137 | |
---|
138 | ContentAssistant assistant = new ContentAssistant(); |
---|
139 | String partitioning = getConfiguredDocumentPartitioning(sourceViewer); |
---|
140 | assistant.setDocumentPartitioning(partitioning); |
---|
141 | |
---|
142 | Set<String> contexts = new HashSet<String>(); |
---|
143 | Document framscriptContext = Framclipse.getDefault().getFramscriptContext(); |
---|
144 | try { |
---|
145 | String query = "//file[@pattern='*." + editorType.getExtension() + "']/code/context"; |
---|
146 | XPath xpath = XPath.newInstance(query); |
---|
147 | List<Element> list = xpath.selectNodes(framscriptContext); |
---|
148 | for (Element element : list) { |
---|
149 | Attribute name = element.getAttribute(SyntaxUtils.NAME_ATTRIBUTE); |
---|
150 | contexts.add(name.getValue()); |
---|
151 | } |
---|
152 | } catch (Exception e) { |
---|
153 | e.printStackTrace(); |
---|
154 | } |
---|
155 | |
---|
156 | FramscriptCompletionProcessor framscriptCompletionProcessor = new FramscriptCompletionProcessor( |
---|
157 | contexts,editor,editorType); |
---|
158 | assistant.setContentAssistProcessor(framscriptCompletionProcessor, |
---|
159 | FramscriptPartitionScanner.CODE); |
---|
160 | |
---|
161 | PropertyCompletionProcessor propertyCompletionProcessor = getPropertyCompletionProcessor( |
---|
162 | contexts, framscriptContext); |
---|
163 | assistant.setContentAssistProcessor(propertyCompletionProcessor, |
---|
164 | IDocument.DEFAULT_CONTENT_TYPE); |
---|
165 | |
---|
166 | assistant.enableAutoActivation(true); |
---|
167 | assistant.setAutoActivationDelay(500); |
---|
168 | int orientation = IContentAssistant.PROPOSAL_OVERLAY; |
---|
169 | assistant.setProposalPopupOrientation(orientation); |
---|
170 | orientation = IContentAssistant.CONTEXT_INFO_ABOVE; |
---|
171 | assistant.setContextInformationPopupOrientation(orientation); |
---|
172 | |
---|
173 | return assistant; |
---|
174 | } |
---|
175 | |
---|
176 | @SuppressWarnings("unchecked") |
---|
177 | private PropertyCompletionProcessor getPropertyCompletionProcessor(Set<String> contexts, |
---|
178 | Document framscriptContext) { |
---|
179 | |
---|
180 | Map<String, String> propertyTypes = new HashMap<String, String>(); |
---|
181 | try { |
---|
182 | String query = "//file[@pattern='*." + editorType.getExtension() + "']/property"; |
---|
183 | XPath xpath = XPath.newInstance(query); |
---|
184 | List<Element> list = xpath.selectNodes(framscriptContext); |
---|
185 | for (Element element : list) { |
---|
186 | Attribute name = element.getAttribute(SyntaxUtils.NAME_ATTRIBUTE); |
---|
187 | Attribute type = element.getAttribute(SyntaxUtils.TYPE_ATTRIBUTE); |
---|
188 | propertyTypes.put(type.getValue(), name.getValue()); |
---|
189 | } |
---|
190 | } catch (Exception e) { |
---|
191 | e.printStackTrace(); |
---|
192 | } |
---|
193 | |
---|
194 | Set<String> objectContexts = new HashSet<String>(); |
---|
195 | try { |
---|
196 | String query = "//file[@pattern='*." + editorType.getExtension() + "']/object"; |
---|
197 | XPath xpath = XPath.newInstance(query); |
---|
198 | List<Element> list = xpath.selectNodes(framscriptContext); |
---|
199 | for (Element element : list) { |
---|
200 | Attribute context = element.getAttribute(SyntaxUtils.CONTEXT_ATTRIBUTE); |
---|
201 | objectContexts.add(context.getValue()); |
---|
202 | } |
---|
203 | } catch (Exception e) { |
---|
204 | e.printStackTrace(); |
---|
205 | } |
---|
206 | |
---|
207 | PropertyCompletionProcessor propertyCompletionProcessor = new PropertyCompletionProcessor( |
---|
208 | contexts, objectContexts, propertyTypes); |
---|
209 | return propertyCompletionProcessor; |
---|
210 | } |
---|
211 | |
---|
212 | // ///////////////////////////////////////////////////////////////////////// |
---|
213 | // text hover |
---|
214 | // /////////////////////////////////////////////////////////////////////// |
---|
215 | |
---|
216 | @Override |
---|
217 | public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) { |
---|
218 | if (FramscriptPartitionScanner.CODE.equals(contentType)) { |
---|
219 | return new FramscriptTextHover(); |
---|
220 | } |
---|
221 | return null; |
---|
222 | } |
---|
223 | |
---|
224 | @Override |
---|
225 | public IReconciler getReconciler(ISourceViewer sourceViewer) { |
---|
226 | FramclipseReconcilingStrategy strategy = new FramclipseReconcilingStrategy(); |
---|
227 | strategy.setEditor(editor); |
---|
228 | |
---|
229 | MonoReconciler reconciler = new MonoReconciler(strategy,false); |
---|
230 | |
---|
231 | |
---|
232 | return reconciler; |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | } |
---|