Thread: [Pydev-cvs] org.python.pydev.core/src/org/python/pydev/core/docutils PyImportsIterator.java, NONE,
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-05-17 14:26:50
|
Update of /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17511/src/org/python/pydev/core/docutils Modified Files: PyDocIterator.java Added Files: PyImportsIterator.java ImportHandle.java PyImportsHandling.java Log Message: Created better utilities to handle imports. --- NEW FILE: PyImportsIterator.java --- package org.python.pydev.core.docutils; import java.util.Iterator; import org.eclipse.jface.text.IDocument; /** * Iterator through imports that yields tuples with the import itself, the initial line of the import * and the final line of the import. * * @author Fabio */ public class PyImportsIterator implements Iterator<ImportHandle> { /** * Helper to iterate in the document */ private PyDocIterator docIterator; /** * Variable holding whether hasNext should return true or not */ private boolean hasNext = true; /** * Variable holding the next return value */ private ImportHandle nextImport; /** * Delimiter to be used to add new lines in the imports found. */ private String delimiter; /** * Document used in the iteration */ private IDocument doc; /** * Constructor * * @param doc the document from where the import should be gathered. */ public PyImportsIterator(IDocument doc) { this.doc = doc; delimiter = PySelection.getDelimiter(doc); this.docIterator = new PyDocIterator(doc, false, false, false, true); //gather the 1st import calcNext(); } /** * Pre-calculates the next return value and whether there is a next value to be returned. */ private void calcNext(){ if(!hasNext){ //only pre-calculate if there's something to pre-calculate. return; } String importFound = null; int startFoundLine=-1; int endFoundLine=-1; while(docIterator.hasNext()){ String str = docIterator.next(); if((str.startsWith("import ") || str.startsWith("from "))){ startFoundLine = docIterator.getLastReturnedLine(); if(str.indexOf('(') != -1){ //we have something like from os import (pipe,\nfoo) while(docIterator.hasNext() && str.indexOf(')') == -1){ str += delimiter+docIterator.next(); } } if(WordUtils.endsWith(str, '\\')){ while(docIterator.hasNext() && WordUtils.endsWith(str, '\\')){ str += delimiter+docIterator.next(); } } importFound = str; endFoundLine = docIterator.getLastReturnedLine(); break; //ok, import found } } hasNext = importFound != null; if(hasNext){ nextImport = new ImportHandle(doc, importFound, startFoundLine, endFoundLine); }else{ nextImport = null; } } /** * From the iterator interface */ public boolean hasNext() { return this.hasNext; } /** * From the iterator interface */ public ImportHandle next() { ImportHandle ret = this.nextImport; calcNext(); //pre-compute the next step return ret; } /** * From the iterator interface (not implemented) */ public void remove() { throw new RuntimeException("Not implemented"); } } --- NEW FILE: ImportHandle.java --- package org.python.pydev.core.docutils; import org.eclipse.jface.text.IDocument; /** * Class that represents an import found in a document. * * @author Fabio */ public class ImportHandle { /** * Document where the import was found */ public IDocument doc; /** * The import string found. Note: it may contain comments and multi-lines. */ public String importFound; /** * The initial line where the import was found */ public int startFoundLine; /** * The final line where the import was found */ public int endFoundLine; /** * Constructor. * * Assigns parameters to fields. */ public ImportHandle(IDocument doc, String importFound, int startFoundLine, int endFoundLine) { this.doc = doc; this.importFound = importFound; this.startFoundLine = startFoundLine; this.endFoundLine = endFoundLine; } } --- NEW FILE: PyImportsHandling.java --- package org.python.pydev.core.docutils; import java.util.Iterator; import org.eclipse.jface.text.IDocument; /** * This class is responsible for gathering information about imports and has utilities to help using them later on. * * @author Fabio */ public class PyImportsHandling implements Iterable<ImportHandle>{ private IDocument doc; public PyImportsHandling(IDocument doc) { this.doc = doc; } /** * @return an iterator that will yield the imports available */ public Iterator<ImportHandle> iterator() { return new PyImportsIterator(this.doc); } } Index: PyDocIterator.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/PyDocIterator.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** PyDocIterator.java 23 Feb 2008 18:58:22 -0000 1.7 --- PyDocIterator.java 17 May 2008 14:26:57 -0000 1.8 *************** *** 32,35 **** --- 32,36 ---- * @param returnNewLinesOnLiterals whether we should return the new lines found in the literals (not the char, but the line itself) * @param changeLiteralsForSpaces whether we should replace the literals with spaces (so that we don't loose offset information) + * @param addComments if true, comments found will be yielded (otherwise, no comments will be shown) */ public PyDocIterator(IDocument doc, boolean addNewLinesToRet, boolean returnNewLinesOnLiterals, boolean changeLiteralsForSpaces, boolean addComments) { |