Package: VdmCompleteProcessor$2

VdmCompleteProcessor$2

nameinstructionbranchcomplexitylinemethod
caseAProductType(AProductType)
M: 72 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
{...}
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.templates;
23:
24: import java.util.ArrayList;
25: import java.util.Iterator;
26: import java.util.List;
27: import java.util.Vector;
28:
29: import org.eclipse.jface.text.ITextViewer;
30: import org.eclipse.jface.text.contentassist.CompletionProposal;
31: import org.eclipse.jface.text.contentassist.ContextInformation;
32: import org.eclipse.jface.text.contentassist.ICompletionProposal;
33: import org.eclipse.jface.text.contentassist.IContextInformation;
34: import org.eclipse.jface.text.templates.TemplateContext;
35: import org.overture.ast.analysis.AnalysisException;
36: import org.overture.ast.analysis.DepthFirstAnalysisAdaptor;
37: import org.overture.ast.definitions.ATypeDefinition;
38: import org.overture.ast.definitions.PDefinition;
39: import org.overture.ast.definitions.SClassDefinition;
40: import org.overture.ast.expressions.AQuoteLiteralExp;
41: import org.overture.ast.expressions.PExp;
42: import org.overture.ast.modules.AModuleModules;
43: import org.overture.ast.node.INode;
44: import org.overture.ast.statements.PStm;
45: import org.overture.ast.types.AFieldField;
46: import org.overture.ast.types.AProductType;
47: import org.overture.ast.types.AQuoteType;
48: import org.overture.ast.types.ARecordInvariantType;
49: import org.overture.ast.types.PType;
50: import org.overture.ide.ui.VdmUIPlugin;
51: import org.overture.ide.ui.editor.core.VdmDocument;
52: import org.overture.ide.ui.internal.viewsupport.VdmElementImageProvider;
53: import org.overture.ide.ui.utility.ast.AstLocationSearcher2;
54: import org.overture.ide.ui.utility.ast.AstLocationSearcher2.TextReference;
55: import org.overture.ide.ui.utility.ast.AstNameUtil;
56:
57: public class VdmCompleteProcessor
58: {
59:         private VdmElementImageProvider imgProvider = new VdmElementImageProvider();
60:         private VdmCompletionHelper VdmHelper = new VdmCompletionHelper();
61:         private ArrayList<String> MkDynamicTemplateProposals = new ArrayList<String>();
62:         public void computeCompletionProposals(VdmCompletionContext info,
63:                         VdmDocument document, List<ICompletionProposal> proposals,
64:                         int offset, ITextViewer viewer,TemplateContext context)
65:         {
66:                 List<ICompletionProposal> calculatedProposals = new Vector<ICompletionProposal>();
67:                 VdmCompletionExtractor vdmCompletionExtractor = new VdmCompletionExtractor();
68:                 VdmNewCompletionExtractor vdmNewCompletionExtractor = new VdmNewCompletionExtractor();
69:                 
70:                 switch (info.getType())
71:                 {
72:                         case CallParam:
73:                                 break;
74:                         case Dot:
75:                                 break;
76:                         case Mk:
77:                                 completeMK(info, document, calculatedProposals, offset, context, viewer);
78:                                 break;
79:                         case New:
80:                                 vdmNewCompletionExtractor.generateNewCompletionProposals(info, document, calculatedProposals, offset, getAst(document), context, viewer);
81:                                 break;
82:                         case Quote:
83:                                 completeQuotes(info, document, calculatedProposals, offset);
84:                                 break;
85:                         case Types:
86:                                 completeTypes(info, document, calculatedProposals, offset);
87:                                 vdmCompletionExtractor.generateCompleteProposals(info, document, calculatedProposals, offset, getAst(document), context,viewer);
88:                                 break;
89:                         default:
90:                                 break;
91:
92:                 }
93:
94:                 List<String> replacementDisplayString = new Vector<String>();
95:                 for (ICompletionProposal proposal : calculatedProposals)
96:                 {
97:                         if (proposal instanceof CompletionProposal)
98:                         {
99:                                 CompletionProposal cp = (CompletionProposal) proposal;
100:                                 if (replacementDisplayString.contains(cp.getDisplayString()))
101:                                 {
102:                                         continue;
103:                                 }
104:
105:                                 replacementDisplayString.add(cp.getDisplayString());
106:                         }
107:                         proposals.add(proposal);
108:
109:                 }
110:         }
111:
112:         private void completeMK(VdmCompletionContext info, VdmDocument document,
113:                         List<ICompletionProposal> calculatedProposals, int offset, final TemplateContext context,final ITextViewer viewer) {
114:
115:                 for(INode def : getAst(document))
116:                 {
117:                         completeRecords(offset, calculatedProposals, info, def, context, viewer);
118:                         completeMk_tokens(offset,calculatedProposals,info, def);
119:                         completetuples(offset, calculatedProposals, info,def);
120:                 }
121:         }
122:
123:         private void completeQuotes(final VdmCompletionContext info,
124:                         VdmDocument document, final List<ICompletionProposal> proposals,
125:                         int offset)
126:         {
127:                 for (INode def : getAst(document))
128:                 {
129:                         completeQuotes(offset, proposals, info, def);
130:                 }
131:
132:         }
133:
134:         private void completeRecords(final int offset,
135:                         final List<ICompletionProposal> calculatedProposals,
136:                         VdmCompletionContext info, final INode def, final TemplateContext context,final ITextViewer viewer) {
137:
138:                 try {
139:                         def.apply(new DepthFirstAnalysisAdaptor() {
140:                                 @Override
141:                                 public void caseARecordInvariantType(ARecordInvariantType arg0)
142:                                                 throws AnalysisException {
143:                                         
144:                                         String name = arg0.getName().getName();
145:
146:                                         String[] functionName = {name, (name + "(")};
147:                                         String displayString = functionName[1];
148:                                         
149:                                         String sep = "";
150:                                  List<String> parameterNameList = new ArrayList<String>();
151:
152:                                         for (Iterator<AFieldField> iterator = arg0.getFields().iterator(); iterator.hasNext();)
153:                                         {
154:                                                 AFieldField field = iterator.next();
155:                                                 parameterNameList.add(field.getTagname().getName());
156:                                                 displayString += sep + field.toString();
157:                                                 sep = ", ";
158:                                         }
159:
160:                                  String extractedName[] = VdmHelper.templatePatternGenerator(parameterNameList,functionName);
161:                                  extractedName[0] = displayString + ")";
162:                                         if(VdmHelper.nullOrEmptyCheck(extractedName[1]) && !VdmHelper.checkForDuplicates(extractedName[1],MkDynamicTemplateProposals)){
163:                                                 VdmHelper.dynamicTemplateCreator(extractedName,"MK Record",offset,context,calculatedProposals,info,viewer,arg0.getLocation().getEndOffset());
164:                                                 MkDynamicTemplateProposals.add(extractedName[1]);
165:                                         }                                
166:                                 }
167:                         });
168:                 } catch (AnalysisException e) {
169:                         VdmUIPlugin.log("Completion error in " + getClass().getSimpleName()
170:                                         + "faild during record search", e);
171:                 }
172:                 
173:         }
174:         
175:         private void completetuples(final int offset,
176:                         final List<ICompletionProposal> calculatedProposals,
177:                         final VdmCompletionContext info, final INode def) {
178:                 try
179:                 {
180:                         def.apply(new DepthFirstAnalysisAdaptor() {                        
181:                                 @Override
182:                                 public void caseAProductType(AProductType arg0)
183:                                                 throws AnalysisException {
184:                                         
185:                                         String name = arg0.toString();
186:                                         IContextInformation info = new ContextInformation(name, name);
187:                                         
188:                                         String replacementString = "(";
189:                                         //String display = replacementString;
190:                                         
191:                                         String sep = "";
192:                                         
193:•                                        for(Iterator<PDefinition> iterator = arg0.getDefinitions().iterator(); iterator.hasNext();)
194:                                         {
195:                                                 PDefinition type = iterator.next();
196:                                                 replacementString += sep + type.getType();
197:                                                 //display += sep + type.toString();
198:                                                 sep = ", ";
199:                                         }
200:                                         replacementString += ")";
201:                                         //display += ")";
202:                                         
203:                                         calculatedProposals.add(new CompletionProposal(replacementString , offset, 0, replacementString.length(), imgProvider.getImageLabel(def, 0), replacementString, info, replacementString));
204:                                 }
205:                         });
206:                         
207:                 }
208:                 catch(AnalysisException e)
209:                 {
210:                         VdmUIPlugin.log("Completion error in " + getClass().getSimpleName()
211:                                         + "faild during tuple search", e);
212:                 }
213:                 //
214:                 
215:                 
216:         }
217:         
218:         private void completeMk_tokens(final int offset,
219:                         final List<ICompletionProposal> calculatedProposals,
220:                         final VdmCompletionContext info, INode def) {
221:                 
222:                 String name = "token()";
223:                 String display = "mk_token() Token Representation, can take an arbitary expression";
224:                 IContextInformation ctxtInfo = new ContextInformation(name, name); //$NON-NLS-1$
225:                 calculatedProposals.add(new CompletionProposal(name, offset, 0, name.length() - 1, imgProvider.getImageLabel(def, 0), name, ctxtInfo, display));
226:                 
227:         }
228:
229:         public void completeTypes(VdmCompletionContext info, VdmDocument document,
230:                         List<ICompletionProposal> proposals, int offset)
231:         {
232:                 for (INode element : getAst(document))
233:                 {
234:                         if (info.getType() == SearchType.Types)
235:                         {
236:                                 String name = AstNameUtil.getName(element);
237:                                 String prefix = info.getProposalPrefix();
238:                                 if (VdmHelper.findInString(prefix, name) || name.length() == 0)
239:                                 {
240:                                         IContextInformation ctxtInfo = new ContextInformation(name, name); //$NON-NLS-1$
241:                                         int newOffset = offset - prefix.length();
242:                                         int prefixLength = prefix.length();
243:                                         
244:                                         proposals.add(new CompletionProposal(name, newOffset, prefixLength, name.length(), imgProvider.getImageLabel(element, 0), name, ctxtInfo, name));
245:                                 }
246:                         }
247:                         //addContainerTypes(element, offset, proposals, info);
248:                 }
249:         }
250:
251:         private void addContainerTypes(INode def, final int offset,
252:                         final List<ICompletionProposal> proposals,
253:                         final VdmCompletionContext info)
254:         {
255:                 if (def instanceof SClassDefinition)
256:                 {
257:                         SClassDefinition cd = (SClassDefinition) def;
258:                         for (PDefinition element : cd.getDefinitions())
259:                         {
260:                                 if (element instanceof ATypeDefinition)
261:                                 {
262:                                         String name = cd.getName() + "`" + element.getName();
263:                                         IContextInformation contextInfo = new ContextInformation(name, name); //$NON-NLS-1$
264:                                         if (VdmHelper.findInString(info.getProposalPrefix(), name) || name.length() == 0)
265:                                         {
266:                                                 proposals.add(new CompletionProposal(name, offset, 0, name.length(), imgProvider.getImageLabel(element, 0), name, contextInfo, name));
267:                                         }
268:                                 }
269:
270:                         }
271:                         completeQuotes(offset, proposals, info, def);
272:                 } else if (def instanceof AModuleModules)
273:                 {
274:                         AModuleModules m = (AModuleModules) def;
275:                         for (PDefinition element : m.getDefs())
276:                         {
277:                                 String prefix = "";
278:                                 if (element.getAncestor(AModuleModules.class) != def)
279:                                 {
280:                                         prefix = m.getName() + "`";
281:                                 }
282:
283:                                 if (element instanceof ATypeDefinition)
284:                                 {
285:                                         String name = prefix + element.getName();
286:                                         IContextInformation contextInfo = new ContextInformation(name, name); //$NON-NLS-1$
287:
288:                                         if (name.toLowerCase().startsWith(info.getProposalPrefix().toLowerCase()))
289:                                         {
290:                                                 proposals.add(new CompletionProposal(name, offset
291:                                                                 - info.getProposalPrefix().length(), info.getProposalPrefix().length(), name.length(), imgProvider.getImageLabel(element, 0), name, contextInfo, element.toString()));
292:                                         }
293:                                 }
294:                         }
295:
296:                         completeQuotes(offset, proposals, info, m);
297:                 }
298:
299:         }
300:
301:         private void completeQuotes(final int offset,
302:                         final List<ICompletionProposal> proposals,
303:                         final VdmCompletionContext info2, INode m)
304:         {
305:                 try
306:                 {
307:                         m.apply(new DepthFirstAnalysisAdaptor()
308:                         {
309:                                 @Override
310:                                 public void caseAQuoteLiteralExp(AQuoteLiteralExp node)
311:                                                 throws AnalysisException
312:                                 {
313:                                         populateQuotes(node, node.getValue().getValue(), node.toString());
314:                                 }
315:
316:                                 @Override
317:                                 public void caseAQuoteType(AQuoteType node)
318:                                                 throws AnalysisException
319:                                 {
320:                                         populateQuotes(node, node.getValue().getValue(), node.toString());
321:                                 }
322:
323:                                 void populateQuotes(INode node, String baseValue, String name)
324:                                 {
325:
326:                                         IContextInformation info = new ContextInformation(name, name); //$NON-NLS-1$
327:
328:                                         int curOffset = offset + info2.getReplacementOffset();// - info2.proposalPrefix.length();
329:                                         int length = name.length();
330:                                         int replacementLength = info2.getProposalPrefix().length();
331:
332:                                         if (info2.getProposalPrefix().equals("<" + baseValue + ">"))
333:                                         {
334:                                                 curOffset = offset;
335:                                                 replacementLength = 0;
336:                                         }
337:
338:                                         if (("<" + baseValue).toLowerCase().startsWith(info2.getProposalPrefix().toLowerCase()))
339:                                         {
340:                                                 proposals.add(new CompletionProposal(name, curOffset, replacementLength, length, imgProvider.getImageLabel(node, 0), name, info, name));
341:                                         }
342:                                 }
343:                         });
344:                 } catch (AnalysisException e)
345:                 {
346:                         VdmUIPlugin.log("Completion error in " + getClass().getSimpleName()
347:                                         + "faild during quote search", e);
348:                 }
349:         }
350:
351:         public void completeFields(VdmCompletionContext info, VdmDocument document,
352:                         List<ICompletionProposal> proposals, int offset)
353:         {
354:                 try
355:                 {
356:                         INode found = new AstLocationSearcher2().getNode(new TextReference(document.getSourceUnit().getSystemFile(), offset), getLocalFileAst(document));
357:
358:                         if (found != null)
359:                         {
360:                                 PType type = null;
361:                                 if (found instanceof PExp)
362:                                 {
363:                                         type = ((PExp) found).getType();
364:                                 } else if (found instanceof PStm)
365:                                 {
366:                                         type = ((PStm) found).getType();
367:                                 }
368:
369:                                 if (type instanceof ARecordInvariantType)
370:                                 {
371:                                         ARecordInvariantType rt = (ARecordInvariantType) type;
372:
373:                                         for (AFieldField field : rt.getFields())
374:                                         {
375:                                                 if (field.getTag().toLowerCase().startsWith(info.getProposalPrefix().toLowerCase()))
376:                                                 {
377:                                                         proposals.add(createProposal(field, offset, info));
378:                                                 }
379:                                         }
380:                                 }
381:                         }
382:                 } catch (Exception e)
383:                 {
384:                         VdmUIPlugin.log("Completion error in " + getClass().getSimpleName(), e);
385:                 }
386:         }
387:
388:         private ICompletionProposal createProposal(INode node, int offset,
389:                         VdmCompletionContext info)
390:         {
391:                 String name = AstNameUtil.getName(node);
392:                 if (node instanceof ATypeDefinition)
393:                 {
394:                         name = ((ATypeDefinition) node).getLocation().getModule() + "`"
395:                                         + name;
396:                 }
397:                 IContextInformation info2 = new ContextInformation(name, name); //$NON-NLS-1$
398:                 return new CompletionProposal(name, offset
399:                                 - info.getProposalPrefix().length(), info.getProposalPrefix().length(), name.length(), imgProvider.getImageLabel(node, 0), name, info2, node.toString());
400:         }
401:
402:         private List<INode> getAst(VdmDocument document)
403:         {
404:                 List<INode> ast = new Vector<INode>();
405:                 ast.addAll(document.getProject().getModel().getRootElementList());
406:                 ast.addAll(document.getSourceUnit().getParseList());// maybe add broken parse tree
407:                 return ast;
408:         }
409:
410:         private List<INode> getLocalFileAst(VdmDocument document)
411:         {
412:                 List<INode> ast = new Vector<INode>();
413:                 ast.addAll(document.getSourceUnit().getParseList());
414:                 return ast;
415:         }
416: }