From ad9f919eede51e6da41e8a14d51a127822881251 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 31 Mar 2013 19:50:02 +0400 Subject: [PATCH] fix toggleComment with blank lines --- lib/ace/ext/emmet.js | 31 ++++++------ lib/ace/mode/javascript_test.js | 27 +++++++++++ lib/ace/mode/text.js | 84 ++++++++++++++++++--------------- 3 files changed, 89 insertions(+), 53 deletions(-) diff --git a/lib/ace/ext/emmet.js b/lib/ace/ext/emmet.js index e13f80fe..785dbccd 100644 --- a/lib/ace/ext/emmet.js +++ b/lib/ace/ext/emmet.js @@ -51,7 +51,7 @@ AceEmmetEditor.prototype = { setupContext: function(editor) { this.ace = editor; this.indentation = editor.session.getTabString(); - emmet.require('resources').setVariable('indentation', this.indentation); + emmet.require("resources").setVariable("indentation", this.indentation); this.$syntax = null; this.$syntax = this.getSyntax(); }, @@ -164,7 +164,7 @@ AceEmmetEditor.prototype = { end = start == null ? content.length : start; if (start == null) start = 0; - var utils = emmet.require('utils'); + var utils = emmet.require("utils"); // indent new value if (!noIndent) { @@ -172,7 +172,7 @@ AceEmmetEditor.prototype = { } // find new caret position - var tabstopData = emmet.require('tabStops').extract(value, { + var tabstopData = emmet.require("tabStops").extract(value, { escape: function(ch) { return ch; } @@ -218,7 +218,7 @@ AceEmmetEditor.prototype = { if (this.$syntax) return this.$syntax; var syntax = this.ace.session.$modeId.split("/").pop(); - if (syntax == 'html' || syntax == "php") { + if (syntax == "html" || syntax == "php") { var cursor = this.ace.getCursorPosition(); var state = this.ace.session.getState(cursor.row); if (typeof state != "string") @@ -240,18 +240,18 @@ AceEmmetEditor.prototype = { */ getProfileName: function() { switch(this.getSyntax()) { - case 'css': return css; - case 'xml': - case 'xsl': - return 'xml'; - case 'html': - var profile = emmet.require('resources').getVariable('profile'); + case "css": return "css"; + case "xml": + case "xsl": + return "xml"; + case "html": + var profile = emmet.require("resources").getVariable("profile"); // no forced profile, guess from content html or xhtml? if (!profile) - profile = this.ace.session.getLines(0,2).join("").search(/]+XHTML/i) != -1 ? 'xhtml': 'html'; + profile = this.ace.session.getLines(0,2).join("").search(/]+XHTML/i) != -1 ? "xhtml": "html"; return profile; } - return 'xhtml'; + return "xhtml"; }, /** @@ -279,7 +279,7 @@ AceEmmetEditor.prototype = { * @since 0.65 */ getFilePath: function() { - return ''; + return ""; } }; @@ -291,7 +291,7 @@ var keymap = { matching_pair: {"mac": "ctrl+alt+j", "win": "alt+j"}, next_edit_point: "alt+right", 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+`"}, remove_tag: {"mac": "command+'", "win": "shift+ctrl+;"}, evaluate_math_expression: {"mac": "shift+command+y", "win": "shift+ctrl+y"}, @@ -318,12 +318,13 @@ function runEmmetCommand(editor) { editorProxy.setupContext(editor); if (editorProxy.getSyntax() == "php") return false; - var actions = emmet.require('actions') + var actions = emmet.require("actions") try { var result = actions.run(this.name, editorProxy); } catch(e) { editor._signal("changeStatus", typeof e == "string" ? e : e.message); + console.log(e); } return result; } diff --git a/lib/ace/mode/javascript_test.js b/lib/ace/mode/javascript_test.js index 7c716a72..e1982bdf 100644 --- a/lib/ace/mode/javascript_test.js +++ b/lib/ace/mode/javascript_test.js @@ -67,6 +67,33 @@ module.exports = { this.mode.toggleCommentLines("start", session, 0, 1); 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() { var session = new EditSession([" abc", "cde", "fg"]); diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index 02cb466e..c0df790a 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -3,7 +3,7 @@ * * Copyright (c) 2010, Ajax.org B.V. * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright @@ -14,7 +14,7 @@ * * Neither the name of Ajax.org B.V. nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. - * + * * 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 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -50,7 +50,7 @@ var Mode = function() { + unicode.packages.Nd + unicode.packages.Pc + "\\$_]+", "g" ); - + this.nonTokenRe = new RegExp("^(?:[^" + unicode.packages.L + unicode.packages.Mn + unicode.packages.Mc @@ -69,39 +69,47 @@ var Mode = function() { return false } else if (Array.isArray(this.lineCommentStart)) { regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|"); - lineCommentStart = this.lineCommentStart[0]; + lineCommentStart = this.lineCommentStart[0] + " "; } else { regexpStart = lang.escapeRegExp(this.lineCommentStart); - lineCommentStart = this.lineCommentStart; + lineCommentStart = this.lineCommentStart + " "; } - regexpStart = new RegExp("^\\s*(?:" + regexpStart + ") ?"); + regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?"); - var removeComment = true; - var minSpace = Infinity; - var indentations = []; - - 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 uncomment(line, i) { + var m = line.match(regexpStart); + m && doc.removeInLine(i, m[1].length, m[0].length); } - - if (removeComment) { - 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++) { + function comment(line, i) { + if (ignoreBlankLines || /\S/.test(line)) 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) { @@ -118,7 +126,7 @@ var Mode = function() { this.$getIndent = function(line) { return line.match(/^\s*/)[0]; }; - + this.createWorker = function(session) { return null; }; @@ -133,7 +141,7 @@ var Mode = function() { this.$modes[this.$embeds[i]] = new mapping[this.$embeds[i]](); } } - + var delegations = ['toggleCommentLines', 'getNextLineIndent', 'checkOutdent', 'autoOutdent', 'transformAction']; for (var i = 0; i < delegations.length; i++) { @@ -145,14 +153,14 @@ var Mode = function() { } } (this)); } - } - + }; + this.$delegator = function(method, args, defaultHandler) { var state = args[0]; - + for (var i = 0; i < this.$embeds.length; i++) { if (!this.$modes[this.$embeds[i]]) continue; - + var split = state.split(this.$embeds[i]); if (!split[0] && split[1]) { args[0] = split[1]; @@ -163,7 +171,7 @@ var Mode = function() { var ret = defaultHandler.apply(this, args); return defaultHandler ? ret : undefined; }; - + this.transformAction = function(state, action, editor, session, param) { if (this.$behaviour) { var behaviours = this.$behaviour.getBehaviours(); @@ -176,8 +184,8 @@ var Mode = function() { } } } - } - + }; + }).call(Mode.prototype); exports.Mode = Mode;