antlr / intellij-plugin-v4

An IntelliJ plugin for ANTLR v4
https://plugins.jetbrains.com/plugin/7358-antlr-v4
BSD 3-Clause "New" or "Revised" License
468 stars 104 forks source link

Exception while trying to sort rule structure view #499

Closed parrt closed 2 years ago

parrt commented 2 years ago

ANTLR PLUGIN 1.17 which is pretty out of date with ANTLR. It has 4.9.1 not 4.9.3.

Intellij:

IntelliJ IDEA 2021.3.2 (Ultimate Edition)
Build #IU-213.6777.52, built on January 27, 2022
Runtime version: 11.0.13+7-b1751.25 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.0.1
  1. Download this grammar Malloy.g4
  2. open in fresh intellij w/o project.
  3. Click "structure" tab.

The A-Z sort button is dehighlighted.

I clicked on the button to its immediate left then A-Z again and got exception in plugin (not ANTLR tool):

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:693)
    at org.antlr.intellij.plugin.structview.ANTLRv4StructureViewModel$1.lambda$getComparator$0(ANTLRv4StructureViewModel.java:30)
    at com.intellij.ide.util.treeView.smartTree.CachingChildrenTreeNode$CompositeComparator.compare(CachingChildrenTreeNode.java:79)
    at com.intellij.ide.util.treeView.smartTree.CachingChildrenTreeNode$CompositeComparator.compare(CachingChildrenTreeNode.java:67)
    at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
    at java.base/java.util.TimSort.sort(TimSort.java:234)
    at java.base/java.util.Arrays.sort(Arrays.java:1515)
    at java.base/java.util.ArrayList.sort(ArrayList.java:1750)
    at com.intellij.ide.util.treeView.smartTree.CachingChildrenTreeNode.sortChildren(CachingChildrenTreeNode.java:91)
    at com.intellij.ide.util.treeView.smartTree.TreeElementWrapper.performTreeActions(TreeElementWrapper.java:78)
    at com.intellij.ide.util.treeView.smartTree.CachingChildrenTreeNode.rebuildSubtree(CachingChildrenTreeNode.java:208)
    at com.intellij.ide.util.treeView.smartTree.CachingChildrenTreeNode.ensureChildrenAreInitialized(CachingChildrenTreeNode.java:41)
    at com.intellij.ide.util.treeView.smartTree.CachingChildrenTreeNode.getChildren(CachingChildrenTreeNode.java:33)
    at com.intellij.ide.structureView.newStructureView.StructureViewComponent$MyNodeWrapper.getChildren(StructureViewComponent.java:796)
    at com.intellij.ide.util.treeView.smartTree.SmartTreeStructure.getChildElements(SmartTreeStructure.java:62)
    at com.intellij.ui.tree.StructureTreeModel.getValidChildren(StructureTreeModel.java:383)
    at com.intellij.ui.tree.StructureTreeModel.validateChildren(StructureTreeModel.java:299)
    at com.intellij.ui.tree.StructureTreeModel.getNode(StructureTreeModel.java:293)
    at com.intellij.ui.tree.StructureTreeModel.getChildren(StructureTreeModel.java:313)
    at com.intellij.ui.tree.AsyncTreeModel$CmdGetChildren.getNode(AsyncTreeModel.java:545)
    at com.intellij.ui.tree.AsyncTreeModel$Command.get(AsyncTreeModel.java:440)
    at com.intellij.ui.tree.AsyncTreeModel$Command.get(AsyncTreeModel.java:406)
    at com.intellij.util.concurrency.Invoker$Task.run(Invoker.java:316)
    at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1084)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runInReadActionWithWriteActionPriority$0(ProgressIndicatorUtils.java:75)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runActionAndCancelBeforeWrite(ProgressIndicatorUtils.java:158)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runWithWriteActionPriority$1(ProgressIndicatorUtils.java:115)
    at com.intellij.openapi.progress.ProgressManager.lambda$runProcess$0(ProgressManager.java:57)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:188)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$12(CoreProgressManager.java:624)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:698)
    at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:646)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:623)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:66)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:175)
    at com.intellij.openapi.progress.ProgressManager.runProcess(ProgressManager.java:57)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runWithWriteActionPriority(ProgressIndicatorUtils.java:112)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runInReadActionWithWriteActionPriority(ProgressIndicatorUtils.java:75)
    at com.intellij.util.concurrency.Invoker.invokeSafely(Invoker.java:205)
    at com.intellij.util.concurrency.Invoker.lambda$offerSafely$0(Invoker.java:183)
    at com.intellij.util.concurrency.Invoker$Background.lambda$offer$0(Invoker.java:486)
    at com.intellij.util.concurrency.BoundedTaskExecutor.doRun(BoundedTaskExecutor.java:246)
    at com.intellij.util.concurrency.BoundedTaskExecutor.access$200(BoundedTaskExecutor.java:32)
    at com.intellij.util.concurrency.BoundedTaskExecutor$1.execute(BoundedTaskExecutor.java:225)
    at com.intellij.util.ConcurrencyUtil.runUnderThreadName(ConcurrencyUtil.java:213)
    at com.intellij.util.concurrency.BoundedTaskExecutor$1.run(BoundedTaskExecutor.java:214)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:668)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:665)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:665)
    at java.base/java.lang.Thread.run(Thread.java:829)
parrt commented 2 years ago

Seems to be a general issue with later IDEs. It looks like getAlphaSortKey() is the problem as element (type ParserRuleRefNode) is not a PsiNamedElement. I'm trying ideaVersion=IC-2020.2.1:

@NotNull
@Override
public String getAlphaSortKey() {
    return element instanceof PsiNamedElement ? ((PsiNamedElement) element).getName() : "";
}

The exception is here:

private static final Sorter PARSER_LEXER_RULE_SORTER = new Sorter() {
public Comparator<?> getComparator() {
    return (o1, o2) -> {
        String s1 = SorterUtil.getStringPresentation(o1);
        String s2 = SorterUtil.getStringPresentation(o2);
        // flip case of char 0 so it puts parser rules first
        if ( Character.isLowerCase(s1.charAt(0)) ) { <---------- s1 is ""