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.lineCommentStart = "";
|
||||||
this.blockComment = "";
|
this.blockComment = "";
|
||||||
|
|
||||||
this.toggleCommentLines = function(state, session, startRow, endRow) {
|
this.toggleCommentLines = function(state, session, startRow, endRow) {
|
||||||
var doc = session.doc;
|
var doc = session.doc;
|
||||||
var regexpStart, lineCommentStart;
|
|
||||||
|
var ignoreBlankLines = true;
|
||||||
|
var shouldRemove = true;
|
||||||
|
var minIndent = Infinity;
|
||||||
|
|
||||||
if (!this.lineCommentStart) {
|
if (!this.lineCommentStart) {
|
||||||
return false
|
if (!this.blockComment)
|
||||||
} else if (Array.isArray(this.lineCommentStart)) {
|
return false;
|
||||||
regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|");
|
var lineCommentStart = this.blockComment.start;
|
||||||
lineCommentStart = this.lineCommentStart[0] + " ";
|
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 {
|
} else {
|
||||||
regexpStart = lang.escapeRegExp(this.lineCommentStart);
|
if (Array.isArray(this.lineCommentStart)) {
|
||||||
lineCommentStart = 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 + ") ?");
|
regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?");
|
||||||
|
|
||||||
function uncomment(line, i) {
|
var uncomment = function(line, i) {
|
||||||
var m = line.match(regexpStart);
|
var m = line.match(regexpStart);
|
||||||
m && doc.removeInLine(i, m[1].length, m[0].length);
|
m && doc.removeInLine(i, m[1].length, m[0].length);
|
||||||
}
|
};
|
||||||
function comment(line, i) {
|
var comment = function(line, i) {
|
||||||
if (ignoreBlankLines || /\S/.test(line))
|
if (!ignoreBlankLines || /\S/.test(line))
|
||||||
doc.insertInLine({row: i, column: minSpace}, lineCommentStart);
|
doc.insertInLine({row: i, column: minIndent}, lineCommentStart);
|
||||||
|
};
|
||||||
|
var testRemove = function(line, i) {
|
||||||
|
return regexpStart.test(line);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function iter(fun) {
|
function iter(fun) {
|
||||||
|
|
@ -94,22 +136,23 @@ var Mode = function() {
|
||||||
fun(doc.getLine(i), i);
|
fun(doc.getLine(i), i);
|
||||||
}
|
}
|
||||||
|
|
||||||
var ignoreBlankLines = false;
|
|
||||||
var shouldRemove = true;
|
var minEmptyLength = Infinity;
|
||||||
var minSpace = Infinity;
|
iter(function(line, i) {
|
||||||
iter(function(line) {
|
|
||||||
var indent = line.search(/\S/);
|
var indent = line.search(/\S/);
|
||||||
if (indent !== -1) {
|
if (indent !== -1) {
|
||||||
if (indent < minSpace)
|
if (indent < minIndent)
|
||||||
minSpace = indent;
|
minIndent = indent;
|
||||||
if (shouldRemove && !regexpStart.test(line))
|
if (shouldRemove && !testRemove(line, i))
|
||||||
shouldRemove = false;
|
shouldRemove = false;
|
||||||
|
} else if (minEmptyLength > line.length) {
|
||||||
|
minEmptyLength = line.length;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (minSpace == Infinity) {
|
if (minIndent == Infinity) {
|
||||||
minSpace = 0;
|
minIndent = minEmptyLength;
|
||||||
ignoreBlankLines = true;
|
ignoreBlankLines = false;
|
||||||
shouldRemove = false;
|
shouldRemove = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue