/
auto_brace_insert.js
202 lines (178 loc) · 6.6 KB
/
auto_brace_insert.js
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
/*
* auto_brace_insert.js
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* The Initial Developer of the Original Code is
* Ajax.org B.V.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
define("mode/auto_brace_insert", function(require, exports, module)
{
var Range = require("ace/range").Range;
var TextMode = require("ace/mode/text").Mode;
(function()
{
this.$complements = {
"(": ")",
"[": "]",
'"': '"',
"{": "}"
};
this.$reOpen = /^[(["{]$/;
this.$reClose = /^[)\]"}]$/;
// reStop is the set of characters before which we allow ourselves to
// automatically insert a closing paren. If any other character
// immediately follows the cursor we will NOT do the insert.
this.$reStop = /^[;,\s)\]}]$/;
this.wrapInsert = function(session, __insert, position, text)
{
if (!this.insertMatching)
return __insert.call(session, position, text);
var cursor = session.selection.getCursor();
var typing = session.selection.isEmpty() &&
position.row == cursor.row &&
position.column == cursor.column;
if (typing) {
var postRng = Range.fromPoints(position, {
row: position.row,
column: position.column + 1});
var postChar = session.doc.getTextRange(postRng);
if (this.$reClose.test(postChar) && postChar == text) {
session.selection.moveCursorTo(postRng.end.row,
postRng.end.column,
false);
return;
}
}
var prevChar = null;
if (typing)
{
var rangeBegin = this.$moveLeft(session.doc, position);
prevChar = session.doc.getTextRange(Range.fromPoints(rangeBegin,
position));
}
var endPos = __insert.call(session, position, text);
// Is this an open paren?
if (typing && this.$reOpen.test(text)) {
// Is the next char not a character or number?
var nextCharRng = Range.fromPoints(endPos, {
row: endPos.row,
column: endPos.column + 1
});
var nextChar = session.doc.getTextRange(nextCharRng);
if (this.$reStop.test(nextChar) || nextChar.length == 0) {
if (this.allowAutoInsert(session, endPos, this.$complements[text])) {
session.doc.insert(endPos, this.$complements[text]);
session.selection.moveCursorTo(endPos.row, endPos.column, false);
}
}
}
else if (typing && text === "\n") {
var rangeEnd = this.$moveRight(session.doc, endPos);
if (prevChar == "{" && "}" == session.doc.getTextRange(Range.fromPoints(endPos, rangeEnd)))
{
var indent;
if (this.getIndentForOpenBrace)
indent = this.getIndentForOpenBrace(this.$moveLeft(session.doc, position));
else
indent = this.$getIndent(session.doc.getLine(endPos.row - 1));
session.doc.insert(endPos, "\n" + indent);
session.selection.moveCursorTo(endPos.row, endPos.column, false);
}
}
return endPos;
};
this.allowAutoInsert = function(session, pos, text)
{
return true;
};
// To enable this, call "this.allowAutoInsert = this.smartAllowAutoInsert"
// in the mode subclass
this.smartAllowAutoInsert = function(session, pos, text)
{
if (text !== "'" && text !== '"')
return true;
// Only allow auto-insertion of a quote char if the actual character
// that was typed, was the start of a new string token
if (pos.column == 0)
return true;
var token = this.$rCodeModel.getTokenForPos(pos, false, true);
return token &&
token.type === 'string' &&
token.column === pos.column-1;
};
this.wrapRemoveLeft = function(editor, __removeLeft)
{
if (!this.insertMatching) {
__removeLeft.call(editor);
return;
}
if (editor.$readOnly)
return;
var secondaryDeletion = null;
if (editor.selection.isEmpty()) {
editor.selection.selectLeft();
var text = editor.session.getDocument().getTextRange(editor.selection.getRange());
if (this.$reOpen.test(text))
{
var nextCharRng = Range.fromPoints(editor.selection.getRange().end, {
row: editor.selection.getRange().end.row,
column: editor.selection.getRange().end.column + 1
});
var nextChar = editor.session.getDocument().getTextRange(nextCharRng);
if (nextChar == this.$complements[text])
{
secondaryDeletion = editor.getSelectionRange();
}
}
}
editor.session.remove(editor.getSelectionRange());
if (secondaryDeletion)
editor.session.remove(secondaryDeletion);
editor.clearSelection();
};
this.$moveLeft = function(doc, pos)
{
if (pos.row == 0 && pos.column == 0)
return pos;
var row = pos.row;
var col = pos.column;
if (col)
col--;
else
{
row--;
col = doc.getLine(row).length;
}
return {row: row, column: col};
};
this.$moveRight = function(doc, pos)
{
var row = pos.row;
var col = pos.column;
if (doc.getLine(row).length != col)
col++;
else
{
row++;
col = 0;
}
if (row >= doc.getLength())
return pos;
else
return {row: row, column: col};
};
}).call(TextMode.prototype);
exports.setInsertMatching = function(insertMatching) {
TextMode.prototype.insertMatching = insertMatching;
};
});