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