gnu.jemacs.swt
Class LineOffsets

java.lang.Object
  extended by gnu.lists.AbstractSequence
      extended by gnu.lists.GapVector
          extended by gnu.jemacs.swt.LineOffsets
All Implemented Interfaces:
Consumable, Sequence, java.lang.Iterable, java.util.Collection, java.util.List

public class LineOffsets
extends GapVector

The purpose of this class is to maintain an ordered set of line offsets for an SwtCharBuffer.

With a LineOffsets instance it's possible to map from the number of a line to the text position where it begins, and back, reasonably fast. (O(1) for line number to line offset, O(log(#lines)) for line offset to line number)

LineOffsets extends GapVector with an U32Vector as base, allowing new line offsets to be inserted quickly during normal text typing.

Instances of SwtCharBuffer should hold an instance LineOffsets class and notify it whenever the it's text changes. The notification happens through the methods:

TODO: decouple this, using a more general event model.. Assume that lineOffset is an instance of LineOffsets, held by swtCharBuffer an instance of SwtCharBuffer.

Then a value of o at index i in lineOffsets.base means that the line with line number n = (i < lOff.gapStart ? i : i + lOff.gapEnd - lOff.gapStart)

starts at text position p = (o < swtCB.gapStart ? o : o + swtCB.gapEnd - swtCB.gapStart)


Field Summary
static int minGapSize
           
 
Fields inherited from class gnu.lists.GapVector
base, gapEnd, gapStart
 
Fields inherited from interface gnu.lists.Sequence
ATTRIBUTE_VALUE, BOOLEAN_VALUE, CDATA_VALUE, CHAR_VALUE, COMMENT_VALUE, DOCUMENT_VALUE, DOUBLE_VALUE, ELEMENT_VALUE, EOF_VALUE, eofValue, FLOAT_VALUE, INT_S16_VALUE, INT_S32_VALUE, INT_S64_VALUE, INT_S8_VALUE, INT_U16_VALUE, INT_U32_VALUE, INT_U64_VALUE, INT_U8_VALUE, OBJECT_VALUE, PRIM_VALUE, PROCESSING_INSTRUCTION_VALUE, TEXT_BYTE_VALUE
 
Constructor Summary
LineOffsets(int initialSize)
           
 
Method Summary
 int countLines(java.lang.String newText)
           
 void deleteLines(int firstLine, int numberOfLines)
           
 int index2offset(int index)
           
 void insertLine(int index, int offSet)
           
 void insertLines(int index, int[] offsets)
           
 boolean isLineDelimiter(char c)
           
 int linesInRange(int startOffset, int endOffset)
           
 int offset2index(int offset)
          We seek the line containing a given text offset using a halfing of intervals algorithm.
 void textDeleted(int startOffset, int endOffset)
           
 void textInserted(int startOffset, java.lang.CharSequence seq)
           
 void textRegionMoved(int regionStart, int regionEnd, int displacement)
           
 java.lang.String toString()
           
 
Methods inherited from class gnu.lists.GapVector
add, addPos, consumePosRange, createPos, fill, fillPosRange, gapReserve, gapReserve, get, getNextKind, getSegment, hasNext, isAfterPos, nextIndex, removePosRange, set, shiftGap, size
 
Methods inherited from class gnu.lists.AbstractSequence
add, addAll, addAll, clear, compare, compare, compare, consume, consumeNext, contains, containsAll, copyPos, createRelativePos, elements, endPos, equals, equals, fill, firstAttributePos, firstChildPos, firstChildPos, fromEndIndex, get, getAttribute, getAttributeLength, getContainingSequenceSize, getEffectiveIndex, getIndexDifference, getIterator, getIterator, getIteratorAtPos, getLowBound, getNextTypeName, getNextTypeObject, getPosNext, getPosPrevious, getSize, gotoAttributesStart, gotoChildrenStart, gotoParent, hashCode, hasPrevious, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, nextIndex, nextMatching, nextPos, parentPos, previousPos, rank, releasePos, remove, remove, removeAll, removePos, retainAll, set, setPosNext, setPosPrevious, stableCompare, startPos, subList, subSequence, subSequencePos, toArray, toArray, toString, unsupported, unsupportedException
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface gnu.lists.Sequence
elements, isEmpty
 
Methods inherited from interface java.util.List
add, addAll, addAll, clear, contains, containsAll, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, retainAll, subList, toArray, toArray
 
Methods inherited from interface gnu.lists.Consumable
consume
 

Field Detail

minGapSize

public static final int minGapSize
See Also:
Constant Field Values
Constructor Detail

LineOffsets

public LineOffsets(int initialSize)
Method Detail

insertLine

public void insertLine(int index,
                       int offSet)

index2offset

public int index2offset(int index)

offset2index

public int offset2index(int offset)
We seek the line containing a given text offset using a halfing of intervals algorithm. Therefore the method will use O(log(n)) time, n being the number of lines.

See Also:
org.eclipse.swt.custom.StyledTextContent#getLineAtOffset(int)

deleteLines

public void deleteLines(int firstLine,
                        int numberOfLines)

insertLines

public void insertLines(int index,
                        int[] offsets)

toString

public java.lang.String toString()
Overrides:
toString in class AbstractSequence

countLines

public int countLines(java.lang.String newText)

linesInRange

public int linesInRange(int startOffset,
                        int endOffset)

textRegionMoved

public void textRegionMoved(int regionStart,
                            int regionEnd,
                            int displacement)

textInserted

public void textInserted(int startOffset,
                         java.lang.CharSequence seq)

textDeleted

public void textDeleted(int startOffset,
                        int endOffset)

isLineDelimiter

public boolean isLineDelimiter(char c)