Changeset 145370 in webkit


Ignore:
Timestamp:
Mar 11, 2013 8:59:24 AM (11 years ago)
Author:
commit-queue@webkit.org
Message:

Web Inspector: [CodeMirror] add token highlight feature
https://bugs.webkit.org/show_bug.cgi?id=112009

Patch by Andrey Lushnikov <lushnikov@chromium.org> on 2013-03-11
Reviewed by Pavel Feldman.

Handle CodeMirror's "cursorActivity" event, check selection for being
a word and highlight all its occurrences via CodeMirror.addOverlay method.

No new tests.

  • inspector/front-end/CodeMirrorTextEditor.js:

(WebInspector.CodeMirrorTextEditor):
(WebInspector.CodeMirrorTextEditor.TokenHighlighter):
(WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._cursorChange):
(WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._isWord):
(WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._removeHighlight):
(WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._addHighlight.nextToken):
(WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._addHighlight):

  • inspector/front-end/cm/cmdevtools.css:

(.cm-token-highlight):

Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r145368 r145370  
     12013-03-11  Andrey Lushnikov  <lushnikov@chromium.org>
     2
     3        Web Inspector: [CodeMirror] add token highlight feature
     4        https://bugs.webkit.org/show_bug.cgi?id=112009
     5
     6        Reviewed by Pavel Feldman.
     7
     8        Handle CodeMirror's "cursorActivity" event, check selection for being
     9        a word and highlight all its occurrences via CodeMirror.addOverlay method.
     10
     11        No new tests.
     12
     13        * inspector/front-end/CodeMirrorTextEditor.js:
     14        (WebInspector.CodeMirrorTextEditor):
     15        (WebInspector.CodeMirrorTextEditor.TokenHighlighter):
     16        (WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._cursorChange):
     17        (WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._isWord):
     18        (WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._removeHighlight):
     19        (WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._addHighlight.nextToken):
     20        (WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype._addHighlight):
     21        * inspector/front-end/cm/cmdevtools.css:
     22        (.cm-token-highlight):
     23
    1242013-03-11  Andrey Lushnikov  <lushnikov@chromium.org>
    225
  • trunk/Source/WebCore/inspector/front-end/CodeMirrorTextEditor.js

    r145368 r145370  
    6565    }
    6666
     67    this._tokenHighlighter = new WebInspector.CodeMirrorTextEditor.TokenHighlighter(this._codeMirror);
     68
    6769    this._codeMirror.on("change", this._change.bind(this));
    6870    this._codeMirror.on("gutterClick", this._gutterClick.bind(this));
     
    418420    __proto__: WebInspector.View.prototype
    419421}
     422
     423WebInspector.CodeMirrorTextEditor.TokenHighlighter = function(codeMirror)
     424{
     425    this._codeMirror = codeMirror;
     426    this._codeMirror.on("cursorActivity", this._cursorChange.bind(this));
     427}
     428
     429WebInspector.CodeMirrorTextEditor.TokenHighlighter.prototype = {
     430    _cursorChange: function()
     431    {
     432        this._codeMirror.operation(this._removeHighlight.bind(this));
     433        var selectionStart = this._codeMirror.getCursor("start");
     434        var selectionEnd = this._codeMirror.getCursor("end");
     435        if (selectionStart.line !== selectionEnd.line)
     436            return;
     437        if (selectionStart.ch === selectionEnd.ch)
     438            return;
     439
     440        var selectedText = this._codeMirror.getSelection();
     441        if (this._isWord(selectedText, selectionStart.line, selectionStart.ch, selectionEnd.ch))
     442            this._codeMirror.operation(this._addHighlight.bind(this, selectedText));
     443    },
     444
     445    _isWord: function(selectedText, lineNumber, startColumn, endColumn)
     446    {
     447        var line = this._codeMirror.getLine(lineNumber);
     448        var leftBound = startColumn === 0 || !WebInspector.TextUtils.isWordChar(line.charAt(startColumn - 1));
     449        var rightBound = endColumn === line.length || !WebInspector.TextUtils.isWordChar(line.charAt(endColumn));
     450        return leftBound && rightBound && WebInspector.TextUtils.isWord(selectedText);
     451    },
     452
     453    _removeHighlight: function()
     454    {
     455        if (this._overlayMode) {
     456            this._codeMirror.removeOverlay(this._overlayMode);
     457            delete this._overlayMode;
     458        }
     459    },
     460
     461    _addHighlight: function(token)
     462    {
     463        const tokenFirstChar = token.charAt(0);
     464        function nextToken(stream)
     465        {
     466            if (stream.match(token))
     467                return "token-highlight";
     468            stream.next();
     469            if (!stream.skipTo(tokenFirstChar))
     470                stream.skipToEnd();
     471        }
     472
     473        this._overlayMode = {
     474            token: nextToken
     475        };
     476        this._codeMirror.addOverlay(this._overlayMode);
     477    }
     478}
  • trunk/Source/WebCore/inspector/front-end/cm/cmdevtools.css

    r145093 r145370  
    3838    background-color: rgb(171, 191, 254) !important;
    3939    outline: 1px solid rgb(64, 115, 244);
     40}
     41
     42.cm-token-highlight {
     43    border: 1px solid gray;
     44    border-radius: 3px;
     45    margin: -1px;
    4046}
    4147
Note: See TracChangeset for help on using the changeset viewer.