add toggle line comment for css/html
This commit is contained in:
parent
5cda3a7be7
commit
782178ecbe
1 changed files with 71 additions and 28 deletions
|
|
@ -66,27 +66,69 @@ var Mode = function() {
|
|||
|
||||
this.lineCommentStart = "";
|
||||
this.blockComment = "";
|
||||
|
||||
this.toggleCommentLines = function(state, session, startRow, endRow) {
|
||||
var doc = session.doc;
|
||||
var regexpStart, lineCommentStart;
|
||||
if (!this.lineCommentStart) {
|
||||
return false
|
||||
} else if (Array.isArray(this.lineCommentStart)) {
|
||||
regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|");
|
||||
lineCommentStart = this.lineCommentStart[0] + " ";
|
||||
} else {
|
||||
regexpStart = lang.escapeRegExp(this.lineCommentStart);
|
||||
lineCommentStart = this.lineCommentStart + " ";
|
||||
}
|
||||
regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?");
|
||||
|
||||
function uncomment(line, i) {
|
||||
var m = line.match(regexpStart);
|
||||
m && doc.removeInLine(i, m[1].length, m[0].length);
|
||||
}
|
||||
function comment(line, i) {
|
||||
if (ignoreBlankLines || /\S/.test(line))
|
||||
doc.insertInLine({row: i, column: minSpace}, lineCommentStart);
|
||||
var ignoreBlankLines = true;
|
||||
var shouldRemove = true;
|
||||
var minIndent = Infinity;
|
||||
|
||||
if (!this.lineCommentStart) {
|
||||
if (!this.blockComment)
|
||||
return false;
|
||||
var lineCommentStart = this.blockComment.start;
|
||||
var lineCommentEnd = this.blockComment.end;
|
||||
var regexpStart = new RegExp("^(\\s*)(?:" + lang.escapeRegExp(lineCommentStart) + ")");
|
||||
var regexpEnd = new RegExp("(?:" + lang.escapeRegExp(lineCommentEnd) + ")\\s*$");
|
||||
|
||||
var comment = function(line, i) {
|
||||
if (testRemove(line, i))
|
||||
return;
|
||||
if (!ignoreBlankLines || /\S/.test(line)) {
|
||||
doc.insertInLine({row: i, column: line.length}, lineCommentEnd);
|
||||
doc.insertInLine({row: i, column: minIndent}, lineCommentStart);
|
||||
}
|
||||
};
|
||||
|
||||
var uncomment = function(line, i) {
|
||||
var m;
|
||||
if (m = line.match(regexpEnd))
|
||||
doc.removeInLine(i, line.length - m[0].length, line.length);
|
||||
if (m = line.match(regexpStart))
|
||||
doc.removeInLine(i, m[1].length, m[0].length);
|
||||
};
|
||||
|
||||
var testRemove = function(line, row) {
|
||||
if (regexpStart.test(line))
|
||||
return true;
|
||||
var tokens = session.getTokens(row);
|
||||
for (var i = 0; i < tokens.length; i++) {
|
||||
if (tokens[i].type === 'comment')
|
||||
return true;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
if (Array.isArray(this.lineCommentStart)) {
|
||||
var regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|");
|
||||
var lineCommentStart = this.lineCommentStart[0] + " ";
|
||||
} else {
|
||||
var regexpStart = lang.escapeRegExp(this.lineCommentStart);
|
||||
var lineCommentStart = this.lineCommentStart + " ";
|
||||
}
|
||||
regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?");
|
||||
|
||||
var uncomment = function(line, i) {
|
||||
var m = line.match(regexpStart);
|
||||
m && doc.removeInLine(i, m[1].length, m[0].length);
|
||||
};
|
||||
var comment = function(line, i) {
|
||||
if (!ignoreBlankLines || /\S/.test(line))
|
||||
doc.insertInLine({row: i, column: minIndent}, lineCommentStart);
|
||||
};
|
||||
var testRemove = function(line, i) {
|
||||
return regexpStart.test(line);
|
||||
};
|
||||
}
|
||||
|
||||
function iter(fun) {
|
||||
|
|
@ -94,22 +136,23 @@ var Mode = function() {
|
|||
fun(doc.getLine(i), i);
|
||||
}
|
||||
|
||||
var ignoreBlankLines = false;
|
||||
var shouldRemove = true;
|
||||
var minSpace = Infinity;
|
||||
iter(function(line) {
|
||||
|
||||
var minEmptyLength = Infinity;
|
||||
iter(function(line, i) {
|
||||
var indent = line.search(/\S/);
|
||||
if (indent !== -1) {
|
||||
if (indent < minSpace)
|
||||
minSpace = indent;
|
||||
if (shouldRemove && !regexpStart.test(line))
|
||||
if (indent < minIndent)
|
||||
minIndent = indent;
|
||||
if (shouldRemove && !testRemove(line, i))
|
||||
shouldRemove = false;
|
||||
} else if (minEmptyLength > line.length) {
|
||||
minEmptyLength = line.length;
|
||||
}
|
||||
});
|
||||
|
||||
if (minSpace == Infinity) {
|
||||
minSpace = 0;
|
||||
ignoreBlankLines = true;
|
||||
if (minIndent == Infinity) {
|
||||
minIndent = minEmptyLength;
|
||||
ignoreBlankLines = false;
|
||||
shouldRemove = false;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue