Title: [233820] trunk/Source/WebInspectorUI
Revision
233820
Author
mattba...@apple.com
Date
2018-07-13 15:27:30 -0700 (Fri, 13 Jul 2018)

Log Message

Web Inspector: Basic blocks highlighting should use line/column locations instead of offsets
https://bugs.webkit.org/show_bug.cgi?id=187613
<rdar://problem/42131808>

Reviewed by Joseph Pecoraro.

* UserInterface/Controllers/BasicBlockAnnotator.js:
Basic blocks sent from the backend include offsets into the original
file, rather than line/column locations. In order to translate to positions
within CodeMirror, we need to calculate the original line and column
for each block.

(WI.BasicBlockAnnotator.prototype.insertAnnotations):
(WI.BasicBlockAnnotator.prototype._calculateBasicBlockPositions.offsetToPosition):
(WI.BasicBlockAnnotator.prototype._calculateBasicBlockPositions):
(WI.BasicBlockAnnotator.prototype._annotateBasicBlockExecutionRanges.):
(WI.BasicBlockAnnotator.prototype._annotateBasicBlockExecutionRanges):
(WI.BasicBlockAnnotator.prototype._highlightTextForBasicBlock):

* UserInterface/Models/SourceCodePosition.js:
(WI.SourceCodePosition.prototype.offsetColumn):

* UserInterface/Views/TextEditor.js:
(WI.TextEditor.prototype.getTextInRange):
(WI.TextEditor.prototype.addStyleToTextRange):
Better encapsulation for CodeMirror positions.

* UserInterface/Workers/Formatter/FormatterUtilities.js:
(get if):
Update String.prototype.lineEndings to support additional line separators.

Modified Paths

Diff

Modified: trunk/Source/WebInspectorUI/ChangeLog (233819 => 233820)


--- trunk/Source/WebInspectorUI/ChangeLog	2018-07-13 22:26:10 UTC (rev 233819)
+++ trunk/Source/WebInspectorUI/ChangeLog	2018-07-13 22:27:30 UTC (rev 233820)
@@ -1,5 +1,38 @@
 2018-07-13  Matt Baker  <mattba...@apple.com>
 
+        Web Inspector: Basic blocks highlighting should use line/column locations instead of offsets
+        https://bugs.webkit.org/show_bug.cgi?id=187613
+        <rdar://problem/42131808>
+
+        Reviewed by Joseph Pecoraro.
+
+        * UserInterface/Controllers/BasicBlockAnnotator.js:
+        Basic blocks sent from the backend include offsets into the original
+        file, rather than line/column locations. In order to translate to positions
+        within CodeMirror, we need to calculate the original line and column
+        for each block.
+
+        (WI.BasicBlockAnnotator.prototype.insertAnnotations):
+        (WI.BasicBlockAnnotator.prototype._calculateBasicBlockPositions.offsetToPosition):
+        (WI.BasicBlockAnnotator.prototype._calculateBasicBlockPositions):
+        (WI.BasicBlockAnnotator.prototype._annotateBasicBlockExecutionRanges.):
+        (WI.BasicBlockAnnotator.prototype._annotateBasicBlockExecutionRanges):
+        (WI.BasicBlockAnnotator.prototype._highlightTextForBasicBlock):
+
+        * UserInterface/Models/SourceCodePosition.js:
+        (WI.SourceCodePosition.prototype.offsetColumn):
+
+        * UserInterface/Views/TextEditor.js:
+        (WI.TextEditor.prototype.getTextInRange):
+        (WI.TextEditor.prototype.addStyleToTextRange):
+        Better encapsulation for CodeMirror positions.
+
+        * UserInterface/Workers/Formatter/FormatterUtilities.js:
+        (get if):
+        Update String.prototype.lineEndings to support additional line separators.
+
+2018-07-13  Matt Baker  <mattba...@apple.com>
+
         Web Inspector: Execution highlighting in the frontend should be line/column-based
         https://bugs.webkit.org/show_bug.cgi?id=187532
         <rdar://problem/42035580>

Modified: trunk/Source/WebInspectorUI/UserInterface/Controllers/BasicBlockAnnotator.js (233819 => 233820)


--- trunk/Source/WebInspectorUI/UserInterface/Controllers/BasicBlockAnnotator.js	2018-07-13 22:26:10 UTC (rev 233819)
+++ trunk/Source/WebInspectorUI/UserInterface/Controllers/BasicBlockAnnotator.js	2018-07-13 22:27:30 UTC (rev 233820)
@@ -46,13 +46,49 @@
     {
         if (!this.isActive())
             return;
-        this._annotateBasicBlockExecutionRanges();
+        this._script.requestContent().then(this._annotateBasicBlockExecutionRanges.bind(this));
     }
 
     // Private
 
+    _calculateBasicBlockPositions(basicBlocks, content)
+    {
+        if (!basicBlocks || !basicBlocks.length)
+            return;
+
+        let lineEndings = [];
+        let lineEndingLengths = [];
+        let pattern = /\r\n?|\n/g;
+        let match = pattern.exec(content);
+        while (match) {
+            lineEndings.push(match.index);
+            lineEndingLengths.push(match[0].length);
+            match = pattern.exec(content)
+        }
+
+        function offsetToPosition(offset) {
+            let lineNumber = lineEndings.upperBound(offset - 1);
+            if (lineNumber) {
+                let previousLine = lineNumber - 1;
+                var columnNumber = offset - lineEndings[previousLine] - lineEndingLengths[previousLine];
+            } else
+                var columnNumber = offset;
+            return new WI.SourceCodePosition(lineNumber, columnNumber);
+        }
+
+        for (let block of basicBlocks) {
+            block.startPosition = offsetToPosition(block.startOffset);
+            block.endPosition = offsetToPosition(block.endOffset);
+        }
+    }
+
     _annotateBasicBlockExecutionRanges()
     {
+        let content = this._script.content;
+        console.assert(content, "Missing script content for basic block annotations.");
+        if (!content)
+            return;
+
         var sourceID = this._script.id;
         var startTime = Date.now();
 
@@ -65,16 +101,18 @@
             if (!this.isActive())
                 return;
 
-            var {startOffset, endOffset} = this.sourceCodeTextEditor.visibleRangeOffsets();
+            this._calculateBasicBlockPositions(basicBlocks, content);
+
+            let {startPosition, endPosition} = this.sourceCodeTextEditor.visibleRangePositions();
             basicBlocks = basicBlocks.filter(function(block) {
                 // Viewport: [--]
                 // Block:         [--]
-                if (block.startOffset > endOffset)
+                if (block.startPosition.isAfter(endPosition))
                     return false;
 
                 // Viewport:      [--]
                 // Block:    [--]
-                if (block.endOffset < startOffset)
+                if (block.endPosition.isBefore(startPosition))
                     return false;
 
                 return true;
@@ -103,13 +141,12 @@
         console.assert(basicBlock.startOffset <= basicBlock.endOffset && basicBlock.startOffset >= 0 && basicBlock.endOffset >= 0, "" + basicBlock.startOffset + ":" + basicBlock.endOffset);
         console.assert(!basicBlock.hasExecuted);
 
-        var startPosition = this.sourceCodeTextEditor.originalOffsetToCurrentPosition(basicBlock.startOffset);
-        var endPosition = this.sourceCodeTextEditor.originalOffsetToCurrentPosition(basicBlock.endOffset);
+        let startPosition = this.sourceCodeTextEditor.originalPositionToCurrentPosition(basicBlock.startPosition);
+        let endPosition = this.sourceCodeTextEditor.originalPositionToCurrentPosition(basicBlock.endPosition);
         if (this._isTextRangeOnlyClosingBrace(startPosition, endPosition))
             return null;
 
-        var marker = this.sourceCodeTextEditor.addStyleToTextRange(startPosition, endPosition, WI.BasicBlockAnnotator.HasNotExecutedClassName);
-        return marker;
+        return this.sourceCodeTextEditor.addStyleToTextRange(startPosition, endPosition, WI.BasicBlockAnnotator.HasNotExecutedClassName);
     }
 
     _isTextRangeOnlyClosingBrace(startPosition, endPosition)

Modified: trunk/Source/WebInspectorUI/UserInterface/Models/SourceCodePosition.js (233819 => 233820)


--- trunk/Source/WebInspectorUI/UserInterface/Models/SourceCodePosition.js	2018-07-13 22:26:10 UTC (rev 233819)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/SourceCodePosition.js	2018-07-13 22:27:30 UTC (rev 233820)
@@ -42,6 +42,12 @@
         return new WI.SourceCodePosition(this._lineNumber, this._columnNumber + delta);
     }
 
+    offsetColumn(delta)
+    {
+        console.assert(this._columnNumber + delta >= 0);
+        return new WI.SourceCodePosition(this._lineNumber, this._columnNumber + delta);
+    }
+
     equals(position)
     {
         return this._lineNumber === position.lineNumber && this._columnNumber === position.columnNumber;

Modified: trunk/Source/WebInspectorUI/UserInterface/Views/TextEditor.js (233819 => 233820)


--- trunk/Source/WebInspectorUI/UserInterface/Views/TextEditor.js	2018-07-13 22:26:10 UTC (rev 233819)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/TextEditor.js	2018-07-13 22:27:30 UTC (rev 233820)
@@ -467,13 +467,13 @@
 
     getTextInRange(startPosition, endPosition)
     {
-        return this._codeMirror.getRange(startPosition, endPosition);
+        return this._codeMirror.getRange(startPosition.toCodeMirror(), endPosition.toCodeMirror());
     }
 
     addStyleToTextRange(startPosition, endPosition, styleClassName)
     {
-        endPosition.ch += 1;
-        return this._codeMirror.getDoc().markText(startPosition, endPosition, {className: styleClassName, inclusiveLeft: true, inclusiveRight: true});
+        endPosition = endPosition.offsetColumn(1);
+        return this._codeMirror.getDoc().markText(startPosition.toCodeMirror(), endPosition.toCodeMirror(), {className: styleClassName, inclusiveLeft: true, inclusiveRight: true});
     }
 
     revealPosition(position, textRangeToSelect, forceUnformatted, noHighlight)

Modified: trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/FormatterUtilities.js (233819 => 233820)


--- trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/FormatterUtilities.js	2018-07-13 22:26:10 UTC (rev 233819)
+++ trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/FormatterUtilities.js	2018-07-13 22:27:30 UTC (rev 233820)
@@ -38,11 +38,11 @@
     value()
     {
         let lineEndings = [];
-
-        let index = this.indexOf("\n");
-        while (index !== -1) {
-            lineEndings.push(index);
-            index = this.indexOf("\n", index + 1);
+        let pattern = /\r\n?|\n/g;
+        let match = pattern.exec(this);
+        while (match) {
+            lineEndings.push(match.index);
+            match = pattern.exec(this)
         }
 
         lineEndings.push(this.length);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to