DevTools: [CodeMirror] remove auto-inserted whitespaces

Currently, text editor auto-inserts spaces/tabs in certain cases. For example,
if you hit "enter" when your cursor is located on indented line, the new line
will be auto-indented. If you hit "enter" one more time, a new line will be
added, but the previous empty line with trailing auto-added whitespaces will
persist.

This patch suggests removing auto-added indents on every next "enter" keypress.
Note, that they will be removed only if they have not been edited.

R=vsevik, apavlov

Review URL: https://codereview.chromium.org/402433007

git-svn-id: svn://svn.chromium.org/blink/trunk@178515 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 61221bf4
This test verifies that auto-appended spaces are removed on consequent enters.
function (){}
if (a == b) {
Running: testCollapsedBlock
function.(){
....|
}
....if.(a.==.b).{
Running: testOpenCurlyBrace
function.(){}
....if.(a.==.b).{
........|
Running: testSmartIndent
function.(){}
..
..|..if.(a.==.b).{
Running: testMultiCursorSelection
function.(){}
..
..|..
....|if.(a.==.b).{
Running: testEditedAutoIndent
function.(){}
....if.(a.==.b).{
........W
........|
<html>
<head>
<script src="../../http/tests/inspector/inspector-test.js"></script>
<script src="editor-test.js"></script>
<script>
function codeSnippet() {
return document.getElementById("codeSnippet").textContent;
}
function test() {
var textEditor = InspectorTest.createTestEditor();
textEditor.setMimeType("text/javascript");
textEditor.setReadOnly(false);
textEditor.element.focus();
InspectorTest.evaluateInPage("codeSnippet();", onCodeSnippet);
var codeSnippetText;
function onCodeSnippet(result)
{
codeSnippetText = result.value;
InspectorTest.runTestSuite(testSuite);
}
function dumpAndNext(next)
{
function innerDumpAndNext()
{
InspectorTest.dumpTextWithSelection(textEditor, true);
next();
}
return innerDumpAndNext;
}
function doubleEnter(next)
{
function onFirstEnter()
{
InspectorTest.fakeKeyEvent(textEditor, "enter", [], dumpAndNext(next));
}
InspectorTest.fakeKeyEvent(textEditor, "enter", [], onFirstEnter);
}
var testSuite = [
function testCollapsedBlock(next)
{
textEditor.setText(codeSnippetText);
InspectorTest.setLineSelections(textEditor, [
{line: 0, column: 12}
]);
doubleEnter(next);
},
function testOpenCurlyBrace(next)
{
textEditor.setText(codeSnippetText);
InspectorTest.setLineSelections(textEditor, [
{line: 1, column: 17}
]);
doubleEnter(next);
},
function testSmartIndent(next)
{
textEditor.setText(codeSnippetText);
InspectorTest.setLineSelections(textEditor, [
{line: 1, column: 2}
]);
doubleEnter(next);
},
function testMultiCursorSelection(next)
{
textEditor.setText(codeSnippetText);
InspectorTest.setLineSelections(textEditor, [
{line: 1, column: 2},
{line: 1, column: 4}
]);
doubleEnter(next);
},
function testEditedAutoIndent(next)
{
textEditor.setText(codeSnippetText);
InspectorTest.setLineSelections(textEditor, [
{line: 1, column: 17}
]);
InspectorTest.fakeKeyEvent(textEditor, "enter", [], onEnter);
function onEnter()
{
InspectorTest.fakeKeyEvent(textEditor, "W", [], onEditedText);
}
function onEditedText()
{
InspectorTest.fakeKeyEvent(textEditor, "enter", [], dumpAndNext(next));
}
},
];
}
</script>
</head>
<body onload="runTest();">
<p>
This test verifies that auto-appended spaces are removed on consequent enters.
</p>
<pre id="codeSnippet">
function (){}
if (a == b) {
</pre>
</body>
</html>
...@@ -28,15 +28,15 @@ function.foo(n).{ ...@@ -28,15 +28,15 @@ function.foo(n).{
....function.bar().{ ....function.bar().{
........return.42; ........return.42;
....} ....}
....
....var.sum.=.0; ....var.sum.=.0;
....for.(var.i.=.0;.i.<.n;.++i).{ ....for.(var.i.=.0;.i.<.n;.++i).{
........for.(var.j.=.0;.j.<.n;.++j).{ ........for.(var.j.=.0;.j.<.n;.++j).{
............sum.+=.i.+.j; ............sum.+=.i.+.j;
........} ........}
....} ....}
....
....
....if.(sum.>.1000).{ ....if.(sum.>.1000).{
........while.(sum.>.0).{ ........while.(sum.>.0).{
............--sum; ............--sum;
......
...@@ -248,6 +248,7 @@ CodeMirror.commands.smartNewlineAndIndent = function(codeMirror) ...@@ -248,6 +248,7 @@ CodeMirror.commands.smartNewlineAndIndent = function(codeMirror)
replacements.push("\n" + indent.substring(0, Math.min(cur.ch, indent.length))); replacements.push("\n" + indent.substring(0, Math.min(cur.ch, indent.length)));
} }
codeMirror.replaceSelections(replacements); codeMirror.replaceSelections(replacements);
codeMirror._codeMirrorTextEditor._onAutoAppendedSpaces();
} }
} }
...@@ -325,6 +326,25 @@ WebInspector.CodeMirrorTextEditor.MaximumNumberOfWhitespacesPerSingleSpan = 16; ...@@ -325,6 +326,25 @@ WebInspector.CodeMirrorTextEditor.MaximumNumberOfWhitespacesPerSingleSpan = 16;
WebInspector.CodeMirrorTextEditor.MaxEditableTextSize = 1024 * 1024 * 10; WebInspector.CodeMirrorTextEditor.MaxEditableTextSize = 1024 * 1024 * 10;
WebInspector.CodeMirrorTextEditor.prototype = { WebInspector.CodeMirrorTextEditor.prototype = {
_onAutoAppendedSpaces: function()
{
this._autoAppendedSpaces = this._autoAppendedSpaces || [];
for (var i = 0; i < this._autoAppendedSpaces.length; ++i) {
var position = this._autoAppendedSpaces[i].resolve();
if (!position)
continue;
var line = this.line(position.lineNumber);
if (line.length === position.columnNumber && WebInspector.TextUtils.lineIndent(line).length === line.length)
this._codeMirror.replaceRange("", new CodeMirror.Pos(position.lineNumber, 0), new CodeMirror.Pos(position.lineNumber, position.columnNumber));
}
this._autoAppendedSpaces = [];
var selections = this.selections();
for (var i = 0; i < selections.length; ++i) {
var selection = selections[i];
this._autoAppendedSpaces.push(this.textEditorPositionHandle(selection.startLine, selection.startColumn));
}
},
/** /**
* @param {number} lineNumber * @param {number} lineNumber
* @param {number} lineLength * @param {number} lineLength
...@@ -1701,8 +1721,10 @@ WebInspector.CodeMirrorTextEditor.BlockIndentController.prototype = { ...@@ -1701,8 +1721,10 @@ WebInspector.CodeMirrorTextEditor.BlockIndentController.prototype = {
allSelectionsAreCollapsedBlocks = isCollapsedBlock; allSelectionsAreCollapsedBlocks = isCollapsedBlock;
} }
codeMirror.replaceSelections(replacements); codeMirror.replaceSelections(replacements);
if (!allSelectionsAreCollapsedBlocks) if (!allSelectionsAreCollapsedBlocks) {
codeMirror._codeMirrorTextEditor._onAutoAppendedSpaces();
return; return;
}
selections = codeMirror.listSelections(); selections = codeMirror.listSelections();
var updatedSelections = []; var updatedSelections = [];
for (var i = 0; i < selections.length; ++i) { for (var i = 0; i < selections.length; ++i) {
...@@ -1715,6 +1737,7 @@ WebInspector.CodeMirrorTextEditor.BlockIndentController.prototype = { ...@@ -1715,6 +1737,7 @@ WebInspector.CodeMirrorTextEditor.BlockIndentController.prototype = {
}); });
} }
codeMirror.setSelections(updatedSelections); codeMirror.setSelections(updatedSelections);
codeMirror._codeMirrorTextEditor._onAutoAppendedSpaces();
}, },
/** /**
......
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