From 60f7236293bc3bd4a9b2962a5f1f9af6f2998808 Mon Sep 17 00:00:00 2001 From: Andreas Heinisch Date: Mon, 3 Jun 2019 07:52:24 +0200 Subject: 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 --- .../provider/beanshell/PlainSourceView.java | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'scripting') 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 unsavedListener = new ArrayList(); 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 -- cgit