TextSegmentCollection - unexpected behaviour when replacing text

オープン
#331 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp
領域
desktop

調査の方向性

ICSharpCode.AvalonEdit/Document/TextSegmentCollection.cs の ReplaceText から始め、特にリンクされた行を確認し、discussion #327 で説明されている TextSegment.OnSegmentChanged の動作を見直してください。置換中に TextMarker コレクションの不整合を再現してください。コールバックがコレクションの中間状態にあるセグメントを観測しなくなり、既存の置換動作が維持されれば完了です。

索引モデルが 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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

icsharpcode/AvalonEdit のほかの issue

icsharpcode/AvalonEdit の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。