TextSegmentCollection - unexpected behaviour when replacing text

未关闭
#331 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp
领域
desktop

调研方向

从 ICSharpCode.AvalonEdit/Document/TextSegmentCollection.cs 中的 ReplaceText 开始,尤其检查链接的行,并审查 discussion #327 中描述的 TextSegment.OnSegmentChanged 行为。重现替换期间 TextMarker 集合的不一致。完成标准是:回调不再观察到处于集合中间状态的 segment,并且现有的替换行为保持不变。

由索引模型根据 Issue 内容生成。

描述

Area-Editing Issue-Question

I've come across an issue which I suspect is caused by the internal implementation of TextSegmentCollection.ReplaceText, specifically this section

https://github.com/icsharpcode/AvalonEdit/blob/47f61e9427b0cb2653f83e8c303b9ea94c8ef60c/ICSharpCode.AvalonEdit/Document/TextSegmentCollection.cs#L200-L203

A segment gets removed from the collection, then manipulated, then re-added to the collection.

If there is any logic reacting to the in-between manipulation, it may behave unexpectedly, since the segment is not a member of the collection (at that time).

My problem were Segments (specifically TextMarkers) which got replaced in their entirety, but would claim not to be members of their corresponding collection during a TextSegment.OnSegmentChanged invocation, only to "magically" reappear during later calls (specifically redraws). See https://github.com/icsharpcode/AvalonEdit/discussions/327

There may be some hacky ways to work around the problem (sadly there are no "added/removed" triggers for the segment collection.. and it's sealed...). One could post some handler via Dispatcher to "recheck" a segment... but that's... messy.

A clean solution to my mind would be adding BeginChange() / EndChange() methods to TextSegment (similar to what exists for TextDocument) which suppress all OnSegmentChanged invocations and rework the ReplaceText method to use those methods:

...
segment.BeginChange();
RemoveSegment(segment); 
segment.StartOffset = offset + change.RemovalLength; 
segment.Length = Math.Max(0, remainingLength); 
AddSegment(segment); 
segment.EndChange();
...

That would defer any OnSegmentChanged invocation until the segment is once again in an expected state.

主要语言
C#
星标
2.1k
派生
496
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

icsharpcode/AvalonEdit 的其他 Issue

查看 icsharpcode/AvalonEdit 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。