1 11 12 package org.eclipse.jdt.internal.ui.refactoring.contentassist; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.NullProgressMonitor; 20 21 import org.eclipse.swt.graphics.Image; 22 23 import org.eclipse.jface.contentassist.IContentAssistSubjectControl; 24 import org.eclipse.jface.contentassist.ISubjectControlContentAssistProcessor; 25 import org.eclipse.jface.preference.IPreferenceStore; 26 import org.eclipse.jface.resource.ImageDescriptor; 27 28 import org.eclipse.jface.text.ITextViewer; 29 import org.eclipse.jface.text.contentassist.ICompletionProposal; 30 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 31 import org.eclipse.jface.text.contentassist.IContextInformation; 32 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 33 34 import org.eclipse.jdt.core.CompletionRequestor; 35 import org.eclipse.jdt.core.ICompilationUnit; 36 import org.eclipse.jdt.core.IJavaProject; 37 import org.eclipse.jdt.core.JavaModelException; 38 import org.eclipse.jdt.core.WorkingCopyOwner; 39 import org.eclipse.jdt.core.compiler.IProblem; 40 41 import org.eclipse.jdt.internal.corext.refactoring.StubTypeContext; 42 43 import org.eclipse.jdt.ui.PreferenceConstants; 44 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator; 45 46 import org.eclipse.jdt.internal.ui.JavaPlugin; 47 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 48 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal; 49 import org.eclipse.jdt.internal.ui.text.java.JavaTypeCompletionProposal; 50 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry; 51 52 53 public class CUPositionCompletionProcessor implements IContentAssistProcessor, ISubjectControlContentAssistProcessor { 54 55 private static final ImageDescriptorRegistry IMAGE_DESC_REGISTRY= JavaPlugin.getImageDescriptorRegistry(); 56 57 private String fErrorMessage; 58 private char[] fProposalAutoActivationSet; 59 private CompletionProposalComparator fComparator; 60 61 private CompletionContextRequestor fCompletionContextRequestor; 62 63 private CUPositionCompletionRequestor fCompletionRequestor; 64 65 70 public CUPositionCompletionProcessor(CUPositionCompletionRequestor completionRequestor) { 71 fCompletionRequestor= completionRequestor; 72 73 fComparator= new CompletionProposalComparator(); 74 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 75 String triggers= preferenceStore.getString(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA); 76 fProposalAutoActivationSet = triggers.toCharArray(); 77 } 78 79 87 public void setCompletionContext(final ICompilationUnit cuHandle, final String beforeString, final String afterString) { 88 fCompletionContextRequestor= new CompletionContextRequestor() { 89 final StubTypeContext fStubTypeContext= new StubTypeContext(cuHandle, beforeString, afterString); 90 public StubTypeContext getStubTypeContext() { 91 return fStubTypeContext; 92 } 93 }; 94 if (cuHandle != null) 95 fCompletionRequestor.setJavaProject(cuHandle.getJavaProject()); 96 } 97 98 public void setCompletionContextRequestor(CompletionContextRequestor completionContextRequestor) { 99 fCompletionContextRequestor= completionContextRequestor; 100 } 101 102 107 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 108 Assert.isTrue(false, "ITextViewer not supported"); return null; 110 } 111 112 117 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 118 Assert.isTrue(false, "ITextViewer not supported"); return null; 120 } 121 122 125 public char[] getCompletionProposalAutoActivationCharacters() { 126 return fProposalAutoActivationSet; 127 } 128 129 132 public char[] getContextInformationAutoActivationCharacters() { 133 return null; 134 } 135 136 139 public String getErrorMessage() { 140 return fErrorMessage; 141 } 142 143 146 public IContextInformationValidator getContextInformationValidator() { 147 return null; } 149 150 153 public IContextInformation[] computeContextInformation(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 154 return null; 155 } 156 157 160 public ICompletionProposal[] computeCompletionProposals(IContentAssistSubjectControl contentAssistSubjectControl, int documentOffset) { 161 if (fCompletionContextRequestor.getOriginalCu() == null) 162 return null; 163 String input= contentAssistSubjectControl.getDocument().get(); 164 if (documentOffset == 0) 165 return null; 166 ICompletionProposal[] proposals= internalComputeCompletionProposals(documentOffset, input); 167 Arrays.sort(proposals, fComparator); 168 return proposals; 169 } 170 171 private ICompletionProposal[] internalComputeCompletionProposals(int documentOffset, String input) { 172 String cuString= fCompletionContextRequestor.getBeforeString() + input + fCompletionContextRequestor.getAfterString(); 173 ICompilationUnit cu= null; 174 try { 175 182 cu= fCompletionContextRequestor.getOriginalCu().getWorkingCopy(new WorkingCopyOwner() {}, null, new NullProgressMonitor()); 183 cu.getBuffer().setContents(cuString); 184 int cuPrefixLength= fCompletionContextRequestor.getBeforeString().length(); 185 fCompletionRequestor.setOffsetReduction(cuPrefixLength); 186 cu.codeComplete(cuPrefixLength + documentOffset, fCompletionRequestor); 187 188 JavaCompletionProposal[] proposals= fCompletionRequestor.getResults(); 189 if (proposals.length == 0) { 190 String errorMsg= fCompletionRequestor.getErrorMessage(); 191 if (errorMsg == null || errorMsg.trim().length() == 0) 192 errorMsg= RefactoringMessages.JavaTypeCompletionProcessor_no_completion; 193 fErrorMessage= errorMsg; 194 } else { 195 fErrorMessage= fCompletionRequestor.getErrorMessage(); 196 } 197 return proposals; 198 } catch (JavaModelException e) { 199 JavaPlugin.log(e); 200 return null; 201 } finally { 202 try { 203 if (cu != null) 204 cu.discardWorkingCopy(); 205 } catch (JavaModelException e) { 206 JavaPlugin.log(e); 207 } 208 } 209 } 210 211 protected abstract static class CUPositionCompletionRequestor extends CompletionRequestor { 212 public static final char[] TRIGGER_CHARACTERS= new char[] { '.' }; 213 214 private int fOffsetReduction; 215 private IJavaProject fJavaProject; 216 217 private List fProposals; 218 private String fErrorMessage2; 219 220 public IJavaProject getJavaProject() { 221 return fJavaProject; 222 } 223 224 private void setJavaProject(IJavaProject javaProject) { 225 fJavaProject= javaProject; 226 } 227 228 private void setOffsetReduction(int offsetReduction) { 229 fOffsetReduction= offsetReduction; 230 fProposals= new ArrayList (); 231 } 232 233 public final void completionFailure(IProblem error) { 234 fErrorMessage2= error.getMessage(); 235 } 236 237 public final JavaCompletionProposal[] getResults() { 238 return (JavaCompletionProposal[]) fProposals.toArray(new JavaCompletionProposal[fProposals.size()]); 239 } 240 241 public final String getErrorMessage() { 242 return fErrorMessage2; 243 } 244 245 protected final void addAdjustedCompletion(String name, String completion, 246 int start, int end, int relevance, ImageDescriptor descriptor) { 247 JavaCompletionProposal javaCompletionProposal= new JavaCompletionProposal(completion, start - fOffsetReduction, end - start, 248 getImage(descriptor), name, relevance); 249 javaCompletionProposal.setTriggerCharacters(TRIGGER_CHARACTERS); 250 fProposals.add(javaCompletionProposal); 251 } 252 253 protected final void addAdjustedTypeCompletion(String name, String completion, 254 int start, int end, int relevance, ImageDescriptor descriptor, String fullyQualifiedName) { 255 JavaTypeCompletionProposal javaCompletionProposal= new JavaTypeCompletionProposal( 256 fullyQualifiedName == null ? completion : fullyQualifiedName, null, 257 fullyQualifiedName == null ? start - fOffsetReduction : 0, 258 end - start, getImage(descriptor), name, relevance, completion); 259 javaCompletionProposal.setTriggerCharacters(TRIGGER_CHARACTERS); 260 fProposals.add(javaCompletionProposal); 261 } 262 263 private static Image getImage(ImageDescriptor descriptor) { 264 return (descriptor == null) ? null : CUPositionCompletionProcessor.IMAGE_DESC_REGISTRY.get(descriptor); 265 } 266 } 267 } 268 | Popular Tags |