RSyntaxTextArea icon indicating copy to clipboard operation
RSyntaxTextArea copied to clipboard

javax.swing.text.BadLocationException: Position not represented by view

Open mbrebion opened this issue 5 years ago • 14 comments

From time to time, I get this error : javax.swing.text.BadLocationException: Position not represented by view at org.fife.ui.rsyntaxtextarea.WrappedSyntaxView.modelToView(WrappedSyntaxView.java:724) at javax.swing.plaf.basic.BasicTextUI$RootView.modelToView(BasicTextUI.java:1509) at javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:1047) at javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:1022) at javax.swing.text.JTextComponent.modelToView(JTextComponent.java:1377) at org.fife.ui.rtextarea.RTextAreaBase.possiblyUpdateCurrentLineHighlightLocation(RTextAreaBase.java:754) at org.fife.ui.rtextarea.RTextArea.fireCaretUpdate(RTextArea.java:614) at org.fife.ui.rsyntaxtextarea.RSyntaxTextArea.fireCaretUpdate(RSyntaxTextArea.java:887) at javax.swing.text.JTextComponent$MutableCaretEvent.fire(JTextComponent.java:4394) at javax.swing.text.JTextComponent$MutableCaretEvent.stateChanged(JTextComponent.java:4416) at javax.swing.text.DefaultCaret.fireStateChanged(DefaultCaret.java:802) at javax.swing.text.DefaultCaret.changeCaretPosition(DefaultCaret.java:1274) at javax.swing.text.DefaultCaret.handleSetDot(DefaultCaret.java:1173) at javax.swing.text.DefaultCaret$DefaultFilterBypass.setDot(DefaultCaret.java:1913) at javax.swing.text.NavigationFilter.setDot(NavigationFilter.java:64) at org.fife.ui.rtextarea.ConfigurableCaret$FoldAwareNavigationFilter.setDot(ConfigurableCaret.java:724) at javax.swing.text.DefaultCaret.setDot(DefaultCaret.java:1151) at javax.swing.text.DefaultCaret$Handler.insertUpdate(DefaultCaret.java:1723) at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:201) at org.fife.ui.rsyntaxtextarea.RSyntaxDocument.fireInsertUpdate(RSyntaxDocument.java:195) at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:748) at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707) at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130) at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:669) at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1328) at org.fife.ui.rtextarea.RTextArea.handleReplaceSelection(RTextArea.java:869) at org.fife.ui.rtextarea.RTextArea.replaceSelection(RTextArea.java:1243) at org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit$InsertBreakAction.insertNewlineWithAutoIndent(RSyntaxTextAreaEditorKit.java:1539) at org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit$InsertBreakAction.handleInsertBreak(RSyntaxTextAreaEditorKit.java:1497) at org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit$InsertBreakAction.actionPerformedImpl(RSyntaxTextAreaEditorKit.java:1447) at org.fife.ui.rtextarea.RecordableTextAction.actionPerformed(RecordableTextAction.java:106) at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663) at javax.swing.JComponent.processKeyBinding(JComponent.java:2879) at javax.swing.JComponent.processKeyBindings(JComponent.java:2926) at javax.swing.JComponent.processKeyEvent(JComponent.java:2842) at java.awt.Component.processEvent(Component.java:6302) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4881) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771) at java.awt.Component.dispatchEventImpl(Component.java:4752) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:719) at java.awt.EventQueue$4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

And I don't know what causes it. This error has no consequences on the jtextarea except that after that, AutoCompletion do not work properly (I want it to start after "" is typed and it stops working after the error is triggered).

Do you know what might causes this error ?

mbrebion avatar Jul 21 '20 09:07 mbrebion

Looks like it happens when you press Enter somewhere in a word-wrapped file. What language are you editing when this happens? Also, even if you can't reproduce it reliably, can you provide a reasonably small text snippet where it happens at least frequently enough to serve as an example?

bobbylight avatar Aug 04 '20 03:08 bobbylight

Hi, I've been looking for this bug for a long time and have not been able yet to catch the exact moment when it is caused. I'm editing a custom language (derived from latex) for which I coded all required classes. So you expect that the problem is linked with word wrapping (I'll try to investigate this idea). Thank you

mbrebion avatar Aug 17 '20 12:08 mbrebion

I can't seem to reproduce this but hopefully between the two of us (or someone else) we'll find a test case.

bobbylight avatar Aug 20 '20 02:08 bobbylight

Ok, I finally took time to investigate on this This error happens when I type the ^ or the ` characters and then press return ; even in a blank text

This happens in RSyntaxTextArea with no particular settings.

Remark : If instead of typing a ^ directly, I paste it, I get no problem when returning to new line.

mbrebion avatar Feb 11 '21 15:02 mbrebion

It appears that the bug occur whenever I type a dead character. I don't know how to fix this bug correctly.

The simplest, but bad solution I've found is to reload the text file whenever such a character is found. Can you think about a better solution ? @bobbylight

mbrebion avatar Mar 13 '21 10:03 mbrebion

@mbrebion , it's probably a bug in your TokenMaker - this is the painful part of writing them, finding and fixing these bugs. Can you post a link to your TokenMaker? I might be able to try to take a look. I ran into issues like that a lot when writing the built-in TokenMakers and it's just something you eventually get better at identifying through practice (and copy-pasting from existing, working TokenMakers).

If you started with RSTA's LatexTokenMaker, maybe the bug is in there too, though ^ and ` aren't special characters there, and I don't konw Latex to guess at what "special" characters might be candidates for triggering this behavior.

bobbylight avatar Mar 13 '21 21:03 bobbylight

After further investigations, it appears that the bug already appears in existing syntaxes and is caused by the used for dead keys such as "^" in French keyboards. In order to check this, I've modified my keyboard layout to produce a normal (and not a dead key one) "^" when pressing the associated key and then the problem disappear. Hovewer, I'm not able to type characters such as "ê" anymore !

Here is the code I used (with RSTA 3.1.3)

package testrsyntaxtextarea;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;


public class TestRSyntaxTextArea extends JFrame{

    
    public  TestRSyntaxTextArea() {
        super();        
        this.setSize(500, 500);        
        RSyntaxTextArea rsta = new RSyntaxTextArea("Hi here !",10,10); 
        rsta.setLineWrap(true);
        this.add(rsta);        
        this.pack();
    }

    public static void main(String[] args) {
        TestRSyntaxTextArea test = new TestRSyntaxTextArea();
        test.setVisible(true);
        test.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    
}

mbrebion avatar Apr 12 '21 12:04 mbrebion

Awesome, thanks for the insight!

"Dead keys" are a new concept to me - it sounds like it's a modifier key that is used to inset characters with accent marks, is that right?

http://xahlee.info/kbd/keyboard_whats_alt_graph__compose__dead_key.html#:~:text=French%20AZERTY%20keyboard.,of%20P%20)%20are%20dead%20keys.

Sounds like those keys mutate the document when in reality they shouldn't. If you can confirm it doesn't happen with a standard JTextArea that would be great. I'm having trouble changing the keyboard layout on my computer to Frnch, and even if I do I'm not sure I could reproduce this locally, but I'll keep checking.

bobbylight avatar Jun 05 '21 15:06 bobbylight

Yes, I have no troubles with a standard JTextArea (even when I try to implement custom text coloring schemes). Any help would be appreciated ! Indeed, the "^" character is really often used in Latex when scientific documents are aimed.

mbrebion avatar Jul 05 '21 07:07 mbrebion

I've another reproducer.

Use case: long documents are slow to display if text kerning is enabled, so I want to add a document listener that would disable kerning if text length exceeds 10'000 chars.

See https://github.com/apache/jmeter/pull/694#issuecomment-1030783752

Unfortunately, it hits printStackTrace() in RSTA: https://github.com/bobbylight/RSyntaxTextArea/blob/d45f9e54e83a4880529b07b5128c7a0dc8db6ea7/RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/RTextAreaBase.java#L759

The stacktrace is as follows:

javax.swing.text.BadLocationException: Position not represented by view
        at org.fife.ui.rsyntaxtextarea.WrappedSyntaxView.modelToView(WrappedSyntaxView.java:770)
        at java.desktop/javax.swing.plaf.basic.BasicTextUI$RootView.modelToView(BasicTextUI.java:1605)
        at java.desktop/javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:1110)
        at java.desktop/javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:1087)
        at java.desktop/javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:1063)
        at java.desktop/javax.swing.text.JTextComponent.modelToView(JTextComponent.java:1392)
        at org.fife.ui.rtextarea.RTextAreaBase.possiblyUpdateCurrentLineHighlightLocation(RTextAreaBase.java:754)
        at org.fife.ui.rtextarea.RTextArea.fireCaretUpdate(RTextArea.java:611)
        at org.fife.ui.rsyntaxtextarea.RSyntaxTextArea.fireCaretUpdate(RSyntaxTextArea.java:874)
        at org.fife.ui.rtextarea.RTextAreaBase.forceCurrentLineHighlightRepaint(RTextAreaBase.java:310)
        at org.fife.ui.rsyntaxtextarea.RSyntaxTextArea.setFont(RSyntaxTextArea.java:2601)
        at org.apache.jorphan.gui.ui.KerningOptimizer.configureKerning(KerningOptimizer.java:50)
        at org.apache.jorphan.gui.ui.KerningOptimizer$DisableKerningForLargeTexts.changedUpdate(KerningOptimizer.java:90)
        at java.desktop/javax.swing.text.AbstractDocument.fireChangedUpdate(AbstractDocument.java:232)
        at org.fife.ui.rsyntaxtextarea.RSyntaxDocument.updateLastTokensBelow(RSyntaxDocument.java:618)
        at org.fife.ui.rsyntaxtextarea.RSyntaxDocument.fireRemoveUpdate(RSyntaxDocument.java:253)
        at java.desktop/javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:628)
        at java.desktop/javax.swing.text.AbstractDocument.remove(AbstractDocument.java:596)
        at java.desktop/javax.swing.text.AbstractDocument.replace(AbstractDocument.java:672)
        at java.desktop/javax.swing.text.JTextComponent.setText(JTextComponent.java:1729)
        at org.apache.jmeter.gui.util.JSyntaxTextArea.setInitialText(JSyntaxTextArea.java:296)
        at org.apache.jmeter.visualizers.RequestViewRaw.clearData(RequestViewRaw.java:82)
        at org.apache.jmeter.visualizers.RequestPanel.clearData(RequestPanel.java:105)
        at org.apache.jmeter.visualizers.SamplerResultTab.clearData(SamplerResultTab.java:215)
        at org.apache.jmeter.visualizers.SamplerResultTab.setupTabPane(SamplerResultTab.java:235)
        at org.apache.jmeter.visualizers.ViewResultsFullVisualizer.valueChanged(ViewResultsFullVisualizer.java:395)
        at org.apache.jmeter.visualizers.ViewResultsFullVisualizer.valueChanged(ViewResultsFullVisualizer.java:373)
        at java.desktop/javax.swing.JTree.fireValueChanged(JTree.java:2967)
        at java.desktop/javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3456)
        at java.desktop/javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:641)
        at java.desktop/javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1111)
        at java.desktop/javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:297)
        at java.desktop/javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:191)
        at java.desktop/javax.swing.JTree.setSelectionPath(JTree.java:1656)
        at java.desktop/javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2736)
        at java.desktop/javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:4016)
        at java.desktop/javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3955)

vlsi avatar Feb 06 '22 09:02 vlsi