michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") // CommonJS michael@0: mod(require("../../lib/codemirror")); michael@0: else if (typeof define == "function" && define.amd) // AMD michael@0: define(["../../lib/codemirror"], mod); michael@0: else // Plain browser env michael@0: mod(CodeMirror); michael@0: })(function(CodeMirror) { michael@0: "use strict"; michael@0: michael@0: var noOptions = {}; michael@0: var nonWS = /[^\s\u00a0]/; michael@0: var Pos = CodeMirror.Pos; michael@0: michael@0: function firstNonWS(str) { michael@0: var found = str.search(nonWS); michael@0: return found == -1 ? 0 : found; michael@0: } michael@0: michael@0: CodeMirror.commands.toggleComment = function(cm) { michael@0: var minLine = Infinity, ranges = cm.listSelections(), mode = null; michael@0: for (var i = ranges.length - 1; i >= 0; i--) { michael@0: var from = ranges[i].from(), to = ranges[i].to(); michael@0: if (from.line >= minLine) continue; michael@0: if (to.line >= minLine) to = Pos(minLine, 0); michael@0: minLine = from.line; michael@0: if (mode == null) { michael@0: if (cm.uncomment(from, to)) mode = "un"; michael@0: else { cm.lineComment(from, to); mode = "line"; } michael@0: } else if (mode == "un") { michael@0: cm.uncomment(from, to); michael@0: } else { michael@0: cm.lineComment(from, to); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: CodeMirror.defineExtension("lineComment", function(from, to, options) { michael@0: if (!options) options = noOptions; michael@0: var self = this, mode = self.getModeAt(from); michael@0: var commentString = options.lineComment || mode.lineComment; michael@0: if (!commentString) { michael@0: if (options.blockCommentStart || mode.blockCommentStart) { michael@0: options.fullLines = true; michael@0: self.blockComment(from, to, options); michael@0: } michael@0: return; michael@0: } michael@0: var firstLine = self.getLine(from.line); michael@0: if (firstLine == null) return; michael@0: var end = Math.min(to.ch != 0 || to.line == from.line ? to.line + 1 : to.line, self.lastLine() + 1); michael@0: var pad = options.padding == null ? " " : options.padding; michael@0: var blankLines = options.commentBlankLines || from.line == to.line; michael@0: michael@0: self.operation(function() { michael@0: if (options.indent) { michael@0: var baseString = firstLine.slice(0, firstNonWS(firstLine)); michael@0: for (var i = from.line; i < end; ++i) { michael@0: var line = self.getLine(i), cut = baseString.length; michael@0: if (!blankLines && !nonWS.test(line)) continue; michael@0: if (line.slice(0, cut) != baseString) cut = firstNonWS(line); michael@0: self.replaceRange(baseString + commentString + pad, Pos(i, 0), Pos(i, cut)); michael@0: } michael@0: } else { michael@0: for (var i = from.line; i < end; ++i) { michael@0: if (blankLines || nonWS.test(self.getLine(i))) michael@0: self.replaceRange(commentString + pad, Pos(i, 0)); michael@0: } michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: CodeMirror.defineExtension("blockComment", function(from, to, options) { michael@0: if (!options) options = noOptions; michael@0: var self = this, mode = self.getModeAt(from); michael@0: var startString = options.blockCommentStart || mode.blockCommentStart; michael@0: var endString = options.blockCommentEnd || mode.blockCommentEnd; michael@0: if (!startString || !endString) { michael@0: if ((options.lineComment || mode.lineComment) && options.fullLines != false) michael@0: self.lineComment(from, to, options); michael@0: return; michael@0: } michael@0: michael@0: var end = Math.min(to.line, self.lastLine()); michael@0: if (end != from.line && to.ch == 0 && nonWS.test(self.getLine(end))) --end; michael@0: michael@0: var pad = options.padding == null ? " " : options.padding; michael@0: if (from.line > end) return; michael@0: michael@0: self.operation(function() { michael@0: if (options.fullLines != false) { michael@0: var lastLineHasText = nonWS.test(self.getLine(end)); michael@0: self.replaceRange(pad + endString, Pos(end)); michael@0: self.replaceRange(startString + pad, Pos(from.line, 0)); michael@0: var lead = options.blockCommentLead || mode.blockCommentLead; michael@0: if (lead != null) for (var i = from.line + 1; i <= end; ++i) michael@0: if (i != end || lastLineHasText) michael@0: self.replaceRange(lead + pad, Pos(i, 0)); michael@0: } else { michael@0: self.replaceRange(endString, to); michael@0: self.replaceRange(startString, from); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: CodeMirror.defineExtension("uncomment", function(from, to, options) { michael@0: if (!options) options = noOptions; michael@0: var self = this, mode = self.getModeAt(from); michael@0: var end = Math.min(to.line, self.lastLine()), start = Math.min(from.line, end); michael@0: michael@0: // Try finding line comments michael@0: var lineString = options.lineComment || mode.lineComment, lines = []; michael@0: var pad = options.padding == null ? " " : options.padding, didSomething; michael@0: lineComment: { michael@0: if (!lineString) break lineComment; michael@0: for (var i = start; i <= end; ++i) { michael@0: var line = self.getLine(i); michael@0: var found = line.indexOf(lineString); michael@0: if (found > -1 && !/comment/.test(self.getTokenTypeAt(Pos(i, found + 1)))) found = -1; michael@0: if (found == -1 && (i != end || i == start) && nonWS.test(line)) break lineComment; michael@0: if (found > -1 && nonWS.test(line.slice(0, found))) break lineComment; michael@0: lines.push(line); michael@0: } michael@0: self.operation(function() { michael@0: for (var i = start; i <= end; ++i) { michael@0: var line = lines[i - start]; michael@0: var pos = line.indexOf(lineString), endPos = pos + lineString.length; michael@0: if (pos < 0) continue; michael@0: if (line.slice(endPos, endPos + pad.length) == pad) endPos += pad.length; michael@0: didSomething = true; michael@0: self.replaceRange("", Pos(i, pos), Pos(i, endPos)); michael@0: } michael@0: }); michael@0: if (didSomething) return true; michael@0: } michael@0: michael@0: // Try block comments michael@0: var startString = options.blockCommentStart || mode.blockCommentStart; michael@0: var endString = options.blockCommentEnd || mode.blockCommentEnd; michael@0: if (!startString || !endString) return false; michael@0: var lead = options.blockCommentLead || mode.blockCommentLead; michael@0: var startLine = self.getLine(start), endLine = end == start ? startLine : self.getLine(end); michael@0: var open = startLine.indexOf(startString), close = endLine.lastIndexOf(endString); michael@0: if (close == -1 && start != end) { michael@0: endLine = self.getLine(--end); michael@0: close = endLine.lastIndexOf(endString); michael@0: } michael@0: if (open == -1 || close == -1 || michael@0: !/comment/.test(self.getTokenTypeAt(Pos(start, open + 1))) || michael@0: !/comment/.test(self.getTokenTypeAt(Pos(end, close + 1)))) michael@0: return false; michael@0: michael@0: self.operation(function() { michael@0: self.replaceRange("", Pos(end, close - (pad && endLine.slice(close - pad.length, close) == pad ? pad.length : 0)), michael@0: Pos(end, close + endString.length)); michael@0: var openEnd = open + startString.length; michael@0: if (pad && startLine.slice(openEnd, openEnd + pad.length) == pad) openEnd += pad.length; michael@0: self.replaceRange("", Pos(start, open), Pos(start, openEnd)); michael@0: if (lead) for (var i = start + 1; i <= end; ++i) { michael@0: var line = self.getLine(i), found = line.indexOf(lead); michael@0: if (found == -1 || nonWS.test(line.slice(0, found))) continue; michael@0: var foundEnd = found + lead.length; michael@0: if (pad && line.slice(foundEnd, foundEnd + pad.length) == pad) foundEnd += pad.length; michael@0: self.replaceRange("", Pos(i, found), Pos(i, foundEnd)); michael@0: } michael@0: }); michael@0: return true; michael@0: }); michael@0: });