try to not remove spaces from indentation when uncommenting

This commit is contained in:
nightwing 2013-04-24 14:38:43 +04:00
commit d7ea9d6a52
2 changed files with 62 additions and 7 deletions

View file

@ -56,6 +56,7 @@ module.exports = {
"test: toggle comment lines should prepend '//' to each line" : function() { "test: toggle comment lines should prepend '//' to each line" : function() {
var session = new EditSession([" abc", "cde", "fg"]); var session = new EditSession([" abc", "cde", "fg"]);
session.setTabSize(1);
this.mode.toggleCommentLines("start", session, 0, 1); 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());
@ -63,6 +64,7 @@ module.exports = {
"test: toggle comment on commented lines should remove leading '//' chars" : function() { "test: toggle comment on commented lines should remove leading '//' chars" : function() {
var session = new EditSession(["// abc", "//cde", "fg"]); var session = new EditSession(["// abc", "//cde", "fg"]);
session.setTabSize(1);
this.mode.toggleCommentLines("start", session, 0, 1); 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());
@ -70,6 +72,7 @@ module.exports = {
"test: toggle comment on all empty lines" : function() { "test: toggle comment on all empty lines" : function() {
var session = new EditSession([" ", " ", " "]); var session = new EditSession([" ", " ", " "]);
session.setTabSize(1);
this.mode.toggleCommentLines("start", session, 0, 1); this.mode.toggleCommentLines("start", session, 0, 1);
assert.equal([" // ", " // ", " "].join("\n"), session.toString()); 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() { "test: toggle comment on multiple lines with one commented line prepend '//' to each line" : function() {
var session = new EditSession([" // abc", " //cde", " fg"]); var session = new EditSession([" // abc", " //cde", " fg"]);
session.setTabSize(1);
this.mode.toggleCommentLines("start", session, 0, 2); this.mode.toggleCommentLines("start", session, 0, 2);
assert.equal([" // // abc", " // //cde", " // fg"].join("\n"), session.toString()); assert.equal([" // // abc", " // //cde", " // fg"].join("\n"), session.toString());
}, },
@ -117,6 +120,27 @@ module.exports = {
assert.equal(["cde", " fg"].join("\n"), session.toString()); 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());
},
//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() { "test: auto indent after opening brace" : function() {
assert.equal(" ", this.mode.getNextLineIndent("start", "if () {", " ")); assert.equal(" ", this.mode.getNextLineIndent("start", "if () {", " "));
}, },

View file

@ -73,6 +73,8 @@ var Mode = function() {
var ignoreBlankLines = true; var ignoreBlankLines = true;
var shouldRemove = true; var shouldRemove = true;
var minIndent = Infinity; var minIndent = Infinity;
var tabSize = session.getTabSize();
var insertAtTabStop = false;
if (!this.lineCommentStart) { if (!this.lineCommentStart) {
if (!this.blockComment) if (!this.blockComment)
@ -111,24 +113,49 @@ var Mode = function() {
} else { } else {
if (Array.isArray(this.lineCommentStart)) { if (Array.isArray(this.lineCommentStart)) {
var regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|"); var regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|");
var lineCommentStart = this.lineCommentStart[0] + " "; var lineCommentStart = this.lineCommentStart[0];
} else { } else {
var regexpStart = lang.escapeRegExp(this.lineCommentStart); var regexpStart = lang.escapeRegExp(this.lineCommentStart);
var lineCommentStart = this.lineCommentStart + " "; var lineCommentStart = this.lineCommentStart;
} }
regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?"); regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?");
insertAtTabStop = session.getUseSoftTabs();
var uncomment = function(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); 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) { var comment = function(line, i) {
if (!ignoreBlankLines || /\S/.test(line)) if (!ignoreBlankLines || /\S/.test(line)) {
doc.insertInLine({row: i, column: minIndent}, lineCommentStart); 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) { var testRemove = function(line, i) {
return regexpStart.test(line); return regexpStart.test(line);
}; };
var shouldInsertSpace = function(line, pos1, pos2) {
var spaces = 0;
while (pos1-- && line.charAt(pos1) == " ")
spaces++;
if (spaces % tabSize != 0)
return false;
var spaces = 0;
while (line.charAt(pos2++) == " ")
spaces++;
if (spaces % tabSize != 0)
return false;
return true;
};
} }
function iter(fun) { function iter(fun) {
@ -156,6 +183,9 @@ var Mode = function() {
shouldRemove = false; shouldRemove = false;
} }
if (insertAtTabStop && minIndent % tabSize != 0)
minIndent = Math.floor(minIndent / tabSize);
iter(shouldRemove ? uncomment : comment); iter(shouldRemove ? uncomment : comment);
}; };
@ -264,7 +294,8 @@ var Mode = function() {
this.$delegator = function(method, args, defaultHandler) { this.$delegator = function(method, args, defaultHandler) {
var state = args[0]; var state = args[0];
if (typeof state != "string")
state = state[0];
for (var i = 0; i < this.$embeds.length; i++) { for (var i = 0; i < this.$embeds.length; i++) {
if (!this.$modes[this.$embeds[i]]) continue; if (!this.$modes[this.$embeds[i]]) continue;