public void InsertAt(int offset, int length) { PruneObsolete(); for (var i = positions.Count - 1; i >= 0; i--) { var pos = positions[i]; if (pos.Offset < offset) { // the list is sorted, so once we hit the first element that is smaller // than the offset we are looking for, we can stop iterating. break; } if (pos.Offset == offset && pos.Bias == Bias.Forward) { // dont modify the start of a element or selection. break; } TextPosition target; if (!pos.Reference.TryGetTarget(out target)) { positions.RemoveAt(i); } else { target.Offset += length; positions[i] = new HardPosition(target); } } positions.Sort(); }
public ITextPosition Create(int offset, Bias bias) { PruneObsolete(); var insertPosition = FindInsertPosition(offset, bias); if (insertPosition >= 0) { TextPosition target; if (positions[insertPosition].Reference.TryGetTarget(out target)) { return(target); } target = new TextPosition(offset, bias); positions[insertPosition] = new HardPosition(target); return(target); } var pos = ~insertPosition; var retval = new TextPosition(offset, bias); positions.Insert(pos, new HardPosition(retval)); return(retval); }
public void RemoveAt(int offset, int length) { PruneObsolete(); var endOffset = offset + length; for (var i = positions.Count - 1; i >= 0; i--) { var pos = positions[i]; if (pos.Offset < offset) { // the list is sorted, so once we hit the first element that is smaller // than the offset we are looking for, we can stop iterating. break; } TextPosition target; if (!pos.Reference.TryGetTarget(out target)) { positions.RemoveAt(i); } else { if (target.Offset >= endOffset) { target.Offset -= length; positions[i] = new HardPosition(target); } else if (target.Offset > offset) { target.Offset = offset; positions[i] = new HardPosition(target); } } } positions.Sort(); }
bool IsReferenceDead(HardPosition position) { TextPosition target; return(!position.Reference.TryGetTarget(out target)); }