| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.dialogs.stoex; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.HashMap; |
| 8 | import java.util.Map.Entry; |
| 9 | |
| 10 | import org.eclipse.jface.text.ITextViewer; |
| 11 | import org.eclipse.jface.text.contentassist.CompletionProposal; |
| 12 | import org.eclipse.jface.text.contentassist.ContextInformation; |
| 13 | import org.eclipse.jface.text.contentassist.ICompletionProposal; |
| 14 | import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
| 15 | import org.eclipse.jface.text.contentassist.IContextInformation; |
| 16 | import org.eclipse.jface.text.contentassist.IContextInformationValidator; |
| 17 | |
| 18 | import de.uka.ipd.sdq.pcm.repository.CollectionDataType; |
| 19 | import de.uka.ipd.sdq.pcm.repository.CompositeDataType; |
| 20 | import de.uka.ipd.sdq.pcm.repository.DataType; |
| 21 | import de.uka.ipd.sdq.pcm.repository.InnerDeclaration; |
| 22 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
| 23 | |
| 24 | /** |
| 25 | * @author Snowball |
| 26 | * |
| 27 | */ |
| 28 | public class StoExCompletionProcessor implements IContentAssistProcessor { |
| 29 | |
| 30 | private HashMap<String,String> defaultCharacterisations = new HashMap<String, String>(); |
| 31 | private HashMap<String,String> parameterNames = new HashMap<String, String>(); |
| 32 | |
| 33 | private String templatePrefixes = "+-*/%(?:"; |
| 34 | |
| 35 | private StoExTemplateCompletionProcessor templateProcessor; |
| 36 | |
| 37 | /** |
| 38 | * |
| 39 | */ |
| 40 | public StoExCompletionProcessor(Parameter[] context) { |
| 41 | templateProcessor = new StoExTemplateCompletionProcessor(); |
| 42 | |
| 43 | defaultCharacterisations.put("BYTESIZE", "Characterise the memory footprint in bytes"); |
| 44 | defaultCharacterisations.put("NUMBER_OF_ELEMENTS", "Characterise the number of elements of a collection datatype"); |
| 45 | defaultCharacterisations.put("STRUCTURE", "Characterise the structure of a datastructure"); |
| 46 | defaultCharacterisations.put("VALUE", "Characterise the actual value of a variable"); |
| 47 | defaultCharacterisations.put("TYPE", "Characterise the type of a variable"); |
| 48 | |
| 49 | for (int i=0; i<context.length; i++){ |
| 50 | String[] parameterPrefixes = getPrefixesFor(context[i]); |
| 51 | for (String parameterPrefix : parameterPrefixes) { |
| 52 | if (parameterPrefix.startsWith("RETURN")) |
| 53 | parameterNames.put(parameterPrefix, "Call Result " + context[i].getParameterName()); |
| 54 | else |
| 55 | parameterNames.put(parameterPrefix, "Signature Parameter " + context[i].getParameterName()); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } |
| 60 | |
| 61 | private String[] getPrefixesFor(Parameter parameter) { |
| 62 | ArrayList<String> prefixes = new ArrayList<String>(); |
| 63 | prefixes.add(parameter.getParameterName()); |
| 64 | appendDatatypePrefixes(prefixes,parameter.getParameterName(),parameter.getDataType__Parameter()); |
| 65 | return prefixes.toArray(new String[]{}); |
| 66 | } |
| 67 | |
| 68 | private void appendDatatypePrefixes(ArrayList<String> prefixes, |
| 69 | String parameterName, DataType datatype__Parameter) { |
| 70 | if (datatype__Parameter instanceof CollectionDataType) { |
| 71 | prefixes.add(parameterName+".INNER"); |
| 72 | appendDatatypePrefixes(prefixes,parameterName+".INNER", ((CollectionDataType)datatype__Parameter).getInnerType_CollectionDataType()); |
| 73 | } else if (datatype__Parameter instanceof CompositeDataType) { |
| 74 | CompositeDataType cdt = (CompositeDataType) datatype__Parameter; |
| 75 | for (InnerDeclaration inner : cdt.getInnerDeclaration_CompositeDataType()) { |
| 76 | prefixes.add(parameterName+"."+inner.getEntityName()); |
| 77 | appendDatatypePrefixes(prefixes, parameterName+"."+inner.getEntityName(), inner.getDatatype_InnerDeclaration()); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* (non-Javadoc) |
| 83 | * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int) |
| 84 | */ |
| 85 | public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, |
| 86 | int offset) { |
| 87 | ArrayList<ICompletionProposal> resultList=new ArrayList<ICompletionProposal>(); |
| 88 | |
| 89 | String currentText = viewer.getDocument().get(); |
| 90 | |
| 91 | // compute variable characterisation completions (i.e., VALUE, TYPE, etc.) |
| 92 | int lastDotIndex = currentText.substring(0,offset).lastIndexOf("."); |
| 93 | if (isCharactersationCompletionApplicable(lastDotIndex,currentText)){ |
| 94 | String typedFragment = currentText.substring(lastDotIndex+1, offset); |
| 95 | addCompletionProposalsString(resultList, lastDotIndex, typedFragment, defaultCharacterisations); |
| 96 | } |
| 97 | |
| 98 | // compute parameter from context completions (i.e., current input parameters) |
| 99 | int lastIndex = getLastIndexOfTemplatePrefix(offset, currentText); |
| 100 | if (isStartOfAtom(lastIndex, currentText)){ |
| 101 | String typedFragment = currentText.substring(lastIndex+1, offset).trim(); |
| 102 | addCompletionProposalsString(resultList, lastIndex, typedFragment, parameterNames); |
| 103 | } |
| 104 | // compute template completions (i.e., IntPMF, DoublePDF, etc.) |
| 105 | if (isStartOfAtom(lastIndex, currentText)) { |
| 106 | String typedFragment = currentText.substring(lastIndex+1, offset).trim(); |
| 107 | for (ICompletionProposal p : templateProcessor.computeCompletionProposals(viewer, offset)){ |
| 108 | if (p.getDisplayString().toUpperCase().startsWith(typedFragment.toUpperCase())){ |
| 109 | resultList.add(p); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return resultList.toArray(new ICompletionProposal[]{}); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Checks, whether the user is characterising a variable (i.e., typed a dot) |
| 119 | * |
| 120 | * @param offset |
| 121 | * @param currentText |
| 122 | * @return |
| 123 | */ |
| 124 | private boolean isCharactersationCompletionApplicable(int offset, String currentText){ |
| 125 | //Is letter or underscore |
| 126 | if(offset-1>=0 && offset-1 < currentText.length() && isIDChar(currentText.charAt(offset-1), 0)) |
| 127 | return true; |
| 128 | else if(offset-1>=0 && offset-1 < currentText.length() && isIDChar(currentText.charAt(offset-1), 1)) |
| 129 | { |
| 130 | //Backtrace till we find an IDChar that is no DIGIT |
| 131 | int i = 2; |
| 132 | while( offset-i>=0 && !isIDChar(currentText.charAt(offset-i), 0) && isIDChar(currentText.charAt(offset-i), 1)) |
| 133 | { |
| 134 | i++; |
| 135 | } |
| 136 | return offset-i>=0 && isIDChar(currentText.charAt(offset-i), 0); |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Checks, whether user started to type a atom. |
| 145 | * |
| 146 | * @param offset |
| 147 | * @param currentText |
| 148 | * @return |
| 149 | */ |
| 150 | private boolean isStartOfAtom(int offset, String currentText) { |
| 151 | if (offset+1 < currentText.length() // cursor is not at last character |
| 152 | && offset+1 >= 0){ // cursor is not at first character |
| 153 | currentText = currentText.substring(offset+1); // cut of everything before cursor |
| 154 | } |
| 155 | |
| 156 | String trimText = currentText.trim(); |
| 157 | |
| 158 | if (trimText.equals("")) |
| 159 | // only whitespace after offset |
| 160 | return true; |
| 161 | |
| 162 | char lastChar = trimText.charAt(trimText.length()-1); |
| 163 | if (templatePrefixes.indexOf(lastChar) >= 0) |
| 164 | // last character before current offset is one of the template prefixes |
| 165 | return true; |
| 166 | |
| 167 | boolean hasIDChars = true; |
| 168 | for (int i=0; i<trimText.length(); i++){ |
| 169 | char c = trimText.charAt(i); |
| 170 | if (!isIDChar(c,i)) hasIDChars = false; |
| 171 | } |
| 172 | return hasIDChars; |
| 173 | |
| 174 | } |
| 175 | |
| 176 | // As defined in the ANTLR grammar |
| 177 | private static boolean isIDChar(char c, int index) |
| 178 | { |
| 179 | return Character.isLetter(c) || c == '.' || c == '_' || (index>0 && Character.isDigit(c)); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | private int getLastIndexOfTemplatePrefix(int offset, String currentText) { |
| 184 | int lastIndex = -1; |
| 185 | String templatePrefixesAndWS = templatePrefixes + " "; |
| 186 | for (int i=0; i<templatePrefixesAndWS.length(); i++) { |
| 187 | int newLastIndex = currentText.substring(0, offset).lastIndexOf( |
| 188 | templatePrefixesAndWS.charAt(i)); |
| 189 | if (newLastIndex > lastIndex) |
| 190 | lastIndex = newLastIndex; |
| 191 | } |
| 192 | return lastIndex; |
| 193 | } |
| 194 | |
| 195 | private void addCompletionProposalsString( |
| 196 | ArrayList<ICompletionProposal> resultList, |
| 197 | int lastIndex, |
| 198 | String typedFragment, |
| 199 | HashMap<String,String> completions) { |
| 200 | for (Entry<String,String> entry : completions.entrySet()){ |
| 201 | String completion = entry.getKey(); |
| 202 | String description = entry.getValue(); |
| 203 | |
| 204 | if (completion.toUpperCase().startsWith(typedFragment.toUpperCase())){ |
| 205 | IContextInformation info = new ContextInformation( |
| 206 | completion, |
| 207 | description); //$NON-NLS-1$ |
| 208 | resultList.add(new CompletionProposal( |
| 209 | completion, |
| 210 | lastIndex+1, |
| 211 | typedFragment.length(), |
| 212 | completion.length(), |
| 213 | null, |
| 214 | completion + " - "+ description, |
| 215 | info, |
| 216 | description)); //$NON-NLS-1$ |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* (non-Javadoc) |
| 222 | * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters() |
| 223 | */ |
| 224 | public char[] getCompletionProposalAutoActivationCharacters() { |
| 225 | ArrayList<Character> result = new ArrayList<Character>(); |
| 226 | for (String parameterName : parameterNames.keySet()) |
| 227 | result.add(parameterName.charAt(0)); |
| 228 | for (int i=0; i < templatePrefixes.length(); i++) |
| 229 | result.add(templatePrefixes.charAt(i)); |
| 230 | result.add('.'); |
| 231 | char[] realResult = new char[result.size()]; |
| 232 | for (int i=0; i < result.size(); i++) |
| 233 | realResult[i] = result.get(i); |
| 234 | return realResult; |
| 235 | } |
| 236 | |
| 237 | /* (non-Javadoc) |
| 238 | * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, int) |
| 239 | */ |
| 240 | public IContextInformation[] computeContextInformation(ITextViewer viewer, |
| 241 | int offset) { |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | /* (non-Javadoc) |
| 246 | * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters() |
| 247 | */ |
| 248 | public char[] getContextInformationAutoActivationCharacters() { |
| 249 | return null; |
| 250 | } |
| 251 | |
| 252 | /* (non-Javadoc) |
| 253 | * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator() |
| 254 | */ |
| 255 | public IContextInformationValidator getContextInformationValidator() { |
| 256 | return null; |
| 257 | } |
| 258 | |
| 259 | /* (non-Javadoc) |
| 260 | * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage() |
| 261 | */ |
| 262 | public String getErrorMessage() { |
| 263 | return "No proposals available"; |
| 264 | } |
| 265 | } |