improve jumptomatching

This commit is contained in:
nightwing 2012-06-08 00:35:29 +04:00
commit 689bed38ff
3 changed files with 55 additions and 30 deletions

View file

@ -295,10 +295,15 @@ exports.commands = [{
readOnly: true readOnly: true
}, { }, {
name: "jumptomatching", name: "jumptomatching",
bindKey: bindKey("Ctrl-Shift-P", "Ctrl-Shift-P"), bindKey: bindKey("Ctrl-P", "Ctrl-P"),
exec: function(editor) { editor.jumpToMatching(); }, exec: function(editor) { editor.jumpToMatching(); },
multiSelectAction: "forEach", multiSelectAction: "forEach",
readOnly: true 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 // commands disabled in readOnly mode

View file

@ -94,7 +94,7 @@ function BracketMatch() {
var match = chr && chr.match(/([\(\[\{])|([\)\]\}])/); var match = chr && chr.match(/([\(\[\{])|([\)\]\}])/);
if (!match) { if (!match) {
chr = line.charAt(pos.column); chr = line.charAt(pos.column);
pos.column++ pos.column++;
match = chr && chr.match(/([\(\[\{])|([\)\]\}])/); match = chr && chr.match(/([\(\[\{])|([\)\]\}])/);
before = false; before = false;
} }
@ -106,8 +106,11 @@ function BracketMatch() {
if (!bracketPos) if (!bracketPos)
return null; return null;
range = Range.fromPoints(pos, bracketPos); range = Range.fromPoints(pos, bracketPos);
if (!before) if (!before) {
range.end.column++; range.end.column++;
range.start.column--;
}
range.cursor = range.end;
} else { } else {
var bracketPos = this.$findOpeningBracket(match[2], pos); var bracketPos = this.$findOpeningBracket(match[2], pos);
if (!bracketPos) if (!bracketPos)
@ -117,8 +120,12 @@ function BracketMatch() {
range.start.column++; range.start.column++;
range.end.column--; range.end.column--;
} }
range.cursor = range.start;
} }
if (!before)
pos.column--;
return range; return range;
}; };

View file

@ -1767,22 +1767,35 @@ var Editor = function(renderer, session) {
* Moves the cursor's row and column to the next matching bracket. * Moves the cursor's row and column to the next matching bracket.
* *
**/ **/
this.jumpToMatching = function() { this.jumpToMatching = function(select) {
var cursor = this.getCursorPosition(); var cursor = this.getCursorPosition();
var pos = this.session.findMatchingBracket(cursor);
if (!pos) { var range = this.session.getBracketRange(cursor);
cursor.column += 1; if (!range) {
pos = this.session.findMatchingBracket(cursor); range = editor.find({
} needle: /[{}()\[\]]/g,
if (!pos) { preventScroll:true,
cursor.column -= 2; start: {row: cursor.row, column: cursor.column - 1}
pos = this.session.findMatchingBracket(cursor); });
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) { if (pos) {
if (select) {
if (range && range.isEqual(editor.getSelectionRange()))
this.clearSelection();
else
this.selection.selectTo(pos.row, pos.column);
} else {
this.clearSelection(); this.clearSelection();
this.moveCursorTo(pos.row, pos.column); this.moveCursorTo(pos.row, pos.column);
} }
}
}; };
/** /**