select block in braces by double clicking on the brace

This commit is contained in:
nightwing 2012-06-07 18:23:37 +04:00
commit ef489ae8e3
2 changed files with 69 additions and 11 deletions

View file

@ -40,6 +40,7 @@ define(function(require, exports, module) {
"use strict"; "use strict";
var TokenIterator = require("../token_iterator").TokenIterator; var TokenIterator = require("../token_iterator").TokenIterator;
var Range = require("../range").Range;
/** /**
* class BracketMatch * class BracketMatch
@ -76,15 +77,49 @@ function BracketMatch() {
if (charBeforeCursor == "") return null; if (charBeforeCursor == "") return null;
var match = charBeforeCursor.match(/([\(\[\{])|([\)\]\}])/); var match = charBeforeCursor.match(/([\(\[\{])|([\)\]\}])/);
if (!match) { if (!match)
return null; 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]) { 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++;
} else { } 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--;
}
} }
return range;
}; };
this.$brackets = { this.$brackets = {

View file

@ -146,8 +146,9 @@ function DefaultHandlers(mouseHandler) {
} else if (cmp == 1) { } else if (cmp == 1) {
anchor = this.$clickSelection.start; anchor = this.$clickSelection.start;
} else { } else {
cursor = this.$clickSelection.end; var orientedRange = calcRangeOrientation(this.$clickSelection, cursor);
anchor = this.$clickSelection.start; cursor = orientedRange.cursor;
anchor = orientedRange.anchor;
} }
editor.selection.setSelectionAnchor(anchor.row, anchor.column); editor.selection.setSelectionAnchor(anchor.row, anchor.column);
} }
@ -175,8 +176,9 @@ function DefaultHandlers(mouseHandler) {
cursor = range.end; cursor = range.end;
anchor = range.start; anchor = range.start;
} else { } else {
cursor = this.$clickSelection.end; var orientedRange = calcRangeOrientation(this.$clickSelection, cursor);
anchor = this.$clickSelection.start; cursor = orientedRange.cursor;
anchor = orientedRange.anchor;
} }
editor.selection.setSelectionAnchor(anchor.row, anchor.column); editor.selection.setSelectionAnchor(anchor.row, anchor.column);
} }
@ -277,12 +279,21 @@ function DefaultHandlers(mouseHandler) {
this.onDoubleClick = function(ev) { this.onDoubleClick = function(ev) {
var pos = ev.getDocumentPosition(); var pos = ev.getDocumentPosition();
var editor = this.editor; 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"); this.setState("selectByWords");
editor.moveCursorToPosition(pos);
editor.selection.selectWord();
this.$clickSelection = editor.getSelectionRange();
}; };
this.onTripleClick = function(ev) { this.onTripleClick = function(ev) {
@ -334,4 +345,16 @@ function calcDistance(ax, ay, bx, by) {
return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2)); 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};
}
}); });