fix toggleComment with blank lines

This commit is contained in:
nightwing 2013-03-31 19:50:02 +04:00
commit ad9f919eed
3 changed files with 89 additions and 53 deletions

View file

@ -51,7 +51,7 @@ AceEmmetEditor.prototype = {
setupContext: function(editor) { setupContext: function(editor) {
this.ace = editor; this.ace = editor;
this.indentation = editor.session.getTabString(); this.indentation = editor.session.getTabString();
emmet.require('resources').setVariable('indentation', this.indentation); emmet.require("resources").setVariable("indentation", this.indentation);
this.$syntax = null; this.$syntax = null;
this.$syntax = this.getSyntax(); this.$syntax = this.getSyntax();
}, },
@ -164,7 +164,7 @@ AceEmmetEditor.prototype = {
end = start == null ? content.length : start; end = start == null ? content.length : start;
if (start == null) if (start == null)
start = 0; start = 0;
var utils = emmet.require('utils'); var utils = emmet.require("utils");
// indent new value // indent new value
if (!noIndent) { if (!noIndent) {
@ -172,7 +172,7 @@ AceEmmetEditor.prototype = {
} }
// find new caret position // find new caret position
var tabstopData = emmet.require('tabStops').extract(value, { var tabstopData = emmet.require("tabStops").extract(value, {
escape: function(ch) { escape: function(ch) {
return ch; return ch;
} }
@ -218,7 +218,7 @@ AceEmmetEditor.prototype = {
if (this.$syntax) if (this.$syntax)
return this.$syntax; return this.$syntax;
var syntax = this.ace.session.$modeId.split("/").pop(); var syntax = this.ace.session.$modeId.split("/").pop();
if (syntax == 'html' || syntax == "php") { if (syntax == "html" || syntax == "php") {
var cursor = this.ace.getCursorPosition(); var cursor = this.ace.getCursorPosition();
var state = this.ace.session.getState(cursor.row); var state = this.ace.session.getState(cursor.row);
if (typeof state != "string") if (typeof state != "string")
@ -240,18 +240,18 @@ AceEmmetEditor.prototype = {
*/ */
getProfileName: function() { getProfileName: function() {
switch(this.getSyntax()) { switch(this.getSyntax()) {
case 'css': return css; case "css": return "css";
case 'xml': case "xml":
case 'xsl': case "xsl":
return 'xml'; return "xml";
case 'html': case "html":
var profile = emmet.require('resources').getVariable('profile'); var profile = emmet.require("resources").getVariable("profile");
// no forced profile, guess from content html or xhtml? // no forced profile, guess from content html or xhtml?
if (!profile) if (!profile)
profile = this.ace.session.getLines(0,2).join("").search(/<!DOCTYPE[^>]+XHTML/i) != -1 ? 'xhtml': 'html'; profile = this.ace.session.getLines(0,2).join("").search(/<!DOCTYPE[^>]+XHTML/i) != -1 ? "xhtml": "html";
return profile; return profile;
} }
return 'xhtml'; return "xhtml";
}, },
/** /**
@ -279,7 +279,7 @@ AceEmmetEditor.prototype = {
* @since 0.65 * @since 0.65
*/ */
getFilePath: function() { getFilePath: function() {
return ''; return "";
} }
}; };
@ -291,7 +291,7 @@ var keymap = {
matching_pair: {"mac": "ctrl+alt+j", "win": "alt+j"}, matching_pair: {"mac": "ctrl+alt+j", "win": "alt+j"},
next_edit_point: "alt+right", next_edit_point: "alt+right",
prev_edit_point: "alt+left", prev_edit_point: "alt+left",
toggle_comment: {"mac": "command+shift+/", "win": "ctrl+shift+/"}, toggle_comment: {"mac": "command+/", "win": "ctrl+/"},
split_join_tag: {"mac": "shift+command+'", "win": "shift+ctrl+`"}, split_join_tag: {"mac": "shift+command+'", "win": "shift+ctrl+`"},
remove_tag: {"mac": "command+'", "win": "shift+ctrl+;"}, remove_tag: {"mac": "command+'", "win": "shift+ctrl+;"},
evaluate_math_expression: {"mac": "shift+command+y", "win": "shift+ctrl+y"}, evaluate_math_expression: {"mac": "shift+command+y", "win": "shift+ctrl+y"},
@ -318,12 +318,13 @@ function runEmmetCommand(editor) {
editorProxy.setupContext(editor); editorProxy.setupContext(editor);
if (editorProxy.getSyntax() == "php") if (editorProxy.getSyntax() == "php")
return false; return false;
var actions = emmet.require('actions') var actions = emmet.require("actions")
try { try {
var result = actions.run(this.name, editorProxy); var result = actions.run(this.name, editorProxy);
} catch(e) { } catch(e) {
editor._signal("changeStatus", typeof e == "string" ? e : e.message); editor._signal("changeStatus", typeof e == "string" ? e : e.message);
console.log(e);
} }
return result; return result;
} }

View file

@ -67,6 +67,33 @@ module.exports = {
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());
}, },
"test: toggle comment on all empty lines" : function() {
var session = new EditSession([" ", " ", " "]);
this.mode.toggleCommentLines("start", session, 0, 1);
assert.equal(["// ", "// ", " "].join("\n"), session.toString());
},
"test: toggle comment with empty lines" : function() {
var session = new EditSession([
" abc",
"",
" cde",
" fg"]);
var initial = session.toString();
this.mode.toggleCommentLines("start", session, 0, 3);
assert.equal([
" // abc",
"",
" // cde",
" // fg"].join("\n"),
session.toString()
);
this.mode.toggleCommentLines("start", session, 0, 3);
assert.equal(initial, session.toString());
},
"test: toggle comment lines twice should return the original text" : function() { "test: toggle comment lines twice should return the original text" : function() {
var session = new EditSession([" abc", "cde", "fg"]); var session = new EditSession([" abc", "cde", "fg"]);

View file

@ -3,7 +3,7 @@
* *
* Copyright (c) 2010, Ajax.org B.V. * Copyright (c) 2010, Ajax.org B.V.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
@ -14,7 +14,7 @@
* * Neither the name of Ajax.org B.V. nor the * * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products * names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -50,7 +50,7 @@ var Mode = function() {
+ unicode.packages.Nd + unicode.packages.Nd
+ unicode.packages.Pc + "\\$_]+", "g" + unicode.packages.Pc + "\\$_]+", "g"
); );
this.nonTokenRe = new RegExp("^(?:[^" this.nonTokenRe = new RegExp("^(?:[^"
+ unicode.packages.L + unicode.packages.L
+ unicode.packages.Mn + unicode.packages.Mc + unicode.packages.Mn + unicode.packages.Mc
@ -69,39 +69,47 @@ var Mode = function() {
return false return false
} else if (Array.isArray(this.lineCommentStart)) { } else if (Array.isArray(this.lineCommentStart)) {
regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|"); regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|");
lineCommentStart = this.lineCommentStart[0]; lineCommentStart = this.lineCommentStart[0] + " ";
} else { } else {
regexpStart = lang.escapeRegExp(this.lineCommentStart); regexpStart = lang.escapeRegExp(this.lineCommentStart);
lineCommentStart = this.lineCommentStart; lineCommentStart = this.lineCommentStart + " ";
} }
regexpStart = new RegExp("^\\s*(?:" + regexpStart + ") ?"); regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?");
var removeComment = true; function uncomment(line, i) {
var minSpace = Infinity; var m = line.match(regexpStart);
var indentations = []; m && doc.removeInLine(i, m[1].length, m[0].length);
for (var i = startRow; i <= endRow; i++) {
var line = doc.getLine(i);
var indent = line.search(/\S|$/);
indentations[i] = indent;
if (indent < minSpace)
minSpace = indent;
if (removeComment && !regexpStart.test(line))
removeComment = false;
} }
function comment(line, i) {
if (removeComment) { if (ignoreBlankLines || /\S/.test(line))
for (var i = startRow; i <= endRow; i++) {
var line = doc.getLine(i);
var m = line.match(regexpStart);
doc.removeInLine(i, indentations[i], m[0].length);
}
} else {
lineCommentStart += " ";
for (var i = startRow; i <= endRow; i++) {
doc.insertInLine({row: i, column: minSpace}, lineCommentStart); doc.insertInLine({row: i, column: minSpace}, lineCommentStart);
}
} }
function iter(fun) {
for (var i = startRow; i <= endRow; i++)
fun(doc.getLine(i), i);
}
var ignoreBlankLines = false;
var shouldRemove = true;
var minSpace = Infinity;
iter(function(line) {
var indent = line.search(/\S/);
if (indent !== -1) {
if (indent < minSpace)
minSpace = indent;
if (shouldRemove && !regexpStart.test(line))
shouldRemove = false;
}
});
if (minSpace == Infinity) {
minSpace = 0;
ignoreBlankLines = true;
shouldRemove = false;
}
iter(shouldRemove ? uncomment : comment);
}; };
this.getNextLineIndent = function(state, line, tab) { this.getNextLineIndent = function(state, line, tab) {
@ -118,7 +126,7 @@ var Mode = function() {
this.$getIndent = function(line) { this.$getIndent = function(line) {
return line.match(/^\s*/)[0]; return line.match(/^\s*/)[0];
}; };
this.createWorker = function(session) { this.createWorker = function(session) {
return null; return null;
}; };
@ -133,7 +141,7 @@ var Mode = function() {
this.$modes[this.$embeds[i]] = new mapping[this.$embeds[i]](); this.$modes[this.$embeds[i]] = new mapping[this.$embeds[i]]();
} }
} }
var delegations = ['toggleCommentLines', 'getNextLineIndent', 'checkOutdent', 'autoOutdent', 'transformAction']; var delegations = ['toggleCommentLines', 'getNextLineIndent', 'checkOutdent', 'autoOutdent', 'transformAction'];
for (var i = 0; i < delegations.length; i++) { for (var i = 0; i < delegations.length; i++) {
@ -145,14 +153,14 @@ var Mode = function() {
} }
} (this)); } (this));
} }
} };
this.$delegator = function(method, args, defaultHandler) { this.$delegator = function(method, args, defaultHandler) {
var state = args[0]; var state = args[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;
var split = state.split(this.$embeds[i]); var split = state.split(this.$embeds[i]);
if (!split[0] && split[1]) { if (!split[0] && split[1]) {
args[0] = split[1]; args[0] = split[1];
@ -163,7 +171,7 @@ var Mode = function() {
var ret = defaultHandler.apply(this, args); var ret = defaultHandler.apply(this, args);
return defaultHandler ? ret : undefined; return defaultHandler ? ret : undefined;
}; };
this.transformAction = function(state, action, editor, session, param) { this.transformAction = function(state, action, editor, session, param) {
if (this.$behaviour) { if (this.$behaviour) {
var behaviours = this.$behaviour.getBehaviours(); var behaviours = this.$behaviour.getBehaviours();
@ -176,8 +184,8 @@ var Mode = function() {
} }
} }
} }
} };
}).call(Mode.prototype); }).call(Mode.prototype);
exports.Mode = Mode; exports.Mode = Mode;