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

@ -1350,22 +1350,22 @@ var Editor = function(renderer, session) {
this.duplicateSelection = function() { this.duplicateSelection = function() {
var sel = this.selection; var sel = this.selection;
var doc = this.session; var doc = this.session;
var range = sel.getRange(); var range = sel.getRange();
if (range.isEmpty()) { if (range.isEmpty()) {
var row = range.start.row; var row = range.start.row;
doc.duplicateLines(row, row); doc.duplicateLines(row, row);
} else { } else {
var reverse = sel.isBackwards() var reverse = sel.isBackwards()
var point = sel.isBackwards() ? range.start : range.end; var point = sel.isBackwards() ? range.start : range.end;
var endPoint = doc.insert(point, doc.getTextRange(range), false); var endPoint = doc.insert(point, doc.getTextRange(range), false);
range.start = point; range.start = point;
range.end = endPoint; range.end = endPoint;
sel.setSelectionRange(range, reverse) sel.setSelectionRange(range, reverse)
} }
}; };
/** related to: EditSession.moveLinesDown /** related to: EditSession.moveLinesDown
* Editor.moveLinesDown() -> Number * Editor.moveLinesDown() -> Number
* + (Number): On success, it returns -1. * + (Number): On success, it returns -1.
@ -1767,21 +1767,34 @@ 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) {
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) { if (pos) {
this.clearSelection(); if (select) {
this.moveCursorTo(pos.row, pos.column); 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);
}
} }
}; };