1 | package com.framsticks.framclipse.editors.configuration; |
---|
2 | |
---|
3 | import java.net.MalformedURLException; |
---|
4 | import java.net.URI; |
---|
5 | import java.net.URL; |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.List; |
---|
8 | |
---|
9 | import org.eclipse.core.resources.IMarker; |
---|
10 | import org.eclipse.core.resources.IResource; |
---|
11 | import org.eclipse.core.runtime.CoreException; |
---|
12 | import org.eclipse.core.runtime.IPath; |
---|
13 | import org.eclipse.core.runtime.IProgressMonitor; |
---|
14 | import org.eclipse.jface.text.BadLocationException; |
---|
15 | import org.eclipse.jface.text.IDocument; |
---|
16 | import org.eclipse.jface.text.IRegion; |
---|
17 | import org.eclipse.jface.text.ITextOperationTarget; |
---|
18 | import org.eclipse.jface.text.Position; |
---|
19 | import org.eclipse.jface.text.TextViewer; |
---|
20 | import org.eclipse.jface.text.reconciler.DirtyRegion; |
---|
21 | import org.eclipse.jface.text.reconciler.IReconcilingStrategy; |
---|
22 | import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension; |
---|
23 | import org.eclipse.swt.widgets.Display; |
---|
24 | import org.eclipse.ui.IEditorInput; |
---|
25 | import org.eclipse.ui.IPathEditorInput; |
---|
26 | import org.eclipse.ui.IURIEditorInput; |
---|
27 | import org.eclipse.ui.ide.ResourceUtil; |
---|
28 | |
---|
29 | import com.framsticks.framclipse.editors.FramclipseEditor; |
---|
30 | import com.framsticks.framclipse.editors.folding.FramclipseFoldingAnnotation; |
---|
31 | import com.framsticks.framclipse.internal.parser.ASTCodeSection; |
---|
32 | import com.framsticks.framclipse.internal.parser.ASTFObject; |
---|
33 | import com.framsticks.framclipse.internal.parser.ASTFunction; |
---|
34 | import com.framsticks.framclipse.internal.parser.ASTGlobalInclude; |
---|
35 | import com.framsticks.framclipse.internal.parser.ASTProperty; |
---|
36 | import com.framsticks.framclipse.internal.parser.ElementWithOffset; |
---|
37 | import com.framsticks.framclipse.internal.parser.FramclipseParser; |
---|
38 | import com.framsticks.framclipse.internal.parser.Node; |
---|
39 | import com.framsticks.framclipse.internal.parser.ParseException; |
---|
40 | import com.framsticks.framclipse.internal.parser.TokenMgrError; |
---|
41 | |
---|
42 | |
---|
43 | public class FramclipseReconcilingStrategy implements IReconcilingStrategy, |
---|
44 | IReconcilingStrategyExtension |
---|
45 | { |
---|
46 | |
---|
47 | FramclipseEditor fEditor = null; |
---|
48 | private IDocument fDocument; |
---|
49 | private FramclipseParser parser = new FramclipseParser(); |
---|
50 | |
---|
51 | protected final ArrayList<Position> fPositions = new ArrayList<Position>(); |
---|
52 | |
---|
53 | public void reconcile(IRegion partition) { |
---|
54 | |
---|
55 | doAll(); |
---|
56 | } |
---|
57 | |
---|
58 | public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) { |
---|
59 | doAll(); |
---|
60 | } |
---|
61 | |
---|
62 | public void setDocument(IDocument document) { |
---|
63 | this.fDocument = document; |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | public FramclipseEditor getEditor() { |
---|
68 | return fEditor; |
---|
69 | } |
---|
70 | |
---|
71 | public void setEditor(FramclipseEditor editor) { |
---|
72 | fEditor = editor; |
---|
73 | } |
---|
74 | |
---|
75 | public void initialReconcile() { |
---|
76 | System.out.println("initial reconcile..."); |
---|
77 | doAll(); |
---|
78 | } |
---|
79 | |
---|
80 | public void setProgressMonitor(IProgressMonitor monitor) { |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | void doAll() |
---|
85 | { |
---|
86 | Display.getDefault().syncExec(new Runnable() { |
---|
87 | |
---|
88 | public void run() { |
---|
89 | Object adapter = fEditor.getAdapter(ITextOperationTarget.class); |
---|
90 | if(adapter instanceof TextViewer) |
---|
91 | { |
---|
92 | TextViewer viewer = (TextViewer)adapter; |
---|
93 | try |
---|
94 | { |
---|
95 | viewer.setRedraw(false); |
---|
96 | |
---|
97 | IResource resource = ResourceUtil.getResource(fEditor.getEditorInput()); |
---|
98 | resource.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_ZERO); |
---|
99 | |
---|
100 | try { |
---|
101 | parseDocument(); |
---|
102 | } catch (ParseException e) { |
---|
103 | ErrorHandler<ParseException> handler = new ErrorHandler.ParseErrorHandler(); |
---|
104 | handler.createMarker(resource, e); |
---|
105 | } catch (TokenMgrError e) { |
---|
106 | ErrorHandler<TokenMgrError> handler = new ErrorHandler.TokenErrorHandler(); |
---|
107 | handler.createMarker(resource, e); |
---|
108 | } |
---|
109 | fEditor.updateOutlinePage(); |
---|
110 | |
---|
111 | collectAndUpdateAnnotations(); |
---|
112 | } catch (CoreException e) { |
---|
113 | e.printStackTrace(); |
---|
114 | } |
---|
115 | finally |
---|
116 | { |
---|
117 | viewer.setRedraw(true); |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | }); |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | void parseDocument() throws ParseException |
---|
127 | { |
---|
128 | |
---|
129 | URL path = null; |
---|
130 | final IEditorInput ei = fEditor.getEditorInput(); |
---|
131 | if (ei instanceof IPathEditorInput) { |
---|
132 | final IPathEditorInput pei = (IPathEditorInput) ei; |
---|
133 | final IPath p = pei.getPath().makeAbsolute(); |
---|
134 | try { |
---|
135 | path = p.toFile().toURL(); |
---|
136 | } catch (MalformedURLException e) { |
---|
137 | path= null; |
---|
138 | } |
---|
139 | } |
---|
140 | if (ei instanceof IURIEditorInput) { |
---|
141 | final IURIEditorInput uei = (IURIEditorInput) ei; |
---|
142 | final URI uri = uei.getURI(); |
---|
143 | try { |
---|
144 | path = uri.toURL(); |
---|
145 | } catch (MalformedURLException e) { |
---|
146 | path = null; |
---|
147 | } |
---|
148 | } |
---|
149 | Node model = parser.parse(fDocument.get(), fEditor.getEditorType(),path); |
---|
150 | fEditor.setFileModel(model); |
---|
151 | } |
---|
152 | |
---|
153 | void recursiveAddAnnotations(List<FramclipseFoldingAnnotation> annotations, Node element) |
---|
154 | { |
---|
155 | if (element instanceof ASTGlobalInclude) |
---|
156 | return; |
---|
157 | if(element instanceof ASTFObject || |
---|
158 | element instanceof ASTProperty || |
---|
159 | element instanceof ASTFunction || |
---|
160 | element instanceof ASTCodeSection) |
---|
161 | { |
---|
162 | try { |
---|
163 | ElementWithOffset ewo = (ElementWithOffset)element; |
---|
164 | /*if(ewo instanceof ASTFObject) |
---|
165 | System.out.println("adding for " + ((ASTFObject)ewo).getClassName());*/ |
---|
166 | if(fDocument.getLineOfOffset(ewo.getBeginOffset()) != fDocument.getLineOfOffset(ewo.getEndOffset())) |
---|
167 | { |
---|
168 | annotations.add(new FramclipseFoldingAnnotation(ewo)); |
---|
169 | } |
---|
170 | |
---|
171 | } |
---|
172 | catch(BadLocationException ex) |
---|
173 | { |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | for(int k = 0; k < element.jjtGetNumChildren(); ++k) |
---|
180 | recursiveAddAnnotations(annotations, element.jjtGetChild(k)); |
---|
181 | } |
---|
182 | |
---|
183 | void collectAndUpdateAnnotations() |
---|
184 | { |
---|
185 | Node model = fEditor.getFileModel(); |
---|
186 | |
---|
187 | if(model != null) |
---|
188 | { |
---|
189 | List<FramclipseFoldingAnnotation> annots = new ArrayList<FramclipseFoldingAnnotation>(); |
---|
190 | recursiveAddAnnotations(annots, model); |
---|
191 | |
---|
192 | //System.out.println("collected annotations: " + annots.size()); |
---|
193 | |
---|
194 | fEditor.updateFoldingStructure(annots); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * @deprecated |
---|
200 | */ |
---|
201 | void scanAll() |
---|
202 | { |
---|
203 | fPositions.clear(); |
---|
204 | |
---|
205 | try |
---|
206 | { |
---|
207 | boolean tildeSectionStartFound = false; |
---|
208 | int tildeStartPos = 0; |
---|
209 | boolean plainSectionStartFound = false; |
---|
210 | int plainStartPos = 0; |
---|
211 | |
---|
212 | String lineSep = System.getProperty("line.separator"); |
---|
213 | |
---|
214 | for(int k = 0; k < fDocument.getNumberOfLines(); ++k) |
---|
215 | { |
---|
216 | String line = fDocument.get(fDocument.getLineOffset(k), fDocument.getLineLength(k)); |
---|
217 | |
---|
218 | if(line.matches(".*:( |\\t)*" + lineSep)){ |
---|
219 | if(!tildeSectionStartFound) |
---|
220 | { |
---|
221 | plainSectionStartFound = true; |
---|
222 | plainStartPos = fDocument.getLineOffset(k); |
---|
223 | } |
---|
224 | } |
---|
225 | else if(line.matches(".*~( |\\t)*" + lineSep)) |
---|
226 | { |
---|
227 | if(!tildeSectionStartFound) |
---|
228 | { |
---|
229 | tildeStartPos = fDocument.getLineOffset(k); |
---|
230 | tildeSectionStartFound = true; |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | fPositions.add(new Position(tildeStartPos, fDocument.getLineOffset(k) + fDocument.getLineLength(k) - tildeStartPos)); |
---|
235 | tildeSectionStartFound = false; |
---|
236 | } |
---|
237 | } |
---|
238 | else if(line.matches("( |\\t)*" + lineSep)) |
---|
239 | { |
---|
240 | if(plainSectionStartFound) |
---|
241 | { |
---|
242 | if(!tildeSectionStartFound) |
---|
243 | { |
---|
244 | plainSectionStartFound = false; |
---|
245 | fPositions.add(new Position(plainStartPos, fDocument.getLineOffset(k) - plainStartPos)); |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | catch(Exception e) |
---|
252 | { |
---|
253 | e.printStackTrace(); |
---|
254 | } |
---|
255 | |
---|
256 | } |
---|
257 | |
---|
258 | } |
---|