OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 /** |
| 32 * @constructor |
| 33 * @param {!WebInspector.SourcesPanel} sourcesPanel |
| 34 * @param {!function():!WebInspector.SourceFrame} currentSourceFrameCallback |
| 35 */ |
| 36 WebInspector.EditingLocationHistoryManager = function(sourcesPanel, currentSourc
eFrameCallback) |
| 37 { |
| 38 this._sourcesPanel = sourcesPanel; |
| 39 this._historyManager = new WebInspector.SimpleHistoryManager(WebInspector.Ed
itingLocationHistoryManager.HistoryDepth); |
| 40 this._currentSourceFrameCallback = currentSourceFrameCallback; |
| 41 } |
| 42 |
| 43 WebInspector.EditingLocationHistoryManager.HistoryDepth = 20; |
| 44 |
| 45 WebInspector.EditingLocationHistoryManager.ShortcutKeys = { |
| 46 JumpToPreviousLocation: WebInspector.KeyboardShortcut.makeDescriptor(WebInsp
ector.KeyboardShortcut.Keys.Minus, WebInspector.KeyboardShortcut.Modifiers.Alt), |
| 47 JumpToNextLocation: WebInspector.KeyboardShortcut.makeDescriptor(WebInspecto
r.KeyboardShortcut.Keys.Plus, WebInspector.KeyboardShortcut.Modifiers.Alt), |
| 48 } |
| 49 |
| 50 WebInspector.EditingLocationHistoryManager.prototype = { |
| 51 /** |
| 52 * @param {!WebInspector.UISourceCodeFrame} sourceFrame |
| 53 */ |
| 54 trackSourceFrameCursorJumps: function(sourceFrame) |
| 55 { |
| 56 sourceFrame.addEventListener(WebInspector.SourceFrame.Events.JumpHappene
d, this._onJumpHappened.bind(this)); |
| 57 }, |
| 58 |
| 59 /** |
| 60 * @param {!WebInspector.Event} event |
| 61 */ |
| 62 _onJumpHappened: function(event) |
| 63 { |
| 64 if (event.data.from) |
| 65 this._updateActiveState(event.data.from); |
| 66 if (event.data.to) |
| 67 this._pushActiveState(event.data.to); |
| 68 }, |
| 69 |
| 70 rollback: function() |
| 71 { |
| 72 this._historyManager.rollback(); |
| 73 }, |
| 74 |
| 75 rollover: function() |
| 76 { |
| 77 this._historyManager.rollover(); |
| 78 }, |
| 79 |
| 80 /** |
| 81 * @param {!KeyboardEvent} event |
| 82 * @return {boolean} |
| 83 */ |
| 84 handleShortcut: function(event) |
| 85 { |
| 86 var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEvent(event); |
| 87 if (shortcutKey === WebInspector.EditingLocationHistoryManager.ShortcutK
eys.JumpToPreviousLocation.key) { |
| 88 event.consume(true); |
| 89 this.rollback(); |
| 90 return true; |
| 91 } else if (shortcutKey === WebInspector.EditingLocationHistoryManager.Sh
ortcutKeys.JumpToNextLocation.key) { |
| 92 event.consume(true); |
| 93 this.rollover(); |
| 94 return true; |
| 95 } |
| 96 return false; |
| 97 }, |
| 98 |
| 99 readOnlyLock: function() |
| 100 { |
| 101 this._historyManager.readOnlyLock(); |
| 102 }, |
| 103 |
| 104 releaseReadOnlyLock: function() |
| 105 { |
| 106 this._historyManager.releaseReadOnlyLock(); |
| 107 }, |
| 108 |
| 109 updateCurrentState: function() |
| 110 { |
| 111 var sourceFrame = this._currentSourceFrameCallback(); |
| 112 if (!sourceFrame) |
| 113 return; |
| 114 this._updateActiveState(sourceFrame.textEditor.selection()); |
| 115 }, |
| 116 |
| 117 pushNewState: function() |
| 118 { |
| 119 var sourceFrame = this._currentSourceFrameCallback(); |
| 120 if (!sourceFrame) |
| 121 return; |
| 122 this._pushActiveState(sourceFrame.textEditor.selection()); |
| 123 }, |
| 124 |
| 125 /** |
| 126 * @param {!WebInspector.TextRange} selection |
| 127 */ |
| 128 _updateActiveState: function(selection) |
| 129 { |
| 130 var active = this._historyManager.active(); |
| 131 if (!active) |
| 132 return; |
| 133 var sourceFrame = this._currentSourceFrameCallback(); |
| 134 if (!sourceFrame) |
| 135 return; |
| 136 var entry = new WebInspector.EditingLocationHistoryEntry(this._sourcesPa
nel, this, sourceFrame, selection); |
| 137 active.merge(entry); |
| 138 }, |
| 139 |
| 140 /** |
| 141 * @param {!WebInspector.TextRange} selection |
| 142 */ |
| 143 _pushActiveState: function(selection) |
| 144 { |
| 145 var sourceFrame = this._currentSourceFrameCallback(); |
| 146 if (!sourceFrame) |
| 147 return; |
| 148 var entry = new WebInspector.EditingLocationHistoryEntry(this._sourcesPa
nel, this, sourceFrame, selection); |
| 149 this._historyManager.push(entry); |
| 150 }, |
| 151 |
| 152 /** |
| 153 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 154 */ |
| 155 removeHistoryForSourceCode: function(uiSourceCode) |
| 156 { |
| 157 function filterOut(entry) |
| 158 { |
| 159 return entry._projectId === uiSourceCode.project().id() && entry._pa
th === uiSourceCode.path(); |
| 160 } |
| 161 |
| 162 this._historyManager.filterOut(filterOut); |
| 163 }, |
| 164 } |
| 165 |
| 166 |
| 167 /** |
| 168 * @constructor |
| 169 * @implements {WebInspector.HistoryEntry} |
| 170 * @param {!WebInspector.SourcesPanel} sourcesPanel |
| 171 * @param {!WebInspector.EditingLocationHistoryManager} editingLocationManager |
| 172 * @param {!WebInspector.SourceFrame} sourceFrame |
| 173 * @param {!WebInspector.TextRange} selection |
| 174 */ |
| 175 WebInspector.EditingLocationHistoryEntry = function(sourcesPanel, editingLocatio
nManager, sourceFrame, selection) |
| 176 { |
| 177 this._sourcesPanel = sourcesPanel; |
| 178 this._editingLocationManager = editingLocationManager; |
| 179 var uiSourceCode = sourceFrame.sourceCode(); |
| 180 this._projectId = uiSourceCode.project().id(); |
| 181 this._path = uiSourceCode.path(); |
| 182 |
| 183 var position = this._positionFromSelection(selection); |
| 184 this._positionHandle = sourceFrame.textEditor.textEditorPositionHandle(posit
ion.lineNumber, position.columnNumber); |
| 185 } |
| 186 |
| 187 WebInspector.EditingLocationHistoryEntry.prototype = { |
| 188 /** |
| 189 * @param {!WebInspector.HistoryEntry} entry |
| 190 */ |
| 191 merge: function(entry) |
| 192 { |
| 193 if (this._projectId !== entry._projectId || this._path !== entry._path) |
| 194 return; |
| 195 this._positionHandle = entry._positionHandle; |
| 196 }, |
| 197 |
| 198 /** |
| 199 * @param {!WebInspector.TextRange} selection |
| 200 * @return {!{lineNumber: number, columnNumber: number}} |
| 201 */ |
| 202 _positionFromSelection: function(selection) |
| 203 { |
| 204 return { |
| 205 lineNumber: selection.endLine, |
| 206 columnNumber: selection.endColumn |
| 207 }; |
| 208 }, |
| 209 |
| 210 /** |
| 211 * @return {boolean} |
| 212 */ |
| 213 valid: function() |
| 214 { |
| 215 var position = this._positionHandle.resolve(); |
| 216 var uiSourceCode = WebInspector.workspace.project(this._projectId).uiSou
rceCode(this._path); |
| 217 return !!(position && uiSourceCode); |
| 218 }, |
| 219 |
| 220 reveal: function() |
| 221 { |
| 222 var position = this._positionHandle.resolve(); |
| 223 var uiSourceCode = WebInspector.workspace.project(this._projectId).uiSou
rceCode(this._path); |
| 224 if (!position || !uiSourceCode) |
| 225 return; |
| 226 |
| 227 this._editingLocationManager.updateCurrentState(); |
| 228 this._sourcesPanel.showUISourceCode(uiSourceCode); |
| 229 this._sourcesPanel.focus(); |
| 230 this._sourcesPanel.highlightPosition(position.lineNumber, position.colum
nNumber); |
| 231 } |
| 232 }; |
OLD | NEW |