Package: VdmCompletionContext$CharacterOrder

VdmCompletionContext$CharacterOrder

nameinstructionbranchcomplexitylinemethod
VdmCompletionContext.CharacterOrder(VdmCompletionContext, String, int, int)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package org.overture.ide.ui.templates;
2:
3: import java.util.ArrayList;
4: import java.util.Iterator;
5: import java.util.List;
6: import java.util.Vector;
7: import java.util.regex.Matcher;
8: import java.util.regex.Pattern;
9:
10: public class VdmCompletionContext
11: {
12:         private StringBuffer rawScan;
13:         private StringBuffer processedScan;
14:
15: /**
16:          * Index where replacement must start. For example, if the
17:          * rawScan is '<q' then the offset is -2 since the complete text '<q' should be replaced
18:          */
19:         private SearchType type = SearchType.Types;
20:
21:         private String proposalPrefix = "";
22:
23:         private List<String> root = new Vector<String>();
24:
25:         public VdmCompletionContext(StringBuffer rawScan)
26:         {
27:                 this.rawScan = rawScan;
28:                 init();
29:         }
30:
31:         private void init()
32:         {
33:                 String[] sepList = {"\n", "\r", "\t", "."};
34:                 proposalPrefix = regexStringCleaner(rawScan.toString(),sepList);
35:                 List<CharacterOrder> specialCharMatches = specialCharacterOrderExtractor(rawScan.toString());
36:                 
37:                 if (checkLastSpecialCharacter(specialCharMatches, "<"))
38:                 {
39:                         // Completion of quote, e.g. <Green>
40:                         consQuoteContext(specialCharMatches.get(specialCharMatches.size()-1).start);
41:                         return;
42:                 }
43:                 
44:                 if (proposalPrefix.contains("new"))
45:                 {
46:                         // Completion of constructors
47:                         consConstructorCallContext();
48:                         return;
49:                 }
50:                 
51:                 if(proposalPrefix.contains("mk_") && checkLastSpecialCharacter(specialCharMatches, "_"))
52:                 {
53:                         consMkContext();
54:                         return;
55:                 }
56:
57:                 // Completion for foo.bar. This covers things such as instance variables,
58:                 // values, a record, a tuple, operations and functions
59:         
60:                 if (checkLastSpecialCharacter(specialCharMatches, "."))
61:                 { // only works for one . atm
62:                         String[] split = rawScan.toString().split("\\.");
63:                         consDotContext(split);
64:                         return;
65:                 }
66:                 
67:                 String[] typeSepList = { " ", "\n", "\r", "\t", "."};
68:                 proposalPrefix = regexStringCleaner(proposalPrefix,typeSepList);                
69:         }
70:         
71:         private String regexStringCleaner(String proposalPrefix,String[] sepList){
72:                 //Default
73:                 
74:                 if( proposalPrefix == null || proposalPrefix.isEmpty()){
75:                         return "";
76:                 }
77:                 
78:                 char charMatch = proposalPrefix.charAt(proposalPrefix.length() - 1); //checks the end of the string
79:                 if(charMatch == '\r' || charMatch == '\n' || charMatch == '\t' )
80:                 {
81:                         proposalPrefix = "";
82:                 }
83:                 else{
84:                         StringBuffer regexp = new StringBuffer("");
85:                         regexp.append("[");
86:                         for(String s : sepList) {
87:                          regexp.append("[");
88:                          regexp.append(Pattern.quote(s));
89:                          regexp.append("]");
90:                         }
91:                         regexp.append("]");
92:                         String[] bits = proposalPrefix.trim().split(regexp.toString());
93:                         proposalPrefix = bits[bits.length-1];
94:                         proposalPrefix = proposalPrefix.trim();
95:
96:                 }
97:                 return proposalPrefix;
98:         }
99:         
100:         private void consDotContext(String[] split)
101:         {
102:                 this.type = SearchType.Dot;
103:                 if(split.length >= 2 ){
104:                         this.proposalPrefix = split[1];
105:                 }
106:                 else{
107:                         this.proposalPrefix = split[0];
108:                 }
109:                 this.root = new Vector<>();
110:                 root.add(split[0]);
111:                 
112:         }
113:
114:         /**
115:          * Constructs the completion context for a constructor call
116:          *
117:          * @param index
118:          */
119:         private void consConstructorCallContext()
120:         {
121:                 // This gives us everything after new, e.g. ' MyClass' if you type 'new MyClass'
122:                 proposalPrefix = rawScan.subSequence(rawScan.indexOf("new"),rawScan.length()).toString();
123:
124:                 processedScan = rawScan; //new StringBuffer(subSeq);
125:                 type = SearchType.New;
126:         }
127:         
128:         /**
129:          * Constructs the completion context for a 'mk_' call
130:          *
131:          */
132:         private void consMkContext() {
133:                 
134:                 final int MK_LENGTH = "mk_".length();
135:                 
136:                 CharSequence subSeq = rawScan.subSequence(MK_LENGTH, rawScan.length());
137:                 processedScan = new StringBuffer(subSeq);
138:                 proposalPrefix = processedScan.toString().trim();
139:                 
140:                 type = SearchType.Mk;
141:         }
142:         
143:         /**
144:          * Contrusts the completion context for the 'mk_token' call
145:          */
146: //        private void consMK_tokenContext() {
147: //                final int MK_LENGTH = "mk_t".length();
148: //                
149: //                CharSequence subSeq = rawScan.subSequence(MK_LENGTH, rawScan.length());
150: //                processedScan = new StringBuffer(subSeq);
151: //                proposalPrefix = processedScan.toString().trim();
152: //                
153: //                type = SearchType.Mk;
154: //                
155: //        }
156:
157: /**
158:          * Constructs the completion context for quotes
159:          *
160:          * @param index The index of the '<' character
161:          */
162:         private void consQuoteContext(int index)
163:         {        
164:                 processedScan = new StringBuffer(proposalPrefix.subSequence(index, proposalPrefix.length()));
165:                 proposalPrefix = processedScan.substring(processedScan.lastIndexOf("<"));
166:                 type = SearchType.Quote;
167:         }
168:
169:         private String getQualifiedSource()
170:         {
171:                 String res = "";
172:                 if (root != null && !root.isEmpty())
173:                 {
174:                         for (Iterator<String> itr = root.iterator(); itr.hasNext();)
175:                         {
176:                                 res += itr.next();
177:                                 if (itr.hasNext())
178:                                         res += ".";
179:                         }
180:                 }
181:                 return res;
182:         }
183:
184:         public int getReplacementOffset()
185:         {
186:                 return -proposalPrefix.length();
187:         }
188:         
189:         public SearchType getType()
190:         {
191:                 return type;
192:         }
193:
194:         public String getProposalPrefix()
195:         {
196:                 return proposalPrefix;
197:         }
198:
199:         public List<String> getRoot()
200:         {
201:                 return root;
202:         }
203:
204:         @Override
205:         public String toString()
206:         {
207:                 return type + " - Root: '" + getQualifiedSource() + "' Proposal: '"
208:                                 + proposalPrefix + "'" + " offset: ";
209:         }
210:         
211:         public List<CharacterOrder> specialCharacterOrderExtractor(String inputString){
212:                 List<CharacterOrder> matches = new ArrayList<CharacterOrder>();
213:          //pattern to compare
214:                 Pattern pattern = Pattern.compile("[^a-zA-Z0-9\\( ]");
215:          String cleanInputString = inputString.replaceAll("[[a-zA-Z0-9]*$]", "");
216:          Matcher matcher = pattern.matcher(cleanInputString);
217:
218:          while (matcher.find()) {
219:                  matches.add(new CharacterOrder(matcherCleanUp(matcher.group()),matcher.start(),matcher.end()));
220:          }
221:          return matches;
222:         }
223:         
224:         public String matcherCleanUp(String match){
225:                 String[] typeSepList = {"\n", "\r", "\t"};
226:                 return regexStringCleaner(match,typeSepList);        
227:         }
228:         
229:         public Boolean checkLastSpecialCharacter(List<CharacterOrder> specialCharMatches, String inputString){
230:
231:                 if (specialCharMatches != null && !specialCharMatches.isEmpty() && inputString.equals(specialCharMatches.get(specialCharMatches.size()-1).content) )
232:                 {
233:                         return true;
234:                 }
235:                 return false;
236:         }
237:         
238:         class CharacterOrder
239:         {
240:                 public CharacterOrder(String c, int s, int e){
241:                         content = c;
242:                         end = e;
243:                         start = s;
244:                 }
245:          public String content;
246:          public int start;
247:          public int end;
248:         };
249:         
250: }