Bug 687160 - Source Editor should provide a line-based API; r=rcampbell

This commit is contained in:
Mihai Sucan 2011-12-09 19:10:15 +02:00
parent 0b6eb6dec2
commit 7cb9ba6af7
4 changed files with 156 additions and 0 deletions

View File

@ -674,6 +674,47 @@ SourceEditor.prototype = {
this._view.setCaretOffset(aOffset, true);
},
/**
* Get the caret position.
*
* @return object
* An object that holds two properties:
* - line: the line number, counting from 0.
* - col: the column number, counting from 0.
*/
getCaretPosition: function SE_getCaretPosition()
{
let offset = this.getCaretOffset();
let line = this._model.getLineAtOffset(offset);
let lineStart = this._model.getLineStart(line);
let column = offset - lineStart;
return {line: line, col: column};
},
/**
* Set the caret position: line and column.
*
* @param number aLine
* The new caret line location. Line numbers start from 0.
* @param number [aColumn=0]
* Optional. The new caret column location. Columns start from 0.
*/
setCaretPosition: function SE_setCaretPosition(aLine, aColumn)
{
this.setCaretOffset(this._model.getLineStart(aLine) + (aColumn || 0));
},
/**
* Get the line count.
*
* @return number
* The number of lines in the document being edited.
*/
getLineCount: function SE_getLineCount()
{
return this._model.getLineCount();
},
/**
* Get the line delimiter used in the document being edited.
*

View File

@ -594,6 +594,41 @@ SourceEditor.prototype = {
this.setSelection(aOffset, aOffset);
},
/**
* Set the caret position: line and column.
*
* @param number aLine
* The new caret line location. Line numbers start from 0.
* @param number [aColumn=0]
* Optional. The new caret column location. Columns start from 0.
*/
setCaretPosition: function SE_setCaretPosition(aLine, aColumn)
{
aColumn = aColumn || 0;
let text = this._textbox.value;
let i = 0, n = text.length, c0, c1;
let line = 0, col = 0;
while (i < n) {
c1 = text.charAt(i++);
if (line < aLine && (c1 == "\r" || (c0 != "\r" && c1 == "\n"))) {
// Count lines and reset the column only until we reach the desired line
// such that if the desired column is out of boundaries we will stop
// after the given number of characters from the line start.
line++;
col = 0;
} else {
col++;
}
if (line == aLine && col == aColumn) {
this.setCaretOffset(i);
return;
}
c0 = c1;
}
},
/**
* Get the line delimiter used in the document being edited.
*

View File

@ -51,6 +51,7 @@ _BROWSER_TEST_FILES = \
browser_bug687568_pagescroll.js \
browser_bug687580_drag_and_drop.js \
browser_bug695035_middle_click_paste.js \
browser_bug687160_line_api.js \
head.js \
libs:: $(_BROWSER_TEST_FILES)

View File

@ -0,0 +1,79 @@
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
Cu.import("resource:///modules/source-editor.jsm");
let testWin;
let editor;
function test()
{
waitForExplicitFinish();
const windowUrl = "data:text/xml,<?xml version='1.0'?>" +
"<window xmlns='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'" +
" title='test for bug 660784' width='600' height='500'><hbox flex='1'/></window>";
const windowFeatures = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
testWin = Services.ww.openWindow(null, windowUrl, "_blank", windowFeatures, null);
testWin.addEventListener("load", function onWindowLoad() {
testWin.removeEventListener("load", onWindowLoad, false);
waitForFocus(initEditor, testWin);
}, false);
}
function initEditor()
{
let hbox = testWin.document.querySelector("hbox");
editor = new SourceEditor();
editor.init(hbox, {}, editorLoaded);
}
function editorLoaded()
{
let component = Services.prefs.getCharPref(SourceEditor.PREFS.COMPONENT);
editor.focus();
editor.setText("line1\nline2\nline3");
if (component != "textarea") {
is(editor.getLineCount(), 3, "getLineCount() works");
}
editor.setCaretPosition(1);
is(editor.getCaretOffset(), 6, "setCaretPosition(line) works");
let pos;
if (component != "textarea") {
pos = editor.getCaretPosition();
ok(pos.line == 1 && pos.col == 0, "getCaretPosition() works");
}
editor.setCaretPosition(1, 3);
is(editor.getCaretOffset(), 9, "setCaretPosition(line, column) works");
if (component != "textarea") {
pos = editor.getCaretPosition();
ok(pos.line == 1 && pos.col == 3, "getCaretPosition() works");
}
editor.setCaretPosition(2);
is(editor.getCaretOffset(), 12, "setCaretLine() works, confirmed");
if (component != "textarea") {
pos = editor.getCaretPosition();
ok(pos.line == 2 && pos.col == 0, "setCaretPosition(line) works, again");
}
editor.destroy();
testWin.close();
testWin = editor = null;
waitForFocus(finish, window);
}