public static CaretVector From(SourcePoint target, SourcePoint source, ElementPosition position)
 {
     CaretVector caretVector = new CaretVector(position);
     caretVector.LineDelta = target.Line - source.Line;
     caretVector.OffsetDelta = target.Offset - source.Offset;
     return caretVector;
 }
        private SourcePoint GetSourcePoint(LanguageElement element, CaretVector vector)
        {
            if (element == null || vector == null)
                return SourcePoint.Empty;
            ICapableBlock iCapableBlock = null;
              switch (vector.ElementPosition)
            {
                case ElementPosition.Start:
                    return GetPosition(element.Range.Start, vector);
                case ElementPosition.End:
                    return GetPosition(element.Range.End, vector);
                case ElementPosition.NameRangeStart:
                    return GetPosition(element.NameRange.Start, vector);
                case ElementPosition.NameRangeEnd:
                    return GetPosition(element.NameRange.End, vector);

                case ElementPosition.BlockBegin:
                    iCapableBlock = element as ICapableBlock;
                    if (iCapableBlock != null)
                        return GetPosition(iCapableBlock.BlockStart.End, vector);
                    else
                        return GetPosition(element.Range.Start, vector);
                case ElementPosition.BlockEnd:
                    iCapableBlock = element as ICapableBlock;
                    if (iCapableBlock != null)
                        return GetPosition(iCapableBlock.BlockEnd.Start, vector);
                    else
                        return GetPosition(element.Range.End, vector);
                case ElementPosition.FirstChildStart:
                    LanguageElement firstChild = element.FirstChild;
                    if (firstChild == null)
                        return SourcePoint.Empty;
                    return GetPosition(firstChild.Range.Start, vector);
                case ElementPosition.LastChildEnd:
                    LanguageElement lastChild = element.LastChild;
                    if (lastChild == null)
                        return SourcePoint.Empty;
                    return GetPosition(lastChild.Range.End, vector);
                case ElementPosition.FirstDetailStart:
                    LanguageElement firstDetail = element.FirstDetail;
                    if (firstDetail == null)
                        return SourcePoint.Empty;
                    return GetPosition(firstDetail.Range.Start, vector);
                case ElementPosition.LastDetailEnd:
                    LanguageElement lastDetail = element.LastDetail;
                    if (lastDetail == null)
                        return SourcePoint.Empty;
                    return GetPosition(lastDetail.Range.End, vector);
            }
            throw new Exception("Unexpected ElementPosition enum element.");
        }
 private static SourcePoint GetPosition(SourcePoint sourcePoint, CaretVector vector)
 {
     return sourcePoint.OffsetPoint(vector.LineDelta, vector.OffsetDelta);
 }