-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insert_possibly_matching_paren.bsh
44 lines (40 loc) · 1.4 KB
/
Insert_possibly_matching_paren.bsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
insert(textArea, sc, ec, init) {
if (textArea.getSelectionCount() == 0) {
sel = new Selection.Range(textArea.getCaretPosition(), textArea.getCaretPosition());
} else if (init && textArea.getSelectionCount() == 1 && textArea.getSelection()[0] instanceof Selection.Range) {
sel = textArea.getSelection()[0];
} else {
// TODO?
selections = textArea.getSelection();
for (int i = 0; i < selections.length; i++) {
textArea.setSelectedText(selections[i], init ? sc : ec);
}
return;
}
if (init) {
position = textArea.getCaretPosition();
prior = new Selection.Range(sel.getStart(), sel.getStart());
post = new Selection.Range(sel.getEnd() + 1, sel.getEnd() + 1);
textArea.setSelectedText(prior, sc);
textArea.setSelectedText(post, ec);
if (position >= prior.getStart()) {
position++;
}
if (position > post.getEnd()) {
position++;
}
textArea.moveCaretPosition(position);
textArea.setSelection(new Selection.Range(sel.getStart(), Math.max(sel.getStart(), sel.getEnd() - 1)));
} else {
target = new Selection.Range(sel.getEnd(), sel.getEnd() + 1);
if (target.getEnd() <= textArea.getBufferLength()) {
if (!textArea.getSelectedText(target).equals(ec)) {
textArea.setSelectedText(new Selection.Range(sel.getEnd(), sel.getEnd()), ec);
}
textArea.setCaretPosition(sel.getEnd() + 1);
} else {
textArea.setSelectedText(ec, true);
}
}
}
insert(textArea, "(", ")", false);