📄 pydociterator.java
字号:
package org.python.pydev.core.docutils;
import java.util.Iterator;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
public class PyDocIterator implements Iterator<String> {
private int offset;
private IDocument doc;
private boolean addNewLinesToRet = true;
private boolean returnNewLinesOnLiterals = false;
private boolean inLiteral = false;
private int literalEnd;
private boolean changeLiteralsForSpaces;
private int lastReturned = -1;
private boolean addComments;
public PyDocIterator(IDocument doc, boolean addNewLinesToRet) {
this(doc, addNewLinesToRet, false, false);
}
public PyDocIterator(IDocument doc, boolean addNewLinesToRet, boolean returnNewLinesOnLiterals, boolean changeLiteralsForSpaces) {
this(doc, addNewLinesToRet, returnNewLinesOnLiterals, changeLiteralsForSpaces, false);
}
/**
* @param doc the document where we will iterate
* @param addNewLinesToRet whether the new line character should be added to the return
* @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)
*/
public PyDocIterator(IDocument doc, boolean addNewLinesToRet, boolean returnNewLinesOnLiterals, boolean changeLiteralsForSpaces, boolean addComments) {
this(doc);
this.addNewLinesToRet = addNewLinesToRet;
this.returnNewLinesOnLiterals = returnNewLinesOnLiterals;
this.changeLiteralsForSpaces = changeLiteralsForSpaces;
this.addComments = addComments;
}
public PyDocIterator(IDocument doc) {
this.doc = doc;
}
public boolean hasNext() {
return offset < doc.getLength();
}
public int getLastReturnedLine(){
try {
lastReturned = doc.getLineOfOffset(offset-1);
} catch (BadLocationException e) {
//ignore (keep the last one)
}
return lastReturned;
}
private String nextInLiteral() {
StringBuffer buf = new StringBuffer();
try {
char ch = doc.getChar(offset);
while (offset < literalEnd && ch != '\n' && ch != '\r') {
ch = doc.getChar(offset);
offset++;
if(changeLiteralsForSpaces && ch != '\n' && ch != '\r'){
buf.append(' ');
}
}
if(offset >= literalEnd){
inLiteral = false;
offset++;
if(changeLiteralsForSpaces){
buf.append(' ');
}
return buf.toString();
}
if(ch == '\r'){
ch = doc.getChar(offset+1);
if(ch == '\n'){
offset++;
ch = '\n';
}
}
buf.append(ch);
return buf.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @return the next line in the document
*/
public String next() {
try {
StringBuffer buf = new StringBuffer();
if(inLiteral){
int initialOffset = offset;
String ret = nextInLiteral();
if(ret.length() > 0 && initialOffset < offset){ //if it didn't move in the offset, disregard the results
if(WordUtils.endsWith(ret, '\r') || WordUtils.endsWith(ret, '\n')){
if(!addNewLinesToRet){
ret = ret.substring(0, ret.length() -1);
}
buf.append(ret);
return ret;
}else{
buf.append(ret);
}
}
}
char ch = 0;
while(ch != '\r' && ch != '\n' && offset < doc.getLength()){
ch = doc.getChar(offset);
if (ch == '#') {
while (offset < doc.getLength() && ch != '\n' && ch != '\r') {
ch = doc.getChar(offset);
if(addComments && ch != '\n' && ch != '\r'){
buf.append(ch);
}
offset++;
}
}else if (ch == '\'' || ch == '"') {
if(returnNewLinesOnLiterals){
inLiteral = true;
literalEnd = ParsingUtils.getLiteralEnd(doc, offset, ch);
String ret = nextInLiteral();
if(ret.length() > 0){
if(WordUtils.endsWith(ret, '\r') || WordUtils.endsWith(ret, '\n')){
if(!addNewLinesToRet){
ret = ret.substring(0, ret.length() -1);
}
buf.append(ret);
return buf.toString();
}else{
buf.append(ret);
}
}
}else{
if(this.changeLiteralsForSpaces){
throw new RuntimeException("Not supported in this case.");
}
offset = ParsingUtils.getLiteralEnd(doc, offset, ch);
offset++;
}
}else if(ch != '\n' && ch != '\r'){
//will be added later
buf.append(ch);
offset++;
}else{
offset++;
}
}
//handle the \r, \n or \r\n
if(ch == '\n' || ch == '\r'){
if(addNewLinesToRet){
buf.append(ch);
}
if(ch == '\r'){
if(offset < doc.getLength() && doc.getChar(offset) == '\n'){
offset++;
if(addNewLinesToRet){
buf.append('\n');
}
}
}
}
return buf.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void remove() {
throw new RuntimeException("Not Impl.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -