-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathPythonEditorKit.java
More file actions
388 lines (342 loc) · 14.5 KB
/
PythonEditorKit.java
File metadata and controls
388 lines (342 loc) · 14.5 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package nodebox.client;
import nodebox.client.syntax.PythonTokenMarker;
import nodebox.client.syntax.SyntaxColoredViewFactory;
import nodebox.client.syntax.SyntaxDocument;
import nodebox.client.syntax.TokenMarker;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.ActionEvent;
public class PythonEditorKit extends StyledEditorKit {
public static final String returnAction = "python-return";
public static final String tabAction = "python-tab";
public static final String indentAction = "python-indent";
public static final String dedentAction = "python-dedent";
public static final String FOUR_SPACE = " ";
public static final int SPACES = 4;
private static final Action[] defaultActions = {
new ReturnAction(returnAction),
new TabAction(tabAction),
new IndentAction(indentAction),
new DedentAction(dedentAction),
};
private SyntaxColoredViewFactory viewFactory;
private TokenMarker tokenMarker;
public PythonEditorKit() {
tokenMarker = new PythonTokenMarker();
viewFactory = new SyntaxColoredViewFactory();
}
public Document createDefaultDocument() {
SyntaxDocument doc = new SyntaxDocument();
doc.setTokenMarker(tokenMarker);
return doc;
}
public ViewFactory getViewFactory() {
return viewFactory;
}
// ---- General purpose line functions ---- //
public static String getLineForOffset(Document doc, int offs) {
int lineNo = doc.getDefaultRootElement().getElementIndex(offs);
Element line = doc.getDefaultRootElement().getElement(lineNo);
try {
String strLine = doc.getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
return strLine;
} catch (BadLocationException e) {
throw new AssertionError("The offset falls outside of the line I am requesting: " + e);
}
}
public static String getLineForLineNum(Document doc, int lineNum) {
Element line = doc.getDefaultRootElement().getElement(lineNum);
try {
String strLine = doc.getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
return strLine;
} catch (BadLocationException e) {
throw new AssertionError("The line number falls outside of the range: " + e);
}
}
public static int getLineNum(Document doc, int offs) {
return doc.getDefaultRootElement().getElementIndex(offs);
}
public static String getIndentForOffset(Document doc, int offs) {
String line = getLineForOffset(doc, offs);
return getIndent(line);
}
public static String getIndentForLine(Document doc, int lineNum) {
String line = getLineForLineNum(doc, lineNum);
return getIndent(line);
}
public static int getOffsetForLine(Document doc, int lineNum) {
Element line = doc.getDefaultRootElement().getElement(lineNum);
return line.getStartOffset();
}
/**
* Get indent for one line of text.
*/
public static String getIndent(String line) {
String indent = "";
int i = 0;
while (i < line.length()) {
char c = line.charAt(i);
if (c == ' ' || c == '\t') {
indent += c;
} else {
break;
}
i++;
}
return indent;
}
public static String indent(String line) {
return getIndent(line) + FOUR_SPACE + stripIndent(line);
}
public static void indent(Document doc, int lineNum) {
String line = getLineForLineNum(doc, lineNum);
int offs = getOffsetForLine(doc, lineNum) + getIndentForLine(doc, lineNum).length();
try {
doc.insertString(offs, FOUR_SPACE, null);
} catch (BadLocationException e) {
throw new AssertionError("A bug in the dendenting code. Line: \"" + line + "\" Offset: " + offs);
}
}
public static String dedent(String line) {
int[] metrics = getDedentMetrics(line);
String begin = line.substring(0, metrics[0]);
String rest = line.substring(metrics[0] + metrics[1]);
return begin + rest;
}
public static void dedent(Document doc, int lineNum) {
String line = getLineForLineNum(doc, lineNum);
int[] metrics = getDedentMetrics(line);
try {
doc.remove(getOffsetForLine(doc, lineNum) + metrics[0], metrics[1]);
} catch (BadLocationException e) {
throw new AssertionError("A bug in the dendenting code. Line: \"" + line + "\" Offset: " + metrics[0] + " length: " + metrics[1]);
}
}
/**
* Returns the offset and length of the dedent.
*
* @return an int array where the first value is the offset and the last the length.
*/
public static int[] getDedentMetrics(String line) {
String indent = getIndent(line);
if (indent.length() == 0) return new int[]{0, 0};
int offs = indent.length(); // Holds the position of where the cut will happen.
int len = 0;
// Indents can be either spaces or tabs.
// When we encounter spaces, try to strip four off (the default).
// If we encounter less than four before the end of the line or before a tab character,
// just delete as much as we can.
// If we encounter a tab character, just delete that.
// Something else is impossible, because indents can only consist of spaces and tabs.
while (offs > 0) {
offs--;
char c = indent.charAt(offs);
if (c == ' ') {
len++;
if (len >= SPACES) break;
} else if (c == '\t') {
// no spaces yet
if (len == 0) {
len = 1; // Just remove a single tab character...
break; // and stop the search.
} else {
// If the len is bigger than 0, we were trimming off spaces until
// we encountered this tab. We only want to delete those spaces,
// and not this tab character, so:
offs++; // Indicate that this character should not be removed...
break; // and stop the search.
}
} else {
throw new AssertionError("An indent should only consist of spaces and tabs. I got a " + c + ".");
}
}
return new int[]{offs, len};
}
public static String stripIndent(Document doc, int offs) {
String line = getLineForOffset(doc, offs);
return stripIndent(line);
}
/**
* Strips the indent from a line of text.
*/
public static String stripIndent(String line) {
int i = 0;
while (i < line.length()) {
char c = line.charAt(i);
if (c == ' ' || c == '\t') {
i++;
} else {
break;
}
}
return line.substring(i);
}
public Action[] getActions() {
return TextAction.augmentList(super.getActions(), defaultActions);
}
public static class BackspaceAction extends StyledTextAction {
public BackspaceAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
boolean beep = true;
if ((target != null) && (target.isEditable())) {
try {
Document doc = target.getDocument();
Caret caret = target.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
beep = false;
} else if (dot > 0) {
int delChars = 1;
// todo: tab-deletion
// Find fake tabs and delete them
// A fake tab is always in the beginning of the line (the indent),
// so we should be near or inside the indent.
// Then, if there are four space characters, that's the tab, and you can delete them all.
if (dot > 1) {
String dotChars = doc.getText(dot - 2, 2);
char c0 = dotChars.charAt(0);
char c1 = dotChars.charAt(1);
if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
c1 >= '\uDC00' && c1 <= '\uDFFF') {
delChars = 2;
}
}
doc.remove(dot - delChars, delChars);
beep = false;
}
} catch (BadLocationException bl) {
}
}
if (beep) {
UIManager.getLookAndFeel().provideErrorFeedback(target);
}
}
}
public static class ReturnAction extends StyledTextAction {
public ReturnAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
if ((target != null) && (target.isEditable())) {
int offs = target.getCaretPosition();
String indent = getIndentForOffset(target.getDocument(), offs);
try {
String prevChar = target.getDocument().getText(offs - 1, 1);
if (":".equals(prevChar)) {
target.getDocument().insertString(offs, "\n" + indent + FOUR_SPACE, null);
return;
}
} catch (BadLocationException e1) {
// Out-of-bounds, there is no prev char.
}
try {
target.getDocument().insertString(offs, "\n" + indent, null);
} catch (BadLocationException e1) {
// pass
}
}
}
}
public static class TabAction extends StyledTextAction {
public TabAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
if ((target != null) && (target.isEditable())) {
Document doc = target.getDocument();
Caret caret = target.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) { // Text is selected. Indent all text.
int begin = Math.min(dot, mark);
int end = Math.max(dot, mark);
// removing one character from the last line compensates for lines
// where only the enter is selected, and which should not be
// indented.
end--;
int firstLine = getLineNum(doc, begin);
int lastLine = getLineNum(doc, end);
// go through each line.
for (int i = firstLine; i <= lastLine; i++) {
indent(doc, i);
}
} else if (dot > 0) { // The cursor is just somewhere. Indent just this line.
try {
doc.insertString(dot, FOUR_SPACE, null);
} catch (BadLocationException e1) {
throw new AssertionError("Position of dot falls outside of the document. Dot: " + dot);
}
}
}
}
}
public static class IndentAction extends StyledTextAction {
public IndentAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
if ((target != null) && (target.isEditable())) {
Document doc = target.getDocument();
Caret caret = target.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) { // Text is selected. Indent all text.
int begin = Math.min(dot, mark);
int end = Math.max(dot, mark);
// removing one character from the last line compensates for lines
// where only the enter is selected, and which should not be
// indented.
end--;
int firstLine = getLineNum(doc, begin);
int lastLine = getLineNum(doc, end);
// go through each line.
for (int i = firstLine; i <= lastLine; i++) {
indent(doc, i);
}
} else if (dot > 0) { // The cursor is just somewhere. Indent just this line.
int line = getLineNum(doc, dot);
indent(doc, line);
}
}
}
}
public static class DedentAction extends StyledTextAction {
public DedentAction(String nm) {
super(nm); //To change body of overridden methods use File | Settings | File Templates.
}
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
if ((target != null) && (target.isEditable())) {
Document doc = target.getDocument();
Caret caret = target.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
// A selection was made
if (dot != mark) {
int begin = Math.min(dot, mark);
int end = Math.max(dot, mark);
// removing one character from the last line compensates for lines
// where only the enter is selected, and which should not be
// dedented.
end--;
int firstLine = getLineNum(doc, begin);
int lastLine = getLineNum(doc, end);
// go through each line.
for (int i = firstLine; i <= lastLine; i++) {
dedent(doc, i);
}
} else if (dot > 0) { // No selection
dedent(doc, getLineNum(doc, dot));
}
}
}
}
}