diff --git a/lib/ace/commands/command_manager.js b/lib/ace/commands/command_manager.js index 72a9942d..7b017ed2 100644 --- a/lib/ace/commands/command_manager.js +++ b/lib/ace/commands/command_manager.js @@ -2,13 +2,12 @@ define(function(require, exports, module) { "use strict"; var oop = require("../lib/oop"); -var HashHandler = require("../keyboard/hash_handler").HashHandler; +var MultiHashHandler = require("../keyboard/hash_handler").MultiHashHandler; var EventEmitter = require("../lib/event_emitter").EventEmitter; /** * @class CommandManager * - * **/ /** @@ -19,20 +18,27 @@ var EventEmitter = require("../lib/event_emitter").EventEmitter; **/ var CommandManager = function(platform, commands) { - HashHandler.call(this, commands, platform); + MultiHashHandler.call(this, commands, platform); this.byName = this.commands; this.setDefaultHandler("exec", function(e) { return e.command.exec(e.editor, e.args || {}); }); }; -oop.inherits(CommandManager, HashHandler); +oop.inherits(CommandManager, MultiHashHandler); (function() { oop.implement(this, EventEmitter); this.exec = function(command, editor, args) { + if (Array.isArray(command)) { + for (var i = command.length; i--; ) { + if (this.exec(command[i], editor, args)) return true; + } + return false; + } + if (typeof command === 'string') command = this.commands[command]; @@ -43,10 +49,10 @@ oop.inherits(CommandManager, HashHandler); return false; var e = {editor: editor, command: command, args: args}; - var retvalue = this._emit("exec", e); + e.returnValue = this._emit("exec", e); this._signal("afterExec", e); - return retvalue === false ? false : true; + return e.returnValue === false ? false : true; }; this.toggleRecording = function(editor) { diff --git a/lib/ace/commands/command_manager_test.js b/lib/ace/commands/command_manager_test.js index 76d973bb..902600be 100644 --- a/lib/ace/commands/command_manager_test.js +++ b/lib/ace/commands/command_manager_test.js @@ -188,7 +188,7 @@ module.exports = { assert.equal(command, "cm2"); var command = this.cm.findKeyCommand(0, "return"); - assert.equal(command, "cm3"); + assert.equal(command + "", ["cm4", "cm3"] + ""); } }; diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 1bc17cf2..20a04987 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -115,30 +115,8 @@ var Editor = function(renderer, session) { function last(a) {return a[a.length - 1]} this.selections = []; - this.commands.on("exec", function(e) { - this.startOperation(e); - - var command = e.command; - if (command.aceCommandGroup == "fileJump") { - var prev = this.prevOp; - if (!prev || prev.command.aceCommandGroup != "fileJump") { - this.lastFileJumpPos = last(this.selections); - } - } else { - this.lastFileJumpPos = null; - } - }.bind(this), true); - - this.commands.on("afterExec", function(e) { - var command = e.command; - - if (command.aceCommandGroup == "fileJump") { - if (this.lastFileJumpPos && !this.curOp.selectionChanged) { - this.selection.fromJSON(this.lastFileJumpPos); - } - } - this.endOperation(e); - }.bind(this), true); + this.commands.on("exec", this.startOperation.bind(this), true); + this.commands.on("afterExec", this.endOperation.bind(this), true); this.$opResetTimer = lang.delayedCall(this.endOperation.bind(this)); @@ -173,18 +151,16 @@ var Editor = function(renderer, session) { scrollTop: this.renderer.scrollTop }; - var command = this.curOp.command; - if (command && command.scrollIntoView) - this.$blockScrolling++; - - this.selections.push(this.selection.toJSON()); + // this.selections.push(this.selection.toJSON()); }; - this.endOperation = function() { + this.endOperation = function(e) { if (this.curOp) { + if (e && e.returnValue === false) + return this.curOp = null; + var command = this.curOp.command; if (command && command.scrollIntoView) { - this.$blockScrolling--; switch (command.scrollIntoView) { case "center": this.renderer.scrollCursorIntoView(null, 0.5); @@ -255,19 +231,19 @@ var Editor = function(renderer, session) { * @param {String} keyboardHandler The new key handler * **/ - this.setKeyboardHandler = function(keyboardHandler) { - if (!keyboardHandler) { - this.keyBinding.setKeyboardHandler(null); - } else if (typeof keyboardHandler === "string") { + this.setKeyboardHandler = function(keyboardHandler, cb) { + if (keyboardHandler && typeof keyboardHandler === "string") { this.$keybindingId = keyboardHandler; var _self = this; config.loadModule(["keybinding", keyboardHandler], function(module) { if (_self.$keybindingId == keyboardHandler) _self.keyBinding.setKeyboardHandler(module && module.handler); + cb && cb(); }); } else { this.$keybindingId = null; this.keyBinding.setKeyboardHandler(keyboardHandler); + cb && cb(); } }; @@ -931,9 +907,8 @@ var Editor = function(renderer, session) { this.insert(e.text, true); }; - this.execCommand = function(command, args) { - this.commands.exec(command, this, args); + return this.commands.exec(command, this, args); }; /** diff --git a/lib/ace/ext/emmet.js b/lib/ace/ext/emmet.js index e547ae4d..75eefa3f 100644 --- a/lib/ace/ext/emmet.js +++ b/lib/ace/ext/emmet.js @@ -34,15 +34,7 @@ var HashHandler = require("ace/keyboard/hash_handler").HashHandler; var Editor = require("ace/editor").Editor; var snippetManager = require("ace/snippets").snippetManager; var Range = require("ace/range").Range; -var emmet; - -Editor.prototype.indexToPosition = function(index) { - return this.session.doc.indexToPosition(index); -}; - -Editor.prototype.positionToIndex = function(pos) { - return this.session.doc.positionToIndex(pos); -}; +var emmet, emmetPath; /** * Implementation of {@link IEmmetEditor} interface for Ace @@ -72,9 +64,10 @@ AceEmmetEditor.prototype = { getSelectionRange: function() { // TODO should start be caret position instead? var range = this.ace.getSelectionRange(); + var doc = this.ace.session.doc; return { - start: this.ace.positionToIndex(range.start), - end: this.ace.positionToIndex(range.end) + start: doc.positionToIndex(range.start), + end: doc.positionToIndex(range.end) }; }, @@ -91,9 +84,10 @@ AceEmmetEditor.prototype = { * editor.createSelection(15); */ createSelection: function(start, end) { + var doc = this.ace.session.doc; this.ace.selection.setRange({ - start: this.ace.indexToPosition(start), - end: this.ace.indexToPosition(end) + start: doc.indexToPosition(start), + end: doc.indexToPosition(end) }); }, @@ -106,9 +100,10 @@ AceEmmetEditor.prototype = { * alert(range.start + ', ' + range.end); */ getCurrentLineRange: function() { - var row = this.ace.getCursorPosition().row; - var lineLength = this.ace.session.getLine(row).length; - var index = this.ace.positionToIndex({row: row, column: 0}); + var ace = this.ace; + var row = ace.getCursorPosition().row; + var lineLength = ace.session.getLine(row).length; + var index = ace.session.doc.positionToIndex({row: row, column: 0}); return { start: index, end: index + lineLength @@ -121,7 +116,7 @@ AceEmmetEditor.prototype = { */ getCaretPos: function(){ var pos = this.ace.getCursorPosition(); - return this.ace.positionToIndex(pos); + return this.ace.session.doc.positionToIndex(pos); }, /** @@ -129,7 +124,7 @@ AceEmmetEditor.prototype = { * @param {Number} index Caret position */ setCaretPos: function(index){ - var pos = this.ace.indexToPosition(index); + var pos = this.ace.session.doc.indexToPosition(index); this.ace.selection.moveToPosition(pos); }, @@ -169,14 +164,15 @@ AceEmmetEditor.prototype = { start = 0; var editor = this.ace; - var range = Range.fromPoints(editor.indexToPosition(start), editor.indexToPosition(end)); + var doc = editor.session.doc; + var range = Range.fromPoints(doc.indexToPosition(start), doc.indexToPosition(end)); editor.session.remove(range); range.end = range.start; //editor.selection.setRange(range); value = this.$updateTabstops(value); - snippetManager.insertSnippet(editor, value) + snippetManager.insertSnippet(editor, value); }, /** @@ -292,7 +288,7 @@ AceEmmetEditor.prototype = { lastZero = range.create(data.start, result); } - return result + return result; }, escape: function(ch) { if (ch == '$') return '\\$'; @@ -363,12 +359,17 @@ exports.runEmmetCommand = function(editor) { }, 0); } + var pos = editor.selection.lead; + var token = editor.session.getTokenAt(pos.row, pos.column); + if (token && /\btag\b/.test(token.type)) + return false; + try { var result = actions.run(this.action, editorProxy); } catch(e) { editor._signal("changeStatus", typeof e == "string" ? e : e.message); console.log(e); - result = false + result = false; } return result; }; @@ -383,21 +384,36 @@ for (var command in keymap) { }); } +exports.updateCommands = function(editor, enabled) { + if (enabled) { + editor.keyBinding.addKeyboardHandler(exports.commands); + } else { + editor.keyBinding.removeKeyboardHandler(exports.commands); + } +}; + +exports.isSupportedMode = function(modeId) { + return modeId && /css|less|scss|sass|stylus|html|php|twig/.test(modeId); +}; + var onChangeMode = function(e, target) { var editor = target; if (!editor) return; - var modeId = editor.session.$modeId; - var enabled = modeId && /css|less|scss|sass|stylus|html|php/.test(modeId); + var enabled = exports.isSupportedMode(editor.session.$modeId); if (e.enableEmmet === false) enabled = false; - if (enabled) - editor.keyBinding.addKeyboardHandler(exports.commands); - else - editor.keyBinding.removeKeyboardHandler(exports.commands); + if (enabled) { + if (typeof emmetPath == "string") { + require("ace/config").loadModule(emmetPath, function() { + + }); + emmetPath = null; + } + } + exports.updateCommands(editor, enabled); }; - exports.AceEmmetEditor = AceEmmetEditor; require("ace/config").defineOptions(Editor.prototype, "editor", { enableEmmet: { @@ -409,7 +425,11 @@ require("ace/config").defineOptions(Editor.prototype, "editor", { } }); - -exports.setCore = function(e) {emmet = e;}; +exports.setCore = function(e) { + if (typeof e == "string") + emmetPath = e; + else + emmet = e; +}; }); diff --git a/lib/ace/ext/language_tools.js b/lib/ace/ext/language_tools.js index c7f40bce..4687efd5 100644 --- a/lib/ace/ext/language_tools.js +++ b/lib/ace/ext/language_tools.js @@ -95,9 +95,7 @@ exports.snippetCompleter = snippetCompleter; var expandSnippet = { name: "expandSnippet", exec: function(editor) { - var success = snippetManager.expandWithTab(editor); - if (!success) - editor.execCommand("indent"); + return snippetManager.expandWithTab(editor); }, bindKey: "Tab" }; diff --git a/lib/ace/ext/menu_tools/get_editor_keyboard_shortcuts.js b/lib/ace/ext/menu_tools/get_editor_keyboard_shortcuts.js index e412bfba..99e006b0 100644 --- a/lib/ace/ext/menu_tools/get_editor_keyboard_shortcuts.js +++ b/lib/ace/ext/menu_tools/get_editor_keyboard_shortcuts.js @@ -69,29 +69,20 @@ module.exports.getEditorKeybordShortcuts = function(editor) { editor.keyBinding.$handlers.forEach(function(handler) { var ckb = handler.commandKeyBinding; for (var i in ckb) { - var modifier = parseInt(i); - if (modifier == -1) { - modifier = ""; - } else if(isNaN(modifier)) { - modifier = i; - } else { - modifier = "" + - (modifier & KEY_MODS.command ? "Cmd-" : "") + - (modifier & KEY_MODS.ctrl ? "Ctrl-" : "") + - (modifier & KEY_MODS.alt ? "Alt-" : "") + - (modifier & KEY_MODS.shift ? "Shift-" : ""); - } - for (var key in ckb[i]) { - var command = ckb[i][key] + var key = i.replace(/(^|-)\w/g, function(x) { return x.toUpperCase(); }); + var commands = ckb[i]; + if (!Array.isArray(commands)) + commands = [commands]; + commands.forEach(function(command) { if (typeof command != "string") command = command.name if (commandMap[command]) { - commandMap[command].key += "|" + modifier + key; + commandMap[command].key += "|" + key; } else { - commandMap[command] = {key: modifier+key, command: command}; + commandMap[command] = {key: key, command: command}; keybindings.push(commandMap[command]); - } - } + } + }); } }); return keybindings; diff --git a/lib/ace/keyboard/emacs.js b/lib/ace/keyboard/emacs.js index df14b9a1..ab4471b2 100644 --- a/lib/ace/keyboard/emacs.js +++ b/lib/ace/keyboard/emacs.js @@ -64,7 +64,7 @@ exports.handler.attach = function(editor) { initialized = true; dom.importCssString('\ .emacs-mode .ace_cursor{\ - border: 2px rgba(50,250,50,0.8) solid!important;\ + border: 1px rgba(50,250,50,0.8) solid!important;\ -moz-box-sizing: border-box!important;\ -webkit-box-sizing: border-box!important;\ box-sizing: border-box!important;\ @@ -195,6 +195,8 @@ exports.handler.onPaste = function(e, editor) { }; exports.handler.bindKey = function(key, command) { + if (typeof key == "object") + key = key[this.platform]; if (!key) return; diff --git a/lib/ace/keyboard/hash_handler.js b/lib/ace/keyboard/hash_handler.js index cb02f170..06badccd 100644 --- a/lib/ace/keyboard/hash_handler.js +++ b/lib/ace/keyboard/hash_handler.js @@ -33,37 +33,25 @@ define(function(require, exports, module) { var keyUtil = require("../lib/keys"); var useragent = require("../lib/useragent"); +var KEY_MODS = keyUtil.KEY_MODS; function HashHandler(config, platform) { this.platform = platform || (useragent.isMac ? "mac" : "win"); this.commands = {}; this.commandKeyBinding = {}; - - // todo remove this after a while - if (this.__defineGetter__ && this.__defineSetter__ && typeof console != "undefined" && console.error) { - var warned = false; - var warn = function() { - if (!warned) { - warned = true; - console.error("commmandKeyBinding has too many m's. use commandKeyBinding"); - } - }; - this.__defineGetter__("commmandKeyBinding", function() { - warn(); - return this.commandKeyBinding; - }); - this.__defineSetter__("commmandKeyBinding", function(val) { - warn(); - return this.commandKeyBinding = val; - }); - } else { - this.commmandKeyBinding = this.commandKeyBinding; - } - this.addCommands(config); -}; + this.$singleCommand = true; +} + +function MultiHashHandler(config, platform) { + HashHandler.call(this, config, platform); + this.$singleCommand = false; +} + +MultiHashHandler.prototype = HashHandler.prototype; (function() { + this.addCommand = function(command) { if (this.commands[command.name]) @@ -75,37 +63,76 @@ function HashHandler(config, platform) { this._buildKeyHash(command); }; - this.removeCommand = function(command) { - var name = (typeof command === 'string' ? command : command.name); + this.removeCommand = function(command, keepCommand) { + var name = command && (typeof command === 'string' ? command : command.name); command = this.commands[name]; - delete this.commands[name]; + if (!keepCommand) + delete this.commands[name]; // exhaustive search is brute force but since removeCommand is // not a performance critical operation this should be OK var ckb = this.commandKeyBinding; - for (var hashId in ckb) { - for (var key in ckb[hashId]) { - if (ckb[hashId][key] == command) - delete ckb[hashId][key]; + for (var keyId in ckb) { + var cmdGroup = ckb[keyId]; + if (cmdGroup == command) { + delete ckb[keyId]; + } else if (Array.isArray(cmdGroup)) { + var i = cmdGroup.indexOf(command); + if (i != -1) { + cmdGroup.splice(i, 1); + if (cmdGroup.length == 1) + ckb[keyId] = cmdGroup[0]; + } } } }; - this.bindKey = function(key, command) { - if(!key) + this.bindKey = function(key, command, asDefault) { + if (typeof key == "object") + key = key[this.platform]; + if (!key) return; - if (typeof command == "function") { - this.addCommand({exec: command, bindKey: key, name: command.name || key}); - return; - } - - var ckb = this.commandKeyBinding; + if (typeof command == "function") + return this.addCommand({exec: command, bindKey: key, name: command.name || key}); + key.split("|").forEach(function(keyPart) { - var binding = this.parseKeys(keyPart, command); - var hashId = binding.hashId; - (ckb[hashId] || (ckb[hashId] = {}))[binding.key] = command; + var chain = ""; + if (keyPart.indexOf(" ") != -1) { + var parts = keyPart.split(/\s+/); + keyPart = parts.pop(); + parts.forEach(function(keyPart) { + var binding = this.parseKeys(keyPart); + var id = KEY_MODS[binding.hashId] + binding.key; + chain += (chain ? " " : "") + id; + this._addCommandToBinding(chain, "chainKeys"); + }, this); + chain += " "; + } + var binding = this.parseKeys(keyPart); + var id = KEY_MODS[binding.hashId] + binding.key; + this._addCommandToBinding(chain + id, command, asDefault); }, this); }; + + this._addCommandToBinding = function(keyId, command, asDefault) { + var ckb = this.commandKeyBinding, i; + if (!command) { + delete ckb[keyId]; + } else if (!ckb[keyId] || this.$singleCommand) { + ckb[keyId] = command; + } else { + if (!Array.isArray(ckb[keyId])) { + ckb[keyId] = [ckb[keyId]]; + } else if ((i = ckb[keyId].indexOf(command)) != -1) { + ckb[keyId].splice(i, 1); + } + + if (asDefault || command.isDefault) + ckb[keyId].unshift(command); + else + ckb[keyId].push(command); + } + }; this.addCommands = function(commands) { commands && Object.keys(commands).forEach(function(name) { @@ -142,21 +169,12 @@ function HashHandler(config, platform) { }; this._buildKeyHash = function(command) { - var binding = command.bindKey; - if (!binding) - return; - - var key = typeof binding == "string" ? binding: binding[this.platform]; - this.bindKey(key, command); + this.bindKey(command.bindKey, command); }; // accepts keys in the form ctrl+Enter or ctrl-Enter // keys without modifiers or shift only this.parseKeys = function(keys) { - // todo support keychains - if (keys.indexOf(" ") != -1) - keys = keys.split(/\s+/).pop(); - var parts = keys.toLowerCase().split(/[\-\+]([\-\+])?/).filter(function(x){return x}); var key = parts.pop(); @@ -173,7 +191,7 @@ function HashHandler(config, platform) { var modifier = keyUtil.KEY_MODS[parts[i]]; if (modifier == null) { if (typeof console != "undefined") - console.error("invalid modifier " + parts[i] + " in " + keys); + console.error("invalid modifier " + parts[i] + " in " + keys); return false; } hashId |= modifier; @@ -182,17 +200,32 @@ function HashHandler(config, platform) { }; this.findKeyCommand = function findKeyCommand(hashId, keyString) { - var ckbr = this.commandKeyBinding; - return ckbr[hashId] && ckbr[hashId][keyString]; + var key = KEY_MODS[hashId] + keyString; + return this.commandKeyBinding[key]; }; this.handleKeyboard = function(data, hashId, keyString, keyCode) { - return { - command: this.findKeyCommand(hashId, keyString) - }; + var key = KEY_MODS[hashId] + keyString; + var command = this.commandKeyBinding[key]; + if (data.$keyChain) { + data.$keyChain += " " + key; + command = this.commandKeyBinding[data.$keyChain] || command; + } + + if (command) { + if (command == "chainKeys" || command[command.length - 1] == "chainKeys") { + data.$keyChain = data.$keyChain || key; + return {command: "null"}; + } + } + + if (data.$keyChain && keyCode > 0) + data.$keyChain = ""; + return {command: command}; }; -}).call(HashHandler.prototype) +}).call(HashHandler.prototype); exports.HashHandler = HashHandler; +exports.MultiHashHandler = MultiHashHandler; }); diff --git a/lib/ace/lib/keys.js b/lib/ace/lib/keys.js index e75a6e47..722c544d 100644 --- a/lib/ace/lib/keys.js +++ b/lib/ace/lib/keys.js @@ -145,6 +145,9 @@ var Keys = (function() { } })(); + ret.KEY_MODS[0] = ""; + ret.KEY_MODS[-1] = "input"; + return ret; })(); oop.mixin(exports, Keys);