diff options
author | Andreas Heinisch <andreas.heinisch@yahoo.de> | 2019-06-03 07:52:24 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-07-09 09:51:59 +0200 |
commit | 60f7236293bc3bd4a9b2962a5f1f9af6f2998808 (patch) | |
tree | 4956d65c6bc59027c4385823e64c0688b2087055 /scripting | |
parent | 5314d69b0b2fa0d04c4562559552ef6b4126d1b1 (diff) |
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>
Diffstat (limited to 'scripting')
-rw-r--r-- | scripting/java/com/sun/star/script/framework/provider/beanshell/PlainSourceView.java | 26 |
1 files changed, 26 insertions, 0 deletions
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 |