package org.eclipse.jface.text;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.text.AbstractLineTracker.DelimiterInfo;
abstract class ListLineTracker implements ILineTracker {
private final List<Line> fLines= new ArrayList<>();
private int fTextLength;
protected ListLineTracker() {
}
private int findLine(int offset) {
if (fLines.isEmpty())
return -1;
int left= 0;
int right= fLines.size() - 1;
int mid= 0;
Line line= null;
while (left < right) {
mid= (left + right) / 2;
line= fLines.get(mid);
if (offset < line.offset) {
if (left == mid)
right= left;
else
right= mid - 1;
} else if (offset > line.offset) {
if (right == mid)
left= right;
else
left= mid + 1;
} else if (offset == line.offset) {
left= right= mid;
}
}
line= fLines.get(left);
if (line.offset > offset)
--left;
return left;
}
private int getNumberOfLines(int startLine, int offset, int length) throws BadLocationException {
if (length == 0)
return 1;
int target= offset + length;
Line l= fLines.get(startLine);
if (l.delimiter == null)
return 1;
if (l.offset + l.length > target)
return 1;
if (l.offset + l.length == target)
return 2;
return getLineNumberOfOffset(target) - startLine + 1;
}
@Override
public final int getLineLength(int line) throws BadLocationException {
int lines= fLines.size();
if (line < 0 || line > lines)
throw new BadLocationException();
if (lines == 0 || lines == line)
return 0;
Line l= fLines.get(line);
return l.length;
}
@Override
public final int getLineNumberOfOffset(int position) throws BadLocationException {
if (position < 0) {
throw new BadLocationException("Negative offset : " + position);
} else if (position > fTextLength) {
throw new BadLocationException("Offset > length: " + position + " > " + fTextLength);
}
if (position == fTextLength) {
int lastLine= fLines.size() - 1;
if (lastLine < 0)
return 0;
Line l= fLines.get(lastLine);
return (l.delimiter != null ? lastLine + 1 : lastLine);
}
return findLine(position);
}
@Override
public final IRegion getLineInformationOfOffset(int position) throws BadLocationException {
if (position > fTextLength)
throw new BadLocationException("Offset > length: " + position + " > " + fTextLength);
if (position == fTextLength) {
int size= fLines.size();
if (size == 0)
return new Region(0, 0);
Line l= fLines.get(size - 1);
return (l.delimiter != null ? new Line(fTextLength, 0) : new Line(fTextLength - l.length, l.length));
}
return getLineInformation(findLine(position));
}
@Override
public final IRegion getLineInformation(int line) throws BadLocationException {
int lines= fLines.size();
if (line < 0 || line > lines)
throw new BadLocationException();
if (lines == 0)
return new Line(0, 0);
if (line == lines) {
Line l= fLines.get(line - 1);
return new Line(l.offset + l.length, 0);
}
Line l= fLines.get(line);
return (l.delimiter != null ? new Line(l.offset, l.length - l.delimiter.length()) : l);
}
@Override
public final int getLineOffset(int line) throws BadLocationException {
int lines= fLines.size();
if (line < 0 || line > lines)
throw new BadLocationException();
if (lines == 0)
return 0;
if (line == lines) {
Line l= fLines.get(line - 1);
if (l.delimiter != null)
return l.offset + l.length;
throw new BadLocationException();
}
Line l= fLines.get(line);
return l.offset;
}
@Override
public final int getNumberOfLines() {
int lines= fLines.size();
if (lines == 0)
return 1;
Line l= fLines.get(lines - 1);
return (l.delimiter != null ? lines + 1 : lines);
}
@Override
public final int getNumberOfLines(int position, int length) throws BadLocationException {
if (position < 0 || position + length > fTextLength)
throw new BadLocationException();
if (length == 0)
return 1;
return getNumberOfLines(getLineNumberOfOffset(position), position, length);
}
@Override
public final int computeNumberOfLines(String text) {
int count= 0;
int start= 0;
DelimiterInfo delimiterInfo= nextDelimiterInfo(text, start);
while (delimiterInfo != null && delimiterInfo.delimiterIndex > -1) {
++count;
start= delimiterInfo.delimiterIndex + delimiterInfo.delimiterLength;
delimiterInfo= nextDelimiterInfo(text, start);
}
return count;
}
@Override
public final String getLineDelimiter(int line) throws BadLocationException {
int lines= fLines.size();
if (line < 0 || line > lines)
throw new BadLocationException();
if (lines == 0)
return null;
if (line == lines)
return null;
Line l= fLines.get(line);
return l.delimiter;
}
protected abstract DelimiterInfo nextDelimiterInfo(String text, int offset);
private int createLines(String text, int insertPosition, int offset) {
int count= 0;
int start= 0;
DelimiterInfo delimiterInfo= nextDelimiterInfo(text, 0);
while (delimiterInfo != null && delimiterInfo.delimiterIndex > -1) {
int index= delimiterInfo.delimiterIndex + (delimiterInfo.delimiterLength - 1);
if (insertPosition + count >= fLines.size())
fLines.add(new Line(offset + start, offset + index, delimiterInfo.delimiter));
else
fLines.add(insertPosition + count, new Line(offset + start, offset + index, delimiterInfo.delimiter));
++count;
start= index + 1;
delimiterInfo= nextDelimiterInfo(text, start);
}
if (start < text.length()) {
if (insertPosition + count < fLines.size()) {
Line l= fLines.get(insertPosition + count);
int delta= text.length() - start;
l.offset-= delta;
l.length+= delta;
} else {
fLines.add(new Line(offset + start, offset + text.length() - 1, null));
++count;
}
}
return count;
}
@Override
public final void replace(int position, int length, String text) throws BadLocationException {
throw new UnsupportedOperationException();
}
@Override
public final void set(String text) {
fLines.clear();
if (text != null) {
fTextLength= text.length();
createLines(text, 0, 0);
} else {
fTextLength= 0;
}
}
final List<Line> getLines() {
return fLines;
}
}