convert Assignment from using plain text to using an Argument

This commit is contained in:
Joe Walker 2010-12-02 12:22:50 +00:00
commit 59794db155

View file

@ -41,16 +41,13 @@ define(function(require, exports, module) {
var console = require('pilot/console'); var console = require('pilot/console');
var util = require('pilot/util'); var util = require('pilot/util');
var oop = require('pilot/oop').oop; var oop = require('pilot/oop').oop;
var EventEmitter = require('pilot/event_emitter').EventEmitter;
//var keyboard = require('keyboard/keyboard'); //var keyboard = require('keyboard/keyboard');
var Status = require('pilot/types').Status; var Status = require('pilot/types').Status;
var canon = require('pilot/canon'); var canon = require('pilot/canon');
var types = require('pilot/types'); var types = require('pilot/types');
var commandType;
exports.startup = function(data, reason) {
commandType = types.getType('command');
};
/** /**
* The information required to tell the user there is a problem with their * The information required to tell the user there is a problem with their
@ -113,6 +110,10 @@ Argument.prototype = {
this.text + following.priorSpace + following.text, this.text + following.priorSpace + following.text,
this.start, following.end, this.start, following.end,
this.priorSpace); this.priorSpace);
},
setText: function(text) {
this.text = text;
} }
}; };
/** /**
@ -202,7 +203,9 @@ Input.prototype = {
var command = _split(args); var command = _split(args);
if (!command) { if (!command) {
// TODO: Should we use this technique in split?
// No command found - bail helpfully. // No command found - bail helpfully.
var commandType = types.getType('command');
var conversion = commandType.parse(typed); var conversion = commandType.parse(typed);
var arg = Argument.mergeAll(args); var arg = Argument.mergeAll(args);
this._addHint(new ConversionHint(conversion, arg)); this._addHint(new ConversionHint(conversion, arg));
@ -218,14 +221,18 @@ Input.prototype = {
this.requisition.setCommand(command); this.requisition.setCommand(command);
this._assign(args); this._assign(args);
this.addHints(this.requisition.getHints()); this._addHint(this.requisition.getHints());
} }
}, },
/** /**
* Some sugar around: 'this.hints.push(new Hint(...));' * Some sugar around: 'this.hints.push(new Hint(...));', but you can also
* pass in an array of Hints or the parameters to create a hint
*/ */
_addHint: function(status, message, start, end) { _addHint: function(status, message, start, end) {
if (status == null) {
return;
}
if (status instanceof Hint) { if (status instanceof Hint) {
this.hints.push(status); this.hints.push(status);
} }
@ -270,7 +277,7 @@ Input.prototype = {
if (this.requisition.assignmentCount == 1) { if (this.requisition.assignmentCount == 1) {
var assignment = this.requisition.getAssignment(0); var assignment = this.requisition.getAssignment(0);
if (assignment.param.type.name === 'text') { if (assignment.param.type.name === 'text') {
assignment.setText(Argument.mergeAll(args).text); assignment.setArgument(Argument.mergeAll(args));
return; return;
} }
} }
@ -305,7 +312,7 @@ Input.prototype = {
args[i]); args[i]);
} else { } else {
args.splice(i + 1, 1); args.splice(i + 1, 1);
assignment.setText(args[i + 1].text); assignment.setArgument(args[i + 1]);
} }
} }
@ -326,7 +333,7 @@ Input.prototype = {
else { else {
var arg = args[i]; var arg = args[i];
args.splice(i, 1); args.splice(i, 1);
assignment.setText(arg.text); assignment.setArgument(arg);
} }
i++; i++;
@ -358,7 +365,7 @@ function _tokenize(typed) {
// We are generally converting to their real values except for \', \" // We are generally converting to their real values except for \', \"
// and '\ ' which we are converting to unicode private characters so we // and '\ ' which we are converting to unicode private characters so we
// can distinguish them from ', " and ' ', which have special meaning. // can distinguish them from ', " and ' ', which have special meaning.
// They need swapping back post-split. // They need swapping back post-split - see unescape()
typed = typed typed = typed
.replace(/\\\\/g, '\\') .replace(/\\\\/g, '\\')
.replace(/\\b/g, '\b') .replace(/\\b/g, '\b')
@ -373,6 +380,13 @@ function _tokenize(typed) {
.replace(/\\'/g, '\uF001') .replace(/\\'/g, '\uF001')
.replace(/\\"/g, '\uF002'); .replace(/\\"/g, '\uF002');
function unescape(str) {
return str
.replace(/\uF000/g, ' ')
.replace(/\uF001/g, '\'')
.replace(/\uF002/g, '"');
}
var i = 0; var i = 0;
var start = 0; // Where did this section start? var start = 0; // Where did this section start?
var priorSpace = ''; var priorSpace = '';
@ -382,7 +396,7 @@ function _tokenize(typed) {
if (i >= typed.length) { if (i >= typed.length) {
// There is no more - tidy up // There is no more - tidy up
if (mode !== OUTSIDE) { if (mode !== OUTSIDE) {
var str = typed.substring(start, i); var str = unescape(typed.substring(start, i));
args.push(new Argument(str, start, i, priorSpace)); args.push(new Argument(str, start, i, priorSpace));
} }
break; break;
@ -415,7 +429,7 @@ function _tokenize(typed) {
// There is an edge case of xx'xx which we are assuming to be // There is an edge case of xx'xx which we are assuming to be
// a single parameter (and same with ") // a single parameter (and same with ")
if (c === ' ') { if (c === ' ') {
var str = typed.substring(start, i); var str = unescape(typed.substring(start, i));
args.push(new Argument(str, start, i, priorSpace)); args.push(new Argument(str, start, i, priorSpace));
mode = OUTSIDE; mode = OUTSIDE;
start = i; start = i;
@ -425,7 +439,7 @@ function _tokenize(typed) {
case IN_SINGLE_Q: case IN_SINGLE_Q:
if (c === '\'') { if (c === '\'') {
var str = typed.substring(start, i); var str = unescape(typed.substring(start, i));
args.push(new Argument(str, start, i, priorSpace)); args.push(new Argument(str, start, i, priorSpace));
mode = OUTSIDE; mode = OUTSIDE;
start = i + 1; start = i + 1;
@ -435,7 +449,7 @@ function _tokenize(typed) {
case IN_DOUBLE_Q: case IN_DOUBLE_Q:
if (c === '"') { if (c === '"') {
var str = typed.substring(start, i); var str = unescape(typed.substring(start, i));
args.push(new Argument(str, start, i, priorSpace)); args.push(new Argument(str, start, i, priorSpace));
mode = OUTSIDE; mode = OUTSIDE;
start = i + 1; start = i + 1;
@ -447,13 +461,6 @@ function _tokenize(typed) {
i++; i++;
} }
args.forEach(function(arg) {
arg.text = arg.text
.replace(/\uF000/g, ' ')
.replace(/\uF001/g, '\'')
.replace(/\uF002/g, '"');
});
return args; return args;
} }
exports._tokenize = _tokenize; exports._tokenize = _tokenize;
@ -724,7 +731,10 @@ Assignment.prototype = {
if (value === undefined) { if (value === undefined) {
value = this.param.defaultValue; value = this.param.defaultValue;
} }
this.text = (value === null) ? '' : this.param.type.stringify(value); var text = (value === null) ? '' : this.param.type.stringify(value);
if (this.arg) {
this.arg.setText(text);
}
this.value = value; this.value = value;
this.status = Status.VALID; this.status = Status.VALID;
this.message = ''; this.message = '';
@ -735,13 +745,13 @@ Assignment.prototype = {
* The textual representation of the current value * The textual representation of the current value
* @readonly - use setValue() to mutate * @readonly - use setValue() to mutate
*/ */
text: undefined, arg: undefined,
setText: function(text) { setArgument: function(arg) {
if (this.text === text) { if (this.arg === arg) {
return; return;
} }
var conversion = this.param.type.parse(text); var conversion = this.param.type.parse(arg.text);
this.text = text; this.arg = arg;
this.value = conversion.value; this.value = conversion.value;
this.status = conversion.status; this.status = conversion.status;
this.message = conversion.message; this.message = conversion.message;