[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor PythonCompletionProcessor.java,1.12,1.13
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-08-10 12:45:28
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32350/src/org/python/pydev/editor Modified Files: PythonCompletionProcessor.java Log Message: Template proposals not active on auto activation chars. Index: PythonCompletionProcessor.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/PythonCompletionProcessor.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** PythonCompletionProcessor.java 10 Aug 2004 11:39:11 -0000 1.12 --- PythonCompletionProcessor.java 10 Aug 2004 12:45:16 -0000 1.13 *************** *** 24,27 **** --- 24,28 ---- import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; + import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; *************** *** 47,52 **** * This class is responsible for code completion / template completion. */ ! public class PythonCompletionProcessor extends TemplateCompletionProcessor implements IContentAssistProcessor { ! /* (non-Javadoc) * @see org.eclipse.jface.text.templates.TemplateCompletionProcessor#getTemplates(java.lang.String) */ --- 48,56 ---- * This class is responsible for code completion / template completion. */ ! public class PythonCompletionProcessor extends TemplateCompletionProcessor ! implements IContentAssistProcessor { ! /* ! * (non-Javadoc) ! * * @see org.eclipse.jface.text.templates.TemplateCompletionProcessor#getTemplates(java.lang.String) */ *************** *** 55,67 **** } ! /* (non-Javadoc) ! * @see org.eclipse.jface.text.templates.TemplateCompletionProcessor#getContextType(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion) */ ! protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) { ! return PydevPlugin.getDefault().getContextTypeRegistry().getContextType(PyContextType.PY_CONTEXT_TYPE); } ! ! /* (non-Javadoc) * @see org.eclipse.jface.text.templates.TemplateCompletionProcessor#getImage(org.eclipse.jface.text.templates.Template) */ --- 59,77 ---- } ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.templates.TemplateCompletionProcessor#getContextType(org.eclipse.jface.text.ITextViewer, ! * org.eclipse.jface.text.IRegion) */ ! protected TemplateContextType getContextType(ITextViewer viewer, ! IRegion region) { ! return PydevPlugin.getDefault().getContextTypeRegistry() ! .getContextType(PyContextType.PY_CONTEXT_TYPE); } ! /* ! * (non-Javadoc) ! * * @see org.eclipse.jface.text.templates.TemplateCompletionProcessor#getImage(org.eclipse.jface.text.templates.Template) */ *************** *** 71,316 **** } ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, ! * int) ! */ ! int docBoundary = -1; // the document prior to the activation token ! int docBoundary2 = 0; ! public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, ! int documentOffset) { ! List propList = new ArrayList(); ! IDocument doc = viewer.getDocument(); ! //System.out.println("The document:"+doc.get()); ! Point selectedRange = viewer.getSelectedRange(); ! // there may not be a selected range ! java.lang.String theDoc = doc.get(); ! calcDocBoundary(theDoc, documentOffset); ! String activationToken = this ! .getActivationToken(theDoc, documentOffset); ! //System.out.println("DBG:theActivationToken: " + activationToken); ! theDoc = partialDocument(theDoc, documentOffset); ! java.lang.String qualifier = getQualifier(doc, documentOffset); ! int qlen = qualifier.length(); ! theDoc += "\n"+activationToken; ! //System.out.println("Interpreted doc: " + theDoc); ! //System.out.println("activationToken: " + activationToken); ! Vector theList = autoComplete(theDoc, activationToken); ! //System.out.println("DBG:vector:" + theList); ! for (Iterator iter = theList.iterator(); iter.hasNext();) { ! String element = (String) iter.next(); ! CompletionProposal proposal = new CompletionProposal(element, ! documentOffset - qlen, qlen, element.length()); ! propList.add(proposal); ! } ! ! //templates proposals are added here. ! ICompletionProposal[] templateProposals = super.computeCompletionProposals(viewer, documentOffset); ! propList.addAll(Arrays.asList(templateProposals)); ! ! ! ICompletionProposal[] proposals = new ICompletionProposal[propList ! .size()]; ! // and fill with list elements ! propList.toArray(proposals); ! // Return the proposals ! return proposals; ! } ! private Vector autoComplete(java.lang.String theCode, ! java.lang.String theActivationToken) { ! Vector theList = new Vector(); ! String s = new String(); ! File tmp = null; ! //System.out.println("DBG:autoComplete"); ! try { ! // get the inspect.py file from the package: ! s = getAutoCompleteScript(); ! //System.out.println("DBG:getAutoCompleteScript() returns" + s); ! } catch (CoreException e) { ! //System.out.println("DBG:getAutoCompleteScript() fails " + e); ! e.printStackTrace(); ! } ! // ! try { ! tmp = bufferContent(theCode); ! if (tmp == null) { ! System.out ! .println("DBG:bufferContent() null. No tip for you!!"); ! return theList; ! } ! String ss = new String("python " + s + " " ! + tmp.getAbsolutePath()); ! //System.out.println("DBG:exec string " + ss); ! Process p = Runtime.getRuntime().exec(ss); ! BufferedReader in = new BufferedReader(new InputStreamReader(p ! .getInputStream())); ! String str; ! while ((str = in.readLine()) != null) { ! if(!str.startsWith("tip: ")) continue; ! str = str.substring(5); ! //System.out.println("DBG:autoComplete:output: " + str); ! theList.add(str); ! } ! in.close(); ! InputStream eInput = p.getErrorStream(); ! BufferedReader eIn = new BufferedReader(new InputStreamReader( ! eInput)); ! while ((str = eIn.readLine()) != null) { ! //System.out.println("error output: " + str); ! } ! p.waitFor(); ! } catch (IOException e) { ! // TODO Auto-generated catch block ! e.printStackTrace(); ! } catch (InterruptedException e) { ! // TODO Auto-generated catch block ! //System.out.println("Interrupted call: error output: "); ! e.printStackTrace(); ! } ! return theList; ! } ! /** ! * @param theCode ! */ ! private File bufferContent(java.lang.String theCode) { ! // ! try { ! // Create temp file. ! File temp = File.createTempFile("PyDev", ".tmp"); ! // Delete temp file when program exits. ! temp.deleteOnExit(); ! // Write to temp file ! BufferedWriter out = new BufferedWriter(new FileWriter(temp)); ! out.write(theCode); ! out.close(); ! return temp; ! } catch (IOException e) { ! return null; ! } ! } ! public static String getAutoCompleteScript() throws CoreException { ! String targetExec = "tipper.py"; ! //System.out.println("DBG:getAutoCompleteScript();"); ! IPath relative = new Path("PySrc").addTrailingSeparator().append( ! targetExec); ! //System.out.println("DBG:getAutoCompleteScript(); relative " + relative); ! Bundle bundle = PydevPlugin.getDefault().getBundle(); ! //System.out.println("DBG:getAutoCompleteScript(); bundle " + bundle); ! URL bundleURL = Platform.find(bundle, relative); ! URL fileURL; ! try { ! fileURL = Platform.asLocalURL(bundleURL); ! String filePath = new File(fileURL.getPath()).getAbsolutePath(); ! //System.out.println("DBG:getAutoCompleteScript();filePath " + filePath); ! return filePath; ! } catch (IOException e) { ! throw new CoreException(PydevPlugin.makeStatus(IStatus.ERROR, ! "Can't find python debug script", null)); ! } ! } ! /** ! * @param qualifier ! * @param documentOffset ! * @param proposals ! */ ! public void calcDocBoundary(String theDoc, int documentOffset) { ! this.docBoundary = theDoc.substring(0, documentOffset) ! .lastIndexOf('\n'); ! } ! public String getActivationToken(String theDoc, int documentOffset) { ! if (this.docBoundary < 0) { ! calcDocBoundary(theDoc, documentOffset); ! } ! return theDoc.substring(this.docBoundary + 1, documentOffset); ! } ! /** ! * @param theDoc ! */ ! private String partialDocument(String theDoc, int documentOffset) { ! if (this.docBoundary < 0) { ! calcDocBoundary(theDoc, documentOffset); ! } ! String before = theDoc.substring(0, this.docBoundary); ! return before; ! } ! /** ! * @param doc ! * @param documentOffset ! */ ! private java.lang.String getQualifier(IDocument doc, int documentOffset) { ! // this routine should return any partial entry after the activation ! // character ! return ""; ! } ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, ! * int) ! */ ! public IContextInformation[] computeContextInformation(ITextViewer viewer, ! int documentOffset) { ! // TODO Auto-generated method stub ! return null; ! } ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters() ! */ ! public char[] getCompletionProposalAutoActivationCharacters() { ! return new char[] { '.', '(', '[' }; ! } ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters() ! */ ! public char[] getContextInformationAutoActivationCharacters() { ! // is this _really_ what we want to use?? ! return new char[] { '.' }; ! } ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage() ! */ ! public java.lang.String getErrorMessage() { ! // TODO Auto-generated method stub ! return null; ! } ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator() ! */ ! public IContextInformationValidator getContextInformationValidator() { ! // TODO Auto-generated method stub ! return null; ! } } \ No newline at end of file --- 81,364 ---- } ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, ! * int) ! */ ! int docBoundary = -1; // the document prior to the activation token ! int docBoundary2 = 0; ! public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, ! int documentOffset) { ! List propList = new ArrayList(); ! IDocument doc = viewer.getDocument(); ! //System.out.println("The document:"+doc.get()); ! Point selectedRange = viewer.getSelectedRange(); ! // there may not be a selected range ! java.lang.String theDoc = doc.get(); ! calcDocBoundary(theDoc, documentOffset); ! String activationToken = this ! .getActivationToken(theDoc, documentOffset); ! //System.out.println("DBG:theActivationToken: " + activationToken); ! theDoc = partialDocument(theDoc, documentOffset); ! java.lang.String qualifier = getQualifier(doc, documentOffset); ! int qlen = qualifier.length(); ! theDoc += "\n" + activationToken; ! //System.out.println("Interpreted doc: " + theDoc); ! //System.out.println("activationToken: " + activationToken); ! Vector theList = autoComplete(theDoc, activationToken); ! //System.out.println("DBG:vector:" + theList); ! for (Iterator iter = theList.iterator(); iter.hasNext();) { ! String element = (String) iter.next(); ! CompletionProposal proposal = new CompletionProposal(element, ! documentOffset - qlen, qlen, element.length()); ! propList.add(proposal); ! } ! //templates proposals are added here. ! addTemplateProposals(viewer, documentOffset, propList); ! ICompletionProposal[] proposals = new ICompletionProposal[propList ! .size()]; ! // and fill with list elements ! propList.toArray(proposals); ! // Return the proposals ! return proposals; ! } ! /** ! * @param viewer ! * @param documentOffset ! * @param propList ! * ! */ ! protected void addTemplateProposals(ITextViewer viewer, int documentOffset, ! List propList) { ! String str = ""; ! int i = documentOffset - 1; ! if (i <= 0) ! return; ! char c; ! try { ! c = viewer.getDocument().getChar(i); ! while (c != ' ' && c != '\n' && c != '\r' && i > 0) { ! str = c + str; ! i--; ! c = viewer.getDocument().getChar(i); ! } ! char [] chars = getCompletionProposalAutoActivationCharacters(); ! ! //we don't want templates on autocompletion proposals. ! for (int j = 0; j < chars.length; j++) { ! if(str.endsWith(chars[j]+"")) ! return; ! } ! ! ICompletionProposal[] templateProposals = super ! .computeCompletionProposals(viewer, documentOffset); ! propList.addAll(Arrays.asList(templateProposals)); ! } catch (BadLocationException e) { ! e.printStackTrace(); ! } ! } ! private Vector autoComplete(java.lang.String theCode, ! java.lang.String theActivationToken) { ! Vector theList = new Vector(); ! String s = new String(); ! File tmp = null; ! //System.out.println("DBG:autoComplete"); ! try { ! // get the inspect.py file from the package: ! s = getAutoCompleteScript(); ! //System.out.println("DBG:getAutoCompleteScript() returns" + s); ! } catch (CoreException e) { ! //System.out.println("DBG:getAutoCompleteScript() fails " + e); ! e.printStackTrace(); ! } ! // ! try { ! tmp = bufferContent(theCode); ! if (tmp == null) { ! System.out ! .println("DBG:bufferContent() null. No tip for you!!"); ! return theList; ! } ! String ss = new String("python " + s + " " + tmp.getAbsolutePath()); ! //System.out.println("DBG:exec string " + ss); ! Process p = Runtime.getRuntime().exec(ss); ! BufferedReader in = new BufferedReader(new InputStreamReader(p ! .getInputStream())); ! String str; ! while ((str = in.readLine()) != null) { ! if (!str.startsWith("tip: ")) ! continue; ! str = str.substring(5); ! //System.out.println("DBG:autoComplete:output: " + str); ! theList.add(str); ! } ! in.close(); ! InputStream eInput = p.getErrorStream(); ! BufferedReader eIn = new BufferedReader(new InputStreamReader( ! eInput)); ! while ((str = eIn.readLine()) != null) { ! //System.out.println("error output: " + str); ! } ! p.waitFor(); ! } catch (IOException e) { ! // TODO Auto-generated catch block ! e.printStackTrace(); ! } catch (InterruptedException e) { ! // TODO Auto-generated catch block ! //System.out.println("Interrupted call: error output: "); ! e.printStackTrace(); ! } ! return theList; ! } ! /** ! * @param theCode ! */ ! private File bufferContent(java.lang.String theCode) { ! // ! try { ! // Create temp file. ! File temp = File.createTempFile("PyDev", ".tmp"); ! // Delete temp file when program exits. ! temp.deleteOnExit(); ! // Write to temp file ! BufferedWriter out = new BufferedWriter(new FileWriter(temp)); ! out.write(theCode); ! out.close(); ! return temp; ! } catch (IOException e) { ! return null; ! } ! } ! public static String getAutoCompleteScript() throws CoreException { ! String targetExec = "tipper.py"; ! //System.out.println("DBG:getAutoCompleteScript();"); ! IPath relative = new Path("PySrc").addTrailingSeparator().append( ! targetExec); ! //System.out.println("DBG:getAutoCompleteScript(); relative " + ! // relative); ! Bundle bundle = PydevPlugin.getDefault().getBundle(); ! //System.out.println("DBG:getAutoCompleteScript(); bundle " + bundle); ! URL bundleURL = Platform.find(bundle, relative); ! URL fileURL; ! try { ! fileURL = Platform.asLocalURL(bundleURL); ! String filePath = new File(fileURL.getPath()).getAbsolutePath(); ! //System.out.println("DBG:getAutoCompleteScript();filePath " + ! // filePath); ! return filePath; ! } catch (IOException e) { ! throw new CoreException(PydevPlugin.makeStatus(IStatus.ERROR, ! "Can't find python debug script", null)); ! } ! } ! /** ! * @param qualifier ! * @param documentOffset ! * @param proposals ! */ ! public void calcDocBoundary(String theDoc, int documentOffset) { ! this.docBoundary = theDoc.substring(0, documentOffset) ! .lastIndexOf('\n'); ! } ! public String getActivationToken(String theDoc, int documentOffset) { ! if (this.docBoundary < 0) { ! calcDocBoundary(theDoc, documentOffset); ! } ! return theDoc.substring(this.docBoundary + 1, documentOffset); ! } ! /** ! * @param theDoc ! */ ! private String partialDocument(String theDoc, int documentOffset) { ! if (this.docBoundary < 0) { ! calcDocBoundary(theDoc, documentOffset); ! } ! String before = theDoc.substring(0, this.docBoundary); ! return before; ! } ! /** ! * @param doc ! * @param documentOffset ! */ ! private java.lang.String getQualifier(IDocument doc, int documentOffset) { ! // this routine should return any partial entry after the activation ! // character ! return ""; ! } ! ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(org.eclipse.jface.text.ITextViewer, ! * int) ! */ ! public IContextInformation[] computeContextInformation(ITextViewer viewer, ! int documentOffset) { ! // TODO Auto-generated method stub ! return null; ! } ! ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getCompletionProposalAutoActivationCharacters() ! */ ! public char[] getCompletionProposalAutoActivationCharacters() { ! return new char[] { '.', '(', '[' }; ! } ! ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationAutoActivationCharacters() ! */ ! public char[] getContextInformationAutoActivationCharacters() { ! // is this _really_ what we want to use?? ! return new char[] { '.' }; ! } ! ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage() ! */ ! public java.lang.String getErrorMessage() { ! // TODO Auto-generated method stub ! return null; ! } ! ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator() ! */ ! public IContextInformationValidator getContextInformationValidator() { ! // TODO Auto-generated method stub ! return null; ! } } \ No newline at end of file |