1 11 package org.eclipse.jdt.internal.debug.ui.display; 12 13 14 import org.eclipse.core.runtime.IAdaptable; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.debug.core.DebugException; 17 import org.eclipse.debug.core.ILaunch; 18 import org.eclipse.debug.core.model.IExpression; 19 import org.eclipse.debug.core.model.IValue; 20 import org.eclipse.debug.core.model.IVariable; 21 import org.eclipse.debug.ui.DebugUITools; 22 import org.eclipse.debug.ui.IDebugView; 23 import org.eclipse.jdt.core.IJavaProject; 24 import org.eclipse.jdt.core.IType; 25 import org.eclipse.jdt.core.JavaModelException; 26 import org.eclipse.jdt.debug.core.IJavaArray; 27 import org.eclipse.jdt.debug.core.IJavaStackFrame; 28 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 29 import org.eclipse.jface.text.ITextSelection; 30 import org.eclipse.jface.text.ITextViewer; 31 import org.eclipse.jface.text.contentassist.ICompletionProposal; 32 import org.eclipse.jface.viewers.ISelection; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.ui.IWorkbenchPage; 35 import org.eclipse.ui.IWorkbenchWindow; 36 37 public class DetailsCompletionProcessor extends DisplayCompletionProcessor { 38 39 42 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 43 try { 44 setErrorMessage(DisplayMessages.DetailsCompletionProcessor_0); IAdaptable context = DebugUITools.getDebugContext(); 46 if (context == null) { 47 return new ICompletionProposal[0]; 48 } 49 IJavaStackFrame stackFrame= (IJavaStackFrame)context.getAdapter(IJavaStackFrame.class); 50 if (stackFrame == null) { 51 return new ICompletionProposal[0]; 52 } 53 54 setErrorMessage(DisplayMessages.DetailsCompletionProcessor_1); IWorkbenchWindow window= JDIDebugUIPlugin.getActiveWorkbenchWindow(); 56 if (window == null) { 57 return new ICompletionProposal[0]; 58 } 59 IWorkbenchPage page= window.getActivePage(); 60 if (page == null) { 61 return new ICompletionProposal[0]; 62 } 63 IDebugView view= (IDebugView)page.getActivePart(); 64 if (view == null) { 65 return new ICompletionProposal[0]; 66 } 67 ISelection selection= view.getViewer().getSelection(); 68 if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) { 69 return super.computeCompletionProposals(stackFrame, viewer, documentOffset); 70 } 71 72 IStructuredSelection viewerSelection= (IStructuredSelection)selection; 73 if (viewerSelection.size() > 1) { 74 return new ICompletionProposal[0]; 75 } 76 Object element= viewerSelection.getFirstElement(); 77 try { 78 setErrorMessage(null); 79 ITextSelection textSelection= (ITextSelection)viewer.getSelectionProvider().getSelection(); 80 IType receivingType= getReceivingType(stackFrame.getLaunch(), element); 81 if (receivingType == null) { 82 setErrorMessage(DisplayMessages.DetailsCompletionProcessor_2); return new ICompletionProposal[0]; 84 } 85 IJavaProject project = receivingType.getJavaProject(); 86 87 configureResultCollector(project, textSelection); 88 int insertionPosition= computeInsertionPosition(receivingType, stackFrame); 89 receivingType.codeComplete(viewer.getDocument().get().toCharArray(), insertionPosition, documentOffset, 90 new char[0][], new char[0][], 91 new int[0], false, getCollector()); 92 93 return order(getCollector().getJavaCompletionProposals()); 96 } catch (JavaModelException x) { 97 handle(viewer, x); 98 } catch (DebugException de) { 99 handle(viewer, de); 100 } 101 return new ICompletionProposal[0]; 102 } finally { 103 releaseCollector(); 104 } 105 } 106 107 private IType getReceivingType(ILaunch launch, Object element) throws DebugException { 108 String originalTypeName= getReceivingTypeName(element); 109 if (originalTypeName == null) { 110 return null; 111 } 112 113 String sourceName= originalTypeName; 114 int genIndex = sourceName.indexOf('<'); 116 if (genIndex >= 0) { 117 sourceName = sourceName.substring(0, genIndex); 118 } 119 int dollarIndex= sourceName.indexOf('$'); 120 if (dollarIndex >= 0) { 121 sourceName= sourceName.substring(0, dollarIndex); 122 } 123 int index = sourceName.lastIndexOf('.'); 124 if (index >= 0) { 125 sourceName = sourceName.replace('.', IPath.SEPARATOR); 126 } 127 sourceName+=".java"; 129 return resolveType(launch, originalTypeName, sourceName); 130 } 131 132 private String getReceivingTypeName(Object element) { 133 134 IValue value= null; 135 try { 136 if (element instanceof IVariable) { 137 value= ((IVariable)element).getValue(); 138 if (value instanceof IJavaArray) { 139 return null; 140 } 141 } else if (element instanceof IExpression) { 142 value= ((IExpression)element).getValue(); 143 } 144 if (value != null) { 145 return value.getReferenceTypeName(); 146 } 147 } catch (DebugException de) { 148 JDIDebugUIPlugin.log(de); 149 } 150 151 return null; 152 } 153 } 154 | Popular Tags |