From 782178ecbe41eafdac413bbcb9e23b3036e281fb Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 2 Apr 2013 02:33:31 +0400 Subject: [PATCH] add toggle line comment for css/html --- lib/ace/mode/text.js | 99 +++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 28 deletions(-) diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index 6e79baf0..ca14fdec 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -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; }