GavriYashar / Matlab-Editor-Plugin

Extends features for the matlab editor, Bookmarks, FileStructure, Clipboard stack
MIT License
74 stars 12 forks source link

Live Templates causes ArrayOutOfBounds exception on Mac OS #151

Closed danielrohrbach closed 3 years ago

danielrohrbach commented 5 years ago

Describe the bug Live Templates causes ArrayOutOfBounds exception if a file with only one line is in Replacement folder. On mac there can be hidden files that will be listed by the File.listFiles function. If they contain only one file the liveTemplate function will crash

To Reproduce Steps to reproduce the behavior:

https://github.com/GavriYashar/Matlab-Editor-Plugin/blob/0f2baad52206b38dac17a7c6d469eaf2030f09af/src/at/mep/gui/mepr/MEPREntries.java#L60

private static String getComment(List<String> lines) {
        if (lines.size() < 1) return "";
        int index = lines.get(1).indexOf('%');
        if (index < 0) return "";
        String retVal = lines.get(1).substring(index + 1);
        return retVal.trim();
    }

The problem is the second line if (lines.size() < 1) return ""; which should read if (lines.size() < 2) return "";

If the file only contains one line this will cause the exception because the next line tries to access the second line int index = lines.get(1).indexOf('%');

Expected behavior liveScript to open

Desktop (please complete the following information):

error message

Error Message ```Matlab >> ent = at.mep.gui.mepr.MEPREntries.getAllEntries Java exception occurred: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at at.mep.gui.mepr.MEPREntries.getComment(MEPREntries.java:64) at at.mep.gui.mepr.MEPREntries.getAllEntries(MEPREntries.java:44) >> % hitting Short cut for kb.liveTemplateViewer Exception in thread "AWT-EventQueue-0": java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at at.mep.gui.mepr.MEPREntries.getComment(MEPREntries.java:64) at at.mep.gui.mepr.MEPREntries.getAllEntries(MEPREntries.java:44) at at.mep.gui.mepr.MEPRViewer.updateList(MEPRViewer.java:307) at at.mep.gui.mepr.MEPRViewer.addScrollPane(MEPRViewer.java:234) at at.mep.gui.mepr.MEPRViewer.setLayout(MEPRViewer.java:85) at at.mep.gui.mepr.MEPRViewer.(MEPRViewer.java:54) at at.mep.gui.mepr.MEPRViewer.getInstance(MEPRViewer.java:59) at at.mep.editor.EMEPAction$17.actionPerformed(EMEPAction.java:162) at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1668) at javax.swing.JComponent.processKeyBinding(JComponent.java:2882) at javax.swing.JComponent.processKeyBindings(JComponent.java:2929) at javax.swing.JComponent.processKeyEvent(JComponent.java:2845) at com.mathworks.widgets.SyntaxTextPaneBase.processKeyEvent(SyntaxTextPaneBase.java:1189) at java.awt.Component.processEvent(Component.java:6316) at java.awt.Container.processEvent(Container.java:2239) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2297) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:835) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1103) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:974) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:800) at java.awt.Component.dispatchEventImpl(Component.java:4760) at java.awt.Container.dispatchEventImpl(Container.java:2297) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84) at java.awt.EventQueue$4.run(EventQueue.java:733) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) 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) ```

Additional context This is probably an issue on a MAC OS system only