tdf#125355 Beanshell Editor: Corrected indentation when Enter is pressed

Change-Id: Ife96256da02c4b21e2649040c53b7d16f236e3a0
Reviewed-on: https://gerrit.libreoffice.org/73371
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
diff --git a/scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java b/scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java
index b2a4dd6..e519587 100644
--- a/scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java
+++ b/scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java
@@ -64,6 +64,7 @@ public class PlainSourceView extends JScrollPane implements
    private List<UnsavedChangesListener> unsavedListener = new ArrayList<UnsavedChangesListener>();

    private static final Pattern tabPattern = Pattern.compile("^ *(\\t)");
    private static final Pattern indentationPattern = Pattern.compile("^([^\\S\\r\\n]*)(([^\\{])*\\{\\s*)*");

    public PlainSourceView(ScriptSourceModel model) {
        this.model = model;
@@ -192,6 +193,31 @@ public class PlainSourceView extends JScrollPane implements
                        // could not find correct location of the tab
                    }
                }
                // if the enter key was pressed, adjust indentation of the current line accordingly
                if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
                    try {
                        int caretOffset = ta.getCaretPosition();
                        int lineOffset = ta.getLineOfOffset(caretOffset);
                        int startOffset = ta.getLineStartOffset(lineOffset);
                        int endOffset = ta.getLineEndOffset(lineOffset);

                        Matcher matcher = indentationPattern.matcher(ta.getText(startOffset, endOffset - startOffset));
                        // insert new line including indentation of the previous line
                        ta.insert("\n", caretOffset++);
                        if (matcher.find()) {
                            if (matcher.group(1).length() > 0) {
                                ta.insert(matcher.group(1), caretOffset++);
                            }
                            // if there is an open curly bracket in the current line, increase indentation level
                            if (matcher.group(3) != null) {
                                ta.insert("\t", caretOffset);
                            }
                        }
                        ke.consume();
                    } catch (BadLocationException e) {
                        // could not find correct location of the indentation
                    }
                }
            }

            @Override