Merge pull request #1392 from ajaxorg/toggleComment
try to not remove spaces from indentation when uncommenting
This commit is contained in:
commit
133d132c61
4 changed files with 74 additions and 10 deletions
|
|
@ -42,6 +42,7 @@ var JavaScriptMode = require("./mode/javascript").Mode;
|
|||
var UndoManager = require("./undomanager").UndoManager;
|
||||
var MockRenderer = require("./test/mockrenderer").MockRenderer;
|
||||
var assert = require("./test/assertions");
|
||||
var whitespace = require("./ext/whitespace");
|
||||
|
||||
module.exports = {
|
||||
"test: delete line from the middle" : function() {
|
||||
|
|
@ -184,7 +185,8 @@ module.exports = {
|
|||
"test: comment lines should perserve selection" : function() {
|
||||
var session = new EditSession([" abc", "cde"].join("\n"), new JavaScriptMode());
|
||||
var editor = new Editor(new MockRenderer(), session);
|
||||
|
||||
whitespace.detectIndentation(session);
|
||||
|
||||
editor.moveCursorTo(0, 2);
|
||||
editor.getSelection().selectDown();
|
||||
editor.toggleCommentLines();
|
||||
|
|
@ -199,6 +201,7 @@ module.exports = {
|
|||
"test: uncomment lines should perserve selection" : function() {
|
||||
var session = new EditSession(["// abc", "//cde"].join("\n"), new JavaScriptMode());
|
||||
var editor = new Editor(new MockRenderer(), session);
|
||||
session.setTabSize(2);
|
||||
|
||||
editor.moveCursorTo(0, 1);
|
||||
editor.getSelection().selectDown();
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ module.exports = {
|
|||
|
||||
"test: toggle comment lines should prepend '//' to each line" : function() {
|
||||
var session = new EditSession([" abc", "cde", "fg"]);
|
||||
session.setTabSize(1);
|
||||
|
||||
this.mode.toggleCommentLines("start", session, 0, 1);
|
||||
assert.equal(["// abc", "// cde", "fg"].join("\n"), session.toString());
|
||||
|
|
@ -63,6 +64,7 @@ module.exports = {
|
|||
|
||||
"test: toggle comment on commented lines should remove leading '//' chars" : function() {
|
||||
var session = new EditSession(["// abc", "//cde", "fg"]);
|
||||
session.setTabSize(1);
|
||||
|
||||
this.mode.toggleCommentLines("start", session, 0, 1);
|
||||
assert.equal([" abc", "cde", "fg"].join("\n"), session.toString());
|
||||
|
|
@ -70,6 +72,7 @@ module.exports = {
|
|||
|
||||
"test: toggle comment on all empty lines" : function() {
|
||||
var session = new EditSession([" ", " ", " "]);
|
||||
session.setTabSize(1);
|
||||
|
||||
this.mode.toggleCommentLines("start", session, 0, 1);
|
||||
assert.equal([" // ", " // ", " "].join("\n"), session.toString());
|
||||
|
|
@ -105,7 +108,7 @@ module.exports = {
|
|||
|
||||
"test: toggle comment on multiple lines with one commented line prepend '//' to each line" : function() {
|
||||
var session = new EditSession([" // abc", " //cde", " fg"]);
|
||||
|
||||
session.setTabSize(1);
|
||||
this.mode.toggleCommentLines("start", session, 0, 2);
|
||||
assert.equal([" // // abc", " // //cde", " // fg"].join("\n"), session.toString());
|
||||
},
|
||||
|
|
@ -117,6 +120,31 @@ module.exports = {
|
|||
assert.equal(["cde", " fg"].join("\n"), session.toString());
|
||||
},
|
||||
|
||||
"test: toggle comment lines should take tabsize into account" : function() {
|
||||
var session = new EditSession([" // abc", " // cde", "// fg"]);
|
||||
session.setTabSize(2);
|
||||
this.mode.toggleCommentLines("start", session, 0, 2);
|
||||
assert.equal([" abc", " cde", " fg"].join("\n"), session.toString());
|
||||
session.setTabSize(4);
|
||||
this.mode.toggleCommentLines("start", session, 0, 2);
|
||||
assert.equal(["// abc", "// cde", "// fg"].join("\n"), session.toString());
|
||||
this.mode.toggleCommentLines("start", session, 0, 2);
|
||||
assert.equal([" abc", " cde", " fg"].join("\n"), session.toString());
|
||||
|
||||
session.insert({row: 0, column: 0}, " ");
|
||||
this.mode.toggleCommentLines("start", session, 0, 2);
|
||||
assert.equal(["// abc", "// cde", "// fg"].join("\n"), session.toString());
|
||||
},
|
||||
//there doesn't seem to be any way to make this work
|
||||
"!test: togglecomment on line with one space" : function() {
|
||||
var session = new EditSession([" abc", " // cde", "// fg"]);
|
||||
var initialValue = session + "";
|
||||
session.setTabSize(4);
|
||||
this.mode.toggleCommentLines("start", session, 0, 0);
|
||||
this.mode.toggleCommentLines("start", session, 0, 0);
|
||||
assert.equal(initialValue, session.toString());
|
||||
},
|
||||
|
||||
"test: auto indent after opening brace" : function() {
|
||||
assert.equal(" ", this.mode.getNextLineIndent("start", "if () {", " "));
|
||||
},
|
||||
|
|
|
|||
|
|
@ -46,10 +46,10 @@ module.exports = {
|
|||
},
|
||||
|
||||
"test: toggle comment lines should prepend '//' to each line" : function() {
|
||||
var session = new EditSession([" abc", "cde", "fg"]);
|
||||
var session = new EditSession([" abc", "cde", "fg"]);
|
||||
|
||||
this.mode.toggleCommentLines("start", session, 0, 1);
|
||||
assert.equal(["// abc", "// cde", "fg"].join("\n"), session.toString());
|
||||
assert.equal(["// abc", "// cde", "fg"].join("\n"), session.toString());
|
||||
},
|
||||
|
||||
"test: auto indent after ->" : function() {
|
||||
|
|
|
|||
|
|
@ -73,6 +73,8 @@ var Mode = function() {
|
|||
var ignoreBlankLines = true;
|
||||
var shouldRemove = true;
|
||||
var minIndent = Infinity;
|
||||
var tabSize = session.getTabSize();
|
||||
var insertAtTabStop = false;
|
||||
|
||||
if (!this.lineCommentStart) {
|
||||
if (!this.blockComment)
|
||||
|
|
@ -111,24 +113,51 @@ var Mode = function() {
|
|||
} else {
|
||||
if (Array.isArray(this.lineCommentStart)) {
|
||||
var regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|");
|
||||
var lineCommentStart = this.lineCommentStart[0] + " ";
|
||||
var lineCommentStart = this.lineCommentStart[0];
|
||||
} else {
|
||||
var regexpStart = lang.escapeRegExp(this.lineCommentStart);
|
||||
var lineCommentStart = this.lineCommentStart + " ";
|
||||
var lineCommentStart = this.lineCommentStart;
|
||||
}
|
||||
regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?");
|
||||
|
||||
insertAtTabStop = session.getUseSoftTabs();
|
||||
|
||||
var uncomment = function(line, i) {
|
||||
var m = line.match(regexpStart);
|
||||
m && doc.removeInLine(i, m[1].length, m[0].length);
|
||||
if (!m) return;
|
||||
var start = m[1].length, end = m[0].length;
|
||||
if (!shouldInsertSpace(line, start, end) && m[0][end - 1] == " ")
|
||||
end--;
|
||||
doc.removeInLine(i, start, end);
|
||||
};
|
||||
var commentWithSpace = lineCommentStart + " ";
|
||||
var comment = function(line, i) {
|
||||
if (!ignoreBlankLines || /\S/.test(line))
|
||||
doc.insertInLine({row: i, column: minIndent}, lineCommentStart);
|
||||
if (!ignoreBlankLines || /\S/.test(line)) {
|
||||
if (shouldInsertSpace(line, minIndent, minIndent))
|
||||
doc.insertInLine({row: i, column: minIndent}, commentWithSpace);
|
||||
else
|
||||
doc.insertInLine({row: i, column: minIndent}, lineCommentStart);
|
||||
}
|
||||
};
|
||||
var testRemove = function(line, i) {
|
||||
return regexpStart.test(line);
|
||||
};
|
||||
|
||||
var shouldInsertSpace = function(line, before, after) {
|
||||
var spaces = 0;
|
||||
while (before-- && line.charAt(before) == " ")
|
||||
spaces++;
|
||||
if (spaces % tabSize != 0)
|
||||
return false;
|
||||
var spaces = 0;
|
||||
while (line.charAt(after++) == " ")
|
||||
spaces++;
|
||||
if (tabSize > 2)
|
||||
return spaces % tabSize != tabSize - 1;
|
||||
else
|
||||
return spaces % tabSize == 0;
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
function iter(fun) {
|
||||
|
|
@ -156,6 +185,9 @@ var Mode = function() {
|
|||
shouldRemove = false;
|
||||
}
|
||||
|
||||
if (insertAtTabStop && minIndent % tabSize != 0)
|
||||
minIndent = Math.floor(minIndent / tabSize) * tabSize;
|
||||
|
||||
iter(shouldRemove ? uncomment : comment);
|
||||
};
|
||||
|
||||
|
|
@ -264,7 +296,8 @@ var Mode = function() {
|
|||
|
||||
this.$delegator = function(method, args, defaultHandler) {
|
||||
var state = args[0];
|
||||
|
||||
if (typeof state != "string")
|
||||
state = state[0];
|
||||
for (var i = 0; i < this.$embeds.length; i++) {
|
||||
if (!this.$modes[this.$embeds[i]]) continue;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue