RSyntaxTextArea icon indicating copy to clipboard operation
RSyntaxTextArea copied to clipboard

NPE in PythonFoldParser

Open paul-griffith opened this issue 5 years ago • 5 comments

RSyntaxTextArea 3.1.1:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at org.fife.ui.rsyntaxtextarea.folding.PythonFoldParser.getFolds(PythonFoldParser.java:87)
	at org.fife.ui.rsyntaxtextarea.folding.DefaultFoldManager.reparse(DefaultFoldManager.java:471)
	at org.fife.ui.rsyntaxtextarea.folding.DefaultFoldManager$1.parse(DefaultFoldManager.java:503)
	at org.fife.ui.rsyntaxtextarea.ParserManager.actionPerformed(ParserManager.java:163)
	at java.desktop/javax.swing.Timer.fireActionPerformed(Timer.java:317)
	at java.desktop/javax.swing.Timer$DoPostEvent.run(Timer.java:249)
	at java.desktop/java.awt.event.InvocationEvent.dispatch$$$capture(InvocationEvent.java:313)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I can't provide detailed replication (yet; trying to isolate it) but it looks like IntelliJ wins this round 🤣

// IntelliJ can't tell, but it's not possible for currentFold to be
// null here

paul-griffith avatar May 12 '20 19:05 paul-griffith

Haha fair enough!

bobbylight avatar May 12 '20 23:05 bobbylight

I believe this bug is caused by this block:

	// A code block without lines should just be removed
	if (!foundBlock && currentFold != null && !currentFold.removeFromParent()) {
		folds.remove(folds.size()-1);
	}

which should probably be:

	// A code block without lines should just be removed
	if (!foundBlock && currentFold != null) {
		Fold parent = currentFold.getParent();
		if (!currentFold.removeFromParent()) {
			folds.remove(folds.size() - 1);
		}
		currentFold = parent;
	}

along with the defensive null check suggested or something similar. Unfortunately I can't reproduce this yet. I'd like to get a unit test in before pushing a change. Still working on this.

bobbylight avatar May 30 '20 14:05 bobbylight

I can reliably reproduce with bottle.py; haven't yet narrowed it down to a smaller SSCCE: https://bottlepy.org/docs/dev/tutorial.html#installation

paul-griffith avatar Jun 15 '22 19:06 paul-griffith

SSCCE:

def abc2():
	doSomething(
			a,
		)
	

pass

Seems to have to do with the mixed indentation on continuing lines. The trailing expression at a different indentation level is also necessary (to close the fold?). I'll probably check out a fix and file a PR soon.

paul-griffith avatar Jun 23 '22 18:06 paul-griffith

Verified the NPE in a unit test. Suggested fix above isn't sufficient, trying some different things here: https://github.com/paul-griffith/RSyntaxTextArea/tree/python-fold-npe

paul-griffith avatar Jun 23 '22 19:06 paul-griffith

Finally fixed by #508 (4a08790)!

bobbylight avatar Jul 28 '23 01:07 bobbylight