1 11 package org.eclipse.jdt.internal.debug.ui.contentassist; 12 13 import java.util.Arrays ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.jdt.core.IJavaProject; 17 import org.eclipse.jdt.core.IType; 18 import org.eclipse.jdt.internal.ui.JavaPlugin; 19 import org.eclipse.jdt.internal.ui.text.java.JavaParameterListValidator; 20 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateEngine; 21 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator; 22 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 23 import org.eclipse.jface.text.ITextSelection; 24 import org.eclipse.jface.text.ITextViewer; 25 import org.eclipse.jface.text.contentassist.ICompletionProposal; 26 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 27 import org.eclipse.jface.text.contentassist.IContextInformation; 28 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 29 import org.eclipse.jface.text.templates.TemplateContextType; 30 31 37 public class JavaDebugContentAssistProcessor implements IContentAssistProcessor { 38 39 private JavaDebugCompletionProposalCollector fCollector; 40 private IContextInformationValidator fValidator; 41 private TemplateEngine fTemplateEngine; 42 private String fErrorMessage = null; 43 44 private char[] fProposalAutoActivationSet; 45 private CompletionProposalComparator fComparator; 46 private IJavaDebugContentAssistContext fContext; 47 48 public JavaDebugContentAssistProcessor(IJavaDebugContentAssistContext context) { 49 fContext = context; 50 TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType("java"); if (contextType != null) { 52 fTemplateEngine= new TemplateEngine(contextType); 53 } 54 fComparator= new CompletionProposalComparator(); 55 } 56 57 60 public String getErrorMessage() { 61 if (fErrorMessage != null) { 62 return fErrorMessage; 63 } 64 if (fCollector != null) { 65 return fCollector.getErrorMessage(); 66 } 67 return null; 68 } 69 70 76 private void setErrorMessage(String string) { 77 if (string != null && string.length() == 0) { 78 string = null; 79 } 80 fErrorMessage = string; 81 } 82 83 86 public IContextInformationValidator getContextInformationValidator() { 87 if (fValidator == null) { 88 fValidator= new JavaParameterListValidator(); 89 } 90 return fValidator; 91 } 92 93 96 public char[] getContextInformationAutoActivationCharacters() { 97 return null; 98 } 99 100 103 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { 104 return null; 105 } 106 107 110 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 111 setErrorMessage(null); 112 try { 113 IType type = fContext.getType(); 114 IJavaProject project = type.getJavaProject(); 115 116 String [][] locals = fContext.getLocalVariables(); 117 int numLocals = 0; 118 if (locals.length > 0) { 119 numLocals = locals[0].length; 120 } 121 char[][] localVariableNames = new char[numLocals][]; 122 char[][] localVariableTypeNames = new char[numLocals][]; 123 for (int i = 0; i < numLocals; i++) { 124 localVariableNames[i] = locals[0][i].toCharArray(); 125 localVariableTypeNames[i] = locals[1][i].toCharArray(); 126 } 127 128 ITextSelection selection= (ITextSelection)viewer.getSelectionProvider().getSelection(); 129 configureResultCollector(project, selection); 130 131 int[] localModifiers= new int[localVariableNames.length]; 132 Arrays.fill(localModifiers, 0); 133 134 String snippet = viewer.getDocument().get(); 135 char[] charSnippet = fContext.getSnippet(snippet).toCharArray(); 136 type.codeComplete(charSnippet, fContext.getInsertionPosition(), documentOffset, 137 localVariableTypeNames, localVariableNames, 138 localModifiers, fContext.isStatic(), fCollector); 139 140 IJavaCompletionProposal[] results= fCollector.getJavaCompletionProposals(); 141 142 if (fTemplateEngine != null) { 143 fTemplateEngine.reset(); 144 fTemplateEngine.complete(viewer, documentOffset, null); 145 IJavaCompletionProposal[] templateResults= fTemplateEngine.getResults(); 146 147 IJavaCompletionProposal[] total= new IJavaCompletionProposal[results.length + templateResults.length]; 149 System.arraycopy(templateResults, 0, total, 0, templateResults.length); 150 System.arraycopy(results, 0, total, templateResults.length, results.length); 151 results= total; 152 } 153 return order(results); 156 } catch (CoreException x) { 157 setErrorMessage(x.getStatus().getMessage()); 158 } finally { 159 releaseCollector(); 160 } 161 162 return null; 163 } 164 165 168 private IJavaCompletionProposal[] order(IJavaCompletionProposal[] proposals) { 169 Arrays.sort(proposals, fComparator); 170 return proposals; 171 } 172 173 176 private void configureResultCollector(IJavaProject project, ITextSelection selection) { 177 fCollector = new JavaDebugCompletionProposalCollector(project); 178 if (selection.getLength() != 0) { 179 fCollector.setReplacementLength(selection.getLength()); 180 } 181 } 182 183 188 public void orderProposalsAlphabetically(boolean order) { 189 fComparator.setOrderAlphabetically(order); 190 } 191 192 195 public char[] getCompletionProposalAutoActivationCharacters() { 196 return fProposalAutoActivationSet; 197 } 198 199 205 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) { 206 fProposalAutoActivationSet= activationSet; 207 } 208 209 212 private void releaseCollector() { 213 if (fCollector != null && fCollector.getErrorMessage().length() > 0 && fErrorMessage != null) { 214 setErrorMessage(fCollector.getErrorMessage()); 215 } 216 fCollector = null; 217 } 218 219 } 220 | Popular Tags |