2
* Copyright 2008 the original author or authors.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package groovy.ui.text
19
import java.awt.event.ActionEvent
20
import javax.swing.AbstractAction
21
import javax.swing.text.AttributeSet
22
import javax.swing.text.SimpleAttributeSet
24
class AutoIndentAction extends AbstractAction {
25
AttributeSet simpleAttributeSet = new SimpleAttributeSet()
27
public void actionPerformed(ActionEvent evt) {
28
def inputArea = evt.source
29
def rootElement = inputArea.document.defaultRootElement
30
def cursorPos = inputArea.getCaretPosition()
31
int rowNum = rootElement.getElementIndex(cursorPos)
32
def rowElement = rootElement.getElement(rowNum)
33
int startOffset = rowElement.getStartOffset()
34
int endOffset = rowElement.getEndOffset()
35
String rowContent = inputArea.document.getText(startOffset, endOffset - startOffset);
36
String contentBeforeCursor = inputArea.document.getText(startOffset, cursorPos - startOffset);
37
String whitespaceStr = ''
38
def matcher = (rowContent =~ /(?m)^(\s*).*\n$/)
39
matcher.each { all, ws ->
43
if (contentBeforeCursor ==~ /(\s)*/) {
44
whitespaceStr = contentBeforeCursor
47
inputArea.document.insertString(cursorPos, '\n' + whitespaceStr, simpleAttributeSet)
b'\\ No newline at end of file'