Merge pull request #894 from ajaxorg/keys

better handling of keys without modifiers
This commit is contained in:
Mostafa Eweda 2012-09-04 02:30:19 -07:00
commit 4fc6114be9
6 changed files with 79 additions and 66 deletions

View file

@ -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() {
@ -161,33 +161,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");
}
};

View file

@ -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) {

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);