Permalink
Browse files

Merge pull request #802 from ajaxorg/misc

Misc
  • Loading branch information...
2 parents f1639ca + 0af6526 commit df0550bf1d501b9f906eb022270dfa554fb90f95 @fjakobs fjakobs committed Jun 11, 2012
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -295,10 +295,15 @@ exports.commands = [{
readOnly: true
}, {
name: "jumptomatching",
- bindKey: bindKey("Ctrl-Shift-P", "Ctrl-Shift-P"),
+ bindKey: bindKey("Ctrl-P", "Ctrl-P"),
exec: function(editor) { editor.jumpToMatching(); },
multiSelectAction: "forEach",
readOnly: true
+}, {
+ name: "selecttomatching",
+ bindKey: bindKey("Ctrl-Shift-P", "Ctrl-Shift-P"),
+ exec: function(editor) { editor.jumpToMatching(true); },
+ readOnly: true
},
// commands disabled in readOnly mode
@@ -320,6 +325,11 @@ exports.commands = [{
exec: function(editor) { editor.removeLines(); },
multiSelectAction: "forEach"
}, {
+ name: "duplicateSelection",
+ bindKey: bindKey("Ctrl-Shift-D", "Command-Shift-D"),
+ exec: function(editor) { editor.duplicateSelection(); },
+ multiSelectAction: "forEach"
+}, {
name: "togglecomment",
bindKey: bindKey("Ctrl-/", "Command-/"),
exec: function(editor) { editor.toggleCommentLines(); },
View
@@ -827,7 +827,8 @@ var EditSession = function(text, mode) {
try {
module = require(mode);
} catch (e) {};
- if (module)
+ // sometimes require returns empty object (this bug is present in requirejs 2 as well)
+ if (module && module.Mode)
return done(module);
// set mode to text until loading is finished
@@ -40,6 +40,7 @@ define(function(require, exports, module) {
"use strict";
var TokenIterator = require("../token_iterator").TokenIterator;
+var Range = require("../range").Range;
/**
* class BracketMatch
@@ -76,15 +77,56 @@ function BracketMatch() {
if (charBeforeCursor == "") return null;
var match = charBeforeCursor.match(/([\(\[\{])|([\)\]\}])/);
- if (!match) {
+ if (!match)
return null;
+
+ if (match[1])
+ return this.$findClosingBracket(match[1], position);
+ else
+ return this.$findOpeningBracket(match[2], position);
+ };
+
+ this.getBracketRange = function(pos) {
+ var line = this.getLine(pos.row);
+ var before = true, range;
+
+ var chr = line.charAt(pos.column-1);
+ var match = chr && chr.match(/([\(\[\{])|([\)\]\}])/);
+ if (!match) {
+ chr = line.charAt(pos.column);
+ pos.column++;
+ match = chr && chr.match(/([\(\[\{])|([\)\]\}])/);
+ before = false;
}
+ if (!match)
+ return null;
if (match[1]) {
- return this.$findClosingBracket(match[1], position);
+ var bracketPos = this.$findClosingBracket(match[1], pos);
+ if (!bracketPos)
+ return null;
+ range = Range.fromPoints(pos, bracketPos);
+ if (!before) {
+ range.end.column++;
+ range.start.column--;
+ }
+ range.cursor = range.end;
} else {
- return this.$findOpeningBracket(match[2], position);
+ var bracketPos = this.$findOpeningBracket(match[2], pos);
+ if (!bracketPos)
+ return null;
+ range = Range.fromPoints(bracketPos, pos);
+ if (!before) {
+ range.start.column++;
+ range.end.column--;
+ }
+ range.cursor = range.start;
}
+
+ if (!before)
+ pos.column--;
+
+ return range;
};
this.$brackets = {
View
@@ -1348,6 +1348,24 @@ var Editor = function(renderer, session) {
this.clearSelection();
};
+ this.duplicateSelection = function() {
+ var sel = this.selection;
+ var doc = this.session;
+ var range = sel.getRange();
+ if (range.isEmpty()) {
+ var row = range.start.row;
+ doc.duplicateLines(row, row);
+ } else {
+ var reverse = sel.isBackwards()
+ var point = sel.isBackwards() ? range.start : range.end;
+ var endPoint = doc.insert(point, doc.getTextRange(range), false);
+ range.start = point;
+ range.end = endPoint;
+
+ sel.setSelectionRange(range, reverse)
+ }
+ };
+
/** related to: EditSession.moveLinesDown
* Editor.moveLinesDown() -> Number
* + (Number): On success, it returns -1.
@@ -1660,8 +1678,11 @@ var Editor = function(renderer, session) {
**/
this.centerSelection = function() {
var range = this.getSelectionRange();
- var line = Math.floor(range.start.row + (range.end.row - range.start.row) / 2);
- this.renderer.scrollToLine(line, true);
+ var pos = {
+ row: Math.floor(range.start.row + (range.end.row - range.start.row) / 2),
+ column: Math.floor(range.start.column + (range.end.column - range.start.column) / 2)
+ }
+ this.renderer.alignCursor(pos, 0.5);
};
/** related to: Selection.getCursor
@@ -1746,21 +1767,34 @@ var Editor = function(renderer, session) {
* Moves the cursor's row and column to the next matching bracket.
*
**/
- this.jumpToMatching = function() {
+ this.jumpToMatching = function(select) {
var cursor = this.getCursorPosition();
- var pos = this.session.findMatchingBracket(cursor);
- if (!pos) {
- cursor.column += 1;
- pos = this.session.findMatchingBracket(cursor);
- }
- if (!pos) {
- cursor.column -= 2;
- pos = this.session.findMatchingBracket(cursor);
- }
+ var range = this.session.getBracketRange(cursor);
+ if (!range) {
+ range = editor.find({
+ needle: /[{}()\[\]]/g,
+ preventScroll:true,
+ start: {row: cursor.row, column: cursor.column - 1}
+ });
+ if (!range)
+ return;
+ var pos = range.start;
+ if (pos.row == cursor.row && Math.abs(pos.column - cursor.column) < 2)
+ range = this.session.getBracketRange(pos);
+ }
+
+ pos = range && range.cursor || pos;
if (pos) {
- this.clearSelection();
- this.moveCursorTo(pos.row, pos.column);
+ if (select) {
+ if (range && range.isEqual(editor.getSelectionRange()))
+ this.clearSelection();
+ else
+ this.selection.selectTo(pos.row, pos.column);
+ } else {
+ this.clearSelection();
+ this.moveCursorTo(pos.row, pos.column);
+ }
}
};
@@ -246,7 +246,6 @@ var TextInput = function(parentNode, host) {
});
this.focus = function() {
- host.onFocus();
reset();
text.focus();
};
@@ -281,10 +280,12 @@ var TextInput = function(parentNode, host) {
if (host.renderer.$keepTextAreaAtCursor)
host.renderer.$keepTextAreaAtCursor = null;
- event.capture(host.container, function(e) {
- text.style.left = e.clientX - 2 + "px";
- text.style.top = e.clientY - 2 + "px";
- }, onContextMenuClose);
+ // on windows context menu is opened after mouseup
+ if (useragent.isGecko && useragent.isWin)
+ event.capture(host.container, function(e) {
+ text.style.left = e.clientX - 2 + "px";
+ text.style.top = e.clientY - 2 + "px";
+ }, onContextMenuClose);
};
function onContextMenuClose() {
@@ -300,27 +300,27 @@ module.exports = {
case "(":
case "{":
case "[":
- var cursor = editor.getCursorPosition()
- var end = editor.session.$findClosingBracket(param, cursor, /paren/)
+ var cursor = editor.getCursorPosition();
+ var end = editor.session.$findClosingBracket(param, cursor, /paren/);
if (!end)
return;
- var start = editor.session.$findOpeningBracket(editor.session.$brackets[param], cursor, /paren/)
+ var start = editor.session.$findOpeningBracket(editor.session.$brackets[param], cursor, /paren/);
if (!start)
return;
start.column ++;
- editor.selection.setSelectionRange(Range.fromPoints(start, end))
- break
+ editor.selection.setSelectionRange(Range.fromPoints(start, end));
+ break;
case "'":
- case "\"":
+ case '"':
case "/":
- var end = find(editor, param, 1)
+ var end = find(editor, param, 1);
if (!end)
return;
- var start = find(editor, param, -1)
+ var start = find(editor, param, -1);
if (!start)
return;
- editor.selection.setSelectionRange(Range.fromPoints(start.end, end.start))
- break
+ editor.selection.setSelectionRange(Range.fromPoints(start.end, end.start));
+ break;
}
}
},
View
@@ -93,7 +93,7 @@ exports.preventDefault = function(e) {
exports.getButton = function(e) {
if (e.type == "dblclick")
return 0;
- else if (e.type == "contextmenu")
+ if (e.type == "contextmenu" || (e.ctrlKey && useragent.isMac))
return 2;
// DOM Event
@@ -80,7 +80,7 @@ var MarkdownHighlightRules = function() {
return "markup.heading." + value.length;
},
regex : "^#{1,6}"
- }, github_embed("javascript", "js-"),
+ }, github_embed("(?:javascript|js)", "js-"),
github_embed("xml", "xml-"),
github_embed("html", "html-"),
github_embed("css", "css-"),
@@ -90,7 +90,7 @@ function DefaultHandlers(mouseHandler) {
// 2: contextmenu, 1: linux paste
editor.textInput.onContextMenu(ev.domEvent);
- return ev.stop();
+ return; // stopping event here breaks contextmenu on ff mac
}
// if this click caused the editor to be focused should not clear the
@@ -109,13 +109,8 @@ function DefaultHandlers(mouseHandler) {
// a selection.
this.startSelect(pos);
} else if (inSelection) {
- var e = ev.domEvent;
- if ((e.ctrlKey || e.altKey)) {
- this.startDrag();
- } else {
- this.mousedownEvent.time = (new Date()).getTime();
- this.setState("dragWait");
- }
+ this.mousedownEvent.time = (new Date()).getTime();
+ this.setState("dragWait");
}
this.captureMouse(ev);
@@ -146,8 +141,9 @@ function DefaultHandlers(mouseHandler) {
} else if (cmp == 1) {
anchor = this.$clickSelection.start;
} else {
- cursor = this.$clickSelection.end;
- anchor = this.$clickSelection.start;
+ var orientedRange = calcRangeOrientation(this.$clickSelection, cursor);
+ cursor = orientedRange.cursor;
+ anchor = orientedRange.anchor;
}
editor.selection.setSelectionAnchor(anchor.row, anchor.column);
}
@@ -175,8 +171,9 @@ function DefaultHandlers(mouseHandler) {
cursor = range.end;
anchor = range.start;
} else {
- cursor = this.$clickSelection.end;
- anchor = this.$clickSelection.start;
+ var orientedRange = calcRangeOrientation(this.$clickSelection, cursor);
+ cursor = orientedRange.cursor;
+ anchor = orientedRange.anchor;
}
editor.selection.setSelectionAnchor(anchor.row, anchor.column);
}
@@ -221,15 +218,15 @@ function DefaultHandlers(mouseHandler) {
this.startSelect();
};
- this.dragWait = function() {
+ this.dragWait = function(e) {
var distance = calcDistance(this.mousedownEvent.x, this.mousedownEvent.y, this.x, this.y);
var time = (new Date()).getTime();
var editor = this.editor;
if (distance > DRAG_OFFSET) {
this.startSelect();
} else if (time - this.mousedownEvent.time > editor.getDragDelay()) {
- this.startDrag()
+ this.startDrag();
}
};
@@ -277,12 +274,21 @@ function DefaultHandlers(mouseHandler) {
this.onDoubleClick = function(ev) {
var pos = ev.getDocumentPosition();
var editor = this.editor;
+ var session = editor.session
+ var range = session.getBracketRange(pos);
+ if (range) {
+ if (range.isEmpty()) {
+ range.start.column--;
+ range.end.column++;
+ }
+ this.$clickSelection = range;
+ this.setState("select");
+ return;
+ }
+
+ this.$clickSelection = editor.selection.getWordRange(pos.row, pos.column);
this.setState("selectByWords");
-
- editor.moveCursorToPosition(pos);
- editor.selection.selectWord();
- this.$clickSelection = editor.getSelectionRange();
};
this.onTripleClick = function(ev) {
@@ -334,4 +340,16 @@ function calcDistance(ax, ay, bx, by) {
return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2));
}
+function calcRangeOrientation(range, cursor) {
+ if (range.start.row == range.end.row)
+ var cmp = 2 * cursor.column - range.start.column - range.end.column;
+ else
+ var cmp = 2 * cursor.row - range.start.row - range.end.row;
+
+ if (cmp < 0)
+ return {cursor: range.start, anchor: range.end};
+ else
+ return {cursor: range.end, anchor: range.start};
+}
+
});
Oops, something went wrong.

0 comments on commit df0550b

Please sign in to comment.