rename selection.selectionLead to selection.lead

This commit is contained in:
nightwing 2012-04-17 19:06:30 +04:00
commit c5bc3d96e0

View file

@ -64,11 +64,11 @@ var Selection = function(session) {
this.doc = session.getDocument(); this.doc = session.getDocument();
this.clearSelection(); this.clearSelection();
this.selectionLead = this.doc.createAnchor(0, 0); this.lead = this.selectionLead = this.doc.createAnchor(0, 0);
this.selectionAnchor = this.doc.createAnchor(0, 0); this.anchor = this.selectionAnchor = this.doc.createAnchor(0, 0);
var self = this; var self = this;
this.selectionLead.on("change", function(e) { this.lead.on("change", function(e) {
self._emit("changeCursor"); self._emit("changeCursor");
if (!self.$isEmpty) if (!self.$isEmpty)
self._emit("changeSelection"); self._emit("changeSelection");
@ -93,8 +93,8 @@ var Selection = function(session) {
**/ **/
this.isEmpty = function() { this.isEmpty = function() {
return (this.$isEmpty || ( return (this.$isEmpty || (
this.selectionAnchor.row == this.selectionLead.row && this.anchor.row == this.lead.row &&
this.selectionAnchor.column == this.selectionLead.column this.anchor.column == this.lead.column
)); ));
}; };
@ -117,7 +117,7 @@ var Selection = function(session) {
* Gets the current position of the cursor. * Gets the current position of the cursor.
**/ **/
this.getCursor = function() { this.getCursor = function() {
return this.selectionLead.getPosition(); return this.lead.getPosition();
}; };
/** /**
@ -128,7 +128,7 @@ var Selection = function(session) {
* Sets the row and column position of the anchor. This function also emits the `'changeSelection'` event. * Sets the row and column position of the anchor. This function also emits the `'changeSelection'` event.
**/ **/
this.setSelectionAnchor = function(row, column) { this.setSelectionAnchor = function(row, column) {
this.selectionAnchor.setPosition(row, column); this.anchor.setPosition(row, column);
if (this.$isEmpty) { if (this.$isEmpty) {
this.$isEmpty = false; this.$isEmpty = false;
@ -146,7 +146,7 @@ var Selection = function(session) {
if (this.$isEmpty) if (this.$isEmpty)
return this.getSelectionLead() return this.getSelectionLead()
else else
return this.selectionAnchor.getPosition(); return this.anchor.getPosition();
}; };
/** /**
@ -155,7 +155,7 @@ var Selection = function(session) {
* Returns an object containing the `row` and `column` of the calling selection lead. * Returns an object containing the `row` and `column` of the calling selection lead.
**/ **/
this.getSelectionLead = function() { this.getSelectionLead = function() {
return this.selectionLead.getPosition(); return this.lead.getPosition();
}; };
/** /**
@ -167,7 +167,7 @@ var Selection = function(session) {
**/ **/
this.shiftSelection = function(columns) { this.shiftSelection = function(columns) {
if (this.$isEmpty) { if (this.$isEmpty) {
this.moveCursorTo(this.selectionLead.row, this.selectionLead.column + columns); this.moveCursorTo(this.lead.row, this.lead.column + columns);
return; return;
}; };
@ -192,8 +192,8 @@ var Selection = function(session) {
* Returns `true` if the selection is going backwards in the document. * Returns `true` if the selection is going backwards in the document.
**/ **/
this.isBackwards = function() { this.isBackwards = function() {
var anchor = this.selectionAnchor; var anchor = this.anchor;
var lead = this.selectionLead; var lead = this.lead;
return (anchor.row > lead.row || (anchor.row == lead.row && anchor.column > lead.column)); return (anchor.row > lead.row || (anchor.row == lead.row && anchor.column > lead.column));
}; };
@ -203,8 +203,8 @@ var Selection = function(session) {
* [Returns the [[Range `Range`]] for the selected text.]{: #Selection.getRange} * [Returns the [[Range `Range`]] for the selected text.]{: #Selection.getRange}
**/ **/
this.getRange = function() { this.getRange = function() {
var anchor = this.selectionAnchor; var anchor = this.anchor;
var lead = this.selectionLead; var lead = this.lead;
if (this.isEmpty()) if (this.isEmpty())
return Range.fromPoints(lead, lead); return Range.fromPoints(lead, lead);
@ -249,18 +249,23 @@ var Selection = function(session) {
* *
**/ **/
this.setSelectionRange = function(range, reverse) { this.setSelectionRange = function(range, reverse) {
if (reverse) { if (range.isEmpty()) {
this.setSelectionAnchor(range.end.row, range.end.column); this.lead.setPosition(range.start.row, range.start.column);
this.selectTo(range.start.row, range.start.column); this.clearSelection();
} else if (reverse) {
this.$isEmpty = false;
this.anchor.setPosition(range.end.row, range.end.column);
this.lead.setPosition(range.start.row, range.start.column);
} else { } else {
this.setSelectionAnchor(range.start.row, range.start.column); this.$isEmpty = false;
this.selectTo(range.end.row, range.end.column); this.anchor.setPosition(range.start.row, range.start.column);
this.lead.setPosition(range.end.row, range.end.column);
} }
this.$desiredColumn = null; this.$desiredColumn = null;
}; };
this.$moveSelection = function(mover) { this.$moveSelection = function(mover) {
var lead = this.selectionLead; var lead = this.lead;
if (this.$isEmpty) if (this.$isEmpty)
this.setSelectionAnchor(lead.row, lead.column); this.setSelectionAnchor(lead.row, lead.column);
@ -391,7 +396,7 @@ var Selection = function(session) {
**/ **/
this.getWordRange = function(row, column) { this.getWordRange = function(row, column) {
if (typeof column == "undefined") { if (typeof column == "undefined") {
var cursor = row || this.selectionLead; var cursor = row || this.lead;
row = cursor.row; row = cursor.row;
column = cursor.column; column = cursor.column;
} }
@ -414,7 +419,7 @@ var Selection = function(session) {
}; };
this.getLineRange = function(row, excludeLastChar) { this.getLineRange = function(row, excludeLastChar) {
var rowStart = typeof row == "number" ? row : this.selectionLead.row; var rowStart = typeof row == "number" ? row : this.lead.row;
var rowEnd; var rowEnd;
var foldLine = this.session.getFoldLine(rowStart); var foldLine = this.session.getFoldLine(rowStart);
@ -463,7 +468,7 @@ var Selection = function(session) {
* Moves the cursor left one column. * Moves the cursor left one column.
**/ **/
this.moveCursorLeft = function() { this.moveCursorLeft = function() {
var cursor = this.selectionLead.getPosition(), var cursor = this.lead.getPosition(),
fold; fold;
if (fold = this.session.getFoldAt(cursor.row, cursor.column, -1)) { if (fold = this.session.getFoldAt(cursor.row, cursor.column, -1)) {
@ -489,19 +494,19 @@ var Selection = function(session) {
* Moves the cursor right one column. * Moves the cursor right one column.
**/ **/
this.moveCursorRight = function() { this.moveCursorRight = function() {
var cursor = this.selectionLead.getPosition(), var cursor = this.lead.getPosition(),
fold; fold;
if (fold = this.session.getFoldAt(cursor.row, cursor.column, 1)) { if (fold = this.session.getFoldAt(cursor.row, cursor.column, 1)) {
this.moveCursorTo(fold.end.row, fold.end.column); this.moveCursorTo(fold.end.row, fold.end.column);
} }
else if (this.selectionLead.column == this.doc.getLine(this.selectionLead.row).length) { else if (this.lead.column == this.doc.getLine(this.lead.row).length) {
if (this.selectionLead.row < this.doc.getLength() - 1) { if (this.lead.row < this.doc.getLength() - 1) {
this.moveCursorTo(this.selectionLead.row + 1, 0); this.moveCursorTo(this.lead.row + 1, 0);
} }
} }
else { else {
var tabSize = this.session.getTabSize(); var tabSize = this.session.getTabSize();
var cursor = this.selectionLead; var cursor = this.lead;
if (this.session.isTabStop(cursor) && this.doc.getLine(cursor.row).slice(cursor.column, cursor.column+tabSize).split(" ").length-1 == tabSize) if (this.session.isTabStop(cursor) && this.doc.getLine(cursor.row).slice(cursor.column, cursor.column+tabSize).split(" ").length-1 == tabSize)
this.moveCursorBy(0, tabSize); this.moveCursorBy(0, tabSize);
else else
@ -515,8 +520,8 @@ var Selection = function(session) {
* Moves the cursor to the start of the line. * Moves the cursor to the start of the line.
**/ **/
this.moveCursorLineStart = function() { this.moveCursorLineStart = function() {
var row = this.selectionLead.row; var row = this.lead.row;
var column = this.selectionLead.column; var column = this.lead.column;
var screenRow = this.session.documentToScreenRow(row, column); var screenRow = this.session.documentToScreenRow(row, column);
// Determ the doc-position of the first character at the screen line. // Determ the doc-position of the first character at the screen line.
@ -548,7 +553,7 @@ var Selection = function(session) {
* Moves the cursor to the end of the line. * Moves the cursor to the end of the line.
**/ **/
this.moveCursorLineEnd = function() { this.moveCursorLineEnd = function() {
var lead = this.selectionLead; var lead = this.lead;
var lastRowColumnPosition = var lastRowColumnPosition =
this.session.getDocumentLastRowColumnPosition(lead.row, lead.column); this.session.getDocumentLastRowColumnPosition(lead.row, lead.column);
this.moveCursorTo( this.moveCursorTo(
@ -583,8 +588,8 @@ var Selection = function(session) {
* Moves the cursor to the word on the right. * Moves the cursor to the word on the right.
**/ **/
this.moveCursorLongWordRight = function() { this.moveCursorLongWordRight = function() {
var row = this.selectionLead.row; var row = this.lead.row;
var column = this.selectionLead.column; var column = this.lead.column;
var line = this.doc.getLine(row); var line = this.doc.getLine(row);
var rightOfCursor = line.substring(column); var rightOfCursor = line.substring(column);
@ -630,8 +635,8 @@ var Selection = function(session) {
* Moves the cursor to the word on the left. * Moves the cursor to the word on the left.
**/ **/
this.moveCursorLongWordLeft = function() { this.moveCursorLongWordLeft = function() {
var row = this.selectionLead.row; var row = this.lead.row;
var column = this.selectionLead.column; var column = this.lead.column;
// skip folds // skip folds
var fold; var fold;
@ -712,8 +717,8 @@ var Selection = function(session) {
}; };
this.moveCursorShortWordRight = function() { this.moveCursorShortWordRight = function() {
var row = this.selectionLead.row; var row = this.lead.row;
var column = this.selectionLead.column; var column = this.lead.column;
var line = this.doc.getLine(row); var line = this.doc.getLine(row);
var rightOfCursor = line.substring(column); var rightOfCursor = line.substring(column);
@ -730,8 +735,8 @@ var Selection = function(session) {
}; };
this.moveCursorShortWordLeft = function() { this.moveCursorShortWordLeft = function() {
var row = this.selectionLead.row; var row = this.lead.row;
var column = this.selectionLead.column; var column = this.lead.column;
var fold; var fold;
if (fold = this.session.getFoldAt(row, column, -1)) if (fold = this.session.getFoldAt(row, column, -1))
@ -770,8 +775,8 @@ var Selection = function(session) {
**/ **/
this.moveCursorBy = function(rows, chars) { this.moveCursorBy = function(rows, chars) {
var screenPos = this.session.documentToScreenPosition( var screenPos = this.session.documentToScreenPosition(
this.selectionLead.row, this.lead.row,
this.selectionLead.column this.lead.column
); );
if (chars === 0) { if (chars === 0) {
@ -814,7 +819,7 @@ var Selection = function(session) {
} }
this.$keepDesiredColumnOnChange = true; this.$keepDesiredColumnOnChange = true;
this.selectionLead.setPosition(row, column); this.lead.setPosition(row, column);
this.$keepDesiredColumnOnChange = false; this.$keepDesiredColumnOnChange = false;
if (!keepDesiredColumn) if (!keepDesiredColumn)
@ -836,8 +841,8 @@ var Selection = function(session) {
// remove listeners from document // remove listeners from document
this.detach = function() { this.detach = function() {
this.selectionLead.detach(); this.lead.detach();
this.selectionAnchor.detach(); this.anchor.detach();
this.session = this.doc = null; this.session = this.doc = null;
} }