Thread: [Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/codecompletion PythonShell.java,1.6,1.7 PyC
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-09-17 19:22:14
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2281/src/org/python/pydev/editor/codecompletion Modified Files: PythonShell.java PyCodeCompletion.java Log Message: Making refactoring. Index: PythonShell.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/PythonShell.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** PythonShell.java 16 Sep 2004 15:33:33 -0000 1.6 --- PythonShell.java 17 Sep 2004 19:22:04 -0000 1.7 *************** *** 10,13 **** --- 10,15 ---- import java.net.ServerSocket; import java.net.Socket; + import java.net.URLDecoder; + import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; *************** *** 205,208 **** --- 207,211 ---- s = s.replaceAll("END@@", ""); j = 0; + s = URLDecoder.decode(s); if(s.trim().equals("") == false){ communicateWork("Processing: "+s, operation); *************** *** 230,234 **** //remove END@@ try { ! return str.substring(0, str.lastIndexOf("END@@")); } catch (RuntimeException e) { System.out.println("ERROR WITH STRING:"+str); --- 233,238 ---- //remove END@@ try { ! str = str.substring(0, str.lastIndexOf("END@@")); ! return str; } catch (RuntimeException e) { System.out.println("ERROR WITH STRING:"+str); *************** *** 244,248 **** */ public String read() throws IOException { ! return read(null); } --- 248,254 ---- */ public String read() throws IOException { ! String r = read(null); ! System.out.println("RETURNING:"+r); ! return r; } *************** *** 253,256 **** --- 259,263 ---- */ public void write(String str) throws IOException { + System.out.println("WRITING:"+str); this.socketToWrite.getOutputStream().write(str.getBytes()); } *************** *** 328,332 **** file = file.getParentFile(); } ! this.write("@@CHANGE_DIR:"+file.getAbsolutePath()+"END@@"); String ok = this.read(); //this should be the ok message... --- 335,342 ---- file = file.getParentFile(); } ! ! String str = file.getAbsolutePath(); ! str = URLEncoder.encode(str); ! this.write("@@CHANGE_DIR:"+str+"END@@"); String ok = this.read(); //this should be the ok message... *************** *** 351,354 **** --- 361,365 ---- */ public List getGlobalCompletions(String str) { + str = URLEncoder.encode(str); return this.getTheCompletions("@@GLOBALS:"+str+"\nEND@@"); } *************** *** 359,362 **** --- 370,375 ---- */ public List getTokenCompletions(String token, String str) { + token = URLEncoder.encode(token); + str = URLEncoder.encode(str); String s = "@@TOKEN_GLOBALS("+token+"):"+str+"\nEND@@"; return this.getTheCompletions(s); *************** *** 369,376 **** --- 382,400 ---- */ public List getClassCompletions(String token, String str) { + token = URLEncoder.encode(token); + str = URLEncoder.encode(str); String s = "@@CLASS_GLOBALS("+token+"):"+str+"\nEND@@"; return this.getTheCompletions(s); } + /** + * @param importsTipper + * @return + */ + public List getImportCompletions(String str) { + str = URLEncoder.encode(str); + return this.getTheCompletions("@@IMPORTS:"+str+"\nEND@@"); + } + private List getTheCompletions(String str){ try { *************** *** 422,427 **** while(tokenizer.hasMoreTokens()){ ! String token = tokenizer.nextToken(); ! String description = tokenizer.nextToken(); list.add(new String[]{token, description}); } --- 446,453 ---- while(tokenizer.hasMoreTokens()){ ! String token = URLDecoder.decode(tokenizer.nextToken()); ! String description = URLDecoder.decode(tokenizer.nextToken()); ! ! list.add(new String[]{token, description}); } *************** *** 432,434 **** --- 458,462 ---- + + } \ No newline at end of file Index: PyCodeCompletion.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/codecompletion/PyCodeCompletion.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** PyCodeCompletion.java 14 Sep 2004 17:42:14 -0000 1.6 --- PyCodeCompletion.java 17 Sep 2004 19:22:04 -0000 1.7 *************** *** 33,44 **** */ public class PyCodeCompletion { ! ! int docBoundary = -1; // the document prior to the activation token private PythonShell pytonShell; /** ! * @param theDoc: the whole document as a string. ! * @param documentOffset: the cursor position */ String partialDocument(String theDoc, int documentOffset) { --- 33,46 ---- */ public class PyCodeCompletion { ! int docBoundary = -1; // the document prior to the activation token + private PythonShell pytonShell; /** ! * @param theDoc: ! * the whole document as a string. ! * @param documentOffset: ! * the cursor position */ String partialDocument(String theDoc, int documentOffset) { *************** *** 46,52 **** calcDocBoundary(theDoc, documentOffset); } ! if(this.docBoundary != -1){ ! String before = theDoc.substring(0, this.docBoundary); ! return before; } return ""; --- 48,54 ---- calcDocBoundary(theDoc, documentOffset); } ! if (this.docBoundary != -1) { ! String before = theDoc.substring(0, this.docBoundary); ! return before; } return ""; *************** *** 56,59 **** --- 58,62 ---- /** * Returns a list with the tokens to use for autocompletion. + * * @param edit * @param doc *************** *** 68,72 **** } - /** * @param edit --- 71,74 ---- *************** *** 74,78 **** * @param documentOffset */ ! private List serverCompletion(String theActivationToken, PyEdit edit, IDocument doc, int documentOffset) { List theList = new ArrayList(); PythonShell serverShell = null; --- 76,81 ---- * @param documentOffset */ ! private List serverCompletion(String theActivationToken, PyEdit edit, ! IDocument doc, int documentOffset) { List theList = new ArrayList(); PythonShell serverShell = null; *************** *** 83,113 **** } - String docToParse = getDocToParse(doc, documentOffset); ! String trimmed = theActivationToken.replace('.',' ').trim(); ! if (trimmed.equals("") == false && theActivationToken.indexOf('.') != -1){ ! List completions; ! if (trimmed.equals("self")){ Location loc = Location.offsetToLocation(doc, documentOffset); ! AbstractNode closest = ModelUtils.getLessOrEqualNode(edit.getPythonModel(),loc); Scope scope = closest.getScope().findContainingClass(); String token = scope.getStartNode().getName(); ! completions = serverShell.getClassCompletions(token, docToParse); ! }else{ ! completions = serverShell.getTokenCompletions(trimmed, docToParse); } theList.addAll(completions); ! ! } ! else{ //go to globals List completions = serverShell.getGlobalCompletions(docToParse); theList.addAll(completions); ! } return theList; } --- 86,175 ---- } String docToParse = getDocToParse(doc, documentOffset); ! String trimmed = theActivationToken.replace('.', ' ').trim(); ! ! String importsTipper = useImportsTipper(theActivationToken, edit, doc, documentOffset); ! if (importsTipper.length()!=0) { //may be space. ! ! List completions = serverShell.getImportCompletions(importsTipper); ! theList.addAll(completions); ! ! } else if (trimmed.equals("") == false ! && theActivationToken.indexOf('.') != -1) { ! List completions; ! if (trimmed.equals("self")) { Location loc = Location.offsetToLocation(doc, documentOffset); ! AbstractNode closest = ModelUtils.getLessOrEqualNode(edit ! .getPythonModel(), loc); Scope scope = closest.getScope().findContainingClass(); String token = scope.getStartNode().getName(); ! completions = serverShell ! .getClassCompletions(token, docToParse); ! } else { ! completions = serverShell.getTokenCompletions(trimmed, ! docToParse); } theList.addAll(completions); ! ! } else { //go to globals List completions = serverShell.getGlobalCompletions(docToParse); theList.addAll(completions); ! } return theList; + + } + + /** + * @param theActivationToken + * @param edit + * @param doc + * @param documentOffset + * @return + */ + private String useImportsTipper(String theActivationToken, PyEdit edit, + IDocument doc, int documentOffset) { + String importMsg = ""; + try { + + IRegion region = doc.getLineInformationOfOffset(documentOffset); + String string = doc.get(region.getOffset(), documentOffset-region.getOffset()); + int fromIndex = string.indexOf("from"); + int importIndex = string.indexOf("import"); + if(fromIndex != -1 || importIndex != -1){ + string = string.replaceAll("#.*", ""); //remove comments + String[] strings = string.split(" "); + + for (int i = 0; i < strings.length; i++) { + if(strings[i].equals("from")==false && strings[i].equals("import")==false){ + if(importMsg.length() != 0){ + importMsg += '.'; + } + importMsg += strings[i]; + } + } + + if(fromIndex != -1 && importIndex != -1){ + if(strings.length == 3){ + importMsg += '.'; + } + } + }else{ + return ""; + } + } catch (BadLocationException e) { + e.printStackTrace(); + } + if (importMsg.indexOf(".") == -1){ + return " "; + } + if (importMsg.length() > 0 && importMsg.endsWith(".") == false ){ + importMsg = importMsg.substring(0, importMsg.lastIndexOf('.')); + } + return importMsg; } *************** *** 123,142 **** int lineOfOffset = doc.getLineOfOffset(documentOffset); IRegion lineInformation = doc.getLineInformation(lineOfOffset); ! int docLength = doc.getLength(); ! String src = doc.get(lineInformation.getOffset(), documentOffset-lineInformation.getOffset()); ! ! String spaces=""; for (int i = 0; i < src.length(); i++) { ! if(src.charAt(i) != ' '){ break; } spaces += ' '; } ! newDoc = wholeDoc.substring(0, lineInformation.getOffset()); ! newDoc += spaces+"pass\n"; ! newDoc += wholeDoc.substring(lineInformation.getOffset() + lineInformation.getLength(), docLength); ! } catch (BadLocationException e1) { e1.printStackTrace(); --- 185,206 ---- int lineOfOffset = doc.getLineOfOffset(documentOffset); IRegion lineInformation = doc.getLineInformation(lineOfOffset); ! int docLength = doc.getLength(); ! String src = doc.get(lineInformation.getOffset(), documentOffset ! - lineInformation.getOffset()); ! ! String spaces = ""; for (int i = 0; i < src.length(); i++) { ! if (src.charAt(i) != ' ') { break; } spaces += ' '; } ! newDoc = wholeDoc.substring(0, lineInformation.getOffset()); ! newDoc += spaces + "pass\n"; ! newDoc += wholeDoc.substring(lineInformation.getOffset() ! + lineInformation.getLength(), docLength); ! } catch (BadLocationException e1) { e1.printStackTrace(); *************** *** 145,150 **** } - - /** * --- 209,212 ---- *************** *** 164,168 **** * @throws CoreException */ ! public static File getScriptWithinPySrc(String targetExec) throws CoreException { IPath relative = new Path("PySrc").addTrailingSeparator().append( --- 226,231 ---- * @throws CoreException */ ! public static File getScriptWithinPySrc(String targetExec) ! throws CoreException { IPath relative = new Path("PySrc").addTrailingSeparator().append( *************** *** 186,191 **** public static File getImageWithinIcons(String icon) throws CoreException { ! IPath relative = new Path("icons").addTrailingSeparator().append( ! icon); Bundle bundle = PydevPlugin.getDefault().getBundle(); --- 249,253 ---- public static File getImageWithinIcons(String icon) throws CoreException { ! IPath relative = new Path("icons").addTrailingSeparator().append(icon); Bundle bundle = PydevPlugin.getDefault().getBundle(); *************** *** 205,210 **** /** ! * The docBoundary should get until the last line before the one ! * we are editing. * * @param qualifier --- 267,272 ---- /** ! * The docBoundary should get until the last line before the one we are ! * editing. * * @param qualifier *************** *** 216,220 **** .lastIndexOf('\n'); } ! /** * Returns the activation token. --- 278,282 ---- .lastIndexOf('\n'); } ! /** * Returns the activation token. *************** *** 229,237 **** } String str = theDoc.substring(this.docBoundary + 1, documentOffset); ! if (str.endsWith(" ")){ ! str = " "; } return str; } ! } --- 291,302 ---- } String str = theDoc.substring(this.docBoundary + 1, documentOffset); ! if (str.endsWith(" ")) { ! return " "; ! } ! if(str.lastIndexOf(' ') != -1){ ! return str.substring(str.lastIndexOf(' '), str.length()); } return str; } ! } \ No newline at end of file |