Thread: [Pydev-cvs] org.python.pydev.core/src/org/python/pydev/core/docutils PySelection.java, 1.61, 1.62 I
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-05-19 00:58:21
|
Update of /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10132/src/org/python/pydev/core/docutils Modified Files: PySelection.java ImportsSelection.java ImportHandle.java Log Message: Auto-import grouping imports when possible Index: PySelection.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/PySelection.java,v retrieving revision 1.61 retrieving revision 1.62 diff -C2 -d -r1.61 -r1.62 *** PySelection.java 1 May 2008 14:25:53 -0000 1.61 --- PySelection.java 19 May 2008 00:58:28 -0000 1.62 *************** *** 1056,1059 **** --- 1056,1062 ---- + /** + * @return the number of line breaks in the passed string. + */ public static int countLineBreaks(String replacementString) { int lineBreaks = 0; Index: ImportHandle.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/ImportHandle.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ImportHandle.java 18 May 2008 20:02:26 -0000 1.2 --- ImportHandle.java 19 May 2008 00:58:28 -0000 1.3 *************** *** 24,29 **** //spaces* 'from' space+ module space+ import (mod as y) ! private static final Pattern FromImportPattern = Pattern.compile("(from\\s+)(\\.*\\w+)(\\s+import\\s+)(\\w+|\\s|,|#|\\(|\\))*(\\z)"); ! private static final Pattern ImportPattern = Pattern.compile("(import\\s+)(\\w+|\\s|,|#|\\(|\\))*(\\z)"); /** --- 24,29 ---- //spaces* 'from' space+ module space+ import (mod as y) ! private static final Pattern FromImportPattern = Pattern.compile("(from\\s+)(\\.*\\w+)+(\\s+import\\s+)(\\w+|\\s|,|#|\\(|\\)|\\\\)*"); ! private static final Pattern ImportPattern = Pattern.compile("(import\\s+)(\\w+|\\s|,|#|\\(|\\)|\\\\)*"); /** *************** *** 44,49 **** --- 44,71 ---- */ private List<String> importedStrComments; + + /** + * Starting line for this import. + */ + private int startLine; + + /** + * Ending line for this import. + */ + private int endLine; + + /** + * Holds whether the import started in the middle of the line (after a ';') + */ + private boolean startedInMiddleOfLine; /** + * Constructor that does not set the line for the import. + */ + public ImportHandleInfo(String importFound) throws ImportNotRecognizedException { + this(importFound, -1, -1, false); + } + + /** * Constructor. * *************** *** 53,57 **** * @throws ImportNotRecognizedException */ ! public ImportHandleInfo(String importFound) throws ImportNotRecognizedException { importFound=importFound.trim(); char firstChar = importFound.charAt(0); --- 75,83 ---- * @throws ImportNotRecognizedException */ ! public ImportHandleInfo(String importFound, int lineStart, int lineEnd, boolean startedInMiddleOfLine) throws ImportNotRecognizedException { ! this.startLine = lineStart; ! this.endLine = lineEnd; ! this.startedInMiddleOfLine = startedInMiddleOfLine; ! importFound=importFound.trim(); char firstChar = importFound.charAt(0); *************** *** 62,66 **** Matcher matcher = FromImportPattern.matcher(importFound); if(matcher.matches()){ ! this.fromStr = matcher.group(2); //we have to do that because the last group will only have the last match in the string --- 88,93 ---- Matcher matcher = FromImportPattern.matcher(importFound); if(matcher.matches()){ ! this.fromStr = importFound.substring(matcher.end(1), ! matcher.end(2)).trim(); //we have to do that because the last group will only have the last match in the string *************** *** 116,120 **** alias = new StringBuffer(); ! }else if(c == '(' || c == ')'){ //do nothing --- 143,147 ---- alias = new StringBuffer(); ! }else if(c == '(' || c == ')' || c == '\\'){ //do nothing *************** *** 184,187 **** --- 211,235 ---- return this.importedStrComments; } + + /** + * @return the start line for this import (0-based) + */ + public int getStartLine() { + return this.startLine; + } + + /** + * @return the end line for this import (0-based) + */ + public int getEndLine() { + return this.endLine; + } + + /** + * @return true if this import was started in the middle of the line. I.e.: after a ';' + */ + public boolean getStartedInMiddleOfLine() { + return this.startedInMiddleOfLine; + } } *************** *** 209,213 **** /** ! * Import informatiot for the import found and handled in this class (only created on request) */ private List<ImportHandleInfo> importInfo; --- 257,261 ---- /** ! * Import information for the import found and handled in this class (only created on request) */ private List<ImportHandleInfo> importInfo; *************** *** 226,230 **** /** ! * @param realImportRep the import to match. Note that only a single import statement may be passed as a parameter. * * @return true if the passed import matches the import in this handle (note: as this class can actually wrap more --- 274,278 ---- /** ! * @param realImportHandleInfo the import to match. Note that only a single import statement may be passed as a parameter. * * @return true if the passed import matches the import in this handle (note: as this class can actually wrap more *************** *** 232,237 **** * @throws ImportNotRecognizedException if the passed import could not be recognized */ ! public boolean contains(String realImportRep) throws ImportNotRecognizedException { ! ImportHandleInfo otherImportInfo = new ImportHandleInfo(realImportRep); List<ImportHandleInfo> importHandleInfo = this.getImportInfo(); --- 280,284 ---- * @throws ImportNotRecognizedException if the passed import could not be recognized */ ! public boolean contains(ImportHandleInfo otherImportInfo) throws ImportNotRecognizedException { List<ImportHandleInfo> importHandleInfo = this.getImportInfo(); *************** *** 268,271 **** --- 315,321 ---- this.importInfo = new ArrayList<ImportHandleInfo>(); + int line = startFoundLine; + boolean startedInMiddle = false; + StringBuffer imp = new StringBuffer(); for(int i=0;i<importFound.length();i++){ *************** *** 277,287 **** }else if(c == ';'){ try { ! this.importInfo.add(new ImportHandleInfo(imp.toString())); } catch (ImportNotRecognizedException e) { //that's ok, not a valid import (at least, we couldn't parse it) } imp = new StringBuffer(); ! }else{ imp.append(c); } --- 327,343 ---- }else if(c == ';'){ try { ! String impStr = imp.toString(); ! int endLine = line+PySelection.countLineBreaks(impStr); ! this.importInfo.add(new ImportHandleInfo(impStr, line, endLine, startedInMiddle)); ! line = endLine; } catch (ImportNotRecognizedException e) { //that's ok, not a valid import (at least, we couldn't parse it) } imp = new StringBuffer(); ! startedInMiddle = true; }else{ + if(c == '\r' || c == '\n'){ + startedInMiddle = false; + } imp.append(c); } *************** *** 289,293 **** } try { ! this.importInfo.add(new ImportHandleInfo(imp.toString())); } catch (ImportNotRecognizedException e) { //that's ok, not a valid import (at least, we couldn't parse it) --- 345,350 ---- } try { ! String impStr = imp.toString(); ! this.importInfo.add(new ImportHandleInfo(impStr, line, line+PySelection.countLineBreaks(impStr), startedInMiddle)); } catch (ImportNotRecognizedException e) { //that's ok, not a valid import (at least, we couldn't parse it) Index: ImportsSelection.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/ImportsSelection.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ImportsSelection.java 18 May 2008 20:02:26 -0000 1.2 --- ImportsSelection.java 19 May 2008 00:58:28 -0000 1.3 *************** *** 170,173 **** --- 170,176 ---- allEndingWithSlash = false; } + }else if(trimmedLine.length() == 0){ + //if some empty line was found, stop the analysis. + break; } } |