From 443b712b8f6cbe54758d5459ff790088bf6a9d77 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Mon, 12 Apr 2010 13:10:47 +0200 Subject: [PATCH] make jslint happy --- experiments/worker.js | 4 +- src/BackgroundTokenizer.js | 2 +- src/CursorLayer.js | 4 +- src/Editor.js | 1264 ++++++++++++++++++------------------ src/GutterLayer.js | 2 +- src/JavaScript.js | 206 +++--- src/MarkerLayer.js | 2 +- src/TextDocument.js | 41 +- src/TextInput.js | 18 +- src/TextLayer.js | 2 +- src/Tokenizer.js | 6 +- src/VirtualRenderer.js | 12 +- src/lib.js | 296 ++++----- test/MockRenderer.js | 26 +- test/NavigationTest.js | 32 + 15 files changed, 968 insertions(+), 949 deletions(-) diff --git a/experiments/worker.js b/experiments/worker.js index 312a6f98..1335f55e 100644 --- a/experiments/worker.js +++ b/experiments/worker.js @@ -1,3 +1,3 @@ onmessage = function(e) { - onmessage = new Function("e", e.data); -} \ No newline at end of file + onmessage = new Function("e", e.data); +}; \ No newline at end of file diff --git a/src/BackgroundTokenizer.js b/src/BackgroundTokenizer.js index d69b420f..6b441601 100644 --- a/src/BackgroundTokenizer.js +++ b/src/BackgroundTokenizer.js @@ -45,7 +45,7 @@ ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) { self.onUpdate(startLine, textLines.length - 1); self.onComplete(); - } + }; }; ace.BackgroundTokenizer.prototype.setLines = function(textLines) { diff --git a/src/CursorLayer.js b/src/CursorLayer.js index 74861421..76ef0640 100644 --- a/src/CursorLayer.js +++ b/src/CursorLayer.js @@ -10,7 +10,7 @@ ace.CursorLayer = function(parentEl) { this.cursor.className = "cursor"; this.isVisible = false; -} +}; ace.CursorLayer.prototype.setCursor = function(position) { this.position = { @@ -47,7 +47,7 @@ ace.CursorLayer.prototype.getPixelPosition = function() { left : 0, top : 0 }; -} +}; ace.CursorLayer.prototype.update = function(config) { if (!this.position) diff --git a/src/Editor.js b/src/Editor.js index 62cb00e7..b3ecfe59 100644 --- a/src/Editor.js +++ b/src/Editor.js @@ -3,684 +3,698 @@ if (!window.ace) (function() { -var keys = { - UP : 38, - RIGHT : 39, - DOWN : 40, - LEFT : 37, - PAGEUP : 33, - PAGEDOWN : 34, - POS1 : 36, - END : 35, - DELETE : 46, - BACKSPACE : 8, - TAB : 9, - A : 65 -} + var keys = { + UP : 38, + RIGHT : 39, + DOWN : 40, + LEFT : 37, + PAGEUP : 33, + PAGEDOWN : 34, + POS1 : 36, + END : 35, + DELETE : 46, + BACKSPACE : 8, + TAB : 9, + A : 65 + }; -var KeyBinding = function(element, host) { - ace.addListener(element, "keydown", function(e) { - var key = e.keyCode; + var KeyBinding = function(element, host) { + ace.addListener(element, "keydown", function(e) { + var key = e.keyCode; - switch (key) { - case keys.A: - if (e.metaKey) { - host.selectAll(); + switch (key) { + case keys.A: + if (e.metaKey) { + host.selectAll(); + return ace.stopEvent(e); + } + break; + + case keys.UP: + if (e.metaKey && e.shiftKey) { + host.selectFileStart(); + } + else if (e.metaKey) { + host.navigateFileStart(); + } + if (e.shiftKey) { + host.selectUp(); + } + else { + host.navigateUp(); + } return ace.stopEvent(e); - } - break; - case keys.UP: - if (e.metaKey) { - host.navigateFileStart(); - } - if (e.shiftKey) { - host.selectUp(); - } - else { - host.navigateUp(); - } - return ace.stopEvent(e); + case keys.DOWN: + if (e.metaKey && e.shiftKey) { + host.selectFileEnd(); + } + else if (e.metaKey) { + host.navigateFileEnd(); + } + if (e.shiftKey) { + host.selectDown(); + } + else { + host.navigateDown(); + } + return ace.stopEvent(e); - case keys.DOWN: - if (e.metaKey) { - host.navigateFileEnd(); - } - if (e.shiftKey) { - host.selectDown(); - } - else { - host.navigateDown(); - } - return ace.stopEvent(e); + case keys.LEFT: + if (e.metaKey && e.shiftKey) { + host.selectLineStart(); + } + else if (e.metaKey) { + host.navigateLineStart(); + } + else if (e.shiftKey) { + host.selectLeft(); + } + else { + host.navigateLeft(); + } + return ace.stopEvent(e); - case keys.LEFT: - if (e.metaKey && e.shiftKey) { - host.selectLineStart(); - } - else if (e.metaKey) { - host.navigateLineStart(); - } - else if (e.shiftKey) { - host.selectLeft(); - } - else { - host.navigateLeft(); - } - return ace.stopEvent(e); + case keys.RIGHT: + if (e.metaKey && e.shiftKey) { + host.selectLineEnd(); + } + else if (e.metaKey) { + host.navigateLineEnd(); + } + else if (e.shiftKey) { + host.selectRight(); + } + else { + host.navigateRight(); + } + return ace.stopEvent(e); - case keys.RIGHT: - if (e.metaKey && e.shiftKey) { - host.selectLineEnd(); - } - else if (e.metaKey) { - host.navigateLineEnd(); - } - else if (e.shiftKey) { - host.selectRight(); - } - else { - host.navigateRight(); - } - return ace.stopEvent(e); + case keys.PAGEDOWN: + if (e.shiftKey) { + host.selectPageDown(); + } + else { + host.scrollPageDown(); + } + return ace.stopEvent(e); - case keys.PAGEDOWN: - if (e.shiftKey) { - host.selectPageDown(); - } - else { - host.scrollPageDown(); - } - return ace.stopEvent(e); + case keys.PAGEUP: + if (e.shiftKey) { + host.selectPageUp(); + } + else { + host.scrollPageUp(); + } + return ace.stopEvent(e); - case keys.PAGEUP: - if (e.shiftKey) { - host.selectPageUp(); - } - else { - host.scrollPageUp(); - } - return ace.stopEvent(e); + case keys.POS1: + if (e.shiftKey) { + host.selectLineStart(); + } + else { + host.navigateLineStart(); + } + return ace.stopEvent(e); - case keys.POS1: - if (e.shiftKey) { - host.selectLineStart(); - } - else { - host.navigateLineStart(); - } - return ace.stopEvent(e); + case keys.END: + if (e.shiftKey) { + host.selectLineEnd(); + } + else { + host.navigateLineEnd(); + } + return ace.stopEvent(e); - case keys.END: - if (e.shiftKey) { - host.selectLineEnd(); - } - else { - host.navigateLineEnd(); - } - return ace.stopEvent(e); + case keys.DELETE: + host.removeRight(); + return ace.stopEvent(e); - case keys.DELETE: - host.removeRight(); - return ace.stopEvent(e); + case keys.BACKSPACE: + host.removeLeft(); + return ace.stopEvent(e); - case keys.BACKSPACE: - host.removeLeft(); - return ace.stopEvent(e); - - case keys.TAB: - host.onTextInput(" "); - return ace.stopEvent(e); - } - }); -}; - -ace.Editor = function(doc, renderer) { - var container = renderer.getContainerElement(); - this.renderer = renderer; - - this.textInput = new ace.TextInput(container, this); - new KeyBinding(container, this); - - ace.addListener(container, "mousedown", ace - .bind(this.onMouseDown, this)); - ace.addListener(container, "dblclick", ace - .bind(this.onMouseDoubleClick, this)); - ace.addMouseWheelListener(container, ace.bind(this.onMouseWheel, this)); - ace.addTripleClickListener(container, ace.bind(this.selectCurrentLine, - this)); - - this.doc = doc; - doc.addChangeListener(ace.bind(this.onDocumentChange, this)); - renderer.setDocument(doc); - - this.tokenizer = new ace.BackgroundTokenizer(new ace.Tokenizer( - ace.XML.RULES), ace.bind(this.onTokenizerUpdate, this)); - - this.tokenizer.setLines(doc.lines); - renderer.setTokenizer(this.tokenizer); - - this.cursor = { - row : 0, - column : 0 - }; - - this.selectionAnchor = null; - this.selectionLead = null; - this.selection = null; - - this.renderer.draw(); -}; - -ace.Editor.prototype.resize = function() { - this.renderer.draw(); -}; - -ace.Editor.prototype.updateCursor = function() { - this.renderer.updateCursor(this.cursor); -}; - -ace.Editor.prototype.onFocus = function() { - this.renderer.showCursor(); - this.renderer.visualizeFocus(); -}; - -ace.Editor.prototype.onBlur = function() { - this.renderer.hideCursor(); - this.renderer.visualizeBlur(); -}; - -ace.Editor.prototype.onDocumentChange = function(startRow, endRow) { - this.tokenizer.start(startRow); - this.renderer.updateLines(startRow, endRow); -}; - -ace.Editor.prototype.onTokenizerUpdate = function(startRow, endRow) { - this.renderer.updateLines(startRow, endRow); -}; - -ace.Editor.prototype.onMouseDown = function(e) { - this.textInput.focus(); - - var pos = this.renderer.screenToTextCoordinates(e.pageX, e.pageY); - this.moveCursorTo(pos.row, pos.column); - this.setSelectionAnchor(pos.row, pos.column); - this.renderer.scrollCursorIntoView(); - - var _self = this; - var mousePageX, mousePageY; - - var onMouseSelection = function(e) { - mousePageX = e.pageX; - mousePageY = e.pageY; - }; - - var onMouseSelectionEnd = function() { - clearInterval(timerId); - }; - - var onSelectionInterval = function() { - if (mousePageX === undefined || mousePageY === undefined) - return; - - selectionLead = _self.renderer.screenToTextCoordinates(mousePageX, - mousePageY); - - _self._moveSelection(function() { - _self.moveCursorTo(selectionLead.row, selectionLead.column); - }); - _self.renderer.scrollCursorIntoView(); - }; - - ace.capture(this.container, onMouseSelection, onMouseSelectionEnd); - var timerId = setInterval(onSelectionInterval, 20); - - return ace.preventDefault(e); -}; - -ace.Editor.prototype.onMouseDoubleClick = function(e) { - var line = this.doc.getLine(this.cursor.row); - var column = this.cursor.column; - - var tokenRe = /[a-zA-Z0-9_]+/g; - var nonTokenRe = /[^a-zA-Z0-9_]+/g; - - var inToken = false; - if (column > 0) { - inToken = !!line.charAt(column - 1).match(tokenRe); - } - - if (!inToken) { - inToken = !!line.charAt(column).match(tokenRe); - } - - var re = inToken ? tokenRe : nonTokenRe; - - var start = column; - if (start > 0) { - do { - start--; - } - while (start >= 0 && line.charAt(start).match(re)) - start++; - } - - var end = column; - while (end < line.length && line.charAt(end).match(re)) { - end++; - } - - this.setSelectionAnchor(this.cursor.row, start); - this._moveSelection(function() { - this.moveCursorTo(this.cursor.row, end); - }); -}; - -ace.Editor.prototype.onMouseWheel = function(e) { - var delta = e.wheel; - this.renderer.scrollToY(this.renderer.getScrollTop() - (delta * 15)); - return ace.preventDefault(e); -}; - -ace.Editor.prototype.getCopyText = function() { - if (this.hasSelection()) { - return this.doc.getTextRange(this.getSelectionRange()); - } - else { - return ""; - } -}; - -ace.Editor.prototype.onCut = function() { - if (this.hasSelection()) { - this.cursor = this.doc.remove(this.getSelectionRange()); - this.clearSelection(); - this.renderer.updateCursor(this.cursor); - } -}; - -ace.Editor.prototype.onTextInput = function(text) { - if (this.hasSelection()) { - this.cursor = this.doc.replace(this.getSelectionRange(), text); - this.clearSelection(); - } - else { - this.cursor = this.doc.insert(this.cursor, text); - } - this.renderer.updateCursor(this.cursor); - this.renderer.scrollCursorIntoView(); -}; - -ace.Editor.prototype.removeRight = function() { - if (this.hasSelection()) { - this.cursor = this.doc.remove(this.getSelectionRange()); - this.renderer.updateCursor(this.cursor); - this.clearSelection(); - } - else { - var rangeEnd = { - row : this.cursor.row, - column : this.cursor.column + 1 - } - if (rangeEnd.column > this.doc.getLine(this.cursor.row).length) { - rangeEnd.row += 1; - rangeEnd.column = 0; - } - this.doc.remove( { - start : this.cursor, - end : renageEnd - }); - } - - this.renderer.scrollCursorIntoView(); -}; - - ace.Editor.prototype.removeLeft = function() { - if (this.hasSelection()) { - this.cursor = this.doc.remove(this.getSelectionRange()); - this.clearSelection(); + case keys.TAB: + host.onTextInput(" "); + return ace.stopEvent(e); } - else { - if (this.cursor.row == 0 && this.cursor.column == 0) { return; } + }); + }; - var rangeStart = { - row : this.cursor.row, - column : this.cursor.column + -1 - } - if (rangeStart.column < 0) { - rangeStart.row -= 1; - rangeStart.column = this.doc - .getLine(this.cursor.row - 1).length; - } - this.cursor = this.doc.remove( { - start : rangeStart, - end : this.cursor - }); - } + ace.Editor = function(doc, renderer) { + var container = renderer.getContainerElement(); + this.renderer = renderer; - this.renderer.updateCursor(this.cursor); - this.renderer.scrollCursorIntoView(); - }, + this.textInput = new ace.TextInput(container, this); + new KeyBinding(container, this); - ace.Editor.prototype.onCompositionStart = function() { - this.renderer.showComposition(this.cursor); - this.onTextInput(" "); + ace.addListener(container, "mousedown", ace + .bind(this.onMouseDown, this)); + ace.addListener(container, "dblclick", ace + .bind(this.onMouseDoubleClick, this)); + ace.addMouseWheelListener(container, ace.bind(this.onMouseWheel, this)); + ace.addTripleClickListener(container, ace.bind(this.selectCurrentLine, + this)); + + this.doc = doc; + doc.addChangeListener(ace.bind(this.onDocumentChange, this)); + renderer.setDocument(doc); + + this.tokenizer = new ace.BackgroundTokenizer(new ace.Tokenizer( + ace.XML.RULES), ace.bind(this.onTokenizerUpdate, this)); + + this.tokenizer.setLines(doc.lines); + renderer.setTokenizer(this.tokenizer); + + this.cursor = { + row : 0, + column : 0 }; -ace.Editor.prototype.onCompositionUpdate = function(text) { - this.renderer.setCompositionText(text); -}; + this.selectionAnchor = null; + this.selectionLead = null; + this.selection = null; -ace.Editor.prototype.onCompositionEnd = function() { - this.renderer.hideComposition(); - this.removeLeft(); -}; - -ace.Editor.prototype.getFirstVisibleRow = function() { - return this.renderer.getFirstVisibleRow(); -}; - -ace.Editor.prototype.getLastVisibleRow = function() { - return this.renderer.getLastVisibleRow(); -}; - -ace.Editor.prototype.getPageDownRow = function() { - return this.renderer.getLastVisibleRow() - 1; -}; - -ace.Editor.prototype.getPageUpRow = function() { - var firstRow = this.renderer.getFirstVisibleRow(); - var lastRow = this.renderer.getLastVisibleRow(); - - return firstRow - (lastRow - firstRow) + 1; -}; - -ace.Editor.prototype.scrollPageDown = function() { - this.scrollToRow(this.getPageDownRow()); -}; - -ace.Editor.prototype.scrollPageUp = function() { - this.renderer.scrollToRow(this.getPageUpRow()); -}; - -ace.Editor.prototype.scrollToRow = function(row) { - this.renderer.scrollToRow(row); -}; - -ace.Editor.prototype.navigateUp = function() { - this.clearSelection(); - this.moveCursorUp(); - this.renderer.scrollCursorIntoView(); -}; - -ace.Editor.prototype.navigateDown = function() { - this.clearSelection(); - this.moveCursorDown(); - this.renderer.scrollCursorIntoView(); -}; - -ace.Editor.prototype.navigateLeft = function() { - if (this.hasSelection()) { - var selectionStart = this.getSelectionRange().start; - this.moveCursorTo(selectionStart.row, selectionStart.column); - } - else { - this.moveCursorLeft(); - } - this.clearSelection(); - - this.renderer.scrollCursorIntoView(); -}; - -ace.Editor.prototype.navigateRight = function() { - if (this.hasSelection()) { - var selectionEnd = this.getSelectionRange().end; - this.moveCursorTo(selectionEnd.row, selectionEnd.column); - } - else { - this.moveCursorRight(); - } - this.clearSelection(); - - this.renderer.scrollCursorIntoView(); -}, - -ace.Editor.prototype.navigateLineStart = function() { - this.clearSelection(); - this.moveCursorLineStart(); - this.renderer.scrollCursorIntoView(); -}; - -ace.Editor.prototype.navigateLineEnd = function() { - this.clearSelection(); - this.moveCursorLineEnd(); - this.renderer.scrollCursorIntoView(); -}; - -ace.Editor.prototype.navigateFileEnd = function() { - this.clearSelection(); - this.moveCursorFileEnd(); - this.renderer.scrollCursorIntoView(); -}, - -ace.Editor.prototype.navigateFileStart = function() { - this.clearSelection(); - this.moveCursorFileStart(); - this.renderer.scrollCursorIntoView(); -}, - -ace.Editor.prototype.moveCursorUp = function() { - this.moveCursorBy(-1, 0); -}; - -ace.Editor.prototype.moveCursorDown = function() { - this.moveCursorBy(1, 0); -}; - -ace.Editor.prototype.moveCursorLeft = function() { - if (this.cursor.column == 0) { - if (this.cursor.row > 0) { - this.moveCursorTo(this.cursor.row - 1, this.doc - .getLine(this.cursor.row - 1).length); - } - } - else { - this.moveCursorBy(0, -1); - } -}; - -ace.Editor.prototype.moveCursorRight = function() { - if (this.cursor.column == this.doc.getLine(this.cursor.row).length) { - if (this.cursor.row < this.doc.getLength() - 1) { - this.moveCursorTo(this.cursor.row + 1, 0); - } - } - else { - this.moveCursorBy(0, 1); - } -}; - -ace.Editor.prototype.moveCursorLineStart = function() { - this.moveCursorTo(this.cursor.row, 0); -}; - -ace.Editor.prototype.moveCursorLineEnd = function() { - this.moveCursorTo(this.cursor.row, - this.doc.getLine(this.cursor.row).length); -}; - -ace.Editor.prototype.moveCursorFileEnd = function() { - var row = this.doc.getLength() - 1; - var column = this.doc.getLine(row).length; - this.moveCursorTo(row, column); -}; - -ace.Editor.prototype.moveCursorFileStart = function() { - this.moveCursorTo(0, 0); -}; - -ace.Editor.prototype.moveCursorBy = function(rows, chars) { - this.moveCursorTo(this.cursor.row + rows, this.cursor.column + chars); -}; - -ace.Editor.prototype.moveCursorTo = function(row, column) { - if (row >= this.doc.getLength()) { - this.cursor.row = this.doc.getLength() - 1; - this.cursor.column = this.doc.getLine(this.cursor.row).length; - } - else if (row < 0) { - this.cursor.row = 0; - this.cursor.column = 0; - } - else { - this.cursor.row = row; - this.cursor.column = Math - .min(this.doc.getLine(this.cursor.row).length, Math - .max(0, column)); - } - this.updateCursor(); -}; - -ace.Editor.prototype.getCursorPosition = function() { - return { - row : this.cursor.row, - column : this.cursor.column - } -}; - -ace.Editor.prototype.hasSelection = function() { - return !!this.selectionLead; -}; - -ace.Editor.prototype.setSelectionAnchor = function(row, column) { - this.clearSelection(); - - this.selectionAnchor = { - row : Math.min(this.doc.getLength() - 1, Math.max(0, row)), - column : Math.min(this.doc.getLine(this.cursor.row).length, Math - .max(0, column)) + this.renderer.draw(); }; - this.selectionLead = null; -}; + ace.Editor.prototype.resize = function() { + this.renderer.draw(); + }; -ace.Editor.prototype.getSelectionRange = function() { - var anchor = this.selectionAnchor; - var lead = this.selectionLead; + ace.Editor.prototype.updateCursor = function() { + this.renderer.updateCursor(this.cursor); + }; - if (!anchor) { - return null; - } - else { - if (anchor.row > lead.row - || (anchor.row == lead.row && anchor.column > lead.column)) { - return { - start : lead, - end : anchor + ace.Editor.prototype.onFocus = function() { + this.renderer.showCursor(); + this.renderer.visualizeFocus(); + }; + + ace.Editor.prototype.onBlur = function() { + this.renderer.hideCursor(); + this.renderer.visualizeBlur(); + }; + + ace.Editor.prototype.onDocumentChange = function(startRow, endRow) { + this.tokenizer.start(startRow); + this.renderer.updateLines(startRow, endRow); + }; + + ace.Editor.prototype.onTokenizerUpdate = function(startRow, endRow) { + this.renderer.updateLines(startRow, endRow); + }; + + ace.Editor.prototype.onMouseDown = function(e) { + this.textInput.focus(); + + var pos = this.renderer.screenToTextCoordinates(e.pageX, e.pageY); + this.moveCursorTo(pos.row, pos.column); + this.setSelectionAnchor(pos.row, pos.column); + this.renderer.scrollCursorIntoView(); + + var _self = this; + var mousePageX, mousePageY; + + var onMouseSelection = function(e) { + mousePageX = e.pageX; + mousePageY = e.pageY; + }; + + var onMouseSelectionEnd = function() { + clearInterval(timerId); + }; + + var onSelectionInterval = function() { + if (mousePageX === undefined || mousePageY === undefined) + return; + + selectionLead = _self.renderer.screenToTextCoordinates(mousePageX, + mousePageY); + + _self._moveSelection(function() { + _self.moveCursorTo(selectionLead.row, selectionLead.column); + }); + _self.renderer.scrollCursorIntoView(); + }; + + ace.capture(this.container, onMouseSelection, onMouseSelectionEnd); + var timerId = setInterval(onSelectionInterval, 20); + + return ace.preventDefault(e); + }; + + ace.Editor.prototype.onMouseDoubleClick = function(e) { + var line = this.doc.getLine(this.cursor.row); + var column = this.cursor.column; + + var tokenRe = /[a-zA-Z0-9_]+/g; + var nonTokenRe = /[^a-zA-Z0-9_]+/g; + + var inToken = false; + if (column > 0) { + inToken = !!line.charAt(column - 1).match(tokenRe); + } + + if (!inToken) { + inToken = !!line.charAt(column).match(tokenRe); + } + + var re = inToken ? tokenRe : nonTokenRe; + + var start = column; + if (start > 0) { + do { + start--; + } + while (start >= 0 && line.charAt(start).match(re)); + start++; + } + + var end = column; + while (end < line.length && line.charAt(end).match(re)) { + end++; + } + + this.setSelectionAnchor(this.cursor.row, start); + this._moveSelection(function() { + this.moveCursorTo(this.cursor.row, end); + }); + }; + + ace.Editor.prototype.onMouseWheel = function(e) { + var delta = e.wheel; + this.renderer.scrollToY(this.renderer.getScrollTop() - (delta * 15)); + return ace.preventDefault(e); + }; + + ace.Editor.prototype.getCopyText = function() { + if (this.hasSelection()) { + return this.doc.getTextRange(this.getSelectionRange()); + } + else { + return ""; + } + }; + + ace.Editor.prototype.onCut = function() { + if (this.hasSelection()) { + this.cursor = this.doc.remove(this.getSelectionRange()); + this.clearSelection(); + this.renderer.updateCursor(this.cursor); + } + }; + + ace.Editor.prototype.onTextInput = function(text) { + if (this.hasSelection()) { + this.cursor = this.doc.replace(this.getSelectionRange(), text); + this.clearSelection(); + } + else { + this.cursor = this.doc.insert(this.cursor, text); + } + this.renderer.updateCursor(this.cursor); + this.renderer.scrollCursorIntoView(); + }; + + ace.Editor.prototype.removeRight = function() { + if (this.hasSelection()) { + this.cursor = this.doc.remove(this.getSelectionRange()); + this.renderer.updateCursor(this.cursor); + this.clearSelection(); + } + else { + var rangeEnd = { + row : this.cursor.row, + column : this.cursor.column + 1 + }; + if (rangeEnd.column > this.doc.getLine(this.cursor.row).length) { + rangeEnd.row += 1; + rangeEnd.column = 0; + } + this.doc.remove( { + start : this.cursor, + end : renageEnd + }); + } + + this.renderer.scrollCursorIntoView(); + }; + + ace.Editor.prototype.removeLeft = function() { + if (this.hasSelection()) { + this.cursor = this.doc.remove(this.getSelectionRange()); + this.clearSelection(); + } + else { + if (this.cursor.row == 0 && this.cursor.column == 0) { return; } + + var rangeStart = { + row : this.cursor.row, + column : this.cursor.column + -1 + }; + if (rangeStart.column < 0) { + rangeStart.row -= 1; + rangeStart.column = this.doc + .getLine(this.cursor.row - 1).length; + } + this.cursor = this.doc.remove( { + start : rangeStart, + end : this.cursor + }); + } + + this.renderer.updateCursor(this.cursor); + this.renderer.scrollCursorIntoView(); + }, + + ace.Editor.prototype.onCompositionStart = function() { + this.renderer.showComposition(this.cursor); + this.onTextInput(" "); + }; + + ace.Editor.prototype.onCompositionUpdate = function(text) { + this.renderer.setCompositionText(text); + }; + + ace.Editor.prototype.onCompositionEnd = function() { + this.renderer.hideComposition(); + this.removeLeft(); + }; + + ace.Editor.prototype.getFirstVisibleRow = function() { + return this.renderer.getFirstVisibleRow(); + }; + + ace.Editor.prototype.getLastVisibleRow = function() { + return this.renderer.getLastVisibleRow(); + }; + + ace.Editor.prototype.getPageDownRow = function() { + return this.renderer.getLastVisibleRow() - 1; + }; + + ace.Editor.prototype.getPageUpRow = function() { + var firstRow = this.renderer.getFirstVisibleRow(); + var lastRow = this.renderer.getLastVisibleRow(); + + return firstRow - (lastRow - firstRow) + 1; + }; + + ace.Editor.prototype.scrollPageDown = function() { + this.scrollToRow(this.getPageDownRow()); + }; + + ace.Editor.prototype.scrollPageUp = function() { + this.renderer.scrollToRow(this.getPageUpRow()); + }; + + ace.Editor.prototype.scrollToRow = function(row) { + this.renderer.scrollToRow(row); + }; + + ace.Editor.prototype.navigateUp = function() { + this.clearSelection(); + this.moveCursorUp(); + this.renderer.scrollCursorIntoView(); + }; + + ace.Editor.prototype.navigateDown = function() { + this.clearSelection(); + this.moveCursorDown(); + this.renderer.scrollCursorIntoView(); + }; + + ace.Editor.prototype.navigateLeft = function() { + if (this.hasSelection()) { + var selectionStart = this.getSelectionRange().start; + this.moveCursorTo(selectionStart.row, selectionStart.column); + } + else { + this.moveCursorLeft(); + } + this.clearSelection(); + + this.renderer.scrollCursorIntoView(); + }; + + ace.Editor.prototype.navigateRight = function() { + if (this.hasSelection()) { + var selectionEnd = this.getSelectionRange().end; + this.moveCursorTo(selectionEnd.row, selectionEnd.column); + } + else { + this.moveCursorRight(); + } + this.clearSelection(); + + this.renderer.scrollCursorIntoView(); + }, + + ace.Editor.prototype.navigateLineStart = function() { + this.clearSelection(); + this.moveCursorLineStart(); + this.renderer.scrollCursorIntoView(); + }; + + ace.Editor.prototype.navigateLineEnd = function() { + this.clearSelection(); + this.moveCursorLineEnd(); + this.renderer.scrollCursorIntoView(); + }; + + ace.Editor.prototype.navigateFileEnd = function() { + this.clearSelection(); + this.moveCursorFileEnd(); + this.renderer.scrollCursorIntoView(); + }, + + ace.Editor.prototype.navigateFileStart = function() { + this.clearSelection(); + this.moveCursorFileStart(); + this.renderer.scrollCursorIntoView(); + }, + + ace.Editor.prototype.moveCursorUp = function() { + this.moveCursorBy(-1, 0); + }; + + ace.Editor.prototype.moveCursorDown = function() { + this.moveCursorBy(1, 0); + }; + + ace.Editor.prototype.moveCursorLeft = function() { + if (this.cursor.column == 0) { + if (this.cursor.row > 0) { + this.moveCursorTo(this.cursor.row - 1, this.doc + .getLine(this.cursor.row - 1).length); } } else { - return { - start : anchor, - end : lead + this.moveCursorBy(0, -1); + } + }; + + ace.Editor.prototype.moveCursorRight = function() { + if (this.cursor.column == this.doc.getLine(this.cursor.row).length) { + if (this.cursor.row < this.doc.getLength() - 1) { + this.moveCursorTo(this.cursor.row + 1, 0); } } - } -}; + else { + this.moveCursorBy(0, 1); + } + }; -ace.Editor.prototype.clearSelection = function() { - this.selectionLead = null; - this.selectionAnchor = null; + ace.Editor.prototype.moveCursorLineStart = function() { + this.moveCursorTo(this.cursor.row, 0); + }; - if (this.selection) { - this.renderer.removeMarker(this.selection); - this.selection = null; - } -}; + ace.Editor.prototype.moveCursorLineEnd = function() { + this.moveCursorTo(this.cursor.row, + this.doc.getLine(this.cursor.row).length); + }; -ace.Editor.prototype.selectAll = function() { - var lastRow = this.doc.getLength() - 1; - this.setSelectionAnchor(lastRow, this.doc.getLine(lastRow).length); + ace.Editor.prototype.moveCursorFileEnd = function() { + var row = this.doc.getLength() - 1; + var column = this.doc.getLine(row).length; + this.moveCursorTo(row, column); + }; - this._moveSelection(function() { + ace.Editor.prototype.moveCursorFileStart = function() { this.moveCursorTo(0, 0); - }); -}; + }; -ace.Editor.prototype._moveSelection = function(mover) { - if (!this.selectionAnchor) { - this.selectionAnchor = { + ace.Editor.prototype.moveCursorBy = function(rows, chars) { + this.moveCursorTo(this.cursor.row + rows, this.cursor.column + chars); + }; + + ace.Editor.prototype.moveCursorTo = function(row, column) { + if (row >= this.doc.getLength()) { + this.cursor.row = this.doc.getLength() - 1; + this.cursor.column = this.doc.getLine(this.cursor.row).length; + } + else if (row < 0) { + this.cursor.row = 0; + this.cursor.column = 0; + } + else { + this.cursor.row = row; + this.cursor.column = Math + .min(this.doc.getLine(this.cursor.row).length, Math + .max(0, column)); + } + this.updateCursor(); + }; + + ace.Editor.prototype.getCursorPosition = function() { + return { row : this.cursor.row, column : this.cursor.column + }; + }; + + ace.Editor.prototype.hasSelection = function() { + return !!this.selectionLead; + }; + + ace.Editor.prototype.setSelectionAnchor = function(row, column) { + this.clearSelection(); + + this.selectionAnchor = { + row : Math.min(this.doc.getLength() - 1, Math.max(0, row)), + column : Math.min(this.doc.getLine(this.cursor.row).length, Math + .max(0, column)) + }; + + this.selectionLead = null; + }; + + ace.Editor.prototype.getSelectionRange = function() { + var anchor = this.selectionAnchor; + var lead = this.selectionLead; + + if (!anchor) { + return null; } - } + else { + if (anchor.row > lead.row + || (anchor.row == lead.row && anchor.column > lead.column)) { + return { + start : lead, + end : anchor + }; + } + else { + return { + start : anchor, + end : lead + }; + } + } + }; - mover.call(this); + ace.Editor.prototype.clearSelection = function() { + this.selectionLead = null; + this.selectionAnchor = null; - this.selectionLead = { - row : this.cursor.row, - column : this.cursor.column - } + if (this.selection) { + this.renderer.removeMarker(this.selection); + this.selection = null; + } + }; - if (this.selection) { - this.renderer.removeMarker(this.selection); - } - this.selection = this.renderer.addMarker(this.getSelectionRange(), - "selection"); - this.renderer.scrollCursorIntoView(); -}; + ace.Editor.prototype.selectAll = function() { + var lastRow = this.doc.getLength() - 1; + this.setSelectionAnchor(lastRow, this.doc.getLine(lastRow).length); -ace.Editor.prototype.selectUp = function() { - this._moveSelection(this.moveCursorUp); -}; + this._moveSelection(function() { + this.moveCursorTo(0, 0); + }); + }; -ace.Editor.prototype.selectDown = function() { - this._moveSelection(this.moveCursorDown); -}; + ace.Editor.prototype._moveSelection = function(mover) { + if (!this.selectionAnchor) { + this.selectionAnchor = { + row : this.cursor.row, + column : this.cursor.column + }; + } -ace.Editor.prototype.selectRight = function() { - this._moveSelection(this.moveCursorRight); -}; + mover.call(this); -ace.Editor.prototype.selectLeft = function() { - this._moveSelection(this.moveCursorLeft); -}; + this.selectionLead = { + row : this.cursor.row, + column : this.cursor.column + }; -ace.Editor.prototype.selectLineStart = function() { - this._moveSelection(this.moveCursorLineStart); -}; + if (this.selection) { + this.renderer.removeMarker(this.selection); + } + this.selection = this.renderer.addMarker(this.getSelectionRange(), + "selection"); + this.renderer.scrollCursorIntoView(); + }; -ace.Editor.prototype.selectLineEnd = function() { - this._moveSelection(this.moveCursorLineEnd); -}; + ace.Editor.prototype.selectUp = function() { + this._moveSelection(this.moveCursorUp); + }; -ace.Editor.prototype.selectPageDown = function() { - var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow(); - var row = this.getPageDownRow() + Math.round(visibleRows / 2); + ace.Editor.prototype.selectDown = function() { + this._moveSelection(this.moveCursorDown); + }; - this.scrollPageDown(); + ace.Editor.prototype.selectRight = function() { + this._moveSelection(this.moveCursorRight); + }; - this._moveSelection(function() { - this.moveCursorTo(row, this.cursor.column); - }); -}; + ace.Editor.prototype.selectLeft = function() { + this._moveSelection(this.moveCursorLeft); + }; -ace.Editor.prototype.selectPageUp = function() { - var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow(); - var row = this.getPageUpRow() + Math.round(visibleRows / 2); + ace.Editor.prototype.selectLineStart = function() { + this._moveSelection(this.moveCursorLineStart); + }; - this.scrollPageUp(); + ace.Editor.prototype.selectLineEnd = function() { + this._moveSelection(this.moveCursorLineEnd); + }; - this._moveSelection(function() { - this.moveCursorTo(row, this.cursor.column); - }); -}; + ace.Editor.prototype.selectPageDown = function() { + var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow(); + var row = this.getPageDownRow() + Math.round(visibleRows / 2); -ace.Editor.prototype.selectCurrentLine = function() { - this.setSelectionAnchor(this.cursor.row, 0); - this._moveSelection(function() { - this.moveCursorTo(this.cursor.row + 1, 0); - }); -} + this.scrollPageDown(); + + this._moveSelection(function() { + this.moveCursorTo(row, this.cursor.column); + }); + }; + + ace.Editor.prototype.selectPageUp = function() { + var visibleRows = this.getLastVisibleRow() - this.getFirstVisibleRow(); + var row = this.getPageUpRow() + Math.round(visibleRows / 2); + + this.scrollPageUp(); + + this._moveSelection(function() { + this.moveCursorTo(row, this.cursor.column); + }); + }; + + ace.Editor.prototype.selectFileEnd = function() { + this._moveSelection(this.moveCursorFileEnd); + }; + + ace.Editor.prototype.selectFileStart = function() { + this._moveSelection(this.moveCursorFileStart); + }; + + ace.Editor.prototype.selectCurrentLine = function() { + this.setSelectionAnchor(this.cursor.row, 0); + this._moveSelection(function() { + this.moveCursorTo(this.cursor.row + 1, 0); + }); + }; })(); \ No newline at end of file diff --git a/src/GutterLayer.js b/src/GutterLayer.js index 1f9fc7c4..42e027cd 100644 --- a/src/GutterLayer.js +++ b/src/GutterLayer.js @@ -5,7 +5,7 @@ ace.GutterLayer = function(parentEl) { this.element = document.createElement("div"); this.element.className = "layer gutter-layer"; parentEl.appendChild(this.element); -} +}; ace.GutterLayer.prototype.update = function(config) { var html = []; diff --git a/src/JavaScript.js b/src/JavaScript.js index ed387e38..254e6e94 100644 --- a/src/JavaScript.js +++ b/src/JavaScript.js @@ -3,112 +3,112 @@ if (!window.ace) (function() { -ace.JavaScript = {}; + ace.JavaScript = {}; -var keywords = { - "break" : 1, - "case" : 1, - "catch" : 1, - "continue" : 1, - "default" : 1, - "delete" : 1, - "do" : 1, - "else" : 1, - "finally" : 1, - "for" : 1, - "function" : 1, - "if" : 1, - "in" : 1, - "instanceof" : 1, - "new" : 1, - "return" : 1, - "switch" : 1, - "throw" : 1, - "try" : 1, - "typeof" : 1, - "var" : 1, - "while" : 1, - "with" : 1 -}; + var keywords = { + "break" : 1, + "case" : 1, + "catch" : 1, + "continue" : 1, + "default" : 1, + "delete" : 1, + "do" : 1, + "else" : 1, + "finally" : 1, + "for" : 1, + "function" : 1, + "if" : 1, + "in" : 1, + "instanceof" : 1, + "new" : 1, + "return" : 1, + "switch" : 1, + "throw" : 1, + "try" : 1, + "typeof" : 1, + "var" : 1, + "while" : 1, + "with" : 1 + }; -// regexp must not have capturing parentheses -// regexps are ordered -> the first match is used + // regexp must not have capturing parentheses + // regexps are ordered -> the first match is used -ace.JavaScript.RULES = { - start : [ { - token : "comment", - regex : "\\/\\/.*$" - }, { - token : "comment", // multi line comment in one line - regex : "\\/\\*.*?\\*\\/" - }, { - token : "comment", // multi line comment start - regex : "\\/\\*.*$", - next : "comment" - }, { - token : "string", // single line - regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' - }, { - token : "string", // multi line string start - regex : '["].*\\\\$', - next : "qqstring" - }, { - token : "string", // single line - regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']" - }, { - token : "string", // multi line string start - regex : "['].*\\\\$", - next : "qstring" - }, { - token : "number", // hex - regex : "0[xX][0-9a-fA-F]+\\b" - }, { - token : "number", // float - regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b" - }, { - token : function(value) { - if (keywords[value]) { - return "keyword"; - } - else { - return "identifier" - } + ace.JavaScript.RULES = { + start : [ { + token : "comment", + regex : "\\/\\/.*$" + }, { + token : "comment", // multi line comment in one line + regex : "\\/\\*.*?\\*\\/" + }, { + token : "comment", // multi line comment start + regex : "\\/\\*.*$", + next : "comment" + }, { + token : "string", // single line + regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' + }, { + token : "string", // multi line string start + regex : '["].*\\\\$', + next : "qqstring" + }, { + token : "string", // single line + regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']" + }, { + token : "string", // multi line string start + regex : "['].*\\\\$", + next : "qstring" + }, { + token : "number", // hex + regex : "0[xX][0-9a-fA-F]+\\b" + }, { + token : "number", // float + regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b" + }, { + token : function(value) { + if (keywords[value]) { + return "keyword"; + } + else { + return "identifier"; + } + }, + regex : "[a-zA-Z_][a-zA-Z0-9_]*\\b" + }, { + token : function(value) { + // return parens[value]; + return "text"; }, - regex : "[a-zA-Z_][a-zA-Z0-9_]*\\b" - }, { - token : function(value) { - // return parens[value]; - return "text"; - }, - regex : "[\\[\\]\\(\\)\\{\\}]" - }, { - token : "text", - regex : "\\s+" - } ], - "comment" : [ { - token : "comment", // closing comment - regex : ".*?\\*\\/", - next : "start" - }, { - token : "comment", // comment spanning whole line - regex : ".+" - } ], - "qqstring" : [ { - token : "string", - regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"', - next : "start" - }, { - token : "string", - regex : '.+' - } ], - "qstring" : [ { - token : "string", - regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'", - next : "start" - }, { - token : "string", - regex : '.+' - } ] -}; + regex : "[\\[\\]\\(\\)\\{\\}]" + }, { + token : "text", + regex : "\\s+" + } ], + "comment" : [ { + token : "comment", // closing comment + regex : ".*?\\*\\/", + next : "start" + }, { + token : "comment", // comment spanning whole line + regex : ".+" + } ], + "qqstring" : [ { + token : "string", + regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"', + next : "start" + }, { + token : "string", + regex : '.+' + } ], + "qstring" : [ { + token : "string", + regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'", + next : "start" + }, { + token : "string", + regex : '.+' + } ] + }; })(); \ No newline at end of file diff --git a/src/MarkerLayer.js b/src/MarkerLayer.js index f1d806b9..c02ea039 100644 --- a/src/MarkerLayer.js +++ b/src/MarkerLayer.js @@ -8,7 +8,7 @@ ace.MarkerLayer = function(parentEl) { this.markers = {}; this._markerId = 1; -} +}; ace.MarkerLayer.prototype.addMarker = function(range, clazz) { var id = this._markerId++; diff --git a/src/TextDocument.js b/src/TextDocument.js index 8136f366..ab1cee94 100644 --- a/src/TextDocument.js +++ b/src/TextDocument.js @@ -6,10 +6,10 @@ ace.TextDocument = function(text) { this.modified = true; this.listeners = []; -} +}; ace.TextDocument.prototype._split = function(text) { - return text.split(/[\n\r]/) + return text.split(/[\n\r]/); }; ace.TextDocument.prototype.addChangeListener = function(listener) { @@ -67,37 +67,6 @@ ace.TextDocument.prototype.keywords = { "with" : 1 }; -ace.TextDocument.prototype.getLineTokens = function(row) { - var tokens = []; - - var re = /(?:(\s+)|("[^"]*")|('[^']*')|([\[\]\(\)\{\}])|([a-zA-Z_][a-zA-Z0-9_]*)|(\/\/.*)|(.))/g - re.lastIndex = 0; - - var match; - var line = this.getLine(row); - while (match = re.exec(line)) { - var token = { - type : "text", - value : match[0] - } - - if (match[2] || match[3]) { - token.type = "string"; - } - else if (match[5] && this.keywords[match[5]]) { - token.type = "keyword"; - } - else if (match[6]) { - token.type = "comment"; - } - - tokens.push(token); - } - ; - - return tokens; -}; - ace.TextDocument.prototype.getLength = function() { return this.lines.length; }; @@ -148,7 +117,7 @@ ace.TextDocument.prototype._insert = function(position, text) { return { row : position.row, column : position.column + text.length - } + }; } else { var line = this.lines[position.row] || ""; @@ -159,7 +128,7 @@ ace.TextDocument.prototype._insert = function(position, text) { + line.substring(position.column); if (newLines.length > 2) { - var args = [ position.row + 1, 0 ] + var args = [ position.row + 1, 0 ]; args.push.apply(args, newLines.slice(1, -1)); this.lines.splice.apply(this.lines, args); } @@ -209,4 +178,4 @@ ace.TextDocument.prototype.replace = function(range, text) { : undefined); return end; -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/TextInput.js b/src/TextInput.js index 616f73b1..ede8a9bd 100644 --- a/src/TextInput.js +++ b/src/TextInput.js @@ -19,8 +19,8 @@ ace.TextInput = function(parentNode, host) { host.onTextInput(text.value); text.value = ""; } - }, 0) - } + }, 0); + }; var onCompositionStart = function(e) { inCompostion = true; @@ -31,28 +31,28 @@ ace.TextInput = function(parentNode, host) { host.onCompositionStart(); setTimeout(onCompositionUpdate, 0); - } + }; var onCompositionUpdate = function() { host.onCompositionUpdate(text.value); - } + }; var onCompositionEnd = function() { inCompostion = false; host.onCompositionEnd(); onTextInput(); - } + }; var onCopy = function() { text.value = host.getCopyText(); text.select(); - } + }; var onCut = function() { text.value = host.getCopyText(); host.onCut(); text.select(); - } + }; ace.addListener(text, "keypress", onTextInput, false); ace.addListener(text, "textInput", onTextInput, false); @@ -77,9 +77,9 @@ ace.TextInput = function(parentNode, host) { this.focus = function() { text.focus(); - } + }; this.blur = function() { this.blur(); - } + }; }; \ No newline at end of file diff --git a/src/TextLayer.js b/src/TextLayer.js index acdcc5c2..bfa95410 100644 --- a/src/TextLayer.js +++ b/src/TextLayer.js @@ -7,7 +7,7 @@ ace.TextLayer = function(parentEl) { parentEl.appendChild(this.element); this._measureSizes(); -} +}; ace.TextLayer.prototype.setTokenizer = function(tokenizer) { this.tokenizer = tokenizer; diff --git a/src/Tokenizer.js b/src/Tokenizer.js index af75ca3c..923f605e 100644 --- a/src/Tokenizer.js +++ b/src/Tokenizer.js @@ -33,9 +33,9 @@ ace.Tokenizer.prototype.getLineTokens = function(line, startState) { var token = { type : "text", value : match[0] - } + }; - if (re.lastIndex == lastIndex) { throw new Error("tokenizer error") } + if (re.lastIndex == lastIndex) { throw new Error("tokenizer error"); } lastIndex = re.lastIndex; // console.log(match); @@ -71,5 +71,5 @@ ace.Tokenizer.prototype.getLineTokens = function(line, startState) { return { tokens : tokens, state : currentState - } + }; }; \ No newline at end of file diff --git a/src/VirtualRenderer.js b/src/VirtualRenderer.js index 9930202d..3034e12b 100644 --- a/src/VirtualRenderer.js +++ b/src/VirtualRenderer.js @@ -32,7 +32,7 @@ ace.VirtualRenderer = function(container) { row : 0, column : 0 }; -} +}; ace.VirtualRenderer.prototype.setDocument = function(doc) { this.lines = doc.lines; @@ -63,7 +63,7 @@ ace.VirtualRenderer.prototype.updateLines = function(firstRow, lastRow) { // if the last row is unknow -> redraw everything if (lastRow === undefined) { - this.draw() + this.draw(); return; } @@ -108,7 +108,7 @@ ace.VirtualRenderer.prototype.draw = function() { this.gutterLayer.element.style.marginTop = (-offset) + "px"; this.gutterLayer.element.style.height = minHeight + "px"; this.gutterLayer.update(layerConfig); -} +}; ace.VirtualRenderer.prototype.addMarker = function(range, clazz) { return this.markerLayer.addMarker(range, clazz); @@ -134,7 +134,7 @@ ace.VirtualRenderer.prototype.showCursor = function() { ace.VirtualRenderer.prototype.scrollCursorIntoView = function() { var pos = this.cursorLayer.getPixelPosition(); - var left = pos.left + var left = pos.left; var top = pos.top; if (this.getScrollTop() > top) { @@ -163,7 +163,7 @@ ace.VirtualRenderer.prototype.getScrollTop = function() { ace.VirtualRenderer.prototype.scrollToRow = function(row) { this.scrollToY(row * this.lineHeight); -} +}; ace.VirtualRenderer.prototype.scrollToY = function(scrollTop) { var maxHeight = this.lines.length * this.lineHeight @@ -187,7 +187,7 @@ ace.VirtualRenderer.prototype.screenToTextCoordinates = function(pageX, pageY) { return { row : row, column : col - } + }; }; ace.VirtualRenderer.prototype.visualizeFocus = function() { diff --git a/src/lib.js b/src/lib.js index 04036745..8873e0aa 100644 --- a/src/lib.js +++ b/src/lib.js @@ -3,157 +3,157 @@ if (!window.ace) (function() { -ace.addListener = function(elem, type, callback) { - if (elem.addEventListener) { return elem.addEventListener(type, - callback, - false); } - if (elem.attachEvent) { - var wrapper = function() { - callback(window.event); + ace.addListener = function(elem, type, callback) { + if (elem.addEventListener) { return elem.addEventListener(type, + callback, + false); } + if (elem.attachEvent) { + var wrapper = function() { + callback(window.event); + }; + callback.$$wrapper = wrapper; + elem.attachEvent("on" + type, wrapper); } - callback.$$wrapper = wrapper; - elem.attachEvent("on" + type, wrapper); - } -} - -ace.removeListener = function(elem, type, callback) { - if (elem.removeEventListener) { return elem - .removeEventListener(type, callback, false); } - if (elem.detachEvent) { - elem.detachEvent("on" + type, callback.$$wrapper || callback); - } -} - -ace.setText = function(elem, text) { - if (elem.innerText !== undefined) { - elem.innerText = text; - } - if (elem.textContent !== undefined) { - elem.textContent = text; - } -} - -ace.stopEvent = function(e) { - ace.stopPropagation(e); - ace.preventDefault(e); - return false; -} - -ace.stopPropagation = function(e) { - if (e.stopPropagation) - e.stopPropagation(); - else - e.cancelBubble = true; -} - -ace.preventDefault = function(e) { - if (e.preventDefault) - e.preventDefault(); - else - e.returnValue = false; -} - -ace.inherits = function(ctor, superCtor) { - var tempCtor = function() { - }; - tempCtor.prototype = superCtor.prototype; - ctor.super_ = superCtor.prototype; - ctor.prototype = new tempCtor(); - ctor.prototype.constructor = ctor; -}; - -ace.getInnerWidth = function(element) { - return (parseInt(ace.computedStyle(element, "paddingLeft")) - + parseInt(ace.computedStyle(element, "paddingRight")) + element.clientWidth); -}; - -ace.getInnerHeight = function(element) { - return (parseInt(ace.computedStyle(element, "paddingTop")) - + parseInt(ace.computedStyle(element, "paddingBottom")) + element.clientHeight); -}; - -ace.computedStyle = function(element, style) { - if (window.getComputedStyle) { - return (window.getComputedStyle(element, null))[style]; - } - else { - return element.currentStyle[style]; - } -} - -ace.scrollbarHeight = function() { - var el = document.createElement("div"); - var style = el.style; - - style.position = "absolute"; - style.left = "-10000px"; - style.overflow = "scroll"; - style.height = "100px"; - - document.body.appendChild(el); - var height = el.offsetHeight - el.clientHeight; - document.body.removeChild(el); - - return height; -} - -ace.bind = function(fcn, context) { - return function() { - return fcn.apply(context, arguments); - } -} - -ace.capture = function(el, eventHandler, releaseCaptureHandler) { - function onMouseMove(e) { - eventHandler(e); - e.stopPropagation(); - } - - function onMouseUp(e) { - eventHandler && eventHandler(e); - releaseCaptureHandler && releaseCaptureHandler(); - - document.removeEventListener("mousemove", onMouseMove, true); - document.removeEventListener("mouseup", onMouseUp, true); - - e.stopPropagation(); - } - - document.addEventListener("mousemove", onMouseMove, true); - document.addEventListener("mouseup", onMouseUp, true); -} - -ace.addMouseWheelListener = function(el, callback) { - var listener = function(e) { - e.wheel = (e.wheelDelta) ? e.wheelDelta / 120 - : -(e.detail || 0) / 3; - callback(e); - } - ace.addListener(el, "DOMMouseScroll", listener); - ace.addListener(el, "mousewheel", listener); -}; - -ace.autoremoveListener = function(el, type, callback, timeout) { - var listener = function(e) { - clearTimeout(timeoutId); - remove(); - callback(e); - } - - var remove = function() { - ace.removeListener(el, type, listener); }; - ace.addListener(el, type, listener); - var timeoutId = setTimeout(remove, timeout); -} + ace.removeListener = function(elem, type, callback) { + if (elem.removeEventListener) { return elem + .removeEventListener(type, callback, false); } + if (elem.detachEvent) { + elem.detachEvent("on" + type, callback.$$wrapper || callback); + } + }; -ace.addTripleClickListener = function(el, callback) { - ace.addListener(el, "mousedown", function() { - ace.autoremoveListener(el, "mousedown", function() { - ace.autoremoveListener(el, "mousedown", callback, 300); - }, 300); - }); -} + ace.setText = function(elem, text) { + if (elem.innerText !== undefined) { + elem.innerText = text; + } + if (elem.textContent !== undefined) { + elem.textContent = text; + } + }; + + ace.stopEvent = function(e) { + ace.stopPropagation(e); + ace.preventDefault(e); + return false; + }; + + ace.stopPropagation = function(e) { + if (e.stopPropagation) + e.stopPropagation(); + else + e.cancelBubble = true; + }; + + ace.preventDefault = function(e) { + if (e.preventDefault) + e.preventDefault(); + else + e.returnValue = false; + }; + + ace.inherits = function(ctor, superCtor) { + var tempCtor = function() { + }; + tempCtor.prototype = superCtor.prototype; + ctor.super_ = superCtor.prototype; + ctor.prototype = new tempCtor(); + ctor.prototype.constructor = ctor; + }; + + ace.getInnerWidth = function(element) { + return (parseInt(ace.computedStyle(element, "paddingLeft")) + + parseInt(ace.computedStyle(element, "paddingRight")) + element.clientWidth); + }; + + ace.getInnerHeight = function(element) { + return (parseInt(ace.computedStyle(element, "paddingTop")) + + parseInt(ace.computedStyle(element, "paddingBottom")) + element.clientHeight); + }; + + ace.computedStyle = function(element, style) { + if (window.getComputedStyle) { + return (window.getComputedStyle(element, null))[style]; + } + else { + return element.currentStyle[style]; + } + }; + + ace.scrollbarHeight = function() { + var el = document.createElement("div"); + var style = el.style; + + style.position = "absolute"; + style.left = "-10000px"; + style.overflow = "scroll"; + style.height = "100px"; + + document.body.appendChild(el); + var height = el.offsetHeight - el.clientHeight; + document.body.removeChild(el); + + return height; + }; + + ace.bind = function(fcn, context) { + return function() { + return fcn.apply(context, arguments); + }; + }; + + ace.capture = function(el, eventHandler, releaseCaptureHandler) { + function onMouseMove(e) { + eventHandler(e); + e.stopPropagation(); + } + + function onMouseUp(e) { + eventHandler && eventHandler(e); + releaseCaptureHandler && releaseCaptureHandler(); + + document.removeEventListener("mousemove", onMouseMove, true); + document.removeEventListener("mouseup", onMouseUp, true); + + e.stopPropagation(); + } + + document.addEventListener("mousemove", onMouseMove, true); + document.addEventListener("mouseup", onMouseUp, true); + }; + + ace.addMouseWheelListener = function(el, callback) { + var listener = function(e) { + e.wheel = (e.wheelDelta) ? e.wheelDelta / 120 + : -(e.detail || 0) / 3; + callback(e); + }; + ace.addListener(el, "DOMMouseScroll", listener); + ace.addListener(el, "mousewheel", listener); + }; + + ace.autoremoveListener = function(el, type, callback, timeout) { + var listener = function(e) { + clearTimeout(timeoutId); + remove(); + callback(e); + }; + + var remove = function() { + ace.removeListener(el, type, listener); + }; + + ace.addListener(el, type, listener); + var timeoutId = setTimeout(remove, timeout); + }; + + ace.addTripleClickListener = function(el, callback) { + ace.addListener(el, "mousedown", function() { + ace.autoremoveListener(el, "mousedown", function() { + ace.autoremoveListener(el, "mousedown", callback, 300); + }, 300); + }); + }; })(); \ No newline at end of file diff --git a/test/MockRenderer.js b/test/MockRenderer.js index e06c0adb..c16c2107 100644 --- a/test/MockRenderer.js +++ b/test/MockRenderer.js @@ -1,16 +1,16 @@ MockRenderer = function() { this.container = document.createElement("div"); this.cursor = { - row: 0, - column: 0 - } - + row : 0, + column : 0 + }; + this.visibleRowCount = 20; - + this.layerConfig = { - firstVisibleRow: 0, - lastVisibleRow: this.visibleRowCount, - } + firstVisibleRow : 0, + lastVisibleRow : this.visibleRowCount + }; }; @@ -41,18 +41,22 @@ MockRenderer.prototype.updateCursor = function(position) { MockRenderer.prototype.scrollCursorIntoView = function() { if (this.cursor.row < this.layerConfig.firstVisibleRow) { this.scrollToRow(this.cursor.row); - } else if (this.cursor.row > this.layerConfig.lastVisibleRow) { + } + else if (this.cursor.row > this.layerConfig.lastVisibleRow) { this.scrollToRow(this.cursor.row); } }; MockRenderer.prototype.scrollToRow = function(row) { - var row = Math.min(this.lines.length - this.visibleRowCount, Math.max(0, row)); + var row = Math.min(this.lines.length - this.visibleRowCount, Math.max(0, + row)); this.layerConfig.firstVisibleRow = row; this.layerConfig.lastVisibleRow = row + this.visibleRowCount; -} +}; MockRenderer.prototype.draw = function() { }; +MockRenderer.prototype.addMarker = function() { +}; diff --git a/test/NavigationTest.js b/test/NavigationTest.js index 14391d65..f74a9b0c 100644 --- a/test/NavigationTest.js +++ b/test/NavigationTest.js @@ -47,5 +47,37 @@ var NavigationTest = TestCase("NavigationTest", { editor.navigateFileStart(); assertEquals(0, editor.getFirstVisibleRow()); + }, + + "test: move selection lead to end of file" : function() { + var editor = new ace.Editor(this.createTextDocument(200, 10), + new MockRenderer()); + + editor.moveCursorTo(100, 5); + editor.selectFileEnd(); + + var selection = editor.getSelectionRange(); + + assertEquals(100, selection.start.row); + assertEquals(5, selection.start.column); + + assertEquals(199, selection.end.row); + assertEquals(10, selection.end.column); + }, + + "test: move selection lead to start of file" : function() { + var editor = new ace.Editor(this.createTextDocument(200, 10), + new MockRenderer()); + + editor.moveCursorTo(100, 5); + editor.selectFileStart(); + + var selection = editor.getSelectionRange(); + + assertEquals(0, selection.start.row); + assertEquals(0, selection.start.column); + + assertEquals(100, selection.end.row); + assertEquals(5, selection.end.column); } });