source: java/FramclipsePlugin/src/main/java/com/framsticks/framclipse/editors/configuration/FramscriptSourceViewerConfiguration.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 9.0 KB
Line 
1package com.framsticks.framclipse.editors.configuration;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.List;
6import java.util.Map;
7import java.util.Set;
8
9import org.eclipse.jface.text.IDocument;
10import org.eclipse.jface.text.ITextHover;
11import org.eclipse.jface.text.TextAttribute;
12import org.eclipse.jface.text.contentassist.ContentAssistant;
13import org.eclipse.jface.text.contentassist.IContentAssistant;
14import org.eclipse.jface.text.presentation.IPresentationReconciler;
15import org.eclipse.jface.text.presentation.PresentationReconciler;
16import org.eclipse.jface.text.reconciler.IReconciler;
17import org.eclipse.jface.text.reconciler.MonoReconciler;
18import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
19import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
20import org.eclipse.jface.text.rules.ITokenScanner;
21import org.eclipse.jface.text.rules.Token;
22import org.eclipse.jface.text.source.ISourceViewer;
23import org.eclipse.jface.text.source.SourceViewerConfiguration;
24import org.eclipse.swt.graphics.Color;
25import org.jdom.Attribute;
26import org.jdom.Document;
27import org.jdom.Element;
28import org.jdom.xpath.XPath;
29
30import com.framsticks.framclipse.Framclipse;
31import com.framsticks.framclipse.editors.EditorType;
32import com.framsticks.framclipse.editors.FramclipseEditor;
33import com.framsticks.framclipse.editors.codeCompletion.FramscriptCompletionProcessor;
34import com.framsticks.framclipse.editors.codeCompletion.PropertyCompletionProcessor;
35import com.framsticks.framclipse.editors.codeCompletion.SyntaxUtils;
36import com.framsticks.framclipse.editors.hover.FramscriptTextHover;
37import com.framsticks.framclipse.syntaxColoring.ColorManager;
38import com.framsticks.framclipse.syntaxColoring.FramclipseColors;
39import com.framsticks.framclipse.syntaxColoring.FramclipseCommentScanner;
40import com.framsticks.framclipse.syntaxColoring.FramscriptCodeScanner;
41
42
43public 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}
Note: See TracBrowser for help on using the repository browser.