package org.eclipse.jface.text;
import java.util.LinkedList;
@Deprecated
public class SequentialRewriteTextStore implements ITextStore {
private static class Replace {
public int newOffset;
public final int offset;
public final int length;
public final String text;
public Replace(int offset, int newOffset, int length, String text) {
this.newOffset= newOffset;
this.offset= offset;
this.length= length;
this.text= text;
}
}
private LinkedList<Replace> fReplaceList;
private ITextStore fSource;
private static final boolean ASSERT_SEQUENTIALITY= false;
public SequentialRewriteTextStore(ITextStore source) {
fReplaceList= new LinkedList<>();
fSource= source;
}
public ITextStore getSourceStore() {
commit();
return fSource;
}
@Override
public void replace(int offset, int length, String text) {
if (text == null)
text= "";
if (fReplaceList.isEmpty()) {
fReplaceList.add(new Replace(offset, offset, length, text));
} else {
Replace firstReplace= fReplaceList.getFirst();
Replace lastReplace= fReplaceList.getLast();
if (offset + length <= firstReplace.newOffset) {
int delta= text.length() - length;
if (delta != 0) {
for (Replace replace : fReplaceList) {
replace.newOffset += delta;
}
}
fReplaceList.addFirst(new Replace(offset, offset, length, text));
} else if (offset >= lastReplace.newOffset + lastReplace.text.length()) {
int delta= getDelta(lastReplace);
fReplaceList.add(new Replace(offset - delta, offset, length, text));
} else if (ASSERT_SEQUENTIALITY) {
throw new IllegalArgumentException();
} else {
commit();
fSource.replace(offset, length, text);
}
}
}
@Override
public void set(String text) {
fSource.set(text);
fReplaceList.clear();
}
@Override
public String get(int offset, int length) {
if (fReplaceList.isEmpty())
return fSource.get(offset, length);
Replace firstReplace= fReplaceList.getFirst();
Replace lastReplace= fReplaceList.getLast();
if (offset + length <= firstReplace.newOffset) {
return fSource.get(offset, length);
} else if (offset >= lastReplace.newOffset + lastReplace.text.length()) {
int delta= getDelta(lastReplace);
return fSource.get(offset - delta, length);
} else if (ASSERT_SEQUENTIALITY) {
throw new IllegalArgumentException();
} else {
int delta= 0;
for (Replace replace : fReplaceList) {
if (offset + length < replace.newOffset) {
return fSource.get(offset - delta, length);
} else if (offset >= replace.newOffset && offset + length <= replace.newOffset + replace.text.length()) {
return replace.text.substring(offset - replace.newOffset, offset - replace.newOffset + length);
} else if (offset >= replace.newOffset + replace.text.length()) {
delta= getDelta(replace);
continue;
} else {
commit();
return fSource.get(offset, length);
}
}
return fSource.get(offset - delta, length);
}
}
private static final int getDelta(Replace replace) {
return replace.newOffset - replace.offset + replace.text.length() - replace.length;
}
@Override
public char get(int offset) {
if (fReplaceList.isEmpty())
return fSource.get(offset);
Replace firstReplace= fReplaceList.getFirst();
Replace lastReplace= fReplaceList.getLast();
if (offset < firstReplace.newOffset) {
return fSource.get(offset);
} else if (offset >= lastReplace.newOffset + lastReplace.text.length()) {
int delta= getDelta(lastReplace);
return fSource.get(offset - delta);
} else if (ASSERT_SEQUENTIALITY) {
throw new IllegalArgumentException();
} else {
int delta= 0;
for (Replace replace : fReplaceList) {
if (offset < replace.newOffset)
return fSource.get(offset - delta);
else if (offset < replace.newOffset + replace.text.length())
return replace.text.charAt(offset - replace.newOffset);
delta= getDelta(replace);
}
return fSource.get(offset - delta);
}
}
@Override
public int getLength() {
if (fReplaceList.isEmpty())
return fSource.getLength();
Replace lastReplace= fReplaceList.getLast();
return fSource.getLength() + getDelta(lastReplace);
}
public void dispose() {
fReplaceList= null;
fSource= null;
}
private void commit() {
if (fReplaceList.isEmpty())
return;
StringBuilder buffer= new StringBuilder();
int delta= 0;
for (Replace replace : fReplaceList) {
int offset= buffer.length() - delta;
buffer.append(fSource.get(offset, replace.offset - offset));
buffer.append(replace.text);
delta= getDelta(replace);
}
int offset= buffer.length() - delta;
buffer.append(fSource.get(offset, fSource.getLength() - offset));
fSource.set(buffer.toString());
fReplaceList.clear();
}
}