[Libreoffice-commits] core.git: scripting/java

Andreas Heinisch (via logerrit) logerrit at kemper.freedesktop.org
Tue Jul 9 07:53:05 UTC 2019


 scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java |   26 ++++++++++
 1 file changed, 26 insertions(+)

New commits:
commit 60f7236293bc3bd4a9b2962a5f1f9af6f2998808
Author:     Andreas Heinisch <andreas.heinisch at yahoo.de>
AuthorDate: Mon Jun 3 07:52:24 2019 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Tue Jul 9 09:51:59 2019 +0200

    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 at 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 b2a4dd61c243..e519587ff944 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


More information about the Libreoffice-commits mailing list