This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggleComment.js
220 lines (196 loc) · 6.58 KB
/
toggleComment.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Toggles HTML and CSS comments depending on current caret context. Unlike
* the same action in most editors, this action toggles comment on currently
* matched item—HTML tag or CSS selector—when nothing is selected.
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var prefs = require('../assets/preferences');
var range = require('../assets/range');
var utils = require('../utils/common');
var actionUtils = require('../utils/action');
var editorUtils = require('../utils/editor');
var htmlMatcher = require('../assets/htmlMatcher');
var cssEditTree = require('../editTree/css');
/**
* Toggle HTML comment on current selection or tag
* @param {IEmmetEditor} editor
* @return {Boolean} Returns <code>true</code> if comment was toggled
*/
function toggleHTMLComment(editor) {
/** @type Range */
var r = range(editor.getSelectionRange());
var info = editorUtils.outputInfo(editor);
if (!r.length()) {
// no selection, find matching tag
var tag = htmlMatcher.tag(info.content, editor.getCaretPos());
if (tag) { // found pair
r = tag.outerRange;
}
}
return genericCommentToggle(editor, '<!--', '-->', r);
}
/**
* Simple CSS commenting
* @param {IEmmetEditor} editor
* @return {Boolean} Returns <code>true</code> if comment was toggled
*/
function toggleCSSComment(editor) {
/** @type Range */
var rng = range(editor.getSelectionRange());
var info = editorUtils.outputInfo(editor);
if (!rng.length()) {
// no selection, try to get current rule
/** @type CSSRule */
var rule = cssEditTree.parseFromPosition(info.content, editor.getCaretPos());
if (rule) {
var property = cssItemFromPosition(rule, editor.getCaretPos());
rng = property
? property.range(true)
: range(rule.nameRange(true).start, rule.source);
}
}
if (!rng.length()) {
// still no selection, get current line
rng = range(editor.getCurrentLineRange());
utils.narrowToNonSpace(info.content, rng);
}
return genericCommentToggle(editor, '/*', '*/', rng);
}
/**
* Returns CSS property from <code>rule</code> that matches passed position
* @param {EditContainer} rule
* @param {Number} absPos
* @returns {EditElement}
*/
function cssItemFromPosition(rule, absPos) {
// do not use default EditContainer.itemFromPosition() here, because
// we need to make a few assumptions to make CSS commenting more reliable
var relPos = absPos - (rule.options.offset || 0);
var reSafeChar = /^[\s\n\r]/;
return utils.find(rule.list(), function(item) {
if (item.range().end === relPos) {
// at the end of property, but outside of it
// if there’s a space character at current position,
// use current property
return reSafeChar.test(rule.source.charAt(relPos));
}
return item.range().inside(relPos);
});
}
/**
* Search for nearest comment in <code>str</code>, starting from index <code>from</code>
* @param {String} text Where to search
* @param {Number} from Search start index
* @param {String} start_token Comment start string
* @param {String} end_token Comment end string
* @return {Range} Returns null if comment wasn't found
*/
function searchComment(text, from, startToken, endToken) {
var commentStart = -1;
var commentEnd = -1;
var hasMatch = function(str, start) {
return text.substr(start, str.length) == str;
};
// search for comment start
while (from--) {
if (hasMatch(startToken, from)) {
commentStart = from;
break;
}
}
if (commentStart != -1) {
// search for comment end
from = commentStart;
var contentLen = text.length;
while (contentLen >= from++) {
if (hasMatch(endToken, from)) {
commentEnd = from + endToken.length;
break;
}
}
}
return (commentStart != -1 && commentEnd != -1)
? range(commentStart, commentEnd - commentStart)
: null;
}
/**
* Generic comment toggling routine
* @param {IEmmetEditor} editor
* @param {String} commentStart Comment start token
* @param {String} commentEnd Comment end token
* @param {Range} range Selection range
* @return {Boolean}
*/
function genericCommentToggle(editor, commentStart, commentEnd, range) {
var content = editorUtils.outputInfo(editor).content;
var caretPos = editor.getCaretPos();
var newContent = null;
/**
* Remove comment markers from string
* @param {Sting} str
* @return {String}
*/
function removeComment(str) {
return str
.replace(new RegExp('^' + utils.escapeForRegexp(commentStart) + '\\s*'), function(str){
caretPos -= str.length;
return '';
}).replace(new RegExp('\\s*' + utils.escapeForRegexp(commentEnd) + '$'), '');
}
// first, we need to make sure that this substring is not inside
// comment
var commentRange = searchComment(content, caretPos, commentStart, commentEnd);
if (commentRange && commentRange.overlap(range)) {
// we're inside comment, remove it
range = commentRange;
newContent = removeComment(range.substring(content));
} else {
// should add comment
// make sure that there's no comment inside selection
newContent = commentStart + ' ' +
range.substring(content)
.replace(new RegExp(utils.escapeForRegexp(commentStart) + '\\s*|\\s*' + utils.escapeForRegexp(commentEnd), 'g'), '') +
' ' + commentEnd;
// adjust caret position
caretPos += commentStart.length + 1;
}
// replace editor content
if (newContent !== null) {
newContent = utils.escapeText(newContent);
editor.setCaretPos(range.start);
editor.replaceContent(editorUtils.unindent(editor, newContent), range.start, range.end);
editor.setCaretPos(caretPos);
return true;
}
return false;
}
return {
/**
* Toggle comment on current editor's selection or HTML tag/CSS rule
* @param {IEmmetEditor} editor
*/
toggleCommentAction: function(editor) {
var info = editorUtils.outputInfo(editor);
if (actionUtils.isSupportedCSS(info.syntax)) {
// in case our editor is good enough and can recognize syntax from
// current token, we have to make sure that cursor is not inside
// 'style' attribute of html element
var caretPos = editor.getCaretPos();
var tag = htmlMatcher.tag(info.content, caretPos);
if (tag && tag.open.range.inside(caretPos)) {
info.syntax = 'html';
}
}
var cssSyntaxes = prefs.getArray('css.syntaxes');
if (~cssSyntaxes.indexOf(info.syntax)) {
return toggleCSSComment(editor);
}
return toggleHTMLComment(editor);
}
};
});