{"record":{"id":"eb5079b20a57e393","repo":"LykosAI/StabilityMatrix","slug":"cannot-create-a-marker-when-not-attached-to-a-document","errorCode":null,"errorMessage":"Cannot create a marker when not attached to a document","messagePattern":"Cannot create a marker when not attached to a document","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Controls/TextMarkers/TextMarkerService.cs","lineNumber":76,"sourceCode":"        //    {\n        //        args.SetToolTip(GetTooltipTextForCollapsedSection(args, collapsedSection));\n        //    }\n        //}\n\n        var markersAtOffset = GetMarkersAtOffset(offset);\n        var markerWithToolTip = markersAtOffset.FirstOrDefault(marker => marker.ToolTip != null);\n        if (markerWithToolTip != null && markerWithToolTip.ToolTip != null)\n        {\n            args.SetToolTip(markerWithToolTip.ToolTip);\n        }\n    }*/\n\n    #region TextMarkerService\n\n    public TextMarker? TryCreate(int startOffset, int length)\n    {\n        if (_markers == null)\n            throw new InvalidOperationException(\"Cannot create a marker when not attached to a document\");\n\n        var textLength = _document.TextLength;\n        if (startOffset < 0 || startOffset > textLength) return null;\n        //throw new ArgumentOutOfRangeException(nameof(startOffset), startOffset, \"Value must be between 0 and \" + textLength);\n        if (length < 0 || startOffset + length > textLength) return null;\n        //throw new ArgumentOutOfRangeException(nameof(length), length, \"length must not be negative and startOffset+length must not be after the end of the document\");\n\n        var marker = new TextMarker(this, startOffset, length);\n        _markers.Add(marker);\n        return marker;\n    }\n\n    public IEnumerable<TextMarker> GetMarkersAtOffset(int offset)\n    {\n        return _markers.FindSegmentsContaining(offset);\n    }\n\n    public IEnumerable<TextMarker> TextMarkers => _markers ?? Enumerable.Empty<TextMarker>();","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Controls/TextMarkers/TextMarkerService.cs#L58-L94","documentation":"TextMarkerService.TryCreate requires the service to be attached to a text document (an AvalonEdit-style document/text area). When _markers is null — meaning the service never attached — it throws InvalidOperationException('Cannot create a marker when not attached to a document') instead of silently returning null.","triggerScenarios":"Calling TryCreate before the service is attached to a TextArea/Document (e.g. during editor construction or after the editor was closed and detached), or creating markers from a background thread before attachment completes.","commonSituations":"Diagnostics/ squiggles pipeline running before the editor finishes initializing; markers requested after document unload; view model calls into the service when the editor control hasn't loaded.","solutions":["Guard calls so TryCreate is only invoked after the TextArea.Document is set and the service is attached","Check an IsAttached/_markers != null condition before calling TryCreate","Re-attach (or recreate) the service when a new document is loaded","Return null or queue pending markers until attachment instead of calling early"],"exampleFix":"// before\nvar marker = _markerService.TryCreate(offset, length);\n// after\nif (_markerService is { } svc && textArea.Document is not null)\n    var marker = svc.TryCreate(offset, length);","handlingStrategy":"try-catch","validationCode":"if (textArea?.Document is null) return null; // not attached yet","typeGuard":"bool IsAttached(TextMarkerService s) => s.GetType().GetField(\"_markers\", BindingFlags.NonPublic|BindingFlags.Instance)?.GetValue(s) != null;","tryCatchPattern":"try { marker = service.TryCreate(offset, length); }\ncatch (InvalidOperationException) { marker = null; /* queue for after attach */ }","preventionTips":["Only create markers after TextArea.Document is assigned","Defer diagnostics until the editor Loaded event","Recreate/reattach the marker service on document change"],"tags":["avaloniaedit","text-markers","invalid-state","editor"],"backgroundTag":"invalid-state-transition","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}