[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor PyEditConfiguration.java,1.10,1.11 PythonCo
Brought to you by:
fabioz
From: Dana M. <dan...@us...> - 2004-06-14 01:12:41
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30964/editor Modified Files: PyEditConfiguration.java PythonCompletionProcessor.java Log Message: autocompletion version 1 Index: PyEditConfiguration.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/PyEditConfiguration.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** PyEditConfiguration.java 25 May 2004 21:10:40 -0000 1.10 --- PyEditConfiguration.java 14 Jun 2004 01:12:30 -0000 1.11 *************** *** 278,283 **** */ public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { ! // public final static String PY_SINGLELINE_STRING = "__python_singleline_string"; ! // public final static String PY_MULTILINE_STRING = "__python_multiline_string";); // create a content assistant: ContentAssistant assistant = new ContentAssistant(); --- 278,283 ---- */ public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { ! final String PY_SINGLELINE_STRING = "__python_singleline_string"; ! final String PY_MULTILINE_STRING = "__python_multiline_string"; // create a content assistant: ContentAssistant assistant = new ContentAssistant(); *************** *** 285,295 **** IContentAssistProcessor processor = new PythonCompletionProcessor(); // No code completion in strings ! // assistant.setContentAssistProcessor(processor,PyPartitionScanner.PY_SINGLELINE_STRING ); ! // assistant.setContentAssistProcessor(processor,PyPartitionScanner.PY_MULTILINE_STRING ); assistant.setContentAssistProcessor(processor,IDocument.DEFAULT_CONTENT_TYPE ); 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)); assistant.setProposalSelectorBackground(bgColor); --- 285,295 ---- IContentAssistProcessor processor = new PythonCompletionProcessor(); // No code completion in strings ! assistant.setContentAssistProcessor(processor,PyPartitionScanner.PY_SINGLELINE_STRING ); ! assistant.setContentAssistProcessor(processor,PyPartitionScanner.PY_MULTILINE_STRING ); assistant.setContentAssistProcessor(processor,IDocument.DEFAULT_CONTENT_TYPE ); 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)); assistant.setProposalSelectorBackground(bgColor); Index: PythonCompletionProcessor.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/PythonCompletionProcessor.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PythonCompletionProcessor.java 27 Apr 2004 01:06:57 -0000 1.2 --- PythonCompletionProcessor.java 14 Jun 2004 01:12:30 -0000 1.3 *************** *** 7,13 **** --- 7,26 ---- package org.python.pydev.editor; + import java.io.BufferedReader; + import java.io.FileInputStream; + import java.io.FileNotFoundException; + import java.io.FileReader; + import java.io.IOException; + import java.io.InputStream; + import java.net.MalformedURLException; + import java.net.URL; import java.util.ArrayList; + import java.util.Enumeration; import java.util.List; + import java.util.Properties; + import java.util.ResourceBundle; + import org.eclipse.core.runtime.Platform; + import org.eclipse.core.runtime.Plugin; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; *************** *** 21,24 **** --- 34,51 ---- import org.eclipse.swt.graphics.Point; + import org.python.core.*; + import org.python.pydev.editor.test.Debug; + import org.python.pydev.editor.test.Inspect; + import org.python.pydev.editor.test.Introspect; + import org.python.pydev.editor.test.Jintrospect; + import org.python.pydev.editor.test.Sre_compile; + import org.python.pydev.editor.test.Sre_constants; + import org.python.pydev.editor.test.Token; + import org.python.pydev.editor.test.Tokenize; + import org.python.pydev.editor.test.Re; + import org.python.pydev.editor.test.Sre; + //import org.python.pydev.editor.test.String; + import org.python.pydev.plugin.PydevPlugin; + import org.python.util.PythonInterpreter; /** *************** *** 33,66 **** * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int) */ public ICompletionProposal[] computeCompletionProposals( ITextViewer viewer, int documentOffset) { ! // get the document to be inspected... IDocument doc = viewer.getDocument(); ! // get the current selection range... Point selectedRange = viewer.getSelectedRange(); ! List propList = new ArrayList(); ! if (selectedRange.y > 0) { ! try { ! String text = doc.get(selectedRange.x, selectedRange.y); ! System.out.println("Document text:"+text); ! computeStyleProposals(text, selectedRange, propList); ! } catch (BadLocationException e) { ! // ??? ! return null; } } ! else{ ! //try to retrv a 'qualifier' from the doc. ! // for example, a partial python statement. This might be used to restrict the set of possible proposals ! String qualifier = getQualifier(doc, documentOffset); ! System.out.println("Document qualifier:"+qualifier); ! computeStructureProposals(qualifier, documentOffset, propList); ! } ! // create an array of completion proposals ! ICompletionProposal [] proposals = new ICompletionProposal[propList.size()]; propList.toArray(proposals); return proposals; ! } --- 60,125 ---- * @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 { ! 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(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; } *************** *** 70,148 **** * @param proposals */ ! private void computeStructureProposals(String qualifier, int documentOffset, List propList) { ! // Not sure how to adapt this one either. ! // The HTML example computes a part 'before the planned cursor' and a part 'after the planned cursor' ! // I think that here is where we want to somehow latch the pointer from the current parse tree. ! // we want to look at the text before the '.', and see what from the parse tree completes it... ! int qlen = qualifier.length(); ! for (int i = 0; i < STRUCTTAGS1.length; i++) { ! String startTag = STRUCTTAGS1[i]; ! // Check if proposal matches qualifier ! if (startTag.startsWith(qualifier)){ ! // Yes.. compute entire proposal text ! String text = startTag + STRUCTTAGS2[i]; ! int cursor = startTag.length(); ! // construct proposal ! CompletionProposal proposal = new CompletionProposal(text, documentOffset - qlen, qlen, cursor); ! // add to results ! propList.add(proposal); ! } ! ! } } /** ! * @param doc ! * @param documentOffset */ ! private String getQualifier(IDocument doc, int documentOffset) { ! // use a StringBuffer to collect the bunch of proposals ! // StringBuffer sB = new StringBuffer(); ! // while (true) { ! // try { ! // char c = doc.getChar(--documentOffset); ! // do something here to test if this was the start of a python statement ! // TODO: Figure this out. For sonmething like HTML, the test might be: ! // if (c == '>' || Character.isWhitespace(c) or something like that ! // Not really sure what our equivaent is? ! // for now.. ! // return ""; ! // } catch (BadLocationException e) { ! // return ""; ! // } ! // } ! return ""; ! } ! // Proposal part before cursor... ! // Let's stub in some static ones for the moment... ! private final static String[] STRUCTTAGS1 = ! {"Dana", "Bill", "Aleks", "Fabio"}; ! private final static String[] STRUCTTAGS2 = ! {"Moore", "Wright", "Totic", "Zadrozny"}; ! ! private final static String[] STYLETAGS = ! {"dana", "bill", "aleks", "fabio"}; ! private final static String[] STYLELABELS = ! {"DANA", "BILL", "ALEKS", "FABIO"}; /** ! * @param text ! * @param selectedRange ! * @param proposals */ ! private void computeStyleProposals(String selectedText, Point selectedRange, List propList) { ! // loop thru the styles.. what does this have to do with completion??? ! for (int i = 0; i < STYLETAGS.length; i++) { ! String tag = STYLETAGS[i]; ! // compute replacement text ! String replacement = "<"+tag+">"+ selectedText +"</"+tag+">"; ! int cursor = tag.length()+2; // ?? why plus 2 ! IContextInformation contextInfo = new ContextInformation(null, STYLELABELS[i]+" Style"); ! CompletionProposal proposal = new CompletionProposal(replacement, selectedRange.x, selectedRange.y, cursor, null, STYLELABELS[i], contextInfo, replacement); ! // add that to the list of proposals ! propList.add(proposal); ! } ! } --- 129,162 ---- * @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 ""; } *************** *** 161,165 **** */ public char[] getCompletionProposalAutoActivationCharacters() { ! return new char[] {'.'}; } --- 175,179 ---- */ public char[] getCompletionProposalAutoActivationCharacters() { ! return new char[] { '.', '(' , '['}; } *************** *** 169,173 **** public char[] getContextInformationAutoActivationCharacters() { // is this _really_ what we want to use?? ! return new char[] {'.'}; } --- 183,187 ---- public char[] getContextInformationAutoActivationCharacters() { // is this _really_ what we want to use?? ! return new char[] { '.' }; } *************** *** 175,179 **** * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage() */ ! public String getErrorMessage() { // TODO Auto-generated method stub return null; --- 189,193 ---- * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getErrorMessage() */ ! public java.lang.String getErrorMessage() { // TODO Auto-generated method stub return null; *************** *** 187,190 **** --- 201,250 ---- 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; + } } |