Skip to content

Commit

Permalink
[comment & continuecomment addons] Make multi-selection aware
Browse files Browse the repository at this point in the history
Issue #778
  • Loading branch information
marijnh committed Jan 23, 2014
1 parent e012745 commit 8ca09be
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
17 changes: 15 additions & 2 deletions addon/comment/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@
}

CodeMirror.commands.toggleComment = function(cm) {
var from = cm.getCursor("start"), to = cm.getCursor("end");
cm.uncomment(from, to) || cm.lineComment(from, to);
var minLine = Infinity, ranges = cm.listSelections(), mode = null;
for (var i = ranges.length - 1; i >= 0; i--) {
var from = ranges[i].from(), to = ranges[i].to();
if (from.line >= minLine) continue;
if (to.line >= minLine) to = Pos(minLine, 0);
minLine = from.line;
if (mode == null) {
if (cm.uncomment(from, to)) mode = "un";
else { cm.lineComment(from, to); mode = "line"; }
} else if (mode == "un") {
cm.uncomment(from, to);
} else {
cm.lineComment(from, to);
}
}
};

CodeMirror.defineExtension("lineComment", function(from, to, options) {
Expand Down
68 changes: 38 additions & 30 deletions addon/comment/continuecomment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,50 @@
CodeMirror.extendMode(modes[i], {blockCommentContinue: " * "});

function continueComment(cm) {
var pos = cm.getCursor(), token = cm.getTokenAt(pos);
if (token.type != "comment" || cm.getOption("disableInput")) return CodeMirror.Pass;
var mode = CodeMirror.innerMode(cm.getMode(), token.state).mode;
if (cm.getOption("disableInput")) return CodeMirror.Pass;
var ranges = cm.listSelections(), mode, inserts = [];
for (var i = 0; i < ranges.length; i++) {
var pos = ranges[i].head, token = cm.getTokenAt(pos);
if (token.type != "comment") return CodeMirror.Pass;
var modeHere = CodeMirror.innerMode(cm.getMode(), token.state).mode;
if (!mode) mode = modeHere;
else if (mode != modeHere) return CodeMirror.Pass;

var insert;
if (mode.blockCommentStart && mode.blockCommentContinue) {
var end = token.string.indexOf(mode.blockCommentEnd);
var full = cm.getRange(CodeMirror.Pos(pos.line, 0), CodeMirror.Pos(pos.line, token.end)), found;
if (end != -1 && end == token.string.length - mode.blockCommentEnd.length) {
// Comment ended, don't continue it
} else if (token.string.indexOf(mode.blockCommentStart) == 0) {
insert = full.slice(0, token.start);
if (!/^\s*$/.test(insert)) {
insert = "";
for (var i = 0; i < token.start; ++i) insert += " ";
var insert = null;
if (mode.blockCommentStart && mode.blockCommentContinue) {
var end = token.string.indexOf(mode.blockCommentEnd);
var full = cm.getRange(CodeMirror.Pos(pos.line, 0), CodeMirror.Pos(pos.line, token.end)), found;
if (end != -1 && end == token.string.length - mode.blockCommentEnd.length) {
// Comment ended, don't continue it
} else if (token.string.indexOf(mode.blockCommentStart) == 0) {
insert = full.slice(0, token.start);
if (!/^\s*$/.test(insert)) {
insert = "";
for (var i = 0; i < token.start; ++i) insert += " ";
}
} else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 &&
found + mode.blockCommentContinue.length > token.start &&
/^\s*$/.test(full.slice(0, found))) {
insert = full.slice(0, found);
}
} else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 &&
found + mode.blockCommentContinue.length > token.start &&
/^\s*$/.test(full.slice(0, found))) {
insert = full.slice(0, found);
if (insert != null) insert += mode.blockCommentContinue;
}
if (insert != null) insert += mode.blockCommentContinue;
}
if (insert == null && mode.lineComment) {
var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment);
if (found > -1) {
insert = line.slice(0, found);
if (/\S/.test(insert)) insert = null;
else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0];
if (insert == null && mode.lineComment) {
var line = cm.getLine(pos.line), found = line.indexOf(mode.lineComment);
if (found > -1) {
insert = line.slice(0, found);
if (/\S/.test(insert)) insert = null;
else insert += mode.lineComment + line.slice(found + mode.lineComment.length).match(/^\s*/)[0];
}
}
if (insert == null) return CodeMirror.Pass;
inserts[i] = "\n" + insert;
}

if (insert != null)
cm.replaceSelection("\n" + insert, "end");
else
return CodeMirror.Pass;
cm.operation(function() {
for (var i = ranges.length - 1; i >= 0; i--)
cm.replaceRange(inserts[i], ranges[i].from(), ranges[i].to(), "+insert");
});
}

CodeMirror.defineOption("continueComments", null, function(cm, val, prev) {
Expand Down

0 comments on commit 8ca09be

Please sign in to comment.