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))); }
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)); }
internal IncludeWithOffset(int offset, [CanBeNull] IT4Include include = null) { Include = include; Offset = offset; }