Sunday, March 3, 2013

New Line Change Detection – Inserting Text

The insert text function of the edit box was implemented to process text inserted into the program so that changed lines could be detected and reported.  It was used to insert a new line, for the return key, the reimplemented pasted function, and the paste selection function (middle-click paste on Linux).  This function is not longer needed and was removed.  The code for the return key was already changed to simply use the insert text function of the text cursor (and the document change function handles the line change detection).

The other two uses were also updated starting with the reimplemented paste function originally necessary to catch the paste action (menu, context menu, and tool bar) and call the insert text function.  The paste function is no longer necessary since the document change slot function will be called for all paste operations.  Therefore the reimplemented paste function was removed.  The check for the paste key sequence is also no longer necessary (will be handled by the plain text edit base class key event handle, which will call the document change slot function).

The mouse release event handler was reimplemented in the edit box class to catch the middle-click paste of the selection and call the insert text function.  This is also no longer necessary, so the handler function was removed.  However, the paste selection function, which was called from the mouse release event handler, is also called for the Control+Shift+Insert key sequence to pasting the selection.  This is still needed, so the insert text function call was replaced with the text cursor insert text function.

[commit 361f10a7f5]

New Line Change Detection Code

Detecting all line changes can be performed by connecting to the edit box document's contents change signal that includes the position in the document of the change along with the number of characters removed and added.  Previously, only the number of character values were being saved for the various detection routines and the position was ignored.  However, the position can be used along with some additional information, to fully detect what has changed in the document, and it turns out the number of characters values no longer need to be stored.

One piece of additional information needed is the net change in the number of lines of the document, which can be used to indicate whether lines were inserted or deleted.  Therefore, a new total line count variable was added to the edit box class and updated after processing a contents change signal.  Other information used is if the position of the change was at the beginning of the line and if the cursor is at the end of the line after the change.

The three signals emitted from the edit box for program changes (line changed, lines inserted and lines deleted) were replaced with a single lines changed signal that includes the line number of the change, the number of lines deleted (could be zero), the number of lines inserted (could be zero) and a list of strings of the lines that were changed (if any) and inserted (if any).  The number of lines changed is the size of this list minus the number of lines inserted.

One of the things the new detection code does not do that the old code attempted to do was determine whether a line actually changed or not, and if not it is not reported.  For example, inserted text that started with a new line inserted at the end of a line, the line is not actually modified so it wasn't reported.  Since the new code does not do this, the receiver of the lines changed signal, will perform this check to see if a particular line was actually changed (to prevent the line from being recompiled).

The new line change detection code is mostly fully working, however, it is not completely hooked up to all the possible document changes that can occur.  In some cases, the code was simply modified to compile, so some code was temporarily commented instead of being changed.  In other cases, the code was modified like the capture deleted lines routine was not needed and was removed.  All of the other causes of changes will be hooked and the code will be cleaned up over the next set of commits.

[commit 00dfbe3268]