Commit e69d304a authored by graouts@apple.com's avatar graouts@apple.com

Web Inspector: provide an abstraction for CodeMirror's TextMarker

https://bugs.webkit.org/show_bug.cgi?id=125695

Reviewed by Timothy Hatcher.

Introduce a new WebInspector.TextMarker class which is used by code with CodeMirror knowledge
to return information related to text markers to objects that should have no direct knowledge
of CodeMirror. Start using this class in TextEditor and SourceCodeTextEditor to remove the
last remaining pieces of CodeMirror knowledge added to SourceCodeTextEditor to fix
https://webkit.org/b/124364.

* UserInterface/CodeMirrorAdditions.js:
Stop using __markedColor to identify a color marker and instead use the type on the matching
WebInspector.TextMarker. Additionally, create a WebInspector.TextMarker with type
WebInspector.TextMarker.Type.Color in createColorMarkers().

* UserInterface/CodeMirrorColorEditingController.js:
(WebInspector.CodeMirrorColorEditingController):
Use a WebInspector.TextRange to track the edited range and obtain it directly from the
WebInspector.TextMaker used to instantiate the object. We also use the new "text" public
property to create the color.

(WebInspector.CodeMirrorColorEditingController.prototype.set color):
Simply assing the serialized color to the new "text" public property.

(WebInspector.CodeMirrorColorEditingController.prototype.get text):
(WebInspector.CodeMirrorColorEditingController.prototype.set text):
New public property to set get and set the text for the edited range, automatically updating
the range upon setting to a new text.

(WebInspector.CodeMirrorColorEditingController.prototype.presentHoverMenu):
Obtain the bounds directly from the WebInspector.TextMarker object.

* UserInterface/CodeMirrorTokenTrackingController.js:
(WebInspector.CodeMirrorTokenTrackingController.prototype._updateHoveredTokenInfo):
Since we're now tracking the hoveredMarker as a WebInspector.TextMarker, get the CodeMirror
TextMarker from that object to check if it's contained within the text markers at the
hovered position.

* UserInterface/Main.html:
Add the new TextMarker class source.

* UserInterface/SourceCodeTextEditor.js:
(WebInspector.SourceCodeTextEditor.prototype._hasColorMarkers):
Use the WebInspector.TextMarker type to identify a given text marker is marking a color.

(WebInspector.SourceCodeTextEditor.prototype.tokenTrackingControllerNewHighlightCandidate):
Use the renamed markersAtPosition() method from TextEditor.

(WebInspector.SourceCodeTextEditor.prototype._tokenTrackingControllerHighlightedMarkedExpression):
Use the WebInspector.TextMarker type to identify a given text marker is marking a color.

(WebInspector.SourceCodeTextEditor.prototype.colorEditingControllerDidFinishEditing):
Since the CodeMirrorColorEditingController is now using a WebInspector.TextRange for its range,
update to use a WebInspector.TextRange API to get the range's start line.

* UserInterface/TextEditor.js:
(WebInspector.TextEditor.prototype.get markers):
Return WebInspector.TextMarker objects instead of CodeMirror TextRange objects.

(WebInspector.TextEditor.prototype.markersAtPosition):
Rename method to a better name instead of using the CodeMirror-influenced name. Also, return
WebInspector.TextMarker objects instead of CodeMirror TextRange objects.

* UserInterface/TextMarker.js: Added.
(WebInspector.TextMarker):
Create a WebInspector.TextMarker with a CodeMirror TextMarker and an optional type.

(WebInspector.TextMarker.textMarkerForCodeMirrorTextMarker):
Static method to either obtain the existing WebInspector.TextMarker for a given CodeMirror
TextMarker, or create a new WebInspector.TextMarker.

(WebInspector.TextMarker.prototype.get codeMirrorTextMarker):
Public property to access the CodeMirror TextMarker objects for classes that have direct
knowledge of CodeMirror.

(WebInspector.TextMarker.prototype.get type):
Public property to access the type used to create this text marker.

(WebInspector.TextMarker.prototype.get range):
(WebInspector.TextMarker.prototype.get bounds):
Public properties allowing easy access to generally useful information related to marked text.

(WebInspector.TextMarker.prototype.clear):
Wrapper for the CodeMirror TextMarker clear() method.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@160552 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent d444fb79
2013-12-13 Antoine Quint <graouts@apple.com>
Web Inspector: provide an abstraction for CodeMirror's TextMarker
https://bugs.webkit.org/show_bug.cgi?id=125695
Reviewed by Timothy Hatcher.
Introduce a new WebInspector.TextMarker class which is used by code with CodeMirror knowledge
to return information related to text markers to objects that should have no direct knowledge
of CodeMirror. Start using this class in TextEditor and SourceCodeTextEditor to remove the
last remaining pieces of CodeMirror knowledge added to SourceCodeTextEditor to fix
https://webkit.org/b/124364.
* UserInterface/CodeMirrorAdditions.js:
Stop using __markedColor to identify a color marker and instead use the type on the matching
WebInspector.TextMarker. Additionally, create a WebInspector.TextMarker with type
WebInspector.TextMarker.Type.Color in createColorMarkers().
* UserInterface/CodeMirrorColorEditingController.js:
(WebInspector.CodeMirrorColorEditingController):
Use a WebInspector.TextRange to track the edited range and obtain it directly from the
WebInspector.TextMaker used to instantiate the object. We also use the new "text" public
property to create the color.
(WebInspector.CodeMirrorColorEditingController.prototype.set color):
Simply assing the serialized color to the new "text" public property.
(WebInspector.CodeMirrorColorEditingController.prototype.get text):
(WebInspector.CodeMirrorColorEditingController.prototype.set text):
New public property to set get and set the text for the edited range, automatically updating
the range upon setting to a new text.
(WebInspector.CodeMirrorColorEditingController.prototype.presentHoverMenu):
Obtain the bounds directly from the WebInspector.TextMarker object.
* UserInterface/CodeMirrorTokenTrackingController.js:
(WebInspector.CodeMirrorTokenTrackingController.prototype._updateHoveredTokenInfo):
Since we're now tracking the hoveredMarker as a WebInspector.TextMarker, get the CodeMirror
TextMarker from that object to check if it's contained within the text markers at the
hovered position.
* UserInterface/Main.html:
Add the new TextMarker class source.
* UserInterface/SourceCodeTextEditor.js:
(WebInspector.SourceCodeTextEditor.prototype._hasColorMarkers):
Use the WebInspector.TextMarker type to identify a given text marker is marking a color.
(WebInspector.SourceCodeTextEditor.prototype.tokenTrackingControllerNewHighlightCandidate):
Use the renamed markersAtPosition() method from TextEditor.
(WebInspector.SourceCodeTextEditor.prototype._tokenTrackingControllerHighlightedMarkedExpression):
Use the WebInspector.TextMarker type to identify a given text marker is marking a color.
(WebInspector.SourceCodeTextEditor.prototype.colorEditingControllerDidFinishEditing):
Since the CodeMirrorColorEditingController is now using a WebInspector.TextRange for its range,
update to use a WebInspector.TextRange API to get the range's start line.
* UserInterface/TextEditor.js:
(WebInspector.TextEditor.prototype.get markers):
Return WebInspector.TextMarker objects instead of CodeMirror TextRange objects.
(WebInspector.TextEditor.prototype.markersAtPosition):
Rename method to a better name instead of using the CodeMirror-influenced name. Also, return
WebInspector.TextMarker objects instead of CodeMirror TextRange objects.
* UserInterface/TextMarker.js: Added.
(WebInspector.TextMarker):
Create a WebInspector.TextMarker with a CodeMirror TextMarker and an optional type.
(WebInspector.TextMarker.textMarkerForCodeMirrorTextMarker):
Static method to either obtain the existing WebInspector.TextMarker for a given CodeMirror
TextMarker, or create a new WebInspector.TextMarker.
(WebInspector.TextMarker.prototype.get codeMirrorTextMarker):
Public property to access the CodeMirror TextMarker objects for classes that have direct
knowledge of CodeMirror.
(WebInspector.TextMarker.prototype.get type):
Public property to access the type used to create this text marker.
(WebInspector.TextMarker.prototype.get range):
(WebInspector.TextMarker.prototype.get bounds):
Public properties allowing easy access to generally useful information related to marked text.
(WebInspector.TextMarker.prototype.clear):
Wrapper for the CodeMirror TextMarker clear() method.
2013-12-13 Brent Fulgham <bfulgham@apple.com>
[Win] Switch WebKit solution to Visual Studio 2013
......
......@@ -450,10 +450,10 @@
var foundColorMarker = false;
var markers = this.findMarksAt(to);
for (var j = 0; j < markers.length; ++j) {
if (!markers[j].__markedColor)
continue;
foundColorMarker = true;
break;
if (WebInspector.TextMarker.textMarkerForCodeMirrorTextMarker(markers[j]).type === WebInspector.TextMarker.Type.Color) {
foundColorMarker = true;
break;
}
}
if (foundColorMarker) {
......@@ -469,7 +469,7 @@
}
var marker = this.markText(from, to);
marker.__markedColor = true;
marker = new WebInspector.TextMarker(marker, WebInspector.TextMarker.Type.Color);
createdMarkers.push(marker);
......
......@@ -31,9 +31,8 @@ WebInspector.CodeMirrorColorEditingController = function(codeMirror, marker)
this._marker = marker;
this._delegate = null;
this._range = marker.find();
this._color = WebInspector.Color.fromString(codeMirror.getRange(this._range.from, this._range.to));
this._range = marker.range;
this._color = WebInspector.Color.fromString(this.text);
this._keyboardShortcutEsc = new WebInspector.KeyboardShortcut(null, WebInspector.KeyboardShortcut.Key.Escape);
}
......@@ -61,10 +60,7 @@ WebInspector.CodeMirrorColorEditingController.prototype = {
set color(color)
{
var colorText = color.toString();
this._codeMirror.replaceRange(colorText, this._range.from, this._range.to);
this._range.to.ch = this._range.from.ch + colorText.length;
this.text = color.toString();
this._color = color;
},
......@@ -77,15 +73,31 @@ WebInspector.CodeMirrorColorEditingController.prototype = {
{
this._delegate = delegate;
},
get text()
{
var from = {line: this._range.startLine, ch: this._range.startColumn};
var to = {line: this._range.endLine, ch: this._range.endColumn};
return this._codeMirror.getRange(from, to);
},
set text(text)
{
var from = {line: this._range.startLine, ch: this._range.startColumn};
var to = {line: this._range.endLine, ch: this._range.endColumn};
this._codeMirror.replaceRange(text, from, to);
var lines = text.split("\n");
var endLine = this._range.startLine + lines.length - 1;
var endColumn = lines.length > 1 ? lines.lastValue.length : this._range.startColumn + text.length;
this._range = new WebInspector.TextRange(this._range.startLine, this._range.startColumn, endLine, endColumn);
},
presentHoverMenu: function()
{
this._hoverMenu = new WebInspector.HoverMenu(this);
this._hoverMenu.element.classList.add("color");
this._bounds = this._codeMirror.boundsForRange({
start: this._range.from,
end: this._range.to
});
this._bounds = this._marker.bounds;
this._hoverMenu.present(this._bounds);
},
......
......@@ -305,8 +305,7 @@ WebInspector.CodeMirrorTokenTrackingController.prototype = {
if (!token || !token.type || !token.string) {
if (this._hoveredMarker && this._delegate && typeof this._delegate.tokenTrackingControllerMouseOutOfHoveredMarker === "function") {
var markers = this._codeMirror.findMarksAt(position);
if (!markers.contains(this._hoveredMarker))
if (!this._codeMirror.findMarksAt(position).contains(this._hoveredMarker.codeMirrorTextMarker))
this._delegate.tokenTrackingControllerMouseOutOfHoveredMarker(this, this._hoveredMarker);
}
......
......@@ -286,6 +286,7 @@
<script src="ResourceSearchMatchObject.js"></script>
<script src="SearchResultTreeElement.js"></script>
<script src="TextRange.js"></script>
<script src="TextMarker.js"></script>
<script src="ConsoleMessage.js"></script>
<script src="ConsoleMessageImpl.js"></script>
<script src="ConsoleGroup.js"></script>
......
......@@ -1016,7 +1016,7 @@ WebInspector.SourceCodeTextEditor.prototype = {
_hasColorMarkers: function()
{
for (var marker of this.markers) {
if (marker.__markedColor)
if (marker.type === WebInspector.TextMarker.Type.Color)
return true;
}
return false;
......@@ -1070,7 +1070,7 @@ WebInspector.SourceCodeTextEditor.prototype = {
}
if (this.tokenTrackingController.mode === WebInspector.CodeMirrorTokenTrackingController.Mode.MarkedTokens) {
var markers = this.findMarkersAtPosition(candidate.hoveredTokenRange.start);
var markers = this.markersAtPosition(candidate.hoveredTokenRange.start);
if (markers.length > 0)
this._tokenTrackingControllerHighlightedMarkedExpression(candidate, markers);
else
......@@ -1298,7 +1298,7 @@ WebInspector.SourceCodeTextEditor.prototype = {
{
var colorMarker;
for (var marker of markers) {
if (marker.__markedColor) {
if (marker.type === WebInspector.TextMarker.Type.Color) {
colorMarker = marker;
break;
}
......@@ -1356,7 +1356,7 @@ WebInspector.SourceCodeTextEditor.prototype = {
colorEditingControllerDidFinishEditing: function(colorEditingController)
{
this._updateColorMarkers(colorEditingController.range.from.line);
this._updateColorMarkers(colorEditingController.range.startLine);
this._ignoreContentDidChange--;
......
......@@ -603,13 +603,16 @@ WebInspector.TextEditor.prototype = {
get markers()
{
// FIXME: we should not return CodeMirror TextMarker objects but rather wrappers.
return this._codeMirror.getAllMarks();
return this._codeMirror.getAllMarks().map(function(codeMirrorTextMarker) {
return WebInspector.TextMarker.textMarkerForCodeMirrorTextMarker(codeMirrorTextMarker);
});
},
findMarkersAtPosition: function(position)
markersAtPosition: function(position)
{
return this._codeMirror.findMarksAt(position);
return this._codeMirror.findMarksAt(position).map(function(codeMirrorTextMarker) {
return WebInspector.TextMarker.textMarkerForCodeMirrorTextMarker(codeMirrorTextMarker);
});
},
createColorMarkers: function(lineNumber)
......
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
WebInspector.TextMarker = function(codeMirrorTextMarker, type)
{
WebInspector.Object.call(this);
this._codeMirrorTextMarker = codeMirrorTextMarker;
codeMirrorTextMarker.__webInspectorTextMarker = this;
this._type = type || WebInspector.TextMarker.Type.Plain;
}
WebInspector.TextMarker.Type = {
Color: "text-marker-type-color",
Plain: "text-marker-type-plain"
};
WebInspector.TextMarker.textMarkerForCodeMirrorTextMarker = function(codeMirrorTextMarker)
{
return codeMirrorTextMarker.__webInspectorTextMarker || new WebInspector.TextMarker(codeMirrorTextMarker);
};
WebInspector.TextMarker.prototype = {
constructor: WebInspector.TextMarker,
__proto__: WebInspector.Object.prototype,
// Public
get codeMirrorTextMarker()
{
return this._codeMirrorTextMarker;
},
get type()
{
return this._type;
},
get range()
{
var range = this._codeMirrorTextMarker.find();
if (!range)
return null;
return new WebInspector.TextRange(range.from.line, range.from.ch, range.to.line, range.to.ch);
},
get bounds()
{
var range = this._codeMirrorTextMarker.find();
if (!range)
return WebInspector.Rect.ZERO_RECT;
return this._codeMirrorTextMarker.doc.cm.boundsForRange({
start: range.from,
end: range.to
});
},
clear: function()
{
this._codeMirrorTextMarker.clear();
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment