From ad9f919eede51e6da41e8a14d51a127822881251 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 31 Mar 2013 19:50:02 +0400 Subject: [PATCH 1/4] 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; From 5cda3a7be735285299cf0fd85e0f924bb8aec9ed Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 2 Apr 2013 01:55:55 +0400 Subject: [PATCH 2/4] add toggleBlockComment ctrl+shift+/ --- lib/ace/commands/default_commands.js | 5 ++ lib/ace/editor.js | 9 +++- lib/ace/mode/text.js | 68 +++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index 88621eaf..e2acb202 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -332,6 +332,11 @@ exports.commands = [{ bindKey: bindKey("Ctrl-/", "Command-/"), exec: function(editor) { editor.toggleCommentLines(); }, multiSelectAction: "forEachLine" +}, { + name: "toggleBlockComment", + bindKey: bindKey("Ctrl-Shift-/", "Command-Shift-/"), + exec: function(editor) { editor.toggleBlockComment(); }, + multiSelectAction: "forEach" }, { name: "modifyNumberUp", bindKey: bindKey("Ctrl-Shift-Up", "Alt-Shift-Up"), diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 0abcecbf..9f22604c 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -1263,7 +1263,6 @@ var Editor = function(renderer, session) { }; /** - * * Given the currently selected range, this function either comments all the lines, or uncomments all of them. **/ this.toggleCommentLines = function() { @@ -1272,6 +1271,13 @@ var Editor = function(renderer, session) { this.session.getMode().toggleCommentLines(state, this.session, rows.first, rows.last); }; + this.toggleBlockComment = function() { + var cursor = this.getCursorPosition(); + var state = this.session.getState(cursor.row); + var range = this.getSelectionRange(); + this.session.getMode().toggleBlockComment(state, this.session, range, cursor); + }; + /** * Works like [[EditSession.getTokenAt]], except it returns a number. * @returns {Number} @@ -1440,7 +1446,6 @@ var Editor = function(renderer, session) { }); }; - /** * Executes a specific function, which can be anything that manipulates selected lines, such as copying them, duplicating them, or shifting them. * @param {Function} mover A method to call on each selected row diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index c0df790a..6e79baf0 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -36,6 +36,8 @@ var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; var Behaviour = require("./behaviour").Behaviour; var unicode = require("../unicode"); var lang = require("../lib/lang"); +var TokenIterator = require("../token_iterator").TokenIterator; +var Range = require("../range").Range; var Mode = function() { this.$tokenizer = new Tokenizer(new TextHighlightRules().getRules()); @@ -62,6 +64,8 @@ var Mode = function() { return this.$tokenizer; }; + this.lineCommentStart = ""; + this.blockComment = ""; this.toggleCommentLines = function(state, session, startRow, endRow) { var doc = session.doc; var regexpStart, lineCommentStart; @@ -84,12 +88,12 @@ var Mode = function() { 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; @@ -112,6 +116,66 @@ var Mode = function() { iter(shouldRemove ? uncomment : comment); }; + this.toggleBlockComment = function(state, session, range, cursor) { + var comment = this.blockComment; + if (!comment) + return; + if (!comment.start && comment[0]) + comment = comment[0]; + + var iterator = new TokenIterator(session, cursor.row, cursor.column); + var token = iterator.getCurrentToken(); + + var sel = session.selection; + var initialRange = session.selection.toOrientedRange(); + var startRow, colDiff; + + if (token && /comment/.test(token.type)) { + var startRange, endRange; + while (token && /comment/.test(token.type)) { + var i = token.value.indexOf(comment.start); + if (i != -1) { + var row = iterator.getCurrentTokenRow(); + var column = iterator.getCurrentTokenColumn() + i; + startRange = new Range(row, column, row, column + comment.start.length); + break + } + token = iterator.stepBackward(); + }; + + var iterator = new TokenIterator(session, cursor.row, cursor.column); + var token = iterator.getCurrentToken(); + while (token && /comment/.test(token.type)) { + var i = token.value.indexOf(comment.end); + if (i != -1) { + var row = iterator.getCurrentTokenRow(); + var column = iterator.getCurrentTokenColumn() + i; + endRange = new Range(row, column, row, column + comment.end.length); + break; + } + token = iterator.stepForward(); + } + if (endRange) + session.remove(endRange); + if (startRange) { + session.remove(startRange); + startRow = startRange.start.row; + colDiff = -comment.start.length + } + } else { + colDiff = comment.start.length + startRow = range.start.row; + session.insert(range.end, comment.end); + session.insert(range.start, comment.start); + } + // todo: selection should have ended up in the right place automatically! + if (initialRange.start.row == startRow) + initialRange.start.column += colDiff; + if (initialRange.end.row == startRow) + initialRange.end.column += colDiff; + session.selection.fromOrientedRange(initialRange); + }; + this.getNextLineIndent = function(state, line, tab) { return this.$getIndent(line); }; From 782178ecbe41eafdac413bbcb9e23b3036e281fb Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 2 Apr 2013 02:33:31 +0400 Subject: [PATCH 3/4] add toggle line comment for css/html --- lib/ace/mode/text.js | 99 +++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 28 deletions(-) diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index 6e79baf0..ca14fdec 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -66,27 +66,69 @@ var Mode = function() { this.lineCommentStart = ""; this.blockComment = ""; + this.toggleCommentLines = function(state, session, startRow, endRow) { var doc = session.doc; - var regexpStart, lineCommentStart; - if (!this.lineCommentStart) { - return false - } else if (Array.isArray(this.lineCommentStart)) { - regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|"); - lineCommentStart = this.lineCommentStart[0] + " "; - } else { - regexpStart = lang.escapeRegExp(this.lineCommentStart); - lineCommentStart = this.lineCommentStart + " "; - } - regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?"); - function uncomment(line, i) { - var m = line.match(regexpStart); - m && doc.removeInLine(i, m[1].length, m[0].length); - } - function comment(line, i) { - if (ignoreBlankLines || /\S/.test(line)) - doc.insertInLine({row: i, column: minSpace}, lineCommentStart); + var ignoreBlankLines = true; + var shouldRemove = true; + var minIndent = Infinity; + + if (!this.lineCommentStart) { + if (!this.blockComment) + return false; + var lineCommentStart = this.blockComment.start; + var lineCommentEnd = this.blockComment.end; + var regexpStart = new RegExp("^(\\s*)(?:" + lang.escapeRegExp(lineCommentStart) + ")"); + var regexpEnd = new RegExp("(?:" + lang.escapeRegExp(lineCommentEnd) + ")\\s*$"); + + var comment = function(line, i) { + if (testRemove(line, i)) + return; + if (!ignoreBlankLines || /\S/.test(line)) { + doc.insertInLine({row: i, column: line.length}, lineCommentEnd); + doc.insertInLine({row: i, column: minIndent}, lineCommentStart); + } + }; + + var uncomment = function(line, i) { + var m; + if (m = line.match(regexpEnd)) + doc.removeInLine(i, line.length - m[0].length, line.length); + if (m = line.match(regexpStart)) + doc.removeInLine(i, m[1].length, m[0].length); + }; + + var testRemove = function(line, row) { + if (regexpStart.test(line)) + return true; + var tokens = session.getTokens(row); + for (var i = 0; i < tokens.length; i++) { + if (tokens[i].type === 'comment') + return true; + } + }; + } else { + if (Array.isArray(this.lineCommentStart)) { + var regexpStart = this.lineCommentStart.map(lang.escapeRegExp).join("|"); + var lineCommentStart = this.lineCommentStart[0] + " "; + } else { + var regexpStart = lang.escapeRegExp(this.lineCommentStart); + var lineCommentStart = this.lineCommentStart + " "; + } + regexpStart = new RegExp("^(\\s*)(?:" + regexpStart + ") ?"); + + var uncomment = function(line, i) { + var m = line.match(regexpStart); + m && doc.removeInLine(i, m[1].length, m[0].length); + }; + var comment = function(line, i) { + if (!ignoreBlankLines || /\S/.test(line)) + doc.insertInLine({row: i, column: minIndent}, lineCommentStart); + }; + var testRemove = function(line, i) { + return regexpStart.test(line); + }; } function iter(fun) { @@ -94,22 +136,23 @@ var Mode = function() { fun(doc.getLine(i), i); } - var ignoreBlankLines = false; - var shouldRemove = true; - var minSpace = Infinity; - iter(function(line) { + + var minEmptyLength = Infinity; + iter(function(line, i) { var indent = line.search(/\S/); if (indent !== -1) { - if (indent < minSpace) - minSpace = indent; - if (shouldRemove && !regexpStart.test(line)) + if (indent < minIndent) + minIndent = indent; + if (shouldRemove && !testRemove(line, i)) shouldRemove = false; + } else if (minEmptyLength > line.length) { + minEmptyLength = line.length; } }); - if (minSpace == Infinity) { - minSpace = 0; - ignoreBlankLines = true; + if (minIndent == Infinity) { + minIndent = minEmptyLength; + ignoreBlankLines = false; shouldRemove = false; } From 57fd7dabf26dc58e8d861d7c120289688d46d21d Mon Sep 17 00:00:00 2001 From: nightwing Date: Thu, 4 Apr 2013 10:07:55 +0400 Subject: [PATCH 4/4] update tests --- lib/ace/mode/coldfusion_test.js | 6 +++--- lib/ace/mode/css_test.js | 4 ++-- lib/ace/mode/html_test.js | 6 +++--- lib/ace/mode/javascript_test.js | 2 +- lib/ace/mode/xml_test.js | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/ace/mode/coldfusion_test.js b/lib/ace/mode/coldfusion_test.js index 97bfb83e..b55fb1c5 100644 --- a/lib/ace/mode/coldfusion_test.js +++ b/lib/ace/mode/coldfusion_test.js @@ -45,12 +45,12 @@ module.exports = { this.mode = new ColdfusionMode(); }, - "test: toggle comment lines should not do anything" : function() { - var session = new EditSession([" abc", "cde", "fg"]); + "test: toggle comment lines" : function() { + var session = new EditSession([" abc", " cde", "fg"]); var range = new Range(0, 3, 1, 1); var comment = this.mode.toggleCommentLines("start", session, 0, 1); - assert.equal([" abc", "cde", "fg"].join("\n"), session.toString()); + assert.equal([" ", " ", "fg"].join("\n"), session.toString()); }, "test: next line indent should be the same as the current line indent" : function() { diff --git a/lib/ace/mode/css_test.js b/lib/ace/mode/css_test.js index b08eeb08..2ccb177d 100644 --- a/lib/ace/mode/css_test.js +++ b/lib/ace/mode/css_test.js @@ -47,11 +47,11 @@ module.exports = { this.mode = new CssMode(); }, - "test: toggle comment lines should not do anything" : function() { + "test: toggle comment lines" : function() { var session = new EditSession([" abc", "cde", "fg"].join("\n")); var comment = 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()); }, diff --git a/lib/ace/mode/html_test.js b/lib/ace/mode/html_test.js index 61fe0199..1649d014 100644 --- a/lib/ace/mode/html_test.js +++ b/lib/ace/mode/html_test.js @@ -45,12 +45,12 @@ module.exports = { this.mode = new HtmlMode(); }, - "test: toggle comment lines should not do anything" : function() { - var session = new EditSession([" abc", "cde", "fg"]); + "test: toggle comment lines" : function() { + var session = new EditSession([" abc", "", "fg"]); var range = new Range(0, 3, 1, 1); var comment = this.mode.toggleCommentLines("start", session, 0, 1); - assert.equal([" abc", "cde", "fg"].join("\n"), session.toString()); + assert.equal([" ", "", "fg"].join("\n"), session.toString()); }, "test: next line indent should be the same as the current line indent" : function() { diff --git a/lib/ace/mode/javascript_test.js b/lib/ace/mode/javascript_test.js index e1982bdf..bd74e2ee 100644 --- a/lib/ace/mode/javascript_test.js +++ b/lib/ace/mode/javascript_test.js @@ -72,7 +72,7 @@ module.exports = { var session = new EditSession([" ", " ", " "]); this.mode.toggleCommentLines("start", session, 0, 1); - assert.equal(["// ", "// ", " "].join("\n"), session.toString()); + assert.equal([" // ", " // ", " "].join("\n"), session.toString()); }, "test: toggle comment with empty lines" : function() { diff --git a/lib/ace/mode/xml_test.js b/lib/ace/mode/xml_test.js index 5bb53d13..7344643b 100644 --- a/lib/ace/mode/xml_test.js +++ b/lib/ace/mode/xml_test.js @@ -55,10 +55,10 @@ module.exports = { }, "test: toggle comment lines should not do anything" : 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([" ", " ", "fg"].join("\n"), session.toString()); }, "test: next line indent should be the same as the current line indent" : function() {