5454 */
5555public class FindReplaceMachine {
5656
57- static private Log _log = new Log ("FindReplace.txt" , false );
57+ static private final Log _log = new Log ("FindReplace.txt" , false );
5858
5959 /* Visible machine state; manipulated directly or indirectly by FindReplacePanel. */
6060 private volatile OpenDefinitionsDocument _doc ; // Current search document
@@ -90,6 +90,8 @@ public FindReplaceMachine(SingleDisplayModel model, DocumentIterator docIterator
9090 _frame = frame ;
9191 _docIterator = docIterator ;
9292 _current = -1 ;
93+ /* Using setters for internal initilization is VERY BAD taste; it obscures the initial state. What
94+ * is the invariant for instances of this class? Who knows? */
9395 setFindAnyOccurrence ();
9496 setFindWord ("" );
9597 setReplaceWord ("" );
@@ -258,7 +260,8 @@ public boolean replaceCurrent() {
258260 */
259261 public void setSelection (MovingDocumentRegion s ) { _selectionRegion = s ; }
260262
261- /** Alternative interface for the private method replaceAll(...) */
263+ /** Alternative interface for the private method replaceAll(...)
264+ * @return the number of replacements */
262265 public int replaceAll () { return replaceAll (_searchAllDocuments , _searchSelectionOnly ); }
263266
264267 /** Replaces all occurences of the find word with the replace word
@@ -313,11 +316,12 @@ private int _replaceAllInCurrentDoc(boolean searchSelectionOnly) {
313316
314317 assert EventQueue .isDispatchThread ();
315318
316- if (!searchSelectionOnly ) {
319+ if (! searchSelectionOnly ) {
317320 _selectionRegion = new MovingDocumentRegion (_doc , 0 , _doc .getLength (),
318321 _doc ._getLineStartPos (0 ),
319322 _doc ._getLineEndPos (_doc .getLength ()));
320323 }
324+ /* _selectionRegion is not degenerate unless document is degenerate; may be entire document. */
321325 if (_isForward ) setPosition (_selectionRegion .getStartOffset ());
322326 else setPosition (_selectionRegion .getEndOffset ());
323327
@@ -336,39 +340,45 @@ private int _replaceAllInCurrentDoc(boolean searchSelectionOnly) {
336340 return count ;
337341 }
338342
339- /** Processes all occurrences of the find word with the replace word in the current document or in all documents
340- * depending the values of fields _searchAllDocuments and _searchSelectionOnly and parameter region. Assumes that
341- * findAction does not modify the document it processes. Only executes in event thread.
343+ /** Replaces all occurrences of the find word with the replace word in the specified region while performing the
344+ * which occurs within the current document. On each match, the findAction command is executed, assuming that
345+ * indAction does not modify the document it processes. Saves value of _searchAllDocuments and _searchSelectionOnly
346+ * and restores trem, an ugly hack dictated by embedding this information in the FindReplaceMachine. During this
347+ * particular search action, _searchAllDocuments is false since it is confined to a region within the current document.
348+ * Only executes in event thread.
342349 * @param findAction action to perform on the occurrences; input is the FindResult, output is ignored
343350 * @param region the selection region
344351 * @return the number of processed occurrences
345352 */
346353 public int processAll (Runnable1 <FindResult > findAction , MovingDocumentRegion region ) {
347354
348355 assert EventQueue .isDispatchThread ();
349-
356+
350357 _selectionRegion = region ;
351- return processAll (findAction );
358+
359+ int count = processAll (findAction );
360+
361+ return count ;
352362 }
353363
354364 /** Processes all occurences of the find word with the replace word in the current document or in all documents
355365 * depending the value of fields _searchAllDocuments, _searchSelectionOnly, _selectionRegion. Assumes that
356366 * findAction does not modify the document it processes. Only executes in event thread.
357- * @param findAction action to perform on the occurrences; input is the FindResult, output is ignored
358- * @param searchAll true if we should search for occurrences in all documents
359- * @param searchSelectionOnly true if we should only search in the current selection of document
367+ * Modifies _searchSelectionOnly if it is inconsistent with _searchAllDocuments
368+ * @param findAction action to perform on the occurrences; input is the FindResult, output is ignored.
360369 * @return the number of replacements
361370 */
362371 private int processAll (Runnable1 <FindResult > findAction ) {
363372
364373 assert EventQueue .isDispatchThread ();
365374
366375 if (_searchAllDocuments ) {
367- int count = 0 ; // the number of replacements done so far
376+ int count = 0 ; // the number of replacements done so far
377+ _searchSelectionOnly = false ; // force _searchSelectionOnly to be false
368378 final int n = _docIterator .getDocumentCount ();
369379 for (int i = 0 ; i < n ; i ++) {
370380 // process all in the rest of the documents
371- _searchSelectionOnly = false ; // force _searchSelectionOnly to be false
381+
372382 count += _processAllInCurrentDoc (findAction );
373383 _doc = _docIterator .getNextDocument (_doc , _frame );
374384
@@ -385,25 +395,27 @@ else if (_searchSelectionOnly) {
385395 count += _processAllInCurrentDoc (findAction );
386396 return count ;
387397 }
388- else return _processAllInCurrentDoc (findAction );
398+ else /* search entire current document */
399+ return _processAllInCurrentDoc (findAction );
389400 }
390401
391- /** Processes all occurences of _findWord in _doc. Never processes other documents. Starts at the beginning or the
392- * end of the document (depending on find direction). This convention ensures that matches created by string
393- * replacement will not be replaced as in the following example:<p>
394- * findString: "hello"<br>
395- * replaceString: "e"<br>
396- * document text: "hhellollo"<p>
397- * Only executes in event thread. Assumes (i) this has exclusive access to _doc (since it only runs in event thread)
398- * and (ii) findAction does not modify _doc.
399- * @param findAction action to perform on the occurrences; input is the FindResult, output is ignored
400- * @return the number of replacements
401- */
402+ /** Processes all occurences of _findWord in _doc depending the values of fields _searchSelectionOnly,
403+ * _selectionRegion and _isForward. Ignores value of _searchAllDocuments. Processes selected region (which may be
404+ * the whole document) sequentially depending on find direction. This convention ensures that matches created by
405+ * string replacement will not be replaced as in the following example:<p>
406+ * findString: "hello"<br>
407+ * replaceString: "e"<br>
408+ * document text: "hhellollo"<p>
409+ * Only executes in event thread. Assumes (i) this has exclusive access to _doc (since it only runs in event thread)
410+ * and (ii) findAction does not modify _doc.
411+ * @param findAction action to perform on the occurrences; input is the FindResult, output is ignored
412+ * @return the number of replacements
413+ */
402414 private int _processAllInCurrentDoc (Runnable1 <FindResult > findAction ) {
403415
404416 assert EventQueue .isDispatchThread ();
405417
406- if (!_searchSelectionOnly ) {
418+ if (! _searchSelectionOnly ) {
407419 _selectionRegion = new MovingDocumentRegion (_doc , 0 , _doc .getLength (), _doc ._getLineStartPos (0 ),
408420 _doc ._getLineEndPos (_doc .getLength ()));
409421 }
0 commit comments