Thread: [Pydev-cvs] org.python.pydev/src/org/python/pydev/editor PyEditConfiguration.java,1.14,1.15 PythonCo
Brought to you by:
fabioz
From: Dana M. <dan...@us...> - 2004-08-09 22:31:08
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8237/src/org/python/pydev/editor Modified Files: PyEditConfiguration.java PythonCompletionProcessor.java Log Message: revamp of the autocompletion code to use only a CPython interpreter Index: PyEditConfiguration.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/PyEditConfiguration.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** PyEditConfiguration.java 6 Aug 2004 17:20:02 -0000 1.14 --- PyEditConfiguration.java 9 Aug 2004 22:30:58 -0000 1.15 *************** *** 297,301 **** assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer)); // Allow automatic activation after 500 msec ! assistant.enableAutoActivation(false); assistant.setAutoActivationDelay(500); Color bgColor = colorCache.getColor(new RGB(230,255,230)); --- 297,301 ---- assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer)); // Allow automatic activation after 500 msec ! assistant.enableAutoActivation(true); assistant.setAutoActivationDelay(500); Color bgColor = colorCache.getColor(new RGB(230,255,230)); Index: PythonCompletionProcessor.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/PythonCompletionProcessor.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** PythonCompletionProcessor.java 6 Aug 2004 17:20:02 -0000 1.8 --- PythonCompletionProcessor.java 9 Aug 2004 22:30:58 -0000 1.9 *************** *** 7,248 **** package org.python.pydev.editor; ! import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.contentassist.IContentAssistProcessor; ! import org.eclipse.jface.text.templates.Template; ! import org.eclipse.jface.text.templates.TemplateCompletionProcessor; ! import org.eclipse.jface.text.templates.TemplateContextType; ! import org.eclipse.swt.graphics.Image; ! import org.python.pydev.editor.templates.PyContextType; import org.python.pydev.plugin.PydevPlugin; ! public class PythonCompletionProcessor extends TemplateCompletionProcessor implements IContentAssistProcessor { ! /* (non-Javadoc) ! * @see org.eclipse.jface.text.templates.TemplateCompletionProcessor#getTemplates(java.lang.String) ! */ ! protected Template[] getTemplates(String contextTypeId) { ! return PydevPlugin.getDefault().getTemplateStore().getTemplates(); ! } ! /* (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) ! */ ! protected Image getImage(Template template) { ! // TODO Auto-generated method stub ! return null; ! } ! } ! ///** ! // * @author Dmoore ! // * ! // * To change the template for this generated type comment go to ! // * Window>Preferences>Java>Code Generation>Code and Comments ! // */ ! //public class PythonCompletionProcessor implements IContentAssistProcessor { ! // ! // /* (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); ! //// System.out.println("DBG:theDoc: "+theDoc); ! // PythonInterpreter interp = initInterpreter(null); ! // java.lang.String qualifier = getQualifier(doc, documentOffset); ! // int qlen = qualifier.length(); ! // try { ! // System.out.println("Interpreted doc: "+theDoc); ! // PyList theList = autoComplete(interp, theDoc, activationToken); ! // PyObject o = new PyObject(); ! // for (int i = 0; i < theList.__len__(); i++) { ! // String p = theList.__getitem__(i).toString(); ! //// System.out.println("Item:" + p); ! // CompletionProposal proposal = ! // new CompletionProposal( ! // p, ! // documentOffset - qlen, ! // qlen, ! // p.length()); ! // propList.add(proposal); ! // } ! // ! // } catch (PyException e) { ! // e.printStackTrace(); ! // } ! // ! // PyObject theCode = null; ! // ! // ICompletionProposal[] proposals = ! // new ICompletionProposal[propList.size()]; ! // // and fill with list elements ! // propList.toArray(proposals); ! // // Return the proposals ! // return proposals; ! // ! // } ! // private PyList autoComplete( ! // PythonInterpreter interp, ! // java.lang.String theCode, ! // java.lang.String theActivationToken) ! // throws PyException { ! // StringBuffer example = new StringBuffer(); ! // interp.exec("from PyDev import jintrospect"); ! //// System.out.println("DBG:from PyDev import jintrospect:done"); ! // interp.exec("class object:pass"); //TODO: REMOVE AFTER JYTHON ADDS SUPPORT TO NEW STYLE CLASSES. ! // interp.exec(theCode); ! // String xCommand = "theList = jintrospect.getAutoCompleteList(command='"+theActivationToken+"', locals=locals())"; ! //// System.out.println("DBG:xCommand:"+xCommand); ! // interp.exec(xCommand); ! // PyList theList = (PyList) interp.get("theList"); ! // return theList; ! // } ! // ! // /** ! // * @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; ! // } ! //// 1. create the PythonSystemState with the embedded $py.class files inserted into its path ! //// 2. ! // protected PythonInterpreter initInterpreter(java.lang.String[] argv) { ! // Properties p = System.getProperties(); ! //// p.setProperty("python.path", "c:\\Jython\\Lib"); ! // PySystemState.initialize(); ! // PythonInterpreter.initialize(System.getProperties(), p, null); ! // return new PythonInterpreter(null, createPySystemState()); ! // } ! // /** ! // * Create a new Python interpreter system state object aware for standard ! // * Jython library. ! // * ! // * @return Python interpreter system state. ! // * ! // * @throws JythonRuntimeException if it fails to locate the Jython ! // * libraries. The exception message will contain an explanation of the ! // * reason to fail. ! // */ ! // private String _jythonLib = null; ! // public PySystemState createPySystemState() { ! // if (_jythonLib == null) { ! // // Locate org.jython plugin and grab the jar location ! // Plugin jythonPlugin = Platform.getPlugin("org.pydev.jython"); ! // ! // String jythonPath = ! // jythonPlugin.getDescriptor().getInstallURL().toString() + "jythonlib.jar"; ! // try { ! // _jythonLib = Platform.asLocalURL(new URL(jythonPath)).getFile(); ! //// System.out.println("_jythonLib:"+_jythonLib); ! // } catch (MalformedURLException e) { ! // System.out.println( ! // "Failed to located Python System library because of invalid URL."+ ! // e); ! // } catch (IOException e) { ! // System.out.println( ! // "Failed to located Python System library because of IO Error."+ ! // e); ! // } ! // } ! // PySystemState result = new PySystemState(); ! // result.path.insert(0, new PyString(_jythonLib + "/Lib")); ! //// System.out.println("result.path: "+result.path); ! // // Location of the jython/python modules ! // return result; ! // } ! // ! //} --- 7,292 ---- package org.python.pydev.editor; ! import java.io.BufferedInputStream; ! import java.io.BufferedReader; ! import java.io.BufferedWriter; ! import java.io.File; ! import java.io.FileWriter; ! import java.io.IOException; ! import java.io.InputStream; ! import java.io.InputStreamReader; ! import java.net.MalformedURLException; ! import java.net.URL; ! import java.util.ArrayList; ! import java.util.Iterator; ! import java.util.List; ! import java.util.Properties; ! import java.util.Vector; ! ! import org.eclipse.core.resources.IFile; ! import org.eclipse.core.resources.ResourcesPlugin; ! import org.eclipse.core.runtime.CoreException; ! import org.eclipse.core.runtime.IPath; ! import org.eclipse.core.runtime.IStatus; ! import org.eclipse.core.runtime.Path; ! import org.eclipse.core.runtime.Platform; ! import org.eclipse.core.runtime.Plugin; ! import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextViewer; + import org.eclipse.jface.text.contentassist.CompletionProposal; + import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContentAssistProcessor; ! import org.eclipse.jface.text.contentassist.IContextInformation; ! import org.eclipse.jface.text.contentassist.IContextInformationValidator; ! import org.eclipse.swt.graphics.Point; ! import org.osgi.framework.Bundle; ! import org.python.parser.ast.If; import org.python.pydev.plugin.PydevPlugin; + /** + * @author Dmoore + * + * To change the template for this generated type comment go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ + public class PythonCompletionProcessor implements IContentAssistProcessor { + /* + * (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); + } ! 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 |