tdf#97635 Added unindentation using shift-tab (mimicked eclipse behavior)

Change-Id: Iaf64451c92f3465751032dde9c283141e548b3ca
Reviewed-on: https://gerrit.libreoffice.org/72228
Tested-by: Jenkins
Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
This commit is contained in:
Andreas Heinisch
2019-05-13 13:08:29 +02:00
committed by Samuel Mehrbrodt
parent 84d8b0b7c4
commit e875a317c4

View File

@@ -45,6 +45,9 @@ import javax.swing.undo.UndoManager;
import java.util.List;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PlainSourceView extends JScrollPane implements
ScriptSourceView, DocumentListener {
@@ -171,6 +174,25 @@ public class PlainSourceView extends JScrollPane implements
ta.addKeyListener(new KeyAdapter(){
@Override
public void keyReleased(KeyEvent ke){
// if shift + tab was pressed, remove the first tab before any code begins
if (ke.isShiftDown() && ke.getKeyCode() == KeyEvent.VK_TAB) {
try {
int caretOffset = ta.getCaretPosition();
int lineOffset = ta.getLineOfOffset(caretOffset);
int startOffset = ta.getLineStartOffset(lineOffset);
int endOffset = ta.getLineEndOffset(lineOffset);
Pattern pattern = Pattern.compile("^ *(\\t)");
Matcher matcher = pattern.matcher(ta.getText(startOffset, endOffset - startOffset));
if (matcher.find()) {
ta.replaceRange(null, startOffset + matcher.start(1), startOffset + matcher.end(1));
}
} catch (BadLocationException e) {
// could not find correct location of the tab
}
}
if(ke.getKeyCode() == KeyEvent.VK_SPACE || ke.getKeyCode() == KeyEvent.VK_ENTER){
compoundEdit.end();
undoManager.addEdit(compoundEdit);