[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/codecompletion ProposalsComparator.java,NON
Brought to you by:
fabioz
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9579/src/org/python/pydev/editor/codecompletion Modified Files: PyCodeCompletion.java CompletionCache.java PythonShell.java PythonCompletionProcessor.java Added Files: ProposalsComparator.java Log Message: Code completion improvements. Starting refactoring integration with bicycle repair man. --- NEW FILE: ProposalsComparator.java --- /* * Created on Sep 14, 2004 * * @author Fabio Zadrozny */ package org.python.pydev.editor.codecompletion; import java.util.Comparator; import org.eclipse.jface.text.contentassist.ICompletionProposal; /** * @author Fabio Zadrozny */ public class ProposalsComparator implements Comparator { public int compare(Object o1, Object o2) { ICompletionProposal p1 = (ICompletionProposal) o1; ICompletionProposal p2 = (ICompletionProposal) o2; return p1.getDisplayString().compareToIgnoreCase(p2.getDisplayString()); } } Index: PythonShell.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/PythonShell.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** PythonShell.java 13 Sep 2004 19:47:54 -0000 1.3 --- PythonShell.java 14 Sep 2004 17:42:14 -0000 1.4 *************** *** 15,27 **** import org.eclipse.core.runtime.CoreException; /** - * TODO: Still only a test!! * @author Fabio Zadrozny */ public class PythonShell { ! public static final int BUFFER_SIZE = 1024; /** * Python server process. --- 15,48 ---- import org.eclipse.core.runtime.CoreException; + import org.python.pydev.plugin.SocketUtil; /** * @author Fabio Zadrozny */ public class PythonShell { + /** + * Reference to a 'global python shell' + */ + private static PythonShell pytonShell; + + + /** + * @return + * @throws CoreException + * @throws IOException + * + */ + public static PythonShell getServerShell() throws IOException, CoreException { + if(pytonShell == null){ + pytonShell = new PythonShell(); + pytonShell.startIt(); + } + return pytonShell; + + } + ! public static final int BUFFER_SIZE = 1024 * 4; /** * Python server process. *************** *** 105,114 **** public void startIt() throws IOException{ try { ! process = Runtime.getRuntime().exec("python "+serverFile.getAbsolutePath()+" 50007 50008"); sleepALittle(); ! socketToWrite = new Socket("127.0.0.1",50007); //we should write in port 50007 ! serverSocket = new ServerSocket(50008); //and read in port 50008 socketToRead = serverSocket.accept(); } catch (IOException e) { --- 126,141 ---- public void startIt() throws IOException{ try { ! ! int pWrite = SocketUtil.findUnusedLocalPort("127.0.0.1", 50000, 55000); ! int pRead = SocketUtil.findUnusedLocalPort("127.0.0.1", 55001, 60000); ! ! if(process != null) ! endIt(); ! process = Runtime.getRuntime().exec("python "+serverFile.getAbsolutePath()+" "+pWrite+" "+pRead); sleepALittle(); ! socketToWrite = new Socket("127.0.0.1",pWrite); //we should write in this port ! serverSocket = new ServerSocket(pRead); //and read in this port socketToRead = serverSocket.accept(); } catch (IOException e) { *************** *** 130,140 **** String str = ""; ! while(str.indexOf("END@@") == -1 ){ byte[] b = new byte[PythonShell.BUFFER_SIZE]; this.socketToRead.getInputStream().read(b); String s = new String(b); str += s; } --- 157,171 ---- String str = ""; ! int j = 0; ! while(str.indexOf("END@@") == -1 && j != 100){ byte[] b = new byte[PythonShell.BUFFER_SIZE]; this.socketToRead.getInputStream().read(b); + // System.out.println("READ:"+new String(b)); + String s = new String(b); str += s; + j++; } *************** *** 142,146 **** str = str.replaceFirst("@@COMPLETIONS",""); //remove END@@ ! return str.substring(0, str.indexOf("END@@")); } --- 173,177 ---- str = str.replaceFirst("@@COMPLETIONS",""); //remove END@@ ! return str.substring(0, str.lastIndexOf("END@@")); } *************** *** 151,154 **** --- 182,186 ---- */ public void write(String str) throws IOException { + // System.out.println("WRITING:"+str); this.socketToWrite.getOutputStream().write(str.getBytes()); } *************** *** 181,185 **** * @throws IOException */ ! void endIt() throws IOException { try { --- 213,217 ---- * @throws IOException */ ! void endIt() { try { *************** *** 189,206 **** } if (process!= null){ ! int i = process.getErrorStream().available(); ! byte b[] = new byte[i]; ! process.getErrorStream().read(b); ! System.out.println(new String(b)); ! ! i = process.getInputStream().available(); ! b = new byte[i]; ! process.getErrorStream().read(b); ! System.out.println(new String(b)); ! ! process.destroy(); try { process.waitFor(); ! } catch (InterruptedException e1) { e1.printStackTrace(); } --- 221,247 ---- } if (process!= null){ ! try { ! int i = process.getErrorStream().available(); ! byte b[] = new byte[i]; ! process.getErrorStream().read(b); ! System.out.println(new String(b)); ! ! i = process.getInputStream().available(); ! b = new byte[i]; ! process.getErrorStream().read(b); ! System.out.println(new String(b)); ! } catch (Exception e1) { ! e1.printStackTrace(); ! } ! ! try { ! process.destroy(); ! } catch (Exception e2) { ! e2.printStackTrace(); ! } ! try { process.waitFor(); ! } catch (Exception e1) { e1.printStackTrace(); } *************** *** 210,214 **** } ! /** --- 251,277 ---- } ! public void sendGoToDirMsg(File file){ ! try { ! if(file.isDirectory() == false){ ! file = file.getParentFile(); ! } ! System.out.println("changing dir:"+file.getAbsolutePath()); ! this.write("@@CHANGE_DIR:"+file.getAbsolutePath()+"END@@"); ! String ok = this.read(); //this should be the ok message... ! ! } catch (IOException e) { ! e.printStackTrace(); ! } ! } ! ! public void sendReloadModulesMsg(){ ! try { ! this.write("@@RELOAD_MODULES_END@@"); ! String ok = this.read(); //this should be the ok message... ! ! } catch (IOException e) { ! e.printStackTrace(); ! } ! } /** *************** *** 217,241 **** */ public List getGlobalCompletions(String str) { ! try { ! this.write("@@GLOBALS:"+str+"\nEND@@"); ! ! return getCompletions(); ! } catch (IOException e) { ! e.printStackTrace(); ! try { ! this.endIt(); ! } catch (IOException e1) { ! // TODO Auto-generated catch block ! e1.printStackTrace(); ! } ! try { ! this.startIt(); ! } catch (IOException e2) { ! // TODO Auto-generated catch block ! e2.printStackTrace(); ! } ! return getInvalidCompletion(); ! } ! } --- 280,284 ---- */ public List getGlobalCompletions(String str) { ! return this.getTheCompletions("@@GLOBALS:"+str+"\nEND@@"); } *************** *** 246,269 **** public List getTokenCompletions(String token, String str) { String s = "@@TOKEN_GLOBALS("+token+"):"+str+"\nEND@@"; ! try { ! this.write(s); ! ! return getCompletions(); ! } catch (IOException e) { ! e.printStackTrace(); ! try { ! this.endIt(); ! } catch (IOException e1) { ! // TODO Auto-generated catch block ! e1.printStackTrace(); ! } ! try { ! this.startIt(); ! } catch (IOException e2) { ! // TODO Auto-generated catch block ! e2.printStackTrace(); ! } ! return getInvalidCompletion(); ! } } --- 289,293 ---- public List getTokenCompletions(String token, String str) { String s = "@@TOKEN_GLOBALS("+token+"):"+str+"\nEND@@"; ! return this.getTheCompletions(s); } *************** *** 275,294 **** public List getClassCompletions(String token, String str) { String s = "@@CLASS_GLOBALS("+token+"):"+str+"\nEND@@"; try { ! this.write(s); return getCompletions(); ! } catch (IOException e) { e.printStackTrace(); ! try { ! this.endIt(); ! } catch (IOException e1) { ! // TODO Auto-generated catch block ! e1.printStackTrace(); ! } try { this.startIt(); } catch (IOException e2) { - // TODO Auto-generated catch block e2.printStackTrace(); } --- 299,317 ---- public List getClassCompletions(String token, String str) { String s = "@@CLASS_GLOBALS("+token+"):"+str+"\nEND@@"; + return this.getTheCompletions(s); + } + + private List getTheCompletions(String str){ try { ! this.write(str); return getCompletions(); ! } catch (Exception e) { e.printStackTrace(); ! ! this.endIt(); try { this.startIt(); } catch (IOException e2) { e2.printStackTrace(); } *************** *** 296,300 **** } } ! /** * @return --- 319,323 ---- } } ! /** * @return Index: PyCodeCompletion.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/PyCodeCompletion.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** PyCodeCompletion.java 13 Sep 2004 19:47:54 -0000 1.5 --- PyCodeCompletion.java 14 Sep 2004 17:42:14 -0000 1.6 *************** *** 6,17 **** package org.python.pydev.editor.codecompletion; - import java.io.BufferedReader; import java.io.File; import java.io.IOException; - import java.io.InputStreamReader; - import java.io.OutputStreamWriter; import java.net.URL; import java.util.ArrayList; - import java.util.Iterator; import java.util.List; --- 6,13 ---- *************** *** 64,78 **** * @param documentOffset * - * @param theCode * @param theActivationToken * @return */ ! public List autoComplete(PyEdit edit, IDocument doc, int documentOffset, java.lang.String theCode, java.lang.String theActivationToken) { ! if(PyCodeCompletionPreferencesPage.useServerTipEnviroment()){ ! return serverCompletion(theActivationToken, edit, doc, documentOffset, theCode); ! }else{ ! return consoleCompletion(theCode); ! } } --- 60,69 ---- * @param documentOffset * * @param theActivationToken * @return */ ! public List autoComplete(PyEdit edit, IDocument doc, int documentOffset, java.lang.String theActivationToken) { ! return serverCompletion(theActivationToken, edit, doc, documentOffset); } *************** *** 82,93 **** * @param doc * @param documentOffset - * @param theCode - * @param theCode */ ! private List serverCompletion(String theActivationToken, PyEdit edit, IDocument doc, int documentOffset, String theCode) { List theList = new ArrayList(); PythonShell serverShell = null; try { ! serverShell = getServerShell(); } catch (Exception e) { throw new RuntimeException(e); --- 73,82 ---- * @param doc * @param documentOffset */ ! private List serverCompletion(String theActivationToken, PyEdit edit, IDocument doc, int documentOffset) { List theList = new ArrayList(); PythonShell serverShell = null; try { ! serverShell = PythonShell.getServerShell(); } catch (Exception e) { throw new RuntimeException(e); *************** *** 98,102 **** String trimmed = theActivationToken.replace('.',' ').trim(); ! if (trimmed.equals("") == false){ List completions; --- 87,91 ---- String trimmed = theActivationToken.replace('.',' ').trim(); ! if (trimmed.equals("") == false && theActivationToken.indexOf('.') != -1){ List completions; *************** *** 115,119 **** } else{ //go to globals - // System.out.println("simpleCompletion - ''"); List completions = serverShell.getGlobalCompletions(docToParse); theList.addAll(completions); --- 104,107 ---- *************** *** 151,156 **** newDoc += wholeDoc.substring(lineInformation.getOffset() + lineInformation.getLength(), docLength); - // System.out.println("DOC:"+newDoc); - } catch (BadLocationException e1) { e1.printStackTrace(); --- 139,142 ---- *************** *** 159,247 **** } - /** - * @return - * @throws CoreException - * @throws IOException - * - */ - private PythonShell getServerShell() throws IOException, CoreException { - if(pytonShell == null){ - pytonShell = new PythonShell(); - pytonShell.startIt(); - } - return pytonShell; - - } - - /** - * @param theCode - */ - private List consoleCompletion(java.lang.String theCode) { - List theList = new ArrayList(); - File tipperFile=null; - try { - - tipperFile = getAutoCompleteScript(false); - - } catch (CoreException e) { - - e.printStackTrace(); - } - - try { - Process p = Runtime.getRuntime().exec(new String[]{"python"}); - - //we have the process... - OutputStreamWriter writer = new OutputStreamWriter(p.getOutputStream()); - BufferedReader in = new BufferedReader(new InputStreamReader(p - .getInputStream())); - BufferedReader eIn = new BufferedReader(new InputStreamReader( - p.getErrorStream())); - - //we have to put the tipper in sys.path - writer.write("import sys\n"); - writer.write("sys.path.insert(0,r'"+tipperFile.getParent()+"')\n"); - - writer.write("from tipper import GenerateTip\n"); - - theCode = theCode.replaceAll("\r",""); - theCode = theCode.replaceAll("\n","\\\\n"); - - theCode = theCode.replaceAll("'","@l@l@*"); //TODO: that's a really bad way to do it... - - writer.write("s = '"+theCode+"'\n"); - writer.write("s = s.replace('@l@l@*', '\\'')\n"); - - writer.write("GenerateTip(s)\n\n\n"); - writer.flush(); - writer.close(); - - String str; - while ((str = in.readLine()) != null) { - if (!str.startsWith("tip: ")){ - // System.out.println("std output: " + str); - continue; - } - - str = str.substring(5); - - theList.add(new String[]{str,""}); - } - in.close(); - while ((str = eIn.readLine()) != null) { - // System.out.println("error output: " + str); - } - eIn.close(); - p.waitFor(); - } catch (IOException e) { - - e.printStackTrace(); - } catch (InterruptedException e) { - - e.printStackTrace(); - } - return theList; - } /** --- 145,149 ---- *************** *** 252,261 **** * @throws CoreException */ ! public static File getAutoCompleteScript(boolean useSimpleTipper) throws CoreException { ! if(useSimpleTipper){ ! return getScriptWithinPySrc("simpleTipper.py"); ! }else{ ! return getScriptWithinPySrc("tipper.py"); ! } } --- 154,159 ---- * @throws CoreException */ ! public static File getAutoCompleteScript() throws CoreException { ! return getScriptWithinPySrc("simpleTipper.py"); } Index: PythonCompletionProcessor.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/PythonCompletionProcessor.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** PythonCompletionProcessor.java 13 Sep 2004 17:11:50 -0000 1.4 --- PythonCompletionProcessor.java 14 Sep 2004 17:42:14 -0000 1.5 *************** *** 6,9 **** --- 6,10 ---- import java.util.ArrayList; + import java.util.Arrays; import java.util.Iterator; import java.util.List; *************** *** 34,37 **** --- 35,40 ---- private PyEdit edit; + private ProposalsComparator proposalsComparator = new ProposalsComparator(); + /** * @param edit *************** *** 66,70 **** java.lang.String qualifier = ""; - char[] cs = new char[]{'.', '(', ' '}; //we complete on '.' and '('. --- 69,72 ---- *************** *** 74,78 **** //we have to get the qualifier. e.g. bla.foo = foo is the qualifier. if(activationToken.indexOf('.')!= -1){ ! while (endsWithSomeChar(cs, activationToken) == false && activationToken.length() > 0) { --- 76,80 ---- //we have to get the qualifier. e.g. bla.foo = foo is the qualifier. if(activationToken.indexOf('.')!= -1){ ! while (endsWithSomeChar(new char[]{'.'}, activationToken) == false && activationToken.length() > 0) { *************** *** 82,85 **** --- 84,90 ---- .length() - 1); } + }else{ //everything is a part of the qualifier. + qualifier = activationToken; + activationToken = ""; } *************** *** 89,92 **** --- 94,105 ---- theDoc += "\n" + activationToken; + + try { + PythonShell.getServerShell().sendGoToDirMsg(edit.getEditorFile()); + } catch (Exception e) { + //if we don't suceed, we don't have to fail... just go on and try to complete... + e.printStackTrace(); + } + List pythonProposals = getPythonProposals(documentOffset, doc, theDoc, activationToken, qlen); *************** *** 110,113 **** --- 123,128 ---- // and fill with list elements returnProposals.toArray(proposals); + + Arrays.sort(proposals, proposalsComparator); // Return the proposals return proposals; Index: CompletionCache.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/CompletionCache.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** CompletionCache.java 13 Sep 2004 17:11:50 -0000 1.2 --- CompletionCache.java 14 Sep 2004 17:42:14 -0000 1.3 *************** *** 44,49 **** if(allProposals == null){ //no cache proposals ! List theList = codeCompletion.autoComplete(edit, doc, ! documentOffset, partialDoc, activationToken); allProposals = new ArrayList(); --- 44,48 ---- if(allProposals == null){ //no cache proposals ! List theList = codeCompletion.autoComplete(edit, doc, documentOffset, activationToken); allProposals = new ArrayList(); *************** *** 56,66 **** allProposals.add(proposal); } ! cacheEntries.add(partialDoc); ! cache.put(partialDoc, allProposals); ! //we don't want this to get huge... ! if (cacheEntries.size() > 2) { ! Object entry = cacheEntries.remove(0); ! cache.remove(entry); ! } } return allProposals; --- 55,59 ---- allProposals.add(proposal); } ! addProposalsToCache(partialDoc, allProposals); } return allProposals; *************** *** 70,73 **** --- 63,80 ---- /** * @param partialDoc + * @param allProposals + */ + private void addProposalsToCache(String partialDoc, List allProposals) { + cacheEntries.add(partialDoc); + cache.put(partialDoc, allProposals); + //we don't want this to get huge... + if (cacheEntries.size() > 4) { + Object entry = cacheEntries.remove(0); + cache.remove(entry); + } + } + + /** + * @param partialDoc * @param documentOffset * @param qlen |