Title: [101511] trunk/Source/WebCore
Revision
101511
Author
commit-qu...@webkit.org
Date
2011-11-30 07:18:38 -0800 (Wed, 30 Nov 2011)

Log Message

Web Inspector: Preserve an indentation level when inserting a new line
https://bugs.webkit.org/show_bug.cgi?id=71625

Indent one level more when a line ends with either "{", "[" or "(".

Patch by Nikita Vasilyev <m...@elv1s.ru> on 2011-11-30
Reviewed by Pavel Feldman.

* inspector/front-end/TextEditorModel.js:
(WebInspector.TextRange.prototype.collapseToEnd):
(WebInspector.TextRange.prototype.normalize):
* inspector/front-end/TextViewer.js:
(WebInspector.TextViewer.prototype._registerShortcuts):
(WebInspector.TextViewer.prototype._handleKeyDown):
(WebInspector.TextEditorMainPanel.prototype.handleEnterKey):
(WebInspector.TextEditorMainPanel.prototype._getSelection):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (101510 => 101511)


--- trunk/Source/WebCore/ChangeLog	2011-11-30 15:14:12 UTC (rev 101510)
+++ trunk/Source/WebCore/ChangeLog	2011-11-30 15:18:38 UTC (rev 101511)
@@ -1,3 +1,21 @@
+2011-11-30  Nikita Vasilyev  <m...@elv1s.ru>
+
+        Web Inspector: Preserve an indentation level when inserting a new line
+        https://bugs.webkit.org/show_bug.cgi?id=71625
+
+        Indent one level more when a line ends with either "{", "[" or "(".
+
+        Reviewed by Pavel Feldman.
+
+        * inspector/front-end/TextEditorModel.js:
+        (WebInspector.TextRange.prototype.collapseToEnd):
+        (WebInspector.TextRange.prototype.normalize):
+        * inspector/front-end/TextViewer.js:
+        (WebInspector.TextViewer.prototype._registerShortcuts):
+        (WebInspector.TextViewer.prototype._handleKeyDown):
+        (WebInspector.TextEditorMainPanel.prototype.handleEnterKey):
+        (WebInspector.TextEditorMainPanel.prototype._getSelection):
+
 2011-11-30  Alexander Pavlov  <apav...@chromium.org>
 
         Web Inspector: Display of data URIs cumbersome in the Elements panel

Modified: trunk/Source/WebCore/inspector/front-end/TextEditorModel.js (101510 => 101511)


--- trunk/Source/WebCore/inspector/front-end/TextEditorModel.js	2011-11-30 15:14:12 UTC (rev 101510)
+++ trunk/Source/WebCore/inspector/front-end/TextEditorModel.js	2011-11-30 15:18:38 UTC (rev 101511)
@@ -50,6 +50,19 @@
         return this.endLine - this.startLine;
     },
 
+    collapseToEnd: function()
+    {
+        return new WebInspector.TextRange(this.endLine, this.endColumn, this.endLine, this.endColumn);
+    },
+
+    normalize: function()
+    {
+        if (this.startLine > this.endLine || (this.startLine === this.endLine && this.startColumn > this.endColumn))
+            return new WebInspector.TextRange(this.endLine, this.endColumn, this.startLine, this.startColumn);
+        else
+            return this;
+    },
+
     clone: function()
     {
         return new WebInspector.TextRange(this.startLine, this.startColumn, this.endLine, this.endColumn);
@@ -75,6 +88,8 @@
     TabCharacter: "\t"
 }
 
+WebInspector.TextEditorModel.endsWithBracketRegex = /[{(\[]\s*$/;
+
 WebInspector.TextEditorModel.prototype = {
     set changeListener(changeListener)
     {
@@ -91,6 +106,11 @@
         return this._lines.join(this._lineBreak);
     },
 
+    get lineBreak()
+    {
+        return this._lineBreak;
+    },
+
     line: function(lineNumber)
     {
         if (lineNumber >= this._lines.length)

Modified: trunk/Source/WebCore/inspector/front-end/TextViewer.js (101510 => 101511)


--- trunk/Source/WebCore/inspector/front-end/TextViewer.js	2011-11-30 15:14:12 UTC (rev 101510)
+++ trunk/Source/WebCore/inspector/front-end/TextViewer.js	2011-11-30 15:18:38 UTC (rev 101511)
@@ -271,6 +271,9 @@
         this._shortcuts[WebInspector.KeyboardShortcut.makeKey(keys.Enter.code, modifiers.CtrlOrMeta)] = commitEditing;
         this._shortcuts[WebInspector.KeyboardShortcut.makeKey(keys.Esc.code)] = cancelEditing;
 
+        var handleEnterKey = this._mainPanel.handleEnterKey.bind(this._mainPanel);
+        this._shortcuts[WebInspector.KeyboardShortcut.makeKey(keys.Enter.code, WebInspector.KeyboardShortcut.Modifiers.None)] = handleEnterKey;
+
         var handleUndo = this._mainPanel.handleUndoRedo.bind(this._mainPanel, false);
         var handleRedo = this._mainPanel.handleUndoRedo.bind(this._mainPanel, true);
         this._shortcuts[WebInspector.KeyboardShortcut.makeKey("z", modifiers.CtrlOrMeta)] = handleUndo;
@@ -284,9 +287,12 @@
 
     _handleKeyDown: function(e)
     {
+        if (this.readOnly)
+            return;
+
         var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEvent(e);
         var handler = this._shortcuts[shortcutKey];
-        if (handler && handler.call(this)) {
+        if (handler && handler()) {
             e.preventDefault();
             e.stopPropagation();
         }
@@ -1026,7 +1032,7 @@
 
     handleUndoRedo: function(redo)
     {
-        if (this._readOnly || this._dirtyLines)
+        if (this._dirtyLines)
             return false;
 
         this.beginUpdates();
@@ -1049,20 +1055,18 @@
 
     handleTabKeyPress: function(shiftKey)
     {
-        if (this._readOnly || this._dirtyLines)
+        if (this._dirtyLines)
             return false;
 
         var selection = this._getSelection();
         if (!selection)
             return false;
 
+        var range = selection.normalize();
+
         this.beginUpdates();
         this._enterTextChangeMode();
 
-        var range = selection;
-        if (range.startLine > range.endLine || (range.startLine === range.endLine && range.startColumn > range.endColumn))
-            range = new WebInspector.TextRange(range.endLine, range.endColumn, range.startLine, range.startColumn);
-
         var newRange;
         if (shiftKey)
             newRange = this._unindentLines(range);
@@ -1135,6 +1139,57 @@
         return newRange;
     },
 
+    handleEnterKey: function()
+    {
+        if (this._dirtyLines)
+            return false;
+
+        var range = this._getSelection();
+        if (!range)
+            return false;
+
+        range.normalize();
+
+        if (range.endColumn === 0)
+            return false;
+
+        var line = this._textModel.line(range.startLine);
+        var linePrefix = line.substring(0, range.startColumn);
+        var indentMatch = linePrefix.match(/^\s+/);
+        var currentIndent = indentMatch ? indentMatch[0] : "";
+
+        var textEditorIndent = WebInspector.settings.textEditorIndent.get();
+        var indent = WebInspector.TextEditorModel.endsWithBracketRegex.test(linePrefix) ? currentIndent + textEditorIndent : currentIndent;
+
+        if (!indent)
+            return false;
+
+        this.beginUpdates();
+        this._enterTextChangeMode();
+
+        var lineBreak = this._textModel.lineBreak;
+        var newRange;
+        if (range.isEmpty() && line.substr(range.endColumn - 1, 2) === '{}') {
+            // {|}
+            // becomes
+            // {
+            //     |
+            // }
+            newRange = this._setText(range, lineBreak + indent + lineBreak + currentIndent);
+            newRange.endLine--;
+            newRange.endColumn += textEditorIndent.length;
+        } else
+            newRange = this._setText(range, lineBreak + indent);
+
+        newRange = newRange.collapseToEnd();
+
+        this._exitTextChangeMode(range, newRange);
+        this.endUpdates();
+        this._restoreSelection(newRange, true);
+
+        return true;
+    },
+
     _splitChunkOnALine: function(lineNumber, chunkNumber, createSuffixChunk)
     {
         var selection = this._getSelection();
@@ -1418,16 +1473,12 @@
         var selection = window.getSelection();
         if (!selection.rangeCount)
             return null;
-        var selectionRange = selection.getRangeAt(0);
         // Selection may be outside of the viewer.
-        if (!this._container.isAncestor(selectionRange.startContainer) || !this._container.isAncestor(selectionRange.endContainer))
+        if (!this._container.isAncestor(selection.anchorNode) || !this._container.isAncestor(selection.focusNode))
             return null;
-        var start = this._selectionToPosition(selectionRange.startContainer, selectionRange.startOffset);
-        var end = selectionRange.collapsed ? start : this._selectionToPosition(selectionRange.endContainer, selectionRange.endOffset);
-        if (selection.anchorNode === selectionRange.startContainer && selection.anchorOffset === selectionRange.startOffset)
-            return new WebInspector.TextRange(start.line, start.column, end.line, end.column);
-        else
-            return new WebInspector.TextRange(end.line, end.column, start.line, start.column);
+        var start = this._selectionToPosition(selection.anchorNode, selection.anchorOffset);
+        var end = selection.isCollapsed ? start : this._selectionToPosition(selection.focusNode, selection.focusOffset);
+        return new WebInspector.TextRange(start.line, start.column, end.line, end.column);
     },
 
     /**
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to