From cc2c0900df2a575bf723c41c2758fb9c87cc036d Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 26 Aug 2012 01:11:17 +0400 Subject: [PATCH 1/2] allow binding keys without modifiers --- lib/ace/commands/command_manager_test.js | 48 ++++++++++++++---------- lib/ace/keyboard/hash_handler.js | 41 ++++++++++---------- lib/ace/lib/keys.js | 7 +++- 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/lib/ace/commands/command_manager_test.js b/lib/ace/commands/command_manager_test.js index 3ad11d59..9e4bfa6f 100644 --- a/lib/ace/commands/command_manager_test.js +++ b/lib/ace/commands/command_manager_test.js @@ -68,20 +68,20 @@ module.exports = { }, "test: mac hotkeys": function() { - var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L"); + var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l"); assert.equal(command, this.command); - var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "L"); + var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "l"); assert.equal(command, undefined); }, "test: win hotkeys": function() { var cm = new CommandManager("win", [this.command]); - var command = cm.findKeyCommand(keys.KEY_MODS.command, "L"); + var command = cm.findKeyCommand(keys.KEY_MODS.command, "l"); assert.equal(command, undefined); - var command = cm.findKeyCommand(keys.KEY_MODS.ctrl, "L"); + var command = cm.findKeyCommand(keys.KEY_MODS.ctrl, "l"); assert.equal(command, this.command); }, @@ -91,7 +91,7 @@ module.exports = { this.cm.exec("gotoline"); assert.ok(!this.command.called); - var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L"); + var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l"); assert.equal(command, null); }, @@ -101,7 +101,7 @@ module.exports = { this.cm.exec("gotoline"); assert.ok(!this.command.called); - var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "L"); + var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l"); assert.equal(command, null); }, @@ -121,7 +121,7 @@ module.exports = { assert.ok(command.called); assert.ok(!this.command.called); - assert.equal(this.cm.findKeyCommand(keys.KEY_MODS.command, "L"), command); + assert.equal(this.cm.findKeyCommand(keys.KEY_MODS.command, "l"), command); }, "test: adding commands and recording a macro": function() { @@ -156,33 +156,41 @@ module.exports = { }, "test: bindkeys": function() { - var called = ""; - this.cm.addCommands({ - cm1: function(editor, arg) { - called += "1" + (arg || ""); - }, - cm2: function(editor) { - called += "2"; - } - }); - this.cm.bindKeys({ "Ctrl-L|Command-C": "cm1", "Ctrl-R": "cm2" }); - var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "C"); + var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "c"); assert.equal(command, "cm1"); - var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "R"); + var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "r"); assert.equal(command, "cm2"); this.cm.bindKeys({ "Ctrl-R": null }); - var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "R"); + var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "r"); assert.equal(command, null); + }, + + "test: binding keys without modifiers": function() { + this.cm.bindKeys({ + "R": "cm1", + "Shift-r": "cm2", + "Return": "cm4", + "Enter": "cm3" + }); + + var command = this.cm.findKeyCommand(-1, "r"); + assert.equal(command, "cm1"); + + var command = this.cm.findKeyCommand(-1, "R"); + assert.equal(command, "cm2"); + + var command = this.cm.findKeyCommand(0, "return"); + assert.equal(command, "cm3"); } }; diff --git a/lib/ace/keyboard/hash_handler.js b/lib/ace/keyboard/hash_handler.js index 3fad3339..18cb7066 100644 --- a/lib/ace/keyboard/hash_handler.js +++ b/lib/ace/keyboard/hash_handler.js @@ -129,33 +129,34 @@ function HashHandler(config, platform) { var key = typeof binding == "string" ? binding: binding[this.platform]; this.bindKey(key, command); }; - + + // accepts keys in the form ctrl+Enter or ctrl-Enter + // keys without modifiers or shift only this.parseKeys = function(keys) { - var key; + var parts = keys.toLowerCase().split(/[\-\+]([\-\+])?/).filter(function(x){return x}); + var key = parts.pop(); + + var keyCode = keyUtil[key]; + if (keyUtil.FUNCTION_KEYS[keyCode]) + key = keyUtil.FUNCTION_KEYS[keyCode].toLowerCase(); + else if (!parts.length) + return {key: key, hashId: -1}; + else if (parts.length == 1 && parts[0] == "shift") + return {key: key.toUpperCase(), hashId: -1}; + var hashId = 0; - var parts = keys.toLowerCase().trim().split(/\s*\-\s*/); - - for (var i = 0, l = parts.length; i < l; i++) { - if (keyUtil.KEY_MODS[parts[i]]) - hashId = hashId | keyUtil.KEY_MODS[parts[i]]; - else - key = parts[i] || "-"; //when empty, the splitSafe removed a '-' + for (var i = parts.length; i--;) { + var modifier = keyUtil.KEY_MODS[parts[i]]; + if (modifier == null) + throw "invalid modifier " + parts[i] + " in " + keys; + hashId |= modifier; } - - if (parts[0] == "text" && parts.length == 2) { - hashId = -1; - key = parts[1]; - } - - return { - key: key, - hashId: hashId - }; + return {key: key, hashId: hashId}; }; this.findKeyCommand = function findKeyCommand(hashId, keyString) { var ckbr = this.commmandKeyBinding; - return ckbr[hashId] && ckbr[hashId][keyString.toLowerCase()]; + return ckbr[hashId] && ckbr[hashId][keyString]; }; this.handleKeyboard = function(data, hashId, keyString, keyCode) { diff --git a/lib/ace/lib/keys.js b/lib/ace/lib/keys.js index 30719ce1..8ebfe542 100644 --- a/lib/ace/lib/keys.js +++ b/lib/ace/lib/keys.js @@ -108,7 +108,7 @@ var Keys = (function() { // A reverse map of FUNCTION_KEYS for (var i in ret.FUNCTION_KEYS) { - var name = ret.FUNCTION_KEYS[i].toUpperCase(); + var name = ret.FUNCTION_KEYS[i].toLowerCase(); ret[name] = parseInt(i, 10); } @@ -118,6 +118,11 @@ var Keys = (function() { oop.mixin(ret, ret.PRINTABLE_KEYS); oop.mixin(ret, ret.FUNCTION_KEYS); + // aliases + ret.enter = ret["return"]; + ret.escape = ret.esc; + ret.del = ret["delete"]; + return ret; })(); oop.mixin(exports, Keys); From 96f46cf2f0d3288c4142c18a0381d191c1718e52 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 26 Aug 2012 01:12:48 +0400 Subject: [PATCH 2/2] add folding fr yaml and tcl --- lib/ace/mode/haxe.js | 45 ++++++++++++++++++++------------------------ lib/ace/mode/tcl.js | 2 ++ lib/ace/mode/yaml.js | 2 ++ 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/ace/mode/haxe.js b/lib/ace/mode/haxe.js index 5c12e41f..d88d03c0 100644 --- a/lib/ace/mode/haxe.js +++ b/lib/ace/mode/haxe.js @@ -19,37 +19,32 @@ oop.inherits(Mode, TextMode); (function() { - this.getNextLineIndent = function(state, line, tab) { - var indent = this.$getIndent(line); + this.getNextLineIndent = function(state, line, tab) { + var indent = this.$getIndent(line); - var tokenizedLine = this.$tokenizer.getLineTokens(line, state); - var tokens = tokenizedLine.tokens; + var tokenizedLine = this.$tokenizer.getLineTokens(line, state); + var tokens = tokenizedLine.tokens; - if (tokens.length && tokens[tokens.length-1].type == "comment") { - return indent; - } + if (tokens.length && tokens[tokens.length-1].type == "comment") { + return indent; + } - if (state == "start") { - var match = line.match(/^.*[\{\(\[]\s*$/); - if (match) { - indent += tab; - } - } + if (state == "start") { + var match = line.match(/^.*[\{\(\[]\s*$/); + if (match) { + indent += tab; + } + } - return indent; - }; + return indent; + }; - this.checkOutdent = function(state, line, input) { - return this.$outdent.checkOutdent(line, input); - }; + this.checkOutdent = function(state, line, input) { + return this.$outdent.checkOutdent(line, input); + }; - this.autoOutdent = function(state, doc, row) { - this.$outdent.autoOutdent(doc, row); - }; - - - this.createWorker = function(session) { - return null; + this.autoOutdent = function(state, doc, row) { + this.$outdent.autoOutdent(doc, row); }; }).call(Mode.prototype); diff --git a/lib/ace/mode/tcl.js b/lib/ace/mode/tcl.js index 6417119a..978d6733 100644 --- a/lib/ace/mode/tcl.js +++ b/lib/ace/mode/tcl.js @@ -42,6 +42,7 @@ define(function(require, exports, module) { var oop = require("../lib/oop"); var TextMode = require("./text").Mode; var Tokenizer = require("../tokenizer").Tokenizer; +var CStyleFoldMode = require("./folding/cstyle").FoldMode; var TclHighlightRules = require("./tcl_highlight_rules").TclHighlightRules; var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent; var Range = require("../range").Range; @@ -49,6 +50,7 @@ var Range = require("../range").Range; var Mode = function() { this.$tokenizer = new Tokenizer(new TclHighlightRules().getRules()); this.$outdent = new MatchingBraceOutdent(); + this.foldingRules = new CStyleFoldMode(); }; oop.inherits(Mode, TextMode); diff --git a/lib/ace/mode/yaml.js b/lib/ace/mode/yaml.js index a252308e..e268ce6d 100644 --- a/lib/ace/mode/yaml.js +++ b/lib/ace/mode/yaml.js @@ -26,10 +26,12 @@ var TextMode = require("./text").Mode; var Tokenizer = require("../tokenizer").Tokenizer; var YamlHighlightRules = require("./yaml_highlight_rules").YamlHighlightRules; var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent; +var FoldMode = require("./folding/coffee").FoldMode; var Mode = function() { this.$tokenizer = new Tokenizer(new YamlHighlightRules().getRules()); this.$outdent = new MatchingBraceOutdent(); + this.foldingRules = new FoldMode(); }; oop.inherits(Mode, TextMode);