{"record":{"id":"b4d2cdab78855f91","repo":"dotnet/wpf","slug":"sr-textsegmentsmustnotoverlap","errorCode":null,"errorMessage":"SR.TextSegmentsMustNotOverlap","messagePattern":"SR\\.TextSegmentsMustNotOverlap","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/TextAnchor.cs","lineNumber":635,"sourceCode":"        }\n\n\n        /// <summary>\n        /// Inserts a segment into this anchor in the right order.  If the new segment\n        /// overlaps with existing anchors it throws an exception.\n        /// </summary>\n        private void InsertSegment(TextSegment newSegment)\n        {\n            int i = 0;\n            for (; i < _segments.Count; i++)\n            {\n                if (newSegment.Start.CompareTo(_segments[i].Start) < 0)\n                    break;\n            }\n\n            // Make sure it starts after the one its being put behind\n            if (i > 0 && newSegment.Start.CompareTo(_segments[i - 1].End) < 0)\n                throw new InvalidOperationException(SR.TextSegmentsMustNotOverlap);\n\n            // Make sure it ends before the one its being put ahead of\n            if (i < _segments.Count && newSegment.End.CompareTo(_segments[i].Start) > 0)\n                throw new InvalidOperationException(SR.TextSegmentsMustNotOverlap);\n\n            _segments.Insert(i, newSegment);\n        }\n\n        /// <summary>\n        /// Creates a new segment with the specified pointers, but first\n        /// normalizes them to make sure they are on insertion positions.\n        /// </summary>\n        /// <param name=\"start\">start of the new segment</param>\n        /// <param name=\"end\">end of the new segment</param>\n        private static TextSegment CreateNormalizedSegment(ITextPointer start, ITextPointer end)\n        {\n            // Normalize the segment\n            if (start.CompareTo(end) == 0)","sourceCodeStart":617,"sourceCodeEnd":653,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/TextAnchor.cs#L617-L653","documentation":"TextAnchor keeps its segments in a sorted, non-overlapping list. InsertSegment throws this InvalidOperationException when the new segment's Start falls before the End of the preceding segment — i.e. the segment would overlap an already-registered segment. Overlapping segments would break the anchor's ordering invariant, so the library rejects the insert.","triggerScenarios":"AddTextSegment or ExclusiveUnion inserting a segment whose Start is before the End of the segment at _segments[i-1].","commonSituations":"Annotation code creating two overlapping highlights over the same document text (e.g. highlight a range that intersects an existing highlight), or programmatic range computation that produces intersecting spans.","solutions":["Compute the segment so it does not intersect existing anchor segments; clamp its Start/End outside existing ranges.","Clip the new range against existing segments (subtract overlaps) before calling AddTextSegment.","Track existing segments (via the anchor's segment list) and merge/union ranges instead of inserting raw overlapping ones."],"exampleFix":"// before\nanchor.AddTextSegment(start, end); // may overlap existing segment\n// after\nif (!existingSegments.Any(s => start.CompareTo(s.End) < 0 && end.CompareTo(s.Start) > 0))\n{\n    anchor.AddTextSegment(start, end);\n}","handlingStrategy":"validation","validationCode":"bool overlaps = existingSegments.Any(s => newStart.CompareTo(s.End) < 0 && newEnd.CompareTo(s.Start) > 0);\nif (overlaps) /* merge/clip before AddTextSegment */;","typeGuard":null,"tryCatchPattern":"try { anchor.AddTextSegment(start, end); }\ncatch (InvalidOperationException) { /* clip range and retry */ }","preventionTips":["Maintain your own sorted list of created segments and check intersection before inserting.","Prefer union/merge operations over raw inserts for intersecting ranges.","Compute annotation ranges from the same resolved offsets used for existing segments."],"tags":["wpf","annotations","invariant","overlapping-segments"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}