Web Inspector: [DTE] implement "tokenAtTextPosition" method

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

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

Source/WebCore:

Test: inspector/editor/text-editor-token-at-position.html

- Add TextEditor.tokenAtTextPosition method
- Implement tokenAtTextPosition method in DefaultTextEditor

* inspector/front-end/DefaultTextEditor.js:
(WebInspector.DefaultTextEditor.prototype.tokenAtTextPosition):
(WebInspector.TextEditorMainPanel.prototype.compare):
(WebInspector.TextEditorMainPanel.prototype.tokenAtTextPosition):
(WebInspector.TextEditorMainPanel.prototype._tokenAtUnhighlightedLine):
(WebInspector.TextEditorMainPanel.prototype.set mimeType):
(WebInspector.TextEditorMainPanel.prototype.get mimeType):
* inspector/front-end/TextEditor.js:
(WebInspector.TextEditor.prototype.tokenAtTextPosition):
* inspector/front-end/TextEditorHighlighter.js:
(WebInspector.TextEditorHighlighter):
(WebInspector.TextEditorHighlighter.prototype.get mimeType):
(WebInspector.TextEditorHighlighter.prototype.set mimeType):

LayoutTests:

Add a test to verify TextEditor.tokenAtTextPosition method.

* inspector/editor/text-editor-token-at-position-expected.txt: Added.
* inspector/editor/text-editor-token-at-position.html: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@147030 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 7a8f3122
2013-03-27 Andrey Lushnikov <lushnikov@chromium.org>
Web Inspector: [DTE] implement "tokenAtTextPosition" method
https://bugs.webkit.org/show_bug.cgi?id=113390
Reviewed by Pavel Feldman.
Add a test to verify TextEditor.tokenAtTextPosition method.
* inspector/editor/text-editor-token-at-position-expected.txt: Added.
* inspector/editor/text-editor-token-at-position.html: Added.
2013-03-27 Sergey Ryazanov <serya@chromium.org>
REGRESSION (r146588): Cannot correctly display Chinese SNS Renren
Test editor tokenAtTextPosition method.
Running: testHighlightedText
Line: function foo(a, b) {
Column #0 (char 'f') - token: {"startColumn":0,"endColumn":7,"type":"javascript-keyword"}
Column #10 (char 'o') - token: {"startColumn":9,"endColumn":11,"type":"javascript-ident"}
Column #13 (char 'a') - token: {"startColumn":13,"endColumn":13,"type":"javascript-ident"}
Line: var f = /.a/.test(a); /*
Column #8 (char 'f') - token: {"startColumn":8,"endColumn":8,"type":"javascript-ident"}
Column #14 (char 'a') - token: {"startColumn":12,"endColumn":15,"type":"javascript-regexp"}
Column #19 (char 's') - token: {"startColumn":17,"endColumn":20,"type":"javascript-ident"}
Line: this is a comment */
Column #0 (char 't') - token: {"startColumn":0,"endColumn":19,"type":"javascript-comment"}
Column #5 (char 'i') - token: {"startColumn":0,"endColumn":19,"type":"javascript-comment"}
Line: return f + "looongword";
Column #5 (char 'e') - token: {"startColumn":4,"endColumn":9,"type":"javascript-keyword"}
Column #20 (char 'n') - token: {"startColumn":15,"endColumn":26,"type":"javascript-string"}
Running: testUnhighlightedText
Line: function foo(a, b) {
Column #0 (char 'f') - token: {"startColumn":0,"endColumn":7,"type":"javascript-keyword"}
Column #10 (char 'o') - token: {"startColumn":9,"endColumn":11,"type":"javascript-ident"}
Column #13 (char 'a') - token: {"startColumn":13,"endColumn":13,"type":"javascript-ident"}
Line: var f = /.a/.test(a); /*
Column #8 (char 'f') - token: {"startColumn":8,"endColumn":8,"type":"javascript-ident"}
Column #14 (char 'a') - token: {"startColumn":12,"endColumn":15,"type":"javascript-regexp"}
Column #19 (char 's') - token: {"startColumn":17,"endColumn":20,"type":"javascript-ident"}
Line: this is a comment */
Column #0 (char 't') - token: {"startColumn":0,"endColumn":3,"type":"javascript-keyword"}
Column #5 (char 'i') - token: {"startColumn":5,"endColumn":6,"type":"javascript-ident"}
Line: return f + "looongword";
Column #5 (char 'e') - token: {"startColumn":4,"endColumn":9,"type":"javascript-keyword"}
Column #20 (char 'n') - token: {"startColumn":15,"endColumn":26,"type":"javascript-string"}
<html>
<head>
<script src="../../http/tests/inspector/inspector-test.js"></script>
<script src="editor-test.js"></script>
<script>
function test()
{
var text = [
"function foo(a, b) {",
" var f = /.a/.test(a); /*",
"this is a comment */",
" return f + \"looongword\";"
];
var positions = [
[0, 10, 13],
[8, 14, 19],
[0, 5],
[5, 20]
];
function testTokenAtPosition(textEditor)
{
for(var i = 0; i < positions.length; ++i) {
var columns = positions[i];
InspectorTest.addResult("Line: " + text[i]);
for(var j = 0; j < columns.length; ++j) {
var column = columns[j];
InspectorTest.addResult("Column #" + column + " (char '" + text[i].charAt(column) + "') - token: " + JSON.stringify(textEditor.tokenAtTextPosition(i, column)));
}
}
}
InspectorTest.runTestSuite([
function testHighlightedText(next)
{
WebInspector.TextEditorHighlighter._MaxLineCount = 1000;
var textEditor = InspectorTest.createTestEditor();
textEditor.mimeType = "text/javascript";
textEditor.setText(text.join("\n"));
testTokenAtPosition(textEditor);
next();
},
function testUnhighlightedText(next)
{
// switch highlight off
WebInspector.TextEditorHighlighter._MaxLineCount = 0;
var textEditor = InspectorTest.createTestEditor();
textEditor.mimeType = "text/javascript";
textEditor.setText(text.join("\n"));
testTokenAtPosition(textEditor);
next();
},
]);
}
</script>
</head>
<body onload="runTest()">
<p>
Test editor tokenAtTextPosition method.
</p>
</body>
</html>
2013-03-27 Andrey Lushnikov <lushnikov@chromium.org>
Web Inspector: [DTE] implement "tokenAtTextPosition" method
https://bugs.webkit.org/show_bug.cgi?id=113390
Reviewed by Pavel Feldman.
Test: inspector/editor/text-editor-token-at-position.html
- Add TextEditor.tokenAtTextPosition method
- Implement tokenAtTextPosition method in DefaultTextEditor
* inspector/front-end/DefaultTextEditor.js:
(WebInspector.DefaultTextEditor.prototype.tokenAtTextPosition):
(WebInspector.TextEditorMainPanel.prototype.compare):
(WebInspector.TextEditorMainPanel.prototype.tokenAtTextPosition):
(WebInspector.TextEditorMainPanel.prototype._tokenAtUnhighlightedLine):
(WebInspector.TextEditorMainPanel.prototype.set mimeType):
(WebInspector.TextEditorMainPanel.prototype.get mimeType):
* inspector/front-end/TextEditor.js:
(WebInspector.TextEditor.prototype.tokenAtTextPosition):
* inspector/front-end/TextEditorHighlighter.js:
(WebInspector.TextEditorHighlighter):
(WebInspector.TextEditorHighlighter.prototype.get mimeType):
(WebInspector.TextEditorHighlighter.prototype.set mimeType):
2013-03-27 Charles Wei <charles.wei@torchmobile.com.cn>
Remove build warning for unused function parameters in indexeddb.
......@@ -122,6 +122,16 @@ WebInspector.DefaultTextEditor.prototype = {
{
this._textModel.markClean();
},
/**
* @param {number} lineNumber
* @param {number} column
* @return {?{startColumn: number, endColumn: number, type: string}}
*/
tokenAtTextPosition: function(lineNumber, column)
{
return this._mainPanel.tokenAtTextPosition(lineNumber, column);
},
/**
* @param {WebInspector.TextRange} range
* @return {string}
......@@ -1406,6 +1416,66 @@ WebInspector.TextEditorMainPanel._ConsecutiveWhitespaceChars = {
};
WebInspector.TextEditorMainPanel.prototype = {
/**
* @param {number} lineNumber
* @param {number} column
* @return {?{startColumn: number, endColumn: number, type: string}}
*/
tokenAtTextPosition: function(lineNumber, column)
{
if (lineNumber >= this._textModel.linesCount || lineNumber < 0)
return null;
var line = this._textModel.line(lineNumber);
if (column >= line.length || column < 0)
return null;
var highlight = this._textModel.getAttribute(lineNumber, "highlight");
if (!highlight)
return this._tokenAtUnhighlightedLine(line, column);
function compare(value, object)
{
if (value >= object.startColumn && value <= object.endColumn)
return 0;
return value - object.startColumn;
}
var index = binarySearch(column, highlight.ranges, compare);
if (index >= 0) {
var range = highlight.ranges[index];
return {
startColumn: range.startColumn,
endColumn: range.endColumn,
type: range.token
};
}
return null;
},
/**
* @param {string} line
* @param {number} column
* @return {?{startColumn: number, endColumn: number, type: string}}
*/
_tokenAtUnhighlightedLine: function(line, column)
{
var tokenizer = WebInspector.SourceTokenizer.Registry.getInstance().getTokenizer(this.mimeType);
tokenizer.condition = tokenizer.createInitialCondition();
tokenizer.line = line;
var lastTokenizedColumn = 0;
while (lastTokenizedColumn < line.length) {
var newColumn = tokenizer.nextToken(lastTokenizedColumn);
if (column < newColumn) {
if (!tokenizer.tokenType)
return null;
return {
startColumn: lastTokenizedColumn,
endColumn: newColumn - 1,
type: tokenizer.tokenType
};
} else
lastTokenizedColumn = newColumn;
}
return null;
},
_registerShortcuts: function()
{
var keys = WebInspector.KeyboardShortcut.Keys;
......@@ -1623,6 +1693,11 @@ WebInspector.TextEditorMainPanel.prototype = {
this._highlighter.mimeType = mimeType;
},
get mimeType()
{
return this._highlighter.mimeType;
},
/**
* @param {boolean} readOnly
* @param {boolean} requestFocus
......
......@@ -45,6 +45,13 @@ WebInspector.TextEditor.prototype = {
isClean: function() { },
markClean: function() { },
/**
* @param {number} lineNumber
* @param {number} column
* @return {?{startColumn: number, endColumn: number, type: string}}
*/
tokenAtTextPosition: function(lineNumber, column) { },
/**
* @param {string} mimeType
*/
......
......@@ -35,7 +35,8 @@
WebInspector.TextEditorHighlighter = function(textModel, damageCallback)
{
this._textModel = textModel;
this._tokenizer = WebInspector.SourceTokenizer.Registry.getInstance().getTokenizer("text/html");
this._mimeType = "text/html";
this._tokenizer = WebInspector.SourceTokenizer.Registry.getInstance().getTokenizer(this._mimeType);
this._damageCallback = damageCallback;
this._highlightChunkLimit = 1000;
this._highlightLineLimit = 500;
......@@ -44,11 +45,22 @@ WebInspector.TextEditorHighlighter = function(textModel, damageCallback)
WebInspector.TextEditorHighlighter._MaxLineCount = 10000;
WebInspector.TextEditorHighlighter.prototype = {
get mimeType()
{
return this._mimeType;
},
/**
* @param {string} mimeType
*/
set mimeType(mimeType)
{
var tokenizer = WebInspector.SourceTokenizer.Registry.getInstance().getTokenizer(mimeType);
if (tokenizer)
if (tokenizer) {
this._tokenizer = tokenizer;
this._mimeType = mimeType;
}
},
set highlightChunkLimit(highlightChunkLimit)
......
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