Package: VdmTemplateAssistProcessor

VdmTemplateAssistProcessor

nameinstructionbranchcomplexitylinemethod
VdmTemplateAssistProcessor()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
computeCompletionProposals(ITextViewer, int)
M: 123 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
extractPrefix(ITextViewer, int)
M: 46 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
getContextType(ITextViewer, IRegion)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getImage(Template)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getTemplates(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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.List;
26:
27: import org.eclipse.jface.text.BadLocationException;
28: import org.eclipse.jface.text.IDocument;
29: import org.eclipse.jface.text.IRegion;
30: import org.eclipse.jface.text.ITextSelection;
31: import org.eclipse.jface.text.ITextViewer;
32: import org.eclipse.jface.text.Region;
33: import org.eclipse.jface.text.contentassist.ICompletionProposal;
34: import org.eclipse.jface.text.templates.Template;
35: import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
36: import org.eclipse.jface.text.templates.TemplateContext;
37: import org.eclipse.jface.text.templates.TemplateContextType;
38: import org.eclipse.jface.text.templates.TemplateException;
39: import org.eclipse.swt.graphics.Image;
40: import org.overture.ide.ui.VdmPluginImages;
41:
42:
43: public abstract class VdmTemplateAssistProcessor extends TemplateCompletionProcessor {
44:
45:         @Override
46:         protected String extractPrefix(ITextViewer viewer, int offset) {
47:                 int i = offset;
48:                 IDocument document = viewer.getDocument();
49:•                if (i > document.getLength())
50:                         return "";
51:                 try {
52:•                        while (i > 0) {
53:                                 char ch = document.getChar(i - 1);
54:•                                if (!Character.isJavaIdentifierPart(ch))
55:                                         break;
56:                                 i--;
57:                         }
58:•                        if (i > 0) {
59:                                 int j = i;
60:•                                if (document.getChar(j - 1) == '<')
61:                                         i--;
62:                         }
63:                         return document.get(i, offset - i);
64:                 } catch (BadLocationException e) {
65:                         return "";
66:                 }
67:         }
68:
69:         protected Template[] getTemplates(String contextTypeId) {
70:                 VdmTemplateManager manager = VdmTemplateManager.getInstance();
71:                 return manager.getTemplateStore().getTemplates();
72:         }
73:
74:         protected TemplateContextType getContextType(ITextViewer viewer,
75:                         IRegion region) {
76:                 VdmTemplateManager manager = VdmTemplateManager.getInstance();
77:                 
78:                 return manager.getContextTypeRegistry().getContextType(getTempleteContextType());
79: //                                VdmUniversalTemplateContextType.CONTEXT_TYPE);
80:         }
81:
82:         abstract protected String getTempleteContextType();
83:
84:         protected Image getImage(Template template) {
85:                 return VdmPluginImages.get(VdmPluginImages.IMG_OBJS_TEMPLATE);
86:         }
87:
88:         public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
89:                         int offset) {
90:                 ITextSelection selection = (ITextSelection) viewer
91:                                 .getSelectionProvider().getSelection();
92:                 // adjust offset to end of normalized selection
93:•                if (selection.getOffset() == offset)
94:                         offset = selection.getOffset() + selection.getLength();
95:                 String prefix = extractPrefix(viewer, offset);
96:                 Region region = new Region(offset - prefix.length(), prefix.length());
97:                 TemplateContext context = createContext(viewer, region);
98:•                if (context == null)
99:                         return new ICompletionProposal[0];
100:                 context.setVariable("selection", selection.getText()); // name of the selection variables {line, word_selection //$NON-NLS-1$
101:                 Template[] templates = getTemplates(context.getContextType().getId());
102:                 List<ICompletionProposal> matches = new ArrayList<ICompletionProposal>();
103:•                for (int i = 0; i < templates.length; i++) {
104:                         Template template = templates[i];
105:                         try {
106:                                 context.getContextType().validate(template.getPattern());
107:                         } catch (TemplateException e) {
108:                                 continue;
109:                         }
110:•                        if (!prefix.equals("") && prefix.charAt(0) == '<')
111:                                 prefix = prefix.substring(1);
112:•                        if (!prefix.equals("")
113:•                                        && (template.getName().startsWith(prefix) && template
114:•                                                        .matches(prefix, context.getContextType().getId())))
115:                                 matches.add(createProposal(template, context, (IRegion) region,
116:                                                 getRelevance(template, prefix)));
117:                 }
118:                 return matches.toArray(new ICompletionProposal[matches.size()]);
119:         }
120:
121: }