internal void DoUpdates()
        {
            Highlight tu;

            //get indices of change
            changeIndices = TextBoxHighlightHandler.IdentifyChange(currentText, oldText);



            //go through underlines and adjust for change
            int changeMagnitude = currentText.Length - oldText.Length;


            //set old text to current
            oldText = currentText;


            for (int i = 0; i < highlights.Count && i >= 0; i++)
            {
                tu = highlights[i];

                //text preceding text in change text US after UE change
                //                   ^--------------------------^
                //			removal
                //move start and end by change magnitude, if this moves index past start of change
                //then set the index to the point of change. (true if index was in removal seciton)

                if (changeIndices[0] == tu.Start || changeIndices[0] == tu.End ||
                    changeIndices[1] == tu.Start || changeIndices[1] == tu.End)
                {
                    tu.Hidden = false;
                }

                int newStart = tu.Start, newEnd = tu.End;

                //change was before underline, update to where underline will be NOW.
                if (changeIndices[0] <= tu.Start)
                {               //if the change began before the underline start (doesnt matter where it ended)
                    if (tu.Start + changeMagnitude >= changeIndices[0])
                    {
                        newStart = tu.Start + changeMagnitude;
                    }
                    else
                    {
                        newStart = changeIndices[0];
                    }
                }
                if (changeIndices[0] < tu.End)
                {               //if the change began before the underline end (doesnt matter where it ended)
                    if (tu.End + changeMagnitude >= changeIndices[0])
                    {
                        newEnd = tu.End + changeMagnitude;
                    }
                    else
                    {
                        newEnd = changeIndices[0];
                    }
                }

                if (newStart != tu.Start || newEnd != tu.End)
                {
                    MoveHighlight(tu, newStart, newEnd);
                }



                int c0 = changeIndices[0]
                , c1   = changeIndices[1]
                , us   = tu.Start
                , ue   = tu.End;



                //We must remove underlines that have been tampered with, because although in most cases it wont matter, if the tamper is an added or removed
                //space we must let the spell check add correct lines.  One underline broken into two, will not happen without this.
                if (
                    (c0 >= us && c0 < ue) ||                                            //change start is inside underline
                    (c1 > us && c1 <= ue) ||                                            //change end is inside underline
                    (c0 <= us && c1 >= ue) ||                                           //change is around underline
                    (us == ue)                                                          //underline shrunk to nothing
                    )
                {
                    RemoveHighlight(tu.Start, tu.End);

                    --i;
                    continue;
                }
            }

            List <Run> runs        = new List <Run>();
            int        rescanStart = changeIndices[0];

            rescanStart -= 30;//recheck some words before
            if (rescanStart < 0)
            {
                rescanStart = 0;
            }
            else //go from the next space
            {
                int possibleStart;
                if ((possibleStart = currentText.IndexOf(' ', rescanStart)) > -1 && possibleStart < changeIndices[0])
                {
                    rescanStart = possibleStart;
                }
            }
            int rescanEnd = changeIndices[1];

            rescanEnd += 30;
            if (rescanEnd > currentText.Length)
            {
                rescanEnd = currentText.Length;
            }
            else
            {//go to a prev space
                int possibleEnd;
                if (rescanEnd < currentText.Length && (possibleEnd = currentText.LastIndexOf(' ', rescanEnd)) > -1 && possibleEnd > changeIndices[1])
                {
                    rescanEnd = possibleEnd;
                }
            }

            runs.Add(new Run(currentText.Substring(rescanStart, rescanEnd - rescanStart).PadLeft(rescanEnd)));
            OnNewSearchNeeded(runs);

            AdornerLayer.Update();
        }
Beispiel #2
0
        internal void DoUpdates()
        {
            RichTextBoxHighlight tu;
            List <Run>           newCheckRuns = new List <Run>();

            //get indices of change
            changeIndices = TextBoxHighlightHandler.IdentifyChange(currentText, oldText);

            TextPointer[] editPoints = adapter.GetPointerAtIndex(changeIndices[0], changeIndices[1]);
            if (editPoints[0] != null && editPoints[1] != null)
            {
                newCheckRuns.AddRange(GetRuns(editPoints[0], editPoints[1]));
            }


            //go through underlines and adjust for change
            int changeMagnitude = currentText.Length - oldText.Length;


            //set old text to current
            oldText = currentText;

            //need highlights to be in sorted order for highlightIndexes to work
            highlights.Sort(highlightComparer);

            List <TextPointer>          highlightPointers = new List <TextPointer>(highlights.Count);
            List <int[]>                highlightIndexes  = new List <int[]>(highlights.Count);
            List <RichTextBoxHighlight> highlightsToMove  = new List <RichTextBoxHighlight>(highlights.Count);

            bool willMove = false;



            for (int i = 0; i < highlights.Count && i >= 0; i++)
            {
                willMove = false;
                tu       = highlights[i] as RichTextBoxHighlight;

                //text preceding text in change text US after UE change
                //                   ^--------------------------^
                //			removal
                //move start and end by change magnitude, if this moves index past start of change
                //then set the index to the point of change. (true if index was in removal seciton)

                if (changeIndices[0] == tu.AbsoluteStart || changeIndices[0] == tu.AbsoluteEnd ||
                    changeIndices[1] == tu.AbsoluteStart || changeIndices[1] == tu.AbsoluteEnd)
                {
                    tu.Hidden = false;
                }

                int newStart = tu.AbsoluteStart, newEnd = tu.AbsoluteEnd;

                //change was before underline, update to where underline will be NOW.
                if (changeIndices[0] <= tu.AbsoluteStart)
                {               //if the change began before the underline start (doesnt matter where it ended)
                    if (tu.AbsoluteStart + changeMagnitude >= changeIndices[0])
                    {
                        newStart = tu.AbsoluteStart + changeMagnitude;
                    }
                    else
                    {
                        newStart = changeIndices[0];
                    }
                }
                if (changeIndices[0] < tu.AbsoluteEnd)
                {               //if the change began before the underline end (doesnt matter where it ended)
                    if (tu.AbsoluteEnd + changeMagnitude >= changeIndices[0])
                    {
                        newEnd = tu.AbsoluteEnd + changeMagnitude;
                    }
                    else
                    {
                        newEnd = changeIndices[0];
                    }
                }

                if (newStart != tu.AbsoluteStart || newEnd != tu.AbsoluteEnd)
                {
                    willMove = true;

                    //   highlightIndexes.Add(new int[2]{newStart, newEnd});
                    //   highlightsToMove.Add(tu);

                    /*    TextPointer[] startPoints = adapter.GetPointerAtIndex(newStart, newEnd);
                     *  if(startPoints!=null)
                     *      MoveHighlight(tu, startPoints[0],newEnd-newStart);
                     */
                }
                //MoveHighlight(tu, newStart, newEnd, true);



                int c0 = changeIndices[0]
                , c1   = changeIndices[1]
                , us   = tu.AbsoluteStart
                , ue   = tu.AbsoluteEnd;



                //We must remove underlines that have been tampered with, because although in most cases it wont matter, if the tamper is an added or removed
                //space we must let the spell check add correct lines.  One underline broken into two, will not happen without this.
                if (
                    (c0 >= us && c0 < ue) ||                                            //change start is inside underline
                    (c1 > us && c1 <= ue) ||                                            //change end is inside underline
                    (c0 <= us && c1 >= ue) ||                                           //change is around underline
                    (us == ue)                                                          //underline shrunk to nothing
                    )
                {
                    RemoveHighlight(tu);
                    willMove = false;
                    //newCheckRuns.Add(tu.Run);


                    --i;
                    //   continue;
                }

                if (willMove && newStart != newEnd)
                {
                    highlightIndexes.Add(new int[2] {
                        newStart, newEnd
                    });
                    highlightsToMove.Add(tu);
                }
            }

            if (highlightsToMove.Count > 0)
            {
                //highlightIndexes.Clear();
                //highlightIndexes.Add(new int[] { 11382, 1 });
                adapter.GetPointerAtIndexes(highlightIndexes, highlightPointers);
                for (int i = 0; i < highlightsToMove.Count; i++)
                {
                    if (i >= highlightPointers.Count)//we have a highlight left over from an edit it seems
                    {
                        RemoveHighlight(highlightsToMove[i]);
                    }
                    else
                    {
                        TextPointer startPoints = highlightPointers[i]; //adapter.GetPointerAtIndex(newStart, newEnd);
                        if (startPoints != null)
                        {
                            MoveHighlight(highlightsToMove[i], startPoints, highlightIndexes[i][1] - highlightIndexes[i][0]);
                        }
                    }
                }
            }


            OnNewSearchNeeded(newCheckRuns);
            AdornerLayer.Update();
        }