예제 #1
0
        public DocumentRange Translate(TreeTextRange range)
        {
            if (!range.IsValid() || !_sourceFile.IsValid())
            {
                return(DocumentRange.InvalidRange);
            }

            IncludeWithOffset atStart = FindIncludeAtOffset(range.StartOffset, true);
            IncludeWithOffset atEnd   = FindIncludeAtOffset(range.EndOffset, atStart.Include == null);

            // two different parts are overlapping
            IT4Include include = atStart.Include;

            if (include != atEnd.Include)
            {
                return(DocumentRange.InvalidRange);
            }

            // recursive includes
            if (include != null)
            {
                if (include.DocumentRangeTranslator != null)
                {
                    return(include.DocumentRangeTranslator.Translate(range));
                }
                return(DocumentRange.InvalidRange);
            }

            int rootStartOffset = _root.GetTreeStartOffset().Offset;

            return(new DocumentRange(_sourceFile.Document, new TextRange(atStart.Offset - rootStartOffset, atEnd.Offset - rootStartOffset)));
        }
예제 #2
0
        private IncludeWithOffset FindIncludeAtOffset(TreeOffset offset, bool preferRoot)
        {
            // no includes, tree and document are matching
            if (_includes.Count == 0)
            {
                return(new IncludeWithOffset(offset.Offset));
            }

            int includesLength = 0;
            int count          = _includes.Count;

            for (int i = 0; i < count; i++)
            {
                IT4Include    include      = _includes[i];
                TreeTextRange includeRange = include.GetTreeTextRange();

                // the offset is before the include, in the root file
                if (offset < includeRange.StartOffset)
                {
                    return(new IncludeWithOffset((offset - includesLength).Offset));
                }

                // the offset is inside the include
                if (offset < includeRange.EndOffset)
                {
                    // we're on an edge position: we can be just after the end of the root file,
                    // or just at the beginning of an include; we make the choice using the preferRoot parameter
                    if (offset == includeRange.StartOffset && preferRoot)
                    {
                        return(new IncludeWithOffset((offset - includesLength).Offset));
                    }
                    return(new IncludeWithOffset(offset - includeRange.StartOffset, include));
                }

                includesLength += includeRange.Length;
            }

            // the offset is after the include, in the root file
            return(new IncludeWithOffset((offset - includesLength).Offset));
        }
예제 #3
0
			internal IncludeWithOffset(int offset, [CanBeNull] IT4Include include = null) {
				Include = include;
				Offset = offset;
			}
예제 #4
0
 internal IncludeWithOffset(int offset, [CanBeNull] IT4Include include = null)
 {
     Include = include;
     Offset  = offset;
 }