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
}, {
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

View file

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

View file

@ -1350,22 +1350,22 @@ var Editor = function(renderer, session) {
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)
}
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.
@ -1767,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);
}
}
};