Merge pull request #1570 from ajaxorg/papercuts
Small improvements to the api
This commit is contained in:
commit
dbff7216f0
10 changed files with 410 additions and 248 deletions
|
|
@ -402,6 +402,8 @@ var Document = function(text) {
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.remove = function(range) {
|
this.remove = function(range) {
|
||||||
|
if (!range instanceof Range)
|
||||||
|
range = Range.fromPoints(range.start, range.end);
|
||||||
// clip to document
|
// clip to document
|
||||||
range.start = this.$clipPosition(range.start);
|
range.start = this.$clipPosition(range.start);
|
||||||
range.end = this.$clipPosition(range.end);
|
range.end = this.$clipPosition(range.end);
|
||||||
|
|
@ -520,6 +522,8 @@ var Document = function(text) {
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.replace = function(range, text) {
|
this.replace = function(range, text) {
|
||||||
|
if (!range instanceof Range)
|
||||||
|
range = Range.fromPoints(range.start, range.end);
|
||||||
if (text.length == 0 && range.isEmpty())
|
if (text.length == 0 && range.isEmpty())
|
||||||
return range.start;
|
return range.start;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -739,25 +739,29 @@ var Editor = function(renderer, session) {
|
||||||
this.renderer.updateFull();
|
this.renderer.updateFull();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the string of text currently highlighted.
|
||||||
|
* @returns {String}
|
||||||
|
**/
|
||||||
|
this.getSelectedText = function() {
|
||||||
|
return this.session.getTextRange(this.getSelectionRange());
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emitted when text is copied.
|
* Emitted when text is copied.
|
||||||
* @event copy
|
* @event copy
|
||||||
* @param {String} text The copied text
|
* @param {String} text The copied text
|
||||||
*
|
*
|
||||||
*
|
|
||||||
**/
|
**/
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Returns the string of text currently highlighted.
|
* Returns the string of text currently highlighted.
|
||||||
* @returns {String}
|
* @returns {String}
|
||||||
|
* @deprecated Use getSelectedText instead.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
this.getCopyText = function() {
|
this.getCopyText = function() {
|
||||||
var text = "";
|
var text = this.getSelectedText();
|
||||||
if (!this.selection.isEmpty())
|
this._signal("copy", text);
|
||||||
text = this.session.getTextRange(this.getSelectionRange());
|
|
||||||
|
|
||||||
this._emit("copy", text);
|
|
||||||
return text;
|
return text;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -2386,6 +2390,7 @@ config.defineOptions(Editor.prototype, "editor", {
|
||||||
maxLines: "renderer",
|
maxLines: "renderer",
|
||||||
minLines: "renderer",
|
minLines: "renderer",
|
||||||
scrollPastEnd: "renderer",
|
scrollPastEnd: "renderer",
|
||||||
|
fixedWidthGutter: "renderer",
|
||||||
|
|
||||||
scrollSpeed: "$mouseHandler",
|
scrollSpeed: "$mouseHandler",
|
||||||
dragDelay: "$mouseHandler",
|
dragDelay: "$mouseHandler",
|
||||||
|
|
|
||||||
|
|
@ -168,7 +168,7 @@ var Gutter = function(parentEl) {
|
||||||
this.element = dom.setInnerHtml(this.element, html.join(""));
|
this.element = dom.setInnerHtml(this.element, html.join(""));
|
||||||
this.element.style.height = config.minHeight + "px";
|
this.element.style.height = config.minHeight + "px";
|
||||||
|
|
||||||
if (this.session.$useWrapMode)
|
if (this.$fixedWidth || this.session.$useWrapMode)
|
||||||
lastLineNumber = this.session.getLength();
|
lastLineNumber = this.session.getLength();
|
||||||
|
|
||||||
var gutterWidth = ("" + lastLineNumber).length * config.characterWidth;
|
var gutterWidth = ("" + lastLineNumber).length * config.characterWidth;
|
||||||
|
|
@ -181,6 +181,8 @@ var Gutter = function(parentEl) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.$fixedWidth = false;
|
||||||
|
|
||||||
this.$showFoldWidgets = true;
|
this.$showFoldWidgets = true;
|
||||||
this.setShowFoldWidgets = function(show) {
|
this.setShowFoldWidgets = function(show) {
|
||||||
if (show)
|
if (show)
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@
|
||||||
define(function(require, exports, module) {
|
define(function(require, exports, module) {
|
||||||
// Generated by CoffeeScript 1.6.3
|
// Generated by CoffeeScript 1.6.3
|
||||||
|
|
||||||
var buildLocationData, extend, flatten, last, repeat, _ref;
|
var buildLocationData, extend, flatten, last, repeat, syntaxErrorToString, _ref;
|
||||||
|
|
||||||
exports.starts = function(string, literal, start) {
|
exports.starts = function(string, literal, start) {
|
||||||
return literal === string.substr(start, literal.length);
|
return literal === string.substr(start, literal.length);
|
||||||
|
|
@ -223,30 +223,48 @@ define(function(require, exports, module) {
|
||||||
}
|
}
|
||||||
error = new SyntaxError(message);
|
error = new SyntaxError(message);
|
||||||
error.location = location;
|
error.location = location;
|
||||||
|
error.toString = syntaxErrorToString;
|
||||||
|
error.stack = error.toString();
|
||||||
throw error;
|
throw error;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.prettyErrorMessage = function(error, filename, code, useColors) {
|
exports.updateSyntaxError = function(error, code, filename) {
|
||||||
var codeLine, colorize, end, first_column, first_line, last_column, last_line, marker, message, start, _ref1;
|
if (error.toString === syntaxErrorToString) {
|
||||||
if (!error.location) {
|
error.code || (error.code = code);
|
||||||
return error.stack || ("" + error);
|
error.filename || (error.filename = filename);
|
||||||
|
error.stack = error.toString();
|
||||||
}
|
}
|
||||||
filename = error.filename || filename;
|
return error;
|
||||||
code = error.code || code;
|
};
|
||||||
_ref1 = error.location, first_line = _ref1.first_line, first_column = _ref1.first_column, last_line = _ref1.last_line, last_column = _ref1.last_column;
|
|
||||||
codeLine = code.split('\n')[first_line];
|
syntaxErrorToString = function() {
|
||||||
|
var codeLine, colorize, colorsEnabled, end, filename, first_column, first_line, last_column, last_line, marker, start, _ref1, _ref2;
|
||||||
|
if (!(this.code && this.location)) {
|
||||||
|
return Error.prototype.toString.call(this);
|
||||||
|
}
|
||||||
|
_ref1 = this.location, first_line = _ref1.first_line, first_column = _ref1.first_column, last_line = _ref1.last_line, last_column = _ref1.last_column;
|
||||||
|
if (last_line == null) {
|
||||||
|
last_line = first_line;
|
||||||
|
}
|
||||||
|
if (last_column == null) {
|
||||||
|
last_column = first_column;
|
||||||
|
}
|
||||||
|
filename = this.filename || '[stdin]';
|
||||||
|
codeLine = this.code.split('\n')[first_line];
|
||||||
start = first_column;
|
start = first_column;
|
||||||
end = first_line === last_line ? last_column + 1 : codeLine.length;
|
end = first_line === last_line ? last_column + 1 : codeLine.length;
|
||||||
marker = repeat(' ', start) + repeat('^', end - start);
|
marker = repeat(' ', start) + repeat('^', end - start);
|
||||||
if (useColors) {
|
if (typeof process !== "undefined" && process !== null) {
|
||||||
|
colorsEnabled = process.stdout.isTTY && !process.env.NODE_DISABLE_COLORS;
|
||||||
|
}
|
||||||
|
if ((_ref2 = this.colorful) != null ? _ref2 : colorsEnabled) {
|
||||||
colorize = function(str) {
|
colorize = function(str) {
|
||||||
return "\x1B[1;31m" + str + "\x1B[0m";
|
return "\x1B[1;31m" + str + "\x1B[0m";
|
||||||
};
|
};
|
||||||
codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);
|
codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);
|
||||||
marker = colorize(marker);
|
marker = colorize(marker);
|
||||||
}
|
}
|
||||||
message = "" + filename + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + error.message + "\n" + codeLine + "\n" + marker;
|
return "" + filename + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + this.message + "\n" + codeLine + "\n" + marker;
|
||||||
return message;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ define(function(require, exports, module) {
|
||||||
}
|
}
|
||||||
this.literate = opts.literate;
|
this.literate = opts.literate;
|
||||||
this.indent = 0;
|
this.indent = 0;
|
||||||
|
this.baseIndent = 0;
|
||||||
this.indebt = 0;
|
this.indebt = 0;
|
||||||
this.outdebt = 0;
|
this.outdebt = 0;
|
||||||
this.indents = [];
|
this.indents = [];
|
||||||
|
|
@ -373,11 +374,17 @@ define(function(require, exports, module) {
|
||||||
this.suppressNewlines();
|
this.suppressNewlines();
|
||||||
return indent.length;
|
return indent.length;
|
||||||
}
|
}
|
||||||
|
if (!this.tokens.length) {
|
||||||
|
this.baseIndent = this.indent = size;
|
||||||
|
return indent.length;
|
||||||
|
}
|
||||||
diff = size - this.indent + this.outdebt;
|
diff = size - this.indent + this.outdebt;
|
||||||
this.token('INDENT', diff, indent.length - size, size);
|
this.token('INDENT', diff, indent.length - size, size);
|
||||||
this.indents.push(diff);
|
this.indents.push(diff);
|
||||||
this.ends.push('OUTDENT');
|
this.ends.push('OUTDENT');
|
||||||
this.outdebt = this.indebt = 0;
|
this.outdebt = this.indebt = 0;
|
||||||
|
} else if (size < this.baseIndent) {
|
||||||
|
this.error('missing indentation', indent.length);
|
||||||
} else {
|
} else {
|
||||||
this.indebt = 0;
|
this.indebt = 0;
|
||||||
this.outdentToken(this.indent - size, noNewlines, indent.length);
|
this.outdentToken(this.indent - size, noNewlines, indent.length);
|
||||||
|
|
@ -801,10 +808,15 @@ define(function(require, exports, module) {
|
||||||
return quote + this.escapeLines(body, heredoc) + quote;
|
return quote + this.escapeLines(body, heredoc) + quote;
|
||||||
};
|
};
|
||||||
|
|
||||||
Lexer.prototype.error = function(message) {
|
Lexer.prototype.error = function(message, offset) {
|
||||||
|
var first_column, first_line, _ref2;
|
||||||
|
if (offset == null) {
|
||||||
|
offset = 0;
|
||||||
|
}
|
||||||
|
_ref2 = this.getLineAndColumnFromChunk(offset), first_line = _ref2[0], first_column = _ref2[1];
|
||||||
return throwSyntaxError(message, {
|
return throwSyntaxError(message, {
|
||||||
first_line: this.chunkLine,
|
first_line: first_line,
|
||||||
first_column: this.chunkColumn
|
first_column: first_column
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -256,7 +256,10 @@ define(function(require, exports, module) {
|
||||||
Base.prototype.assigns = NO;
|
Base.prototype.assigns = NO;
|
||||||
|
|
||||||
Base.prototype.updateLocationDataIfMissing = function(locationData) {
|
Base.prototype.updateLocationDataIfMissing = function(locationData) {
|
||||||
this.locationData || (this.locationData = locationData);
|
if (this.locationData) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
this.locationData = locationData;
|
||||||
return this.eachChild(function(child) {
|
return this.eachChild(function(child) {
|
||||||
return child.updateLocationDataIfMissing(locationData);
|
return child.updateLocationDataIfMissing(locationData);
|
||||||
});
|
});
|
||||||
|
|
@ -849,11 +852,11 @@ define(function(require, exports, module) {
|
||||||
|
|
||||||
Comment.prototype.compileNode = function(o, level) {
|
Comment.prototype.compileNode = function(o, level) {
|
||||||
var code;
|
var code;
|
||||||
code = "/*" + (multident(this.comment, this.tab)) + (__indexOf.call(this.comment, '\n') >= 0 ? "\n" + this.tab : '') + "*/\n";
|
code = "/*" + (multident(this.comment, this.tab)) + (__indexOf.call(this.comment, '\n') >= 0 ? "\n" + this.tab : '') + "*/";
|
||||||
if ((level || o.level) === LEVEL_TOP) {
|
if ((level || o.level) === LEVEL_TOP) {
|
||||||
code = o.indent + code;
|
code = o.indent + code;
|
||||||
}
|
}
|
||||||
return [this.makeCode(code)];
|
return [this.makeCode("\n"), this.makeCode(code)];
|
||||||
};
|
};
|
||||||
|
|
||||||
return Comment;
|
return Comment;
|
||||||
|
|
@ -2879,6 +2882,7 @@ define(function(require, exports, module) {
|
||||||
} else {
|
} else {
|
||||||
this.isChain = elseBody instanceof If;
|
this.isChain = elseBody instanceof If;
|
||||||
this.elseBody = this.ensureBlock(elseBody);
|
this.elseBody = this.ensureBlock(elseBody);
|
||||||
|
this.elseBody.updateLocationDataIfMissing(elseBody.locationData);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -72,7 +72,7 @@ module.exports = {
|
||||||
"test unexpected indent": function() {
|
"test unexpected indent": function() {
|
||||||
var e = parse("a\n a\n");
|
var e = parse("a\n a\n");
|
||||||
assert.equal(e.message, "Unexpected 'INDENT'");
|
assert.equal(e.message, "Unexpected 'INDENT'");
|
||||||
assertLocation(e, 0, 0, 0, 0);
|
assertLocation(e, 1, 0, 1, 1);
|
||||||
},
|
},
|
||||||
"test invalid destructuring": function() {
|
"test invalid destructuring": function() {
|
||||||
var e = parse("\n{b: 5} = {}");
|
var e = parse("\n{b: 5} = {}");
|
||||||
|
|
|
||||||
|
|
@ -519,7 +519,7 @@ var Editor = require("./editor").Editor;
|
||||||
this.multiSelect.toSingleRange();
|
this.multiSelect.toSingleRange();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.getCopyText = function() {
|
this.getSelectedText = function() {
|
||||||
var text = "";
|
var text = "";
|
||||||
if (this.inMultiSelectMode && !this.inVirtualSelectionMode) {
|
if (this.inMultiSelectMode && !this.inVirtualSelectionMode) {
|
||||||
var ranges = this.multiSelect.rangeList.ranges;
|
var ranges = this.multiSelect.rangeList.ranges;
|
||||||
|
|
@ -534,7 +534,6 @@ var Editor = require("./editor").Editor;
|
||||||
} else if (!this.selection.isEmpty()) {
|
} else if (!this.selection.isEmpty()) {
|
||||||
text = this.session.getTextRange(this.getSelectionRange());
|
text = this.session.getTextRange(this.getSelectionRange());
|
||||||
}
|
}
|
||||||
this._signal("copy", text);
|
|
||||||
return text;
|
return text;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -887,24 +886,25 @@ function MultiSelect(editor) {
|
||||||
function addAltCursorListeners(editor){
|
function addAltCursorListeners(editor){
|
||||||
var el = editor.textInput.getElement();
|
var el = editor.textInput.getElement();
|
||||||
var altCursor = false;
|
var altCursor = false;
|
||||||
var contentEl = editor.renderer.content;
|
|
||||||
event.addListener(el, "keydown", function(e) {
|
event.addListener(el, "keydown", function(e) {
|
||||||
if (e.keyCode == 18 && !(e.ctrlKey || e.shiftKey || e.metaKey)) {
|
if (e.keyCode == 18 && !(e.ctrlKey || e.shiftKey || e.metaKey)) {
|
||||||
if (!altCursor) {
|
if (!altCursor) {
|
||||||
contentEl.style.cursor = "crosshair";
|
editor.renderer.setMouseCursor("crosshair");
|
||||||
altCursor = true;
|
altCursor = true;
|
||||||
}
|
}
|
||||||
} else if (altCursor) {
|
} else if (altCursor) {
|
||||||
contentEl.style.cursor = "";
|
reset();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
event.addListener(el, "keyup", reset);
|
event.addListener(el, "keyup", reset);
|
||||||
event.addListener(el, "blur", reset);
|
event.addListener(el, "blur", reset);
|
||||||
function reset() {
|
function reset(e) {
|
||||||
if (altCursor) {
|
if (altCursor) {
|
||||||
contentEl.style.cursor = "";
|
editor.renderer.setMouseCursor("");
|
||||||
altCursor = false;
|
altCursor = false;
|
||||||
|
// TODO disable menu poping up
|
||||||
|
// e && e.preventDefault()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1292,12 +1292,20 @@ var VirtualRenderer = function(container, theme) {
|
||||||
this.$loop.schedule(this.CHANGE_H_SCROLL);
|
this.$loop.schedule(this.CHANGE_H_SCROLL);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scrolls the editor across both x- and y-axes.
|
||||||
|
* @param {Number} x The x value to scroll to
|
||||||
|
* @param {Number} y The y value to scroll to
|
||||||
|
**/
|
||||||
|
this.scrollTo = function(x, y) {
|
||||||
|
this.session.setScrollTop(y);
|
||||||
|
this.session.setScrollLeft(y);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scrolls the editor across both x- and y-axes.
|
* Scrolls the editor across both x- and y-axes.
|
||||||
* @param {Number} deltaX The x value to scroll by
|
* @param {Number} deltaX The x value to scroll by
|
||||||
* @param {Number} deltaY The y value to scroll by
|
* @param {Number} deltaY The y value to scroll by
|
||||||
*
|
|
||||||
*
|
|
||||||
**/
|
**/
|
||||||
this.scrollBy = function(deltaX, deltaY) {
|
this.scrollBy = function(deltaX, deltaY) {
|
||||||
deltaY && this.session.setScrollTop(this.session.getScrollTop() + deltaY);
|
deltaY && this.session.setScrollTop(this.session.getScrollTop() + deltaY);
|
||||||
|
|
@ -1496,9 +1504,8 @@ var VirtualRenderer = function(container, theme) {
|
||||||
* [Adds a new class, `style`, to the editor.]{: #VirtualRenderer.setStyle}
|
* [Adds a new class, `style`, to the editor.]{: #VirtualRenderer.setStyle}
|
||||||
* @param {String} style A class name
|
* @param {String} style A class name
|
||||||
*
|
*
|
||||||
*
|
|
||||||
**/
|
**/
|
||||||
this.setStyle = function setStyle(style, include) {
|
this.setStyle = function(style, include) {
|
||||||
dom.setCssClass(this.container, style, include != false);
|
dom.setCssClass(this.container, style, include != false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -1506,14 +1513,20 @@ var VirtualRenderer = function(container, theme) {
|
||||||
* [Removes the class `style` from the editor.]{: #VirtualRenderer.unsetStyle}
|
* [Removes the class `style` from the editor.]{: #VirtualRenderer.unsetStyle}
|
||||||
* @param {String} style A class name
|
* @param {String} style A class name
|
||||||
*
|
*
|
||||||
*
|
|
||||||
**/
|
**/
|
||||||
this.unsetStyle = function unsetStyle(style) {
|
this.unsetStyle = function(style) {
|
||||||
dom.removeCssClass(this.container, style);
|
dom.removeCssClass(this.container, style);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param {String} cursorStyle A css cursor style
|
||||||
*
|
*
|
||||||
|
**/
|
||||||
|
this.setMouseCursor = function(cursorStyle) {
|
||||||
|
this.content.style.cursor = cursorStyle;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
* Destroys the text and cursor layers for this renderer.
|
* Destroys the text and cursor layers for this renderer.
|
||||||
**/
|
**/
|
||||||
this.destroy = function() {
|
this.destroy = function() {
|
||||||
|
|
@ -1642,6 +1655,12 @@ config.defineOptions(VirtualRenderer.prototype, "renderer", {
|
||||||
},
|
},
|
||||||
initialValue: 0,
|
initialValue: 0,
|
||||||
handlesSet: true
|
handlesSet: true
|
||||||
|
},
|
||||||
|
fixedWidthGutter: {
|
||||||
|
set: function(val) {
|
||||||
|
this.gutterLayer.fixedWidth = !!val;
|
||||||
|
this.$loop.schedule(this.CHANGE_GUTTER);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue