nordfalk / jsyntaxpane

An updated version of code.google.com/p/jsyntaxpane r096 branch
36 stars 22 forks source link

Replace-all and then undo will erase buffer. #117

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Open a file in the buffer
2. Do a replace all
3. ctrl-z will erase the buffer

The problem seems to be in the FindReplaceAction under replaceAll:

        String newText = matcher.replaceAll(replacement);
        target.setText(newText);

Using setText probably makes the entire insertion an UndoableEdit so it
gets erased when you do ctrl-z.

Original issue reported on code.google.com by smcal...@gmail.com on 16 Oct 2009 at 5:09

GoogleCodeExporter commented 9 years ago
Doing this instead seems to work much more nicely with the undo command:

        // Create matcher off of document text
        Matcher matcher;
        try {
            matcher = pattern.matcher(sDoc.getText(0, sDoc.getLength()));
        } catch (javax.swing.text.BadLocationException e) {
            return;
        }

        if (matcher == null) 
            return;

        // Loop over matches, replacing text in the widget in a way that plays
        // nicely with UndoManager
        int offset   = 0;
        while (matcher.find()) {
            int start = matcher.start() + offset;
            int end   = matcher.end()   + offset;

            // Offset future selections by difference in text length
            offset += replacement.length() - (end-start);

            target.select(start, end);
            target.replaceSelection(replacement);
        }

Original comment by smcal...@gmail.com on 16 Oct 2009 at 6:10

GoogleCodeExporter commented 9 years ago
this seems to be related to issue 112

Original comment by elib...@gmail.com on 22 Oct 2009 at 7:40

GoogleCodeExporter commented 9 years ago
Something similar done.

I did more changes in r117.  Will create a binary download shortly.

See issue 88

Original comment by ayman.al...@gmail.com on 8 Nov 2009 at 10:19